v2board/app/Payments/MGate.php

104 lines
3.0 KiB
PHP
Raw Normal View History

2021-04-28 16:56:08 +08:00
<?php
/**
* 自己写别抄抄NMB抄
*/
namespace App\Payments;
use \Curl\Curl;
class MGate {
2021-08-24 15:50:45 +08:00
private $config;
2021-04-28 16:56:08 +08:00
public function __construct($config)
{
$this->config = $config;
}
public function form()
{
return [
'mgate_url' => [
'label' => 'API地址',
'description' => '',
'type' => 'input',
],
'mgate_app_id' => [
'label' => 'APPID',
'description' => '',
'type' => 'input',
],
'mgate_app_secret' => [
'label' => 'AppSecret',
'description' => '',
'type' => 'input',
2022-02-09 02:39:19 +08:00
],
'notify_domain' => [
'label' => '通知域名(选填)',
'description' => '用于接收来自网关的支付通知',
'type' => 'input'
2021-04-28 16:56:08 +08:00
]
];
}
public function pay($order)
{
2022-02-09 12:18:42 +08:00
if ($this->config['notify_domain']) {
$parseUrl = parse_url($order['notify_url']);
$notifyUrl = "{$parseUrl['scheme']}://{$this->config['notify_domain']}{$parseUrl['path']}";
}
2021-04-28 16:56:08 +08:00
$params = [
'out_trade_no' => $order['trade_no'],
'total_amount' => $order['total_amount'],
2022-02-09 12:18:42 +08:00
'notify_url' => $notifyUrl ?? $order['notify_url'],
2021-04-28 16:56:08 +08:00
'return_url' => $order['return_url']
];
$params['app_id'] = $this->config['mgate_app_id'];
ksort($params);
$str = http_build_query($params) . $this->config['mgate_app_secret'];
$params['sign'] = md5($str);
$curl = new Curl();
2021-08-24 15:50:45 +08:00
$curl->setUserAgent('MGate');
2022-01-06 03:22:14 +08:00
$curl->setOpt(CURLOPT_SSL_VERIFYPEER, 0);
2021-04-28 16:56:08 +08:00
$curl->post($this->config['mgate_url'] . '/v1/gateway/fetch', http_build_query($params));
$result = $curl->response;
if (!$result) {
abort(500, '网络异常');
}
if ($curl->error) {
if (isset($result->errors)) {
$errors = (array)$result->errors;
abort(500, $errors[array_keys($errors)[0]][0]);
}
if (isset($result->message)) {
abort(500, $result->message);
}
abort(500, '未知错误');
}
$curl->close();
if (!isset($result->data->trade_no)) {
abort(500, '接口请求失败');
}
return [
'type' => 1, // 0:qrcode 1:url
'data' => $result->data->pay_url
];
}
public function notify($params)
{
$sign = $params['sign'];
unset($params['sign']);
ksort($params);
reset($params);
$str = http_build_query($params) . $this->config['mgate_app_secret'];
if ($sign !== md5($str)) {
return false;
}
return [
'trade_no' => $params['out_trade_no'],
'callback_no' => $params['trade_no']
];
}
}