forked from jzxyouok/yii2-payment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseGateway.php
490 lines (440 loc) · 12.7 KB
/
BaseGateway.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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
<?php
/**
* @link http://www.tintsoft.com/
* @copyright Copyright (c) 2012 TintSoft Technology Co. Ltd.
* @license http://www.tintsoft.com/license/
*/
namespace yuncms\payment;
use Yii;
use yii\web\Request;
use yii\helpers\Url;
use yii\base\Exception;
use yii\base\Component;
use yii\httpclient\Client;
use yii\helpers\Inflector;
use yii\helpers\StringHelper;
use yii\base\InvalidConfigException;
use yuncms\payment\models\Payment;
use Endroid\QrCode\QrCode;
/**
* 网关基类
* @package yuncms\payment
*/
abstract class BaseGateway extends Component implements GatewayInterface
{
/**
* @var string gateway service id.
* This value mainly used as HTTP request parameter.
*/
private $_id;
/**
* @var string auth service name.
* This value may be used in database records, CSS files and so on.
*/
private $_name;
/**
* @var string auth service title to display in views.
*/
private $_title;
/**
* @var string protocol version.
*/
public $version = '1.0';
public $charset = 'utf-8';
/**
* @var string API base URL.
*/
public $apiBaseUrl;
/**
* @var string 跳转方法
*/
public $redirectMethod = 'POST';
/**
* @var string 跳转URL
*/
public $redirectUrl;
/**
* @var string URL, which user will be redirected after authentication at the Payment provider web site.
* Note: this should be absolute URL (with http:// or https:// leading).
* By default current URL will be used.
*/
public $_returnUrl;
/**
* @var string 后端通知地址
*/
public $_noticeUrl;
/**
* @var Client
*/
public $_httpClient;
/**
* @var array 支持的币种
*/
public $currencies = [];
/**
* @var bool 是否开启Debug
*/
public $debug = false;
/**
* @throws InvalidConfigException
*/
public function init()
{
parent::init();
if (empty ($this->id)) {
throw new InvalidConfigException ('The "id" property must be set.');
}
}
/**
* 获取Http Client
* @return Client
*/
public function getHttpClient()
{
if (!is_object($this->_httpClient)) {
$this->_httpClient = new Client();
}
return $this->_httpClient;
}
/**
* @param string $id service id.
*/
public function setId($id)
{
$this->_id = $id;
}
/**
* @return string service id
*/
public function getId()
{
if (empty($this->_id)) {
$this->_id = $this->getName();
}
return $this->_id;
}
/**
* @param string $name service name.
*/
public function setName($name)
{
$this->_name = $name;
}
/**
* @return string service name.
*/
public function getName()
{
if ($this->_name === null) {
$this->_name = $this->defaultName();
}
return $this->_name;
}
/**
* @param string $title service title.
*/
public function setTitle($title)
{
$this->_title = $title;
}
/**
* @return string service title.
*/
public function getTitle()
{
if ($this->_title === null) {
$this->_title = $this->defaultTitle();
}
return $this->_title;
}
/**
* 检查币种是否受支持
* @param string $currency 合法的货币代码
* @return boolean
*/
public function checkCurrency($currency)
{
return in_array($currency, $this->currencies);
}
/**
* 获取货币代码
* @param string $currency 合法的货币代码
* @return integer|null
*/
public function getCurrencyNumeric($currency)
{
if ($currencyObj = Currency::find($currency)) {
return $currencyObj->getNumeric();
}
}
/**
* 获取货币小数位
* @param string $currency 合法的货币代码
* @return integer
*/
public function getCurrencyDecimalPlaces($currency)
{
if ($currencyObj = Currency::find($currency)) {
return $currencyObj->getDecimals();
}
return 2;
}
/**
* Generates service name.
* @return string service name.
*/
protected function defaultName()
{
return Inflector::camel2id(StringHelper::basename(get_class($this)));
}
/**
* Generates service title.
* @return string service title.
*/
protected function defaultTitle()
{
return StringHelper::basename(get_class($this));
}
/**
* @param string $returnUrl return URL
*/
public function setReturnUrl($returnUrl)
{
$this->_returnUrl = $returnUrl;
}
/**
* @return string return URL.
*/
public function getReturnUrl()
{
if ($this->_returnUrl === null) {
$this->_returnUrl = $this->defaultReturnUrl();
}
return $this->_returnUrl;
}
/**
* @param string $noticeUrl return URL
*/
public function setNoticeUrl($noticeUrl)
{
$this->_noticeUrl = $noticeUrl;
}
/**
* @return string return URL.
*/
public function getNoticeUrl()
{
if ($this->_noticeUrl === null) {
$this->_noticeUrl = $this->defaultNoticeUrl();
}
return $this->_noticeUrl;
}
/**
* Sets persistent state.
* @param string $key state key.
* @param mixed $value state value
* @return $this the object itself
*/
protected function setState($key, $value)
{
if (!Yii::$app->has('session')) {
return $this;
}
/* @var \yii\web\Session $session */
$session = Yii::$app->get('session');
$key = $this->getStateKeyPrefix() . $key;
$session->set($key, $value);
return $this;
}
/**
* Returns persistent state value.
* @param string $key state key.
* @return mixed state value.
*/
protected function getState($key)
{
if (!Yii::$app->has('session')) {
return null;
}
/* @var \yii\web\Session $session */
$session = Yii::$app->get('session');
$key = $this->getStateKeyPrefix() . $key;
$value = $session->get($key);
return $value;
}
/**
* Removes persistent state value.
* @param string $key state key.
* @return boolean success.
*/
protected function removeState($key)
{
if (!Yii::$app->has('session')) {
return true;
}
/* @var \yii\web\Session $session */
$session = Yii::$app->get('session');
$key = $this->getStateKeyPrefix() . $key;
$session->remove($key);
return true;
}
/**
* Returns session key prefix, which is used to store internal states.
* @return string session key prefix.
*/
protected function getStateKeyPrefix()
{
return get_class($this) . '_';
}
/**
* Composes default [[returnUrl]] value.
* @return string return URL.
*/
public function defaultReturnUrl()
{
return Url::to(['/payment/response/return', 'gateway' => $this->getId()], true);
}
/**
* Composes default [[noticeUrl]] value.
* @return string return URL.
*/
public function defaultNoticeUrl()
{
return Url::to(['/payment/response/notice', 'gateway' => $this->getId()], true);
}
/**
* 跳转去支付
* @param array $params
* @throws \Exception
*/
public function getRedirectResponse($params)
{
if ('GET' === $this->redirectMethod) {
$url = $this->composeUrl($this->redirectUrl, $params);
Yii::$app->response->redirect($url);
Yii::$app->end();
} elseif ('POST' === $this->redirectMethod) {
$hiddenFields = '';
foreach ($params as $key => $value) {
$hiddenFields .= sprintf(
'<input type="hidden" name="%1$s" value="%2$s" />',
htmlentities($key, ENT_QUOTES, 'UTF-8', false),
htmlentities($value, ENT_QUOTES, 'UTF-8', false)
) . "\n";
}
$output = '<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Redirecting...</title>
</head>
<body onload="document.forms[0].submit();" style=" text-align:center;background-color: #eff0f1;">
<img src="data:image/gif;base64,R0lGODlhMgAyAKIHAP////vZu/JyBfvavvvavfJ2DPN4D////yH/C05FVFNDQVBFMi4wAwEAAAAh+QQFBwAHACwAAAAAMgAyAEADpXi63P4wygmFETTLwHvIQCgCWjOKpXOGqcmmaNuupGzfeK5PwfAxgqAQw4jtjjlj8mWjuWoLJ3JKrVqvk6EQe/B0GhZidHXs/UrKJnMJ1UjVJ65c8ZbUcel7Gruf+/+AgYKDO0MFhApaFnIeBA0FQodFIztePSBkOgQdjm6UiKBccWxrFHcwmZ59qKuhrqRtE6OwpRCnMrN0n2OtR7d/ua/Cw8Q7CQAh+QQFBwAHACwMAAgAGwALAEADNHi6rPEtHkArkFHoLVRtTwgxG8aIoTlZFDguXLleaqbVjoib6IuxrhSJM7PsYjfg7hA7JAAAIfkEBQcABwAsFgAIABUAFQBAAzd4B6z+MMZAa5DYic0F+81TZWRpHpwCfqfDihbVKuMcpXOXrvManjxY7RSTtWK2pLKky+kEzlQCACH5BAUHAAcALCAADAALABsAQAM0eAes/jC6QJW4WDzGm6RgdWBRx5WeNIlPCKpORkIzZNomgJ9w7x8uiIulCP58MppM80g+EgAh+QQFBwAHACwWABYAFQAVAEADOHi63AfQyamEvaKFTfuDINBtJNdcXiqFoLq0KjmhTCkvGO2q7M5Grt5uSNSUdjYTDmO8LXM7qCsBACH5BAUHAAcALAwAIAAbAAsAQAMzeCes/tCBGWO4OFTHensYNI2T120iSS2fk4XPiELwzJ62RN5efqgUEy2Tks1eF9/mZUsAACH5BAUHAAcALAgAFgAVABUAQAM4eCes/jDKOYOFIGvAeqOVJQZgiWWK15nPyobke4ziCcjHpi2erH4sFW5ILNFktMvr+NDJdKiXMwEAIfkEBQcABwAsCAAMAAsAGwBAAzR4utL7cIUZH7gYvMbb/FSlYFHHlY4UPuAnvmN2RSR6bqYAmXCvuqxWANLyGSsyiGxmyRwSADs=" style="width:80px;margin-left:auto; margin-top:200px ">
<form action="%1$s" method="post" style="opacity:0">
<p>Redirecting to payment page...</p>
<p>
%2$s
<input type="submit" value="Continue" />
</p>
</form>
</body>
</html>';
$output = sprintf(
$output,
htmlentities($this->redirectUrl, ENT_QUOTES, 'UTF-8', false),
$hiddenFields
);
Yii::$app->response->content = $output;
Yii::$app->end();
}
throw new \Exception('Invalid redirect method "' . $this->redirectMethod . '".');
}
/**
* Sends HTTP request.
* @param string $method request type.
* @param string $url request URL.
* @param array $params request params.
* @param array $headers additional request headers.
* @return object response.
* @throws Exception on failure.
*/
protected function sendRequest($method, $url, array $params = [], array $headers = [])
{
$response = $request = $this->getHttpClient()->createRequest()
->setUrl($url)
->setMethod($method)
->setHeaders($headers)
->setData($params)
->send();
if (!$response->isOk && $this->debug) {
throw new Exception ('Http request failed.');
}
return $response;
}
/**
* 发送API请求
* @param string $url
* @param string $method
* @param array $params
* @param array $headers
* @return object response.
*/
public function api($url, $method, array $params = [], array $headers = [])
{
try {
return $this->sendRequest($method, $url, $params, $headers);
} catch (\Exception $e) {
sleep(2);
return $this->sendRequest($method, $url, $params, $headers);
}
}
/**
* 工具方法 从基本网址和GET参数中生成网址。
* @param string $url base URL.
* @param array $params GET params.
* @return string composed URL.
*/
public function composeUrl($url, array $params = [])
{
if (strpos($url, '?') === false) {
$url .= '?';
} else {
$url .= '&';
}
$url .= http_build_query($params, '', '&', PHP_QUERY_RFC3986);
return $url;
}
/**
* 去支付
* @param Payment $payment 支付模型对象
* @param array $paymentParams 支付参数
* @return void
*/
abstract function payment(Payment $payment, &$paymentParams);
/**
* 支付响应
* @param Request $request
* @param $paymentId
* @param $money
* @param $message
* @param $payId
* @return mixed
*/
abstract public function callback(Request $request, &$paymentId, &$money, &$message, &$payId);
/**
* 服务端通知
* @param Request $request
* @param $paymentId
* @param $money
* @param $message
* @param $payId
* @return mixed
*/
abstract public function notice(Request $request, &$paymentId, &$money, &$message, &$payId);
/**
* 查询支付是否成功,对账作用
* @param string $paymentId
* @return mixed
*/
abstract public function queryOrder($paymentId);
}