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
|
|
|
namespace EasyWeChat\Payment; |
13
|
|
|
|
14
|
|
|
use Closure; |
15
|
|
|
use EasyWeChat\BasicService; |
16
|
|
|
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException; |
17
|
|
|
use EasyWeChat\Kernel\ServiceContainer; |
18
|
|
|
use EasyWeChat\Kernel\Support; |
19
|
|
|
use EasyWeChat\OfficialAccount; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class Application. |
23
|
|
|
* |
24
|
|
|
* @property \EasyWeChat\Payment\Bill\Client $bill |
25
|
|
|
* @property \EasyWeChat\Payment\Jssdk\Client $jssdk |
26
|
|
|
* @property \EasyWeChat\Payment\Order\Client $order |
27
|
|
|
* @property \EasyWeChat\Payment\Refund\Client $refund |
28
|
|
|
* @property \EasyWeChat\Payment\Coupon\Client $coupon |
29
|
|
|
* @property \EasyWeChat\Payment\Reverse\Client $reverse |
30
|
|
|
* @property \EasyWeChat\Payment\Redpack\Client $redpack |
31
|
|
|
* @property \EasyWeChat\BasicService\Url\Client $url |
32
|
|
|
* @property \EasyWeChat\Payment\Transfer\Client $transfer |
33
|
|
|
* @property \EasyWeChat\Payment\Security\Client $security |
34
|
|
|
* @property \EasyWeChat\Payment\ProfitSharing\Client $profit_sharing |
35
|
|
|
* @property \EasyWeChat\OfficialAccount\Auth\AccessToken $access_token |
36
|
|
|
* |
37
|
|
|
* @method mixed pay(array $attributes) |
38
|
|
|
* @method mixed authCodeToOpenid(string $authCode) |
39
|
|
|
*/ |
40
|
|
|
class Application extends ServiceContainer |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $providers = [ |
46
|
|
|
OfficialAccount\Auth\ServiceProvider::class, |
47
|
|
|
BasicService\Url\ServiceProvider::class, |
48
|
|
|
Base\ServiceProvider::class, |
49
|
|
|
Bill\ServiceProvider::class, |
50
|
|
|
Coupon\ServiceProvider::class, |
51
|
|
|
Jssdk\ServiceProvider::class, |
52
|
|
|
Merchant\ServiceProvider::class, |
53
|
|
|
Order\ServiceProvider::class, |
54
|
|
|
Redpack\ServiceProvider::class, |
55
|
|
|
Refund\ServiceProvider::class, |
56
|
|
|
Reverse\ServiceProvider::class, |
57
|
|
|
Sandbox\ServiceProvider::class, |
58
|
|
|
Transfer\ServiceProvider::class, |
59
|
|
|
Security\ServiceProvider::class, |
60
|
|
|
ProfitSharing\ServiceProvider::class, |
61
|
|
|
]; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var array |
65
|
|
|
*/ |
66
|
|
|
protected $defaultConfig = [ |
67
|
|
|
'http' => [ |
68
|
|
|
'base_uri' => 'https://api.mch.weixin.qq.com/', |
69
|
|
|
], |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Build payment scheme for product. |
74
|
|
|
* |
75
|
|
|
* @param string $productId |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
1 |
|
public function scheme(string $productId): string |
80
|
|
|
{ |
81
|
|
|
$params = [ |
82
|
1 |
|
'appid' => $this['config']->app_id, |
83
|
1 |
|
'mch_id' => $this['config']->mch_id, |
84
|
1 |
|
'time_stamp' => time(), |
85
|
1 |
|
'nonce_str' => uniqid(), |
86
|
1 |
|
'product_id' => $productId, |
87
|
|
|
]; |
88
|
|
|
|
89
|
1 |
|
$params['sign'] = Support\generate_sign($params, $this['config']->key); |
|
|
|
|
90
|
|
|
|
91
|
1 |
|
return 'weixin://wxpay/bizpayurl?'.http_build_query($params); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $codeUrl |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
1 |
|
public function codeUrlScheme(string $codeUrl) |
100
|
|
|
{ |
101
|
1 |
|
return \sprintf('weixin://wxpay/bizpayurl?sr=%s', $codeUrl); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param \Closure $closure |
106
|
|
|
* |
107
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
108
|
|
|
* |
109
|
|
|
* @codeCoverageIgnore |
110
|
|
|
* |
111
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\Exception |
112
|
|
|
*/ |
113
|
|
|
public function handlePaidNotify(Closure $closure) |
114
|
|
|
{ |
115
|
|
|
return (new Notify\Paid($this))->handle($closure); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param \Closure $closure |
120
|
|
|
* |
121
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
122
|
|
|
* |
123
|
|
|
* @codeCoverageIgnore |
124
|
|
|
* |
125
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\Exception |
126
|
|
|
*/ |
127
|
|
|
public function handleRefundedNotify(Closure $closure) |
128
|
|
|
{ |
129
|
|
|
return (new Notify\Refunded($this))->handle($closure); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param \Closure $closure |
134
|
|
|
* |
135
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
136
|
|
|
* |
137
|
|
|
* @codeCoverageIgnore |
138
|
|
|
* |
139
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\Exception |
140
|
|
|
*/ |
141
|
|
|
public function handleScannedNotify(Closure $closure) |
142
|
|
|
{ |
143
|
|
|
return (new Notify\Scanned($this))->handle($closure); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Set sub-merchant. |
148
|
|
|
* |
149
|
|
|
* @param string $mchId |
150
|
|
|
* @param string|null $appId |
151
|
|
|
* |
152
|
|
|
* @return $this |
153
|
|
|
*/ |
154
|
1 |
|
public function setSubMerchant(string $mchId, string $appId = null) |
155
|
|
|
{ |
156
|
1 |
|
$this['config']->set('sub_mch_id', $mchId); |
157
|
1 |
|
$this['config']->set('sub_appid', $appId); |
158
|
|
|
|
159
|
1 |
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return bool |
164
|
|
|
*/ |
165
|
26 |
|
public function inSandbox(): bool |
166
|
|
|
{ |
167
|
26 |
|
return (bool) $this['config']->get('sandbox'); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param string|null $endpoint |
172
|
|
|
* |
173
|
|
|
* @return string |
174
|
|
|
* |
175
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
176
|
|
|
*/ |
177
|
10 |
|
public function getKey(string $endpoint = null) |
178
|
|
|
{ |
179
|
10 |
|
if ('sandboxnew/pay/getsignkey' === $endpoint) { |
180
|
1 |
|
return $this['config']->key; |
181
|
|
|
} |
182
|
|
|
|
183
|
10 |
|
$key = $this->inSandbox() ? $this['sandbox']->getKey() : $this['config']->key; |
184
|
|
|
|
185
|
10 |
|
if (empty($key)) { |
186
|
|
|
throw new InvalidArgumentException('config key should not empty.'); |
187
|
|
|
} |
188
|
|
|
|
189
|
10 |
|
if (32 !== strlen($key)) { |
190
|
1 |
|
throw new InvalidArgumentException(sprintf("'%s' should be 32 chars length.", $key)); |
191
|
|
|
} |
192
|
|
|
|
193
|
10 |
|
return $key; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param string $name |
198
|
|
|
* @param array $arguments |
199
|
|
|
* |
200
|
|
|
* @return mixed |
201
|
|
|
*/ |
202
|
1 |
|
public function __call($name, $arguments) |
203
|
|
|
{ |
204
|
1 |
|
return call_user_func_array([$this['base'], $name], $arguments); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|