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\MicroMerchant; |
13
|
|
|
|
14
|
|
|
use EasyWeChat\BasicService; |
15
|
|
|
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException; |
16
|
|
|
use EasyWeChat\Kernel\ServiceContainer; |
17
|
|
|
use EasyWeChat\Kernel\Support; |
18
|
|
|
use EasyWeChat\MicroMerchant\Kernel\Exceptions\InvalidSignException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class Application. |
22
|
|
|
* |
23
|
|
|
* @author liuml <[email protected]> |
24
|
|
|
* |
25
|
|
|
* @property \EasyWeChat\MicroMerchant\Certficates\Client $certficates |
26
|
|
|
* @property \EasyWeChat\MicroMerchant\Material\Client $material |
27
|
|
|
* @property \EasyWeChat\MicroMerchant\MerchantConfig\Client $merchantConfig |
28
|
|
|
* @property \EasyWeChat\MicroMerchant\Withdraw\Client $withdraw |
29
|
|
|
* @property \EasyWeChat\MicroMerchant\Media\Client $media |
30
|
|
|
* |
31
|
|
|
* @method mixed applyForEnter(array $params) |
32
|
|
|
* @method mixed getStatus(string $applymentId, string $businessCode = '') |
33
|
|
|
* @method mixed upgrade(array $params) |
34
|
|
|
* @method mixed getUpgradeStatus(string $subMchId = '') |
35
|
|
|
*/ |
36
|
|
|
class Application extends ServiceContainer |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $providers = [ |
42
|
|
|
// Base services |
43
|
|
|
BasicService\Media\ServiceProvider::class, |
44
|
|
|
Base\ServiceProvider::class, |
45
|
|
|
Certficates\ServiceProvider::class, |
46
|
|
|
MerchantConfig\ServiceProvider::class, |
47
|
|
|
Material\ServiceProvider::class, |
48
|
|
|
Withdraw\ServiceProvider::class, |
49
|
|
|
Media\ServiceProvider::class, |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
protected $defaultConfig = [ |
56
|
|
|
'http' => [ |
57
|
|
|
'base_uri' => 'https://api.mch.weixin.qq.com/', |
58
|
|
|
], |
59
|
|
|
'log' => [ |
60
|
|
|
'default' => 'dev', // 默认使用的 channel,生产环境可以改为下面的 prod |
61
|
|
|
'channels' => [ |
62
|
|
|
// 测试环境 |
63
|
|
|
'dev' => [ |
64
|
|
|
'driver' => 'single', |
65
|
|
|
'path' => '/tmp/easywechat.log', |
66
|
|
|
'level' => 'debug', |
67
|
|
|
], |
68
|
|
|
// 生产环境 |
69
|
|
|
'prod' => [ |
70
|
|
|
'driver' => 'daily', |
71
|
|
|
'path' => '/tmp/easywechat.log', |
72
|
|
|
'level' => 'info', |
73
|
|
|
], |
74
|
|
|
], |
75
|
|
|
], |
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return string |
80
|
|
|
* |
81
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
82
|
|
|
*/ |
83
|
7 |
|
public function getKey() |
84
|
|
|
{ |
85
|
7 |
|
$key = $this['config']->key; |
86
|
|
|
|
87
|
7 |
|
if (empty($key)) { |
88
|
|
|
throw new InvalidArgumentException('config key connot be empty.'); |
89
|
|
|
} |
90
|
|
|
|
91
|
7 |
|
if (32 !== strlen($key)) { |
92
|
1 |
|
throw new InvalidArgumentException(sprintf("'%s' should be 32 chars length.", $key)); |
93
|
|
|
} |
94
|
|
|
|
95
|
7 |
|
return $key; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* get certficates. |
100
|
|
|
* |
101
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
102
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
103
|
|
|
* @throws \EasyWeChat\MicroMerchant\Kernel\Exceptions\InvalidExtensionException |
104
|
|
|
* @throws \EasyWeChat\MicroMerchant\Kernel\Exceptions\InvalidSignException |
105
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
106
|
|
|
*/ |
107
|
1 |
|
public function getCertficates() |
108
|
|
|
{ |
109
|
1 |
|
return $this->certficates->get(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* set sub-mch-id and appid. |
114
|
|
|
* |
115
|
|
|
* @param string $subMchId Identification Number of Small and Micro Businessmen Reported by Service Providers |
116
|
|
|
* @param string $appid Public Account ID of Service Provider |
117
|
|
|
* |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
1 |
|
public function setSubMchId(string $subMchId, string $appid = '') |
121
|
|
|
{ |
122
|
1 |
|
$this['config']->set('sub_mch_id', $subMchId); |
123
|
1 |
|
$this['config']->set('appid', $appid); |
124
|
|
|
|
125
|
1 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Returning true indicates that the verification is successful, |
130
|
|
|
* returning false indicates that the signature field does not exist or is empty, |
131
|
|
|
* and if the signature verification is wrong, the InvalidSignException will be thrown directly. |
132
|
|
|
* |
133
|
|
|
* @param array $data |
134
|
|
|
* |
135
|
|
|
* @return bool |
136
|
|
|
* |
137
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
138
|
|
|
* @throws \EasyWeChat\MicroMerchant\Kernel\Exceptions\InvalidSignException |
139
|
|
|
*/ |
140
|
5 |
|
public function verifySignature(array $data) |
141
|
|
|
{ |
142
|
5 |
|
if (!isset($data['sign']) || empty($data['sign'])) { |
143
|
5 |
|
return false; |
144
|
|
|
} |
145
|
|
|
|
146
|
1 |
|
$sign = $data['sign']; |
|
|
|
|
147
|
1 |
|
strlen($sign) > 32 && $signType = 'HMAC-SHA256'; |
148
|
1 |
|
unset($data['sign']); |
149
|
1 |
|
$secretKey = $this->getKey(); |
150
|
|
|
|
151
|
1 |
|
if ('HMAC-SHA256' === ($signType ?? 'MD5')) { |
152
|
|
|
$encryptMethod = function ($str) use ($secretKey) { |
153
|
|
|
return hash_hmac('sha256', $str, $secretKey); |
154
|
|
|
}; |
155
|
|
|
} else { |
156
|
1 |
|
$encryptMethod = 'md5'; |
157
|
|
|
} |
158
|
|
|
|
159
|
1 |
|
if (Support\generate_sign($data, $secretKey, $encryptMethod) === $sign) { |
|
|
|
|
160
|
1 |
|
return true; |
161
|
|
|
} |
162
|
|
|
|
163
|
1 |
|
throw new InvalidSignException('return value signature verification error'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param string $name |
168
|
|
|
* @param array $arguments |
169
|
|
|
* |
170
|
|
|
* @return mixed |
171
|
|
|
*/ |
172
|
|
|
public function __call($name, $arguments) |
173
|
|
|
{ |
174
|
|
|
return call_user_func_array([$this['base'], $name], $arguments); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.