1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the overtrue/wechat. |
5
|
|
|
* |
6
|
|
|
* (c) overtrue <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Payment.php. |
14
|
|
|
* |
15
|
|
|
* @author overtrue <[email protected]> |
16
|
|
|
* @copyright 2015 overtrue <[email protected]> |
17
|
|
|
* |
18
|
|
|
* @link https://github.com/overtrue |
19
|
|
|
* @link http://overtrue.me |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace EasyWeChat\Payment; |
23
|
|
|
|
24
|
|
|
use EasyWeChat\Core\Exceptions\FaultException; |
25
|
|
|
use EasyWeChat\Support\Url as UrlHelper; |
26
|
|
|
use EasyWeChat\Support\XML; |
27
|
|
|
use Overtrue\Socialite\AccessTokenInterface; |
28
|
|
|
use Symfony\Component\HttpFoundation\Response; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Class Payment. |
32
|
|
|
*/ |
33
|
|
|
class Payment |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* Scheme base path. |
37
|
|
|
*/ |
38
|
|
|
const SCHEME_PATH = 'weixin://wxpay/bizpayurl'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var API |
42
|
|
|
*/ |
43
|
|
|
protected $api; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Merchant instance. |
47
|
|
|
* |
48
|
|
|
* @var \EasyWeChat\Payment\Merchant |
49
|
|
|
*/ |
50
|
|
|
protected $merchant; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Constructor. |
54
|
|
|
* |
55
|
|
|
* @param Merchant $merchant |
56
|
|
|
*/ |
57
|
9 |
|
public function __construct(Merchant $merchant) |
58
|
|
|
{ |
59
|
9 |
|
$this->merchant = $merchant; |
60
|
9 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Build payment scheme for product. |
64
|
|
|
* |
65
|
|
|
* @param string $productId |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
1 |
|
public function scheme($productId) |
70
|
|
|
{ |
71
|
|
|
$params = [ |
72
|
1 |
|
'appid' => $this->merchant->app_id, |
73
|
1 |
|
'mch_id' => $this->merchant->merchant_id, |
74
|
1 |
|
'time_stamp' => time(), |
75
|
1 |
|
'nonce_str' => uniqid(), |
76
|
1 |
|
'product_id' => $productId, |
77
|
1 |
|
]; |
78
|
|
|
|
79
|
1 |
|
$params['sign'] = generate_sign($params, $this->merchant->key, 'md5'); |
80
|
|
|
|
81
|
1 |
|
return self::SCHEME_PATH.'?'.http_build_query($params); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Handle payment notify. |
86
|
|
|
* |
87
|
|
|
* @param callable $callback |
88
|
|
|
* |
89
|
|
|
* @return Response |
90
|
|
|
*/ |
91
|
2 |
|
public function handleNotify(callable $callback) |
92
|
|
|
{ |
93
|
2 |
|
$notify = $this->getNotify(); |
94
|
|
|
|
95
|
2 |
|
if (!$notify->isValid()) { |
96
|
1 |
|
throw new FaultException('Invalid request XML.', 400); |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
$notify = $notify->getNotify(); |
100
|
1 |
|
$successful = $notify->get('result_code') === 'SUCCESS'; |
101
|
|
|
|
102
|
1 |
|
$handleResult = call_user_func_array($callback, [$notify, $successful]); |
103
|
|
|
|
104
|
1 |
|
if (is_bool($handleResult) && $handleResult) { |
105
|
|
|
$response = [ |
106
|
1 |
|
'return_code' => 'SUCCESS', |
107
|
1 |
|
'return_msg' => 'OK', |
108
|
1 |
|
]; |
109
|
1 |
|
} else { |
110
|
|
|
$response = [ |
111
|
1 |
|
'return_code' => 'FAIL', |
112
|
1 |
|
'return_msg' => $handleResult, |
113
|
1 |
|
]; |
114
|
|
|
} |
115
|
|
|
|
116
|
1 |
|
return new Response(XML::build($response)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* [WeixinJSBridge] Generate js config for payment. |
121
|
|
|
* |
122
|
|
|
* <pre> |
123
|
|
|
* WeixinJSBridge.invoke( |
124
|
|
|
* 'getBrandWCPayRequest', |
125
|
|
|
* ... |
126
|
|
|
* ); |
127
|
|
|
* </pre> |
128
|
|
|
* |
129
|
|
|
* @param string $prepayId |
130
|
|
|
* @param bool $json |
131
|
|
|
* |
132
|
|
|
* @return string|array |
133
|
|
|
*/ |
134
|
2 |
|
public function configForPayment($prepayId, $json = true) |
135
|
|
|
{ |
136
|
|
|
$params = [ |
137
|
2 |
|
'appId' => $this->merchant->app_id, |
138
|
2 |
|
'timeStamp' => strval(time()), |
139
|
2 |
|
'nonceStr' => uniqid(), |
140
|
2 |
|
'package' => "prepay_id=$prepayId", |
141
|
2 |
|
'signType' => 'MD5', |
142
|
2 |
|
]; |
143
|
|
|
|
144
|
2 |
|
$params['paySign'] = generate_sign($params, $this->merchant->key, 'md5'); |
145
|
|
|
|
146
|
2 |
|
return $json ? json_encode($params) : $params; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* [JSSDK] Generate js config for payment. |
151
|
|
|
* |
152
|
|
|
* <pre> |
153
|
|
|
* wx.chooseWXPay({...}); |
154
|
|
|
* </pre> |
155
|
|
|
* |
156
|
|
|
* @param string $prepayId |
157
|
|
|
* @param bool $json |
|
|
|
|
158
|
|
|
* |
159
|
|
|
* @return array|string |
160
|
|
|
*/ |
161
|
1 |
|
public function configForJSSDKPayment($prepayId) |
162
|
|
|
{ |
163
|
1 |
|
$config = $this->configForPayment($prepayId, false); |
164
|
|
|
|
165
|
1 |
|
$config['timestamp'] = $config['timeStamp']; |
166
|
1 |
|
unset($config['timeStamp']); |
167
|
|
|
|
168
|
1 |
|
return $config; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Generate app payment parameters. |
173
|
|
|
* |
174
|
|
|
* @param string $prepayId |
175
|
|
|
* |
176
|
|
|
* @return array |
177
|
|
|
*/ |
178
|
1 |
|
public function configForAppPayment($prepayId) |
179
|
|
|
{ |
180
|
|
|
$params = [ |
181
|
1 |
|
'appid' => $this->merchant->app_id, |
182
|
1 |
|
'partnerid' => $this->merchant->merchant_id, |
183
|
1 |
|
'prepayid' => $prepayId, |
184
|
1 |
|
'noncestr' => uniqid(), |
185
|
1 |
|
'timestamp' => time(), |
186
|
1 |
|
'package' => 'Sign=WXPay', |
187
|
1 |
|
]; |
188
|
|
|
|
189
|
1 |
|
$params['sign'] = generate_sign($params, $this->merchant->key); |
190
|
|
|
|
191
|
1 |
|
return $params; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Generate js config for share user address. |
196
|
|
|
* |
197
|
|
|
* @param string|\Overtrue\Socialite\AccessTokenInterface $accessToken |
198
|
|
|
* @param bool $json |
199
|
|
|
* |
200
|
|
|
* @return string|array |
201
|
|
|
*/ |
202
|
1 |
|
public function configForShareAddress($accessToken, $json = true) |
203
|
|
|
{ |
204
|
1 |
|
if ($accessToken instanceof AccessTokenInterface) { |
205
|
1 |
|
$accessToken = $accessToken->getToken(); |
206
|
1 |
|
} |
207
|
|
|
|
208
|
|
|
$params = [ |
209
|
1 |
|
'appId' => $this->merchant->app_id, |
210
|
1 |
|
'scope' => 'jsapi_address', |
211
|
1 |
|
'timeStamp' => strval(time()), |
212
|
1 |
|
'nonceStr' => uniqid(), |
213
|
1 |
|
'signType' => 'SHA1', |
214
|
1 |
|
]; |
215
|
|
|
|
216
|
|
|
$signParams = [ |
217
|
1 |
|
'appid' => $params['appId'], |
218
|
1 |
|
'url' => UrlHelper::current(), |
219
|
1 |
|
'timestamp' => $params['timeStamp'], |
220
|
1 |
|
'noncestr' => $params['nonceStr'], |
221
|
1 |
|
'accesstoken' => strval($accessToken), |
222
|
1 |
|
]; |
223
|
|
|
|
224
|
1 |
|
ksort($signParams); |
225
|
|
|
|
226
|
1 |
|
$params['addrSign'] = sha1(urldecode(http_build_query($signParams))); |
227
|
|
|
|
228
|
1 |
|
return $json ? json_encode($params) : $params; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Merchant setter. |
233
|
|
|
* |
234
|
|
|
* @param Merchant $merchant |
235
|
|
|
*/ |
236
|
1 |
|
public function setMerchant(Merchant $merchant) |
237
|
|
|
{ |
238
|
1 |
|
$this->merchant = $merchant; |
239
|
1 |
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Merchant getter. |
243
|
|
|
* |
244
|
|
|
* @return Merchant |
245
|
|
|
*/ |
246
|
1 |
|
public function getMerchant() |
247
|
|
|
{ |
248
|
1 |
|
return $this->merchant; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Return Notify instance. |
253
|
|
|
* |
254
|
|
|
* @return \EasyWeChat\Payment\Notify |
255
|
|
|
*/ |
256
|
1 |
|
public function getNotify() |
257
|
|
|
{ |
258
|
1 |
|
return new Notify($this->merchant); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* API setter. |
263
|
|
|
* |
264
|
|
|
* @param API $api |
265
|
|
|
*/ |
266
|
1 |
|
public function setAPI(API $api) |
267
|
|
|
{ |
268
|
1 |
|
$this->api = $api; |
269
|
1 |
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Return API instance. |
273
|
|
|
* |
274
|
|
|
* @return API |
275
|
|
|
*/ |
276
|
1 |
|
public function getAPI() |
277
|
|
|
{ |
278
|
1 |
|
return $this->api ?: $this->api = new API($this->getMerchant()); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Magic call. |
283
|
|
|
* |
284
|
|
|
* @param string $method |
285
|
|
|
* @param array $args |
286
|
|
|
* |
287
|
|
|
* @return mixed |
288
|
|
|
* |
289
|
|
|
* @codeCoverageIgnore |
290
|
|
|
*/ |
291
|
|
|
public function __call($method, $args) |
292
|
|
|
{ |
293
|
|
|
if (is_callable([$this->getAPI(), $method])) { |
294
|
|
|
return call_user_func_array([$this->api, $method], $args); |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.