-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathToken188.php
More file actions
167 lines (138 loc) · 4.83 KB
/
Token188.php
File metadata and controls
167 lines (138 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
namespace App\Payments;
class Token188 {
const DEFAULT_GATEWAY_URL = 'https://api2.188pay.top';
const DEFAULT_PAYMENT_TYPE = 'usdt';
protected $config = [];
public function __construct($config) {
$this->config = $config;
}
public function form()
{
return [
'token188_url' => [
'label' => '网关地址',
'description' => '填写 https://api2.188pay.top;如填完整 /submit.php 地址也可自动兼容',
'type' => 'input',
],
'token188_mchid' => [
'label' => '商户ID(PID)',
'description' => '188Pay 商户后台 API 密钥页面获取',
'type' => 'input',
],
'token188_key' => [
'label' => '商户密钥(KEY)',
'description' => '188Pay 商户后台 API 密钥页面获取',
'type' => 'input',
],
'token188_type' => [
'label' => '支付类型',
'description' => '默认 usdt;支付宝填写 fiat_alipay;TRX 填 trx',
'type' => 'input',
],
'token188_display' => [
'label' => '支付方式:无特别说明,请留空',
'description' => '保留旧版配置兼容,当前不参与请求',
'type' => 'input',
],
];
}
public function pay($order) {
$params = [
'pid' => $this->configValue('token188_mchid'),
'type' => $this->resolvePaymentType(),
'out_trade_no' => $order['trade_no'],
'money' => sprintf('%.2f', $order['total_amount'] / 100),
'name' => $order['trade_no'],
'notify_url' => $order['notify_url'],
'return_url' => $order['return_url'],
];
$params['sign'] = self::getSign($this->configValue('token188_key'), $params);
$params['sign_type'] = 'MD5';
return [
'type' => 1, // Redirect to url
'data' => $this->buildSubmitUrl($params),
];
}
public function notify($params) {
if (!isset($params['sign'])) {
return false;
}
$receivedSign = strtolower(trim($params['sign']));
$expectedSign = self::getSign($this->configValue('token188_key'), $params);
if (!self::hashEquals($expectedSign, $receivedSign)) {
return false;
}
if (($params['trade_status'] ?? '') !== 'TRADE_SUCCESS') {
return false;
}
if (empty($params['out_trade_no']) || empty($params['trade_no'])) {
return false;
}
return [
'trade_no' => $params['out_trade_no'],
'callback_no' => $params['trade_no']
];
}
public static function getSign($secret, $params)
{
$data = [];
foreach ($params as $key => $value) {
if ($key === 'sign' || $key === 'sign_type') {
continue;
}
if ($value === null || $value === '') {
continue;
}
$data[$key] = $value;
}
ksort($data);
$pairs = [];
foreach ($data as $key => $value) {
$pairs[] = $key . '=' . $value;
}
// 188Pay EPay 模式:参数串后直接追加密钥,不带 &key= 前缀。
return strtolower(md5(implode('&', $pairs) . trim($secret)));
}
private function resolvePaymentType()
{
$type = $this->configValue('token188_type');
if ($type !== '') {
return $type;
}
return self::DEFAULT_PAYMENT_TYPE;
}
private function buildSubmitUrl($params)
{
$baseUrl = $this->configValue('token188_url', self::DEFAULT_GATEWAY_URL);
$baseUrl = rtrim($baseUrl, '/');
if (preg_match('/\/submit$/i', $baseUrl) && !preg_match('/\/epay\/submit$/i', $baseUrl)) {
$baseUrl .= '.php';
} elseif (!preg_match('/\/(submit\.php|epay\/submit|epay\/submit\.php)$/i', $baseUrl)) {
$baseUrl .= '/submit.php';
}
return $baseUrl . '?' . http_build_query($params);
}
private function configValue($key, $default = '')
{
if (!isset($this->config[$key])) {
return $default;
}
$value = trim((string)$this->config[$key]);
return $value === '' ? $default : $value;
}
private static function hashEquals($known, $user)
{
if (function_exists('hash_equals')) {
return hash_equals($known, $user);
}
if (strlen($known) !== strlen($user)) {
return false;
}
$result = 0;
for ($i = 0; $i < strlen($known); $i++) {
$result |= ord($known[$i]) ^ ord($user[$i]);
}
return $result === 0;
}
}