v2board/library/MGate.php

55 lines
1.3 KiB
PHP
Raw Normal View History

2020-01-12 19:48:13 +08:00
<?php
namespace Library;
use \Curl\Curl;
2020-07-20 19:56:39 +08:00
class MGate
2020-01-12 19:48:13 +08:00
{
private $appId;
private $appSecret;
2020-07-20 19:56:39 +08:00
private $url;
2020-01-12 19:48:13 +08:00
2020-07-20 19:56:39 +08:00
public function __construct($url, $appId, $appSecret)
2020-01-12 19:48:13 +08:00
{
$this->appId = $appId;
$this->appSecret = $appSecret;
2020-07-20 19:56:39 +08:00
$this->url = $url;
2020-01-12 19:48:13 +08:00
}
public function pay($params)
{
ksort($params);
2020-01-12 22:38:29 +08:00
$str = http_build_query($params) . $this->appSecret;
2020-01-12 19:48:13 +08:00
$params['sign'] = md5($str);
$curl = new Curl();
2020-07-20 19:56:39 +08:00
$curl->post($this->url . '/v1/gateway/fetch', http_build_query($params));
2020-02-12 14:01:00 +08:00
$result = $curl->response;
2020-06-08 01:08:07 +08:00
if (!$result) {
abort(500, '网络异常');
}
2020-01-12 19:48:13 +08:00
if ($curl->error) {
2020-02-12 14:01:40 +08:00
$errors = (array)$result->errors;
abort(500, $errors[array_keys($errors)[0]][0]);
2020-01-12 19:48:13 +08:00
}
$curl->close();
2020-01-14 00:29:59 +08:00
if (!isset($result->data->trade_no)) {
2020-01-12 19:48:13 +08:00
abort(500, '接口请求失败');
}
2020-01-14 00:29:59 +08:00
return $result->data->pay_url;
2020-01-12 19:48:13 +08:00
}
public function verify($params)
{
$sign = $params['sign'];
unset($params['sign']);
ksort($params);
reset($params);
2020-01-14 19:45:19 +08:00
$str = http_build_query($params) . $this->appSecret;
2020-01-12 19:48:13 +08:00
if ($sign !== md5($str)) {
return false;
}
return true;
}
}