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