Alipay.php
7.6 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
namespace Yansongda\Pay\Gateways\Alipay;
use Yansongda\Pay\Contracts\GatewayInterface;
use Yansongda\Pay\Exceptions\GatewayException;
use Yansongda\Pay\Exceptions\InvalidArgumentException;
use Yansongda\Pay\Support\Config;
use Yansongda\Pay\Traits\HasHttpRequest;
abstract class Alipay implements GatewayInterface
{
use HasHttpRequest;
/**
* @var string
*/
protected $gateway = 'https://openapi.alipay.com/gateway.do?charset=UTF-8';
/**
* alipay global config params.
*
* @var array
*/
protected $config;
/**
* user's config params.
*
* @var \Yansongda\Pay\Support\Config
*/
protected $user_config;
/**
* [__construct description].
*
* @author yansongda <me@yansongda.cn>
*
* @param array $config [description]
*/
public function __construct(array $config)
{
$this->user_config = new Config($config);
if (is_null($this->user_config->get('app_id'))) {
throw new InvalidArgumentException('Missing Config -- [app_id]');
}
$this->config = [
'app_id' => $this->user_config->get('app_id'),
'method' => '',
'format' => 'JSON',
'charset' => 'UTF-8',
'sign_type' => 'RSA2',
'version' => '1.0',
'return_url' => $this->user_config->get('return_url', ''),
'notify_url' => $this->user_config->get('notify_url', ''),
'timestamp' => date('Y-m-d H:i:s'),
'sign' => '',
'biz_content' => '',
];
}
/**
* pay a order.
*
* @author yansongda <me@yansongda.cn>
*
* @param array $config_biz
*
* @return mixed
*/
public function pay(array $config_biz)
{
$config_biz['product_code'] = $this->getProductCode();
$this->config['method'] = $this->getMethod();
$this->config['biz_content'] = json_encode($config_biz);
$this->config['sign'] = $this->getSign();
}
/**
* refund a order.
*
* @author yansongda <me@yansongda.cn>
*
* @param mixed $config_biz
*
* @return array|bool
*/
public function refund($config_biz, $refund_amount = null)
{
if (!is_array($config_biz)) {
$config_biz = [
'out_trade_no' => $config_biz,
'refund_amount' => $refund_amount,
];
}
return $this->getResult($config_biz, 'alipay.trade.refund');
}
/**
* close a order.
*
* @author yansongda <me@yansongda.cn>
*
* @param array|string $config_biz
*
* @return array|bool
*/
public function close($config_biz)
{
if (!is_array($config_biz)) {
$config_biz = [
'out_trade_no' => $config_biz,
];
}
return $this->getResult($config_biz, 'alipay.trade.close');
}
/**
* find a order.
*
* @author yansongda <me@yansongda.cn>
*
* @param string $out_trade_no
*
* @return array|bool
*/
public function find($out_trade_no = '')
{
$config_biz = [
'out_trade_no' => $out_trade_no,
];
return $this->getResult($config_biz, 'alipay.trade.query');
}
/**
* verify the notify.
*
* @author yansongda <me@yansongda.cn>
*
* @param array $data
* @param string $sign
* @param bool $sync
*
* @return array|bool
*/
public function verify($data, $sign = null, $sync = false)
{
if (is_null($this->user_config->get('ali_public_key'))) {
throw new InvalidArgumentException('Missing Config -- [ali_public_key]');
}
$sign = is_null($sign) ? $data['sign'] : $sign;
$res = "-----BEGIN PUBLIC KEY-----\n".
wordwrap($this->user_config->get('ali_public_key'), 64, "\n", true).
"\n-----END PUBLIC KEY-----";
$toVerify = $sync ? json_encode($data) : $this->getSignContent($data, true);
return openssl_verify($toVerify, base64_decode($sign), $res, OPENSSL_ALGO_SHA256) === 1 ? $data : false;
}
/**
* get method config.
*
* @author yansongda <me@yansongda.cn>
*
* @return string
*/
abstract protected function getMethod();
/**
* get productCode config.
*
* @author yansongda <me@yansongda.cn>
*
* @return string
*/
abstract protected function getProductCode();
/**
* build pay html.
*
* @author yansongda <me@yansongda.cn>
*
* @return string
*/
protected function buildPayHtml()
{
$sHtml = "<form id='alipaysubmit' name='alipaysubmit' action='".$this->gateway."' method='POST'>";
foreach ($this->config as $key => $val) {
$val = str_replace("'", ''', $val);
$sHtml .= "<input type='hidden' name='".$key."' value='".$val."'/>";
}
$sHtml .= "<input type='submit' value='ok' style='display:none;'></form>";
$sHtml .= "<script>document.forms['alipaysubmit'].submit();</script>";
return $sHtml;
}
/**
* get alipay api result.
*
* @author yansongda <me@yansongda.cn>
*
* @param array $config_biz
* @param string $method
*
* @return array|bool
*/
protected function getResult($config_biz, $method)
{
$this->config['biz_content'] = json_encode($config_biz);
$this->config['method'] = $method;
$this->config['sign'] = $this->getSign();
$this->config = array_filter($this->config, function ($value) {
return $value !== '' && !is_null($value);
});
$method = str_replace('.', '_', $method).'_response';
$data = json_decode($this->post($this->gateway, $this->config), true);
if (!isset($data[$method]['code']) || $data[$method]['code'] !== '10000') {
throw new GatewayException(
'get result error:'.$data[$method]['msg'].' - '.$data[$method]['sub_code'],
$data[$method]['code'],
$data);
}
return $this->verify($data[$method], $data['sign'], true);
}
/**
* get sign.
*
* @author yansongda <me@yansongda.cn>
*
* @return string
*/
protected function getSign()
{
if (is_null($this->user_config->get('private_key'))) {
throw new InvalidArgumentException('Missing Config -- [private_key]');
}
$res = "-----BEGIN RSA PRIVATE KEY-----\n".
wordwrap($this->user_config->get('private_key'), 64, "\n", true).
"\n-----END RSA PRIVATE KEY-----";
openssl_sign($this->getSignContent($this->config), $sign, $res, OPENSSL_ALGO_SHA256);
return base64_encode($sign);
}
/**
* get signContent that is to be signed.
*
* @author yansongda <me@yansongda.cn>
*
* @param array $toBeSigned
* @param bool $verify
*
* @return string
*/
protected function getSignContent(array $toBeSigned, $verify = false)
{
ksort($toBeSigned);
$stringToBeSigned = '';
foreach ($toBeSigned as $k => $v) {
if ($verify && $k != 'sign' && $k != 'sign_type') {
$stringToBeSigned .= $k.'='.$v.'&';
}
if (!$verify && $v !== '' && !is_null($v) && $k != 'sign' && '@' != substr($v, 0, 1)) {
$stringToBeSigned .= $k.'='.$v.'&';
}
}
$stringToBeSigned = substr($stringToBeSigned, 0, -1);
unset($k, $v);
return $stringToBeSigned;
}
}