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