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\Exception\GatewayException; |
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @method Wechat\AppPayment app(array $array) |
13
|
|
|
* @method Wechat\WapPayment wap(array $array) |
14
|
|
|
* @method Wechat\ScanPayment scan(array $array) |
15
|
|
|
*/ |
16
|
|
|
class Wechat implements GatewayInterface |
17
|
|
|
{ |
18
|
|
|
use LogTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var \Illuminate\Contracts\Config\Repository |
22
|
|
|
*/ |
23
|
|
|
protected $config; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $payload; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $gateway; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Wechat constructor. |
37
|
|
|
* |
38
|
|
|
* @param array $config |
39
|
|
|
* |
40
|
|
|
* @throws \Exception |
41
|
|
|
*/ |
42
|
6 |
|
public function __construct(array $config) |
43
|
|
|
{ |
44
|
6 |
|
$this->config = new Repository($config); |
45
|
6 |
|
$env = $this->config->get('env', 'pro'); |
46
|
6 |
|
$this->gateway = self::getGatewayUrl($env); |
47
|
6 |
|
$this->payload = [ |
48
|
|
|
// 公众账号 ID - 微信分配的公众账号 ID |
49
|
6 |
|
'appid' => '', |
50
|
|
|
|
51
|
|
|
// 商户号 - 微信支付分配的商户号 |
52
|
6 |
|
'mch_id' => $this->config->get('mch_id', ''), |
53
|
|
|
|
54
|
|
|
// 随机字符串 - 随机字符串,不长于32位 |
55
|
6 |
|
'nonce_str' => Str::random(), |
56
|
|
|
|
57
|
|
|
// 签名 |
58
|
6 |
|
'sign' => '', |
59
|
|
|
|
60
|
|
|
// 终端 IP - 必须传正确的客户端 IP |
61
|
6 |
|
'spbill_create_ip' => Request::createFromGlobals()->getClientIp(), |
62
|
|
|
|
63
|
|
|
// 通知地址 - 接收微信支付异步通知回调地址,通知 url 必须为直接可访问的 url,不能携带参数 |
64
|
6 |
|
'notify_url' => $this->config->get('notify_url'), |
65
|
|
|
|
66
|
|
|
// 交易类型 - H5 支付的交易类型为 MWEB |
67
|
6 |
|
'trade_type' => '', |
68
|
|
|
]; |
69
|
|
|
|
70
|
6 |
|
if ($this->config->has('log.file')) { |
71
|
|
|
$this->registerLogger($this->config, 'Wxpay'); |
72
|
|
|
} |
73
|
6 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $method |
77
|
|
|
* @param array $arguments |
78
|
|
|
* |
79
|
|
|
* @return mixed |
80
|
|
|
* |
81
|
|
|
* @throws \Nilnice\Payment\Exception\GatewayException |
82
|
|
|
*/ |
83
|
3 |
|
public function __call(string $method, array $arguments) |
84
|
|
|
{ |
85
|
1 |
|
switch ($method) { |
86
|
2 |
|
case 'app': // APP 支付 |
87
|
|
|
$this->payload['appid'] = $this->config->get('app_appid'); |
88
|
|
|
break; |
89
|
2 |
|
case 'wap': // H5 支付 |
90
|
|
|
case 'bar': // 刷卡支付 |
91
|
|
|
case 'scan': // 扫码支付 |
92
|
3 |
|
$this->payload['appid'] = $this->config->get('app_id'); |
93
|
3 |
|
break; |
94
|
|
|
case 'pub': // 公众号支付 |
95
|
|
|
$this->payload['appid'] = $this->config->get('pub_appid'); |
96
|
|
|
break; |
97
|
|
|
case 'xcx': // 小程序支付 |
98
|
|
|
$this->payload['appid'] = $this->config->get('xcx_appid'); |
99
|
|
|
break; |
100
|
|
|
} |
101
|
|
|
|
102
|
3 |
|
return $this->dispatcher($method, ...$arguments); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Query an order information. |
108
|
|
|
* |
109
|
|
|
* @param array|string $order |
110
|
|
|
* |
111
|
|
|
* @return \Illuminate\Support\Collection |
112
|
|
|
*/ |
113
|
|
|
public function query($order) : Collection |
114
|
|
|
{ |
115
|
|
|
return new Collection([]); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Close an order. |
120
|
|
|
* |
121
|
|
|
* @param array|string $order |
122
|
|
|
* |
123
|
|
|
* @return \Illuminate\Support\Collection |
124
|
|
|
*/ |
125
|
|
|
public function close($order) : Collection |
126
|
|
|
{ |
127
|
|
|
return new Collection([]); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Cancel an order. |
132
|
|
|
* |
133
|
|
|
* @param array|string $order |
134
|
|
|
* |
135
|
|
|
* @return \Illuminate\Support\Collection |
136
|
|
|
*/ |
137
|
|
|
public function cancel($order) : Collection |
138
|
|
|
{ |
139
|
|
|
return new Collection([]); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Refund an order. |
144
|
|
|
* |
145
|
|
|
* @param array|string $order |
146
|
|
|
* |
147
|
|
|
* @return \Illuminate\Support\Collection |
148
|
|
|
*/ |
149
|
|
|
public function refund($order) : Collection |
150
|
|
|
{ |
151
|
|
|
return new Collection([]); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* To pay. |
156
|
|
|
* |
157
|
|
|
* @param string $gateway |
158
|
|
|
* |
159
|
|
|
* @return mixed |
160
|
|
|
* |
161
|
|
|
* @throws \Nilnice\Payment\Exception\GatewayException |
162
|
|
|
*/ |
163
|
3 |
|
protected function toPay(string $gateway) |
164
|
|
|
{ |
165
|
3 |
|
$class = new $gateway($this->config); // Instantiate different gateways. |
166
|
|
|
|
167
|
3 |
|
if ($class instanceof PaymentInterface) { |
168
|
3 |
|
return $class->toPay($this->gateway, $this->payload); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
throw new GatewayException( |
172
|
|
|
"Pay gateway [{$gateway}] must be an instance of the GatewayInterface.", |
173
|
|
|
2 |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Pay dispatcher. |
179
|
|
|
* |
180
|
|
|
* @param string $gateway |
181
|
|
|
* @param array $array |
182
|
|
|
* |
183
|
|
|
* @return mixed |
184
|
|
|
* |
185
|
|
|
* @throws \Nilnice\Payment\Exception\GatewayException |
186
|
|
|
*/ |
187
|
3 |
|
private function dispatcher(string $gateway, array $array = []) |
188
|
|
|
{ |
189
|
3 |
|
$this->payload = array_merge($this->payload, $array); |
190
|
3 |
|
$class = \get_class($this) . '\\' . Str::studly($gateway) . 'Payment'; |
191
|
|
|
|
192
|
3 |
|
if (class_exists($class)) { |
193
|
3 |
|
return $this->toPay($class); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
throw new GatewayException("Pay gateway [{$gateway}] not exists", 1); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Get gateway url. |
201
|
|
|
* |
202
|
|
|
* @param string $env |
203
|
|
|
* |
204
|
|
|
* @return string |
205
|
|
|
*/ |
206
|
6 |
|
private static function getGatewayUrl($env = '') : string |
207
|
|
|
{ |
208
|
6 |
|
$uri = ''; |
209
|
2 |
|
switch ($env) { |
210
|
4 |
|
case 'pro': |
211
|
3 |
|
$uri = Constant::WX_PAY_PRO_URI; |
212
|
3 |
|
break; |
213
|
2 |
|
case 'dev': |
214
|
3 |
|
$uri = Constant::WX_PAY_DEV_URI; |
215
|
3 |
|
break; |
216
|
|
|
case 'hk': |
217
|
|
|
$uri = Constant::WX_PAY_PRO_HK_URI; |
218
|
|
|
break; |
219
|
|
|
default: |
220
|
|
|
break; |
221
|
|
|
} |
222
|
|
|
|
223
|
6 |
|
return $uri; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|