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