-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathToken188.php
152 lines (129 loc) · 4.64 KB
/
Token188.php
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
<?php
namespace App\Payments;
class Token188 {
public function __construct($config) {
$this->config = $config;
}
public function form()
{
return [
'token188_url' => [
'label' => '接口地址',
'description' => '',
'type' => 'input',
],
'token188_mchid' => [
'label' => '商户ID',
'description' => '',
'type' => 'input',
],
'token188_key' => [
'label' => '商户密钥',
'description' => '',
'type' => 'input',
],
'token188_display' => [
'label' => '支付方式:无特别说明,请留空',
'description' => '',
'type' => 'input',
],
];
}
public function pay($order) {
$params = [
'merchantId' => $this->config['token188_mchid'],
'outTradeNo' => $order['trade_no'],
'subject' => $order['trade_no'],
'totalAmount' => sprintf('%.2f', $order['total_amount'] / 100),
'attach' => (string)$order['total_amount'],
'body' => $order['trade_no'],
'coinName' => 'USDT-TRC20',
'notifyUrl' => $order['notify_url'],
'callBackUrl' => $order['return_url'],
'timestamp' => $this->msectime(),
'nonceStr' => $this->getNonceStr(16)
];
//echo $params['totalAmount'];
$mysign = self::GetSign($this->config['token188_key'], $params);
// 网关连接
$ret_raw = self::_curlPost($this->config['token188_url'], $params,$mysign,1);
$ret = @json_decode($ret_raw, true);
if(empty($ret['data']['paymentUrl'])) {
abort(500, $ret["msg"]);
}
return [
'type' => 1, // Redirect to url
'data' => $ret['data']['paymentUrl'],
];
}
public function notify($params) {
$content = file_get_contents('php://input');
//$content = file_get_contents('php://input', 'r');
$json_param = json_decode($content, true); //convert JSON into array
$coinPay_sign = $json_param['sign'];
unset($json_param['sign']);
unset($json_param['notifyId']);
$sign = self::GetSign($this->config['token188_key'], $json_param);
if ($sign !== $coinPay_sign) {
echo json_encode(['status' => 400]);
return false;
}
$out_trade_no = $json_param['outTradeNo'];
$pay_trade_no=$json_param['tradeNo'];
return [
'trade_no' => $out_trade_no,
'callback_no' => $pay_trade_no
];
}
public function GetSign($secret, $params)
{
$p=ksort($params);
reset($params);
if ($p) {
$str = '';
foreach ($params as $k => $val) {
$str .= $k . '=' . $val . '&';
}
$strs = rtrim($str, '&');
}
$strs .='&key='.$secret;
$signature = md5($strs);
//$params['sign'] = base64_encode($signature);
return $signature;
}
public function msectime() {
list($msec, $sec) = explode(' ', microtime());
$msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
return $msectime;
}
/**
* 返回随机字符串
* @param int $length
* @return string
*/
public static function getNonceStr($length = 32)
{
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
private function _curlPost($url,$params=false,$signature,$ispost=0){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 300); //设置超时
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt(
$ch, CURLOPT_HTTPHEADER, array('token:'.$signature)
);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}