1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nilnice\Payment; |
4
|
|
|
|
5
|
|
|
use Illuminate\Config\Repository; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use Nilnice\Payment\Alipay\Traits\RequestTrait; |
9
|
|
|
use Nilnice\Payment\Alipay\Traits\SecurityTrait; |
10
|
|
|
use Nilnice\Payment\Exception\GatewayException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @method Alipay\BarPayment bar(array $array) |
14
|
|
|
* @method Alipay\ScanPayment scan(array $array) |
15
|
|
|
* @method Alipay\AppPayment app(array $array) |
16
|
|
|
* @method Alipay\WapPayment wap(array $array) |
17
|
|
|
* @method Alipay\WebPayment web(array $array) |
18
|
|
|
*/ |
19
|
|
|
class Alipay implements GatewayInterface |
20
|
|
|
{ |
21
|
|
|
use RequestTrait; |
22
|
|
|
use SecurityTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \Illuminate\Contracts\Config\Repository |
26
|
|
|
*/ |
27
|
|
|
protected $config; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $payload; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $gateway; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Alipay constructor. |
41
|
|
|
* |
42
|
|
|
* @param array $config |
43
|
|
|
*/ |
44
|
9 |
|
public function __construct(array $config) |
45
|
|
|
{ |
46
|
9 |
|
$this->config = new Repository($config); |
47
|
9 |
|
$env = $this->config->get('env', 'pro'); |
48
|
9 |
|
$this->gateway = self::getGatewayUrl($env); |
49
|
9 |
|
$this->payload = [ |
50
|
|
|
// 支付宝分配给开发者的应用 ID |
51
|
9 |
|
'app_id' => $this->config->get('app_id'), |
52
|
|
|
|
53
|
|
|
// 接口名称 |
54
|
9 |
|
'method' => '', |
55
|
|
|
|
56
|
|
|
// 数据格式,仅支持 JSON |
57
|
9 |
|
'format' => 'JSON', |
58
|
|
|
|
59
|
|
|
// 同步返回地址,HTTP/HTTPS 开头字符串 |
60
|
9 |
|
'return_url' => $this->config->get('return_url'), |
61
|
|
|
|
62
|
|
|
// 请求使用的编码格式,如:UTF-8, GBK, GB2312 等 |
63
|
9 |
|
'charset' => 'utf-8', |
64
|
|
|
|
65
|
|
|
// 商户生成签名字符串所使用的签名算法类型,目前支持 RSA2 和 RSA,推荐使用 RSA2 |
66
|
9 |
|
'sign_type' => 'RSA2', |
67
|
|
|
|
68
|
|
|
// 商户请求参数的签名串,详见签名 |
69
|
9 |
|
'sign' => '', |
70
|
|
|
|
71
|
|
|
// 发送请求的时间,格式"yyyy-MM-dd HH:mm:ss" |
72
|
9 |
|
'timestamp' => date('Y-m-d H:i:s'), |
73
|
|
|
|
74
|
|
|
// 调用的接口版本,固定为:1.0 |
75
|
9 |
|
'version' => '1.0', |
76
|
|
|
|
77
|
|
|
// 支付宝服务器主动通知商户服务器里指定的页面 http/https 路径 |
78
|
9 |
|
'notify_url' => $this->config->get('notify_url'), |
79
|
|
|
|
80
|
|
|
// 业务请求参数的集合,最大长度不限,除公共参数外所有请求参数都必须放在这个参数中传递,具体参照各产品快速接入文档 |
81
|
9 |
|
'biz_content' => '', |
82
|
|
|
]; |
83
|
9 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $method |
87
|
|
|
* @param array $arguments |
88
|
|
|
* |
89
|
|
|
* @return mixed |
90
|
|
|
* @throws \Nilnice\Payment\Exception\GatewayException |
91
|
|
|
*/ |
92
|
6 |
|
public function __call(string $method, array $arguments) |
93
|
|
|
{ |
94
|
6 |
|
return $this->dispatcher($method, ...$arguments); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Pay dispatcher. |
99
|
|
|
* |
100
|
|
|
* @param string $gateway |
101
|
|
|
* @param array $array |
102
|
|
|
* |
103
|
|
|
* @return mixed |
104
|
|
|
* @throws \Nilnice\Payment\Exception\GatewayException |
105
|
|
|
*/ |
106
|
6 |
|
public function dispatcher(string $gateway, array $array = []) |
107
|
|
|
{ |
108
|
6 |
|
$this->payload['biz_content'] = $array; |
109
|
6 |
|
$class = \get_class($this) . '\\' . Str::studly($gateway) . 'Payment'; |
110
|
|
|
|
111
|
6 |
|
if (class_exists($class)) { |
112
|
3 |
|
return $this->toPay($class); |
113
|
|
|
} |
114
|
|
|
|
115
|
3 |
|
throw new GatewayException("Pay gateway [{$gateway}] not exists.", 1); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Query an order information. |
120
|
|
|
* |
121
|
|
|
* @param array|string $order |
122
|
|
|
* |
123
|
|
|
* @return \Illuminate\Support\Collection |
124
|
|
|
* @throws \Nilnice\Payment\Exception\GatewayException |
125
|
|
|
* @throws \Nilnice\Payment\Exception\InvalidKeyException |
126
|
|
|
* @throws \Nilnice\Payment\Exception\InvalidSignException |
127
|
|
|
* @throws \RuntimeException |
128
|
|
|
*/ |
129
|
|
|
public function query($order) : Collection |
130
|
|
|
{ |
131
|
|
|
$key = $this->config->get('private_key'); |
132
|
|
|
$order = \is_array($order) ? $order : ['out_trade_no' => $order]; |
133
|
|
|
$this->payload['method'] = Constant::ALI_PAY_QUERY; |
134
|
|
|
$this->payload['biz_content'] = json_encode($order); |
135
|
|
|
$this->payload['sign'] = self::generateSign($this->payload, $key); |
136
|
|
|
|
137
|
|
|
return $this->send($this->payload, $this->config->get('public_key')); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* To pay. |
142
|
|
|
* |
143
|
|
|
* @param string $gateway |
144
|
|
|
* |
145
|
|
|
* @return mixed |
146
|
|
|
* @throws \Nilnice\Payment\Exception\GatewayException |
147
|
|
|
*/ |
148
|
3 |
|
protected function toPay(string $gateway) |
149
|
|
|
{ |
150
|
3 |
|
$class = new $gateway($this->config); // Instantiate different gateways. |
151
|
|
|
|
152
|
3 |
|
if ($class instanceof PaymentInterface) { |
153
|
3 |
|
return $class->toPay($this->gateway, $this->payload); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
throw new GatewayException( |
157
|
|
|
"Pay gateway [{$gateway}] must be an instance of the GatewayInterface.", |
158
|
|
|
2 |
159
|
|
|
); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Get gateway url. |
164
|
|
|
* |
165
|
|
|
* @param string $env |
166
|
|
|
* |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
9 |
|
private static function getGatewayUrl($env = '') : string |
170
|
|
|
{ |
171
|
9 |
|
$uri = ''; |
172
|
9 |
|
if ($env === 'pro') { |
173
|
6 |
|
$uri = Constant::ALI_PAY_PRO_URI; |
174
|
3 |
|
} elseif ($env === 'dev') { |
175
|
3 |
|
$uri = Constant::ALI_PAY_DEV_URI; |
176
|
|
|
} |
177
|
|
|
|
178
|
9 |
|
return $uri; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|