2023-11-17 14:44:01 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
2025-07-26 18:49:58 +08:00
|
|
|
|
namespace Plugin\Mgate;
|
2023-11-17 14:44:01 +08:00
|
|
|
|
|
2025-07-26 18:49:58 +08:00
|
|
|
|
use App\Services\Plugin\AbstractPlugin;
|
2025-01-21 14:57:54 +08:00
|
|
|
|
use App\Contracts\PaymentInterface;
|
2025-07-26 18:49:58 +08:00
|
|
|
|
use App\Exceptions\ApiException;
|
|
|
|
|
|
use Curl\Curl;
|
2023-11-17 14:44:01 +08:00
|
|
|
|
|
2025-07-26 18:49:58 +08:00
|
|
|
|
class Plugin extends AbstractPlugin implements PaymentInterface
|
|
|
|
|
|
{
|
|
|
|
|
|
public function boot(): void
|
2023-11-17 14:44:01 +08:00
|
|
|
|
{
|
2025-07-26 18:49:58 +08:00
|
|
|
|
$this->filter('available_payment_methods', function ($methods) {
|
|
|
|
|
|
if ($this->getConfig('enabled', true)) {
|
|
|
|
|
|
$methods['MGate'] = [
|
|
|
|
|
|
'name' => $this->getConfig('display_name', 'MGate'),
|
|
|
|
|
|
'icon' => $this->getConfig('icon', '🏛️'),
|
|
|
|
|
|
'plugin_code' => $this->getPluginCode(),
|
|
|
|
|
|
'type' => 'plugin'
|
|
|
|
|
|
];
|
|
|
|
|
|
}
|
|
|
|
|
|
return $methods;
|
|
|
|
|
|
});
|
2023-11-17 14:44:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-21 14:57:54 +08:00
|
|
|
|
public function form(): array
|
2023-11-17 14:44:01 +08:00
|
|
|
|
{
|
|
|
|
|
|
return [
|
|
|
|
|
|
'mgate_url' => [
|
|
|
|
|
|
'label' => 'API地址',
|
2025-07-26 18:49:58 +08:00
|
|
|
|
'type' => 'string',
|
|
|
|
|
|
'required' => true,
|
|
|
|
|
|
'description' => 'MGate支付网关API地址'
|
2023-11-17 14:44:01 +08:00
|
|
|
|
],
|
|
|
|
|
|
'mgate_app_id' => [
|
2025-07-26 18:49:58 +08:00
|
|
|
|
'label' => 'APP ID',
|
|
|
|
|
|
'type' => 'string',
|
|
|
|
|
|
'required' => true,
|
|
|
|
|
|
'description' => 'MGate应用标识符'
|
2023-11-17 14:44:01 +08:00
|
|
|
|
],
|
|
|
|
|
|
'mgate_app_secret' => [
|
2025-07-26 18:49:58 +08:00
|
|
|
|
'label' => 'App Secret',
|
|
|
|
|
|
'type' => 'string',
|
|
|
|
|
|
'required' => true,
|
|
|
|
|
|
'description' => 'MGate应用密钥'
|
2023-11-17 14:44:01 +08:00
|
|
|
|
],
|
|
|
|
|
|
'mgate_source_currency' => [
|
|
|
|
|
|
'label' => '源货币',
|
2025-07-26 18:49:58 +08:00
|
|
|
|
'type' => 'string',
|
|
|
|
|
|
'description' => '默认CNY,源货币类型'
|
2023-11-17 14:44:01 +08:00
|
|
|
|
]
|
|
|
|
|
|
];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-21 14:57:54 +08:00
|
|
|
|
public function pay($order): array
|
2023-11-17 14:44:01 +08:00
|
|
|
|
{
|
|
|
|
|
|
$params = [
|
|
|
|
|
|
'out_trade_no' => $order['trade_no'],
|
|
|
|
|
|
'total_amount' => $order['total_amount'],
|
|
|
|
|
|
'notify_url' => $order['notify_url'],
|
|
|
|
|
|
'return_url' => $order['return_url']
|
|
|
|
|
|
];
|
2025-07-26 18:49:58 +08:00
|
|
|
|
|
|
|
|
|
|
if ($this->getConfig('mgate_source_currency')) {
|
|
|
|
|
|
$params['source_currency'] = $this->getConfig('mgate_source_currency');
|
2023-11-17 14:44:01 +08:00
|
|
|
|
}
|
2025-07-26 18:49:58 +08:00
|
|
|
|
|
|
|
|
|
|
$params['app_id'] = $this->getConfig('mgate_app_id');
|
2023-11-17 14:44:01 +08:00
|
|
|
|
ksort($params);
|
2025-07-26 18:49:58 +08:00
|
|
|
|
$str = http_build_query($params) . $this->getConfig('mgate_app_secret');
|
2023-11-17 14:44:01 +08:00
|
|
|
|
$params['sign'] = md5($str);
|
2025-07-26 18:49:58 +08:00
|
|
|
|
|
2023-11-17 14:44:01 +08:00
|
|
|
|
$curl = new Curl();
|
|
|
|
|
|
$curl->setUserAgent('MGate');
|
|
|
|
|
|
$curl->setOpt(CURLOPT_SSL_VERIFYPEER, 0);
|
2025-07-26 18:49:58 +08:00
|
|
|
|
$curl->post($this->getConfig('mgate_url') . '/v1/gateway/fetch', http_build_query($params));
|
2023-11-17 14:44:01 +08:00
|
|
|
|
$result = $curl->response;
|
2025-07-26 18:49:58 +08:00
|
|
|
|
|
2023-11-17 14:44:01 +08:00
|
|
|
|
if (!$result) {
|
2023-12-07 04:01:32 +08:00
|
|
|
|
throw new ApiException('网络异常');
|
2023-11-17 14:44:01 +08:00
|
|
|
|
}
|
2025-07-26 18:49:58 +08:00
|
|
|
|
|
2023-11-17 14:44:01 +08:00
|
|
|
|
if ($curl->error) {
|
|
|
|
|
|
if (isset($result->errors)) {
|
2025-01-21 14:57:54 +08:00
|
|
|
|
$errors = (array) $result->errors;
|
2023-12-07 04:01:32 +08:00
|
|
|
|
throw new ApiException($errors[array_keys($errors)[0]][0]);
|
2023-11-17 14:44:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (isset($result->message)) {
|
2023-12-07 04:01:32 +08:00
|
|
|
|
throw new ApiException($result->message);
|
2023-11-17 14:44:01 +08:00
|
|
|
|
}
|
2023-12-07 04:01:32 +08:00
|
|
|
|
throw new ApiException('未知错误');
|
2023-11-17 14:44:01 +08:00
|
|
|
|
}
|
2025-07-26 18:49:58 +08:00
|
|
|
|
|
2023-11-17 14:44:01 +08:00
|
|
|
|
$curl->close();
|
2025-07-26 18:49:58 +08:00
|
|
|
|
|
2023-11-17 14:44:01 +08:00
|
|
|
|
if (!isset($result->data->trade_no)) {
|
2023-12-07 04:01:32 +08:00
|
|
|
|
throw new ApiException('接口请求失败');
|
2023-11-17 14:44:01 +08:00
|
|
|
|
}
|
2025-07-26 18:49:58 +08:00
|
|
|
|
|
2023-11-17 14:44:01 +08:00
|
|
|
|
return [
|
2025-07-26 18:49:58 +08:00
|
|
|
|
'type' => 1,
|
2023-11-17 14:44:01 +08:00
|
|
|
|
'data' => $result->data->pay_url
|
|
|
|
|
|
];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-21 14:57:54 +08:00
|
|
|
|
public function notify($params): array|bool
|
2023-11-17 14:44:01 +08:00
|
|
|
|
{
|
|
|
|
|
|
$sign = $params['sign'];
|
|
|
|
|
|
unset($params['sign']);
|
|
|
|
|
|
ksort($params);
|
|
|
|
|
|
reset($params);
|
2025-07-26 18:49:58 +08:00
|
|
|
|
$str = http_build_query($params) . $this->getConfig('mgate_app_secret');
|
|
|
|
|
|
|
2023-11-17 14:44:01 +08:00
|
|
|
|
if ($sign !== md5($str)) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2025-07-26 18:49:58 +08:00
|
|
|
|
|
2023-11-17 14:44:01 +08:00
|
|
|
|
return [
|
|
|
|
|
|
'trade_no' => $params['out_trade_no'],
|
|
|
|
|
|
'callback_no' => $params['trade_no']
|
|
|
|
|
|
];
|
|
|
|
|
|
}
|
2025-07-26 18:49:58 +08:00
|
|
|
|
}
|