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