Files
Xboard/plugins/Mgate/Plugin.php

124 lines
3.6 KiB
PHP
Raw Normal View History

2023-11-17 14:44:01 +08:00
<?php
namespace Plugin\Mgate;
2023-11-17 14:44:01 +08:00
use App\Services\Plugin\AbstractPlugin;
2025-01-21 14:57:54 +08:00
use App\Contracts\PaymentInterface;
use App\Exceptions\ApiException;
use Curl\Curl;
2023-11-17 14:44:01 +08:00
class Plugin extends AbstractPlugin implements PaymentInterface
{
public function boot(): void
2023-11-17 14:44:01 +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地址',
'type' => 'string',
'required' => true,
'description' => 'MGate支付网关API地址'
2023-11-17 14:44:01 +08:00
],
'mgate_app_id' => [
'label' => 'APP ID',
'type' => 'string',
'required' => true,
'description' => 'MGate应用标识符'
2023-11-17 14:44:01 +08:00
],
'mgate_app_secret' => [
'label' => 'App Secret',
'type' => 'string',
'required' => true,
'description' => 'MGate应用密钥'
2023-11-17 14:44:01 +08:00
],
'mgate_source_currency' => [
'label' => '源货币',
'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']
];
if ($this->getConfig('mgate_source_currency')) {
$params['source_currency'] = $this->getConfig('mgate_source_currency');
2023-11-17 14:44:01 +08:00
}
$params['app_id'] = $this->getConfig('mgate_app_id');
2023-11-17 14:44:01 +08:00
ksort($params);
$str = http_build_query($params) . $this->getConfig('mgate_app_secret');
2023-11-17 14:44:01 +08:00
$params['sign'] = md5($str);
2023-11-17 14:44:01 +08:00
$curl = new Curl();
$curl->setUserAgent('MGate');
$curl->setOpt(CURLOPT_SSL_VERIFYPEER, 0);
$curl->post($this->getConfig('mgate_url') . '/v1/gateway/fetch', http_build_query($params));
2023-11-17 14:44:01 +08:00
$result = $curl->response;
2023-11-17 14:44:01 +08:00
if (!$result) {
throw new ApiException('网络异常');
2023-11-17 14:44:01 +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;
throw new ApiException($errors[array_keys($errors)[0]][0]);
2023-11-17 14:44:01 +08:00
}
if (isset($result->message)) {
throw new ApiException($result->message);
2023-11-17 14:44:01 +08:00
}
throw new ApiException('未知错误');
2023-11-17 14:44:01 +08:00
}
2023-11-17 14:44:01 +08:00
$curl->close();
2023-11-17 14:44:01 +08:00
if (!isset($result->data->trade_no)) {
throw new ApiException('接口请求失败');
2023-11-17 14:44:01 +08:00
}
2023-11-17 14:44:01 +08:00
return [
'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);
$str = http_build_query($params) . $this->getConfig('mgate_app_secret');
2023-11-17 14:44:01 +08:00
if ($sign !== md5($str)) {
return false;
}
2023-11-17 14:44:01 +08:00
return [
'trade_no' => $params['out_trade_no'],
'callback_no' => $params['trade_no']
];
}
}