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\Certficates; |
13
|
|
|
|
14
|
|
|
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException; |
15
|
|
|
use EasyWeChat\MicroMerchant\Kernel\BaseClient; |
16
|
|
|
use EasyWeChat\MicroMerchant\Kernel\Exceptions\InvalidExtensionException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Client. |
20
|
|
|
* |
21
|
|
|
* @author liuml <[email protected]> |
22
|
|
|
* @DateTime 2019-05-30 14:19 |
23
|
|
|
*/ |
24
|
|
|
class Client extends BaseClient |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* get certficates. |
28
|
|
|
* |
29
|
|
|
* @param bool $returnRaw |
30
|
|
|
* |
31
|
|
|
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string |
32
|
|
|
* |
33
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
34
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
35
|
|
|
* @throws \EasyWeChat\MicroMerchant\Kernel\Exceptions\InvalidExtensionException |
36
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
37
|
|
|
*/ |
38
|
|
|
public function get(bool $returnRaw = false) |
39
|
|
|
{ |
40
|
|
|
$params = [ |
41
|
|
|
'sign_type' => 'HMAC-SHA256', |
42
|
|
|
'nonce_str' => uniqid('micro'), |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
if (true === $returnRaw) { |
46
|
|
|
return $this->requestRaw('risk/getcertficates', $params); |
47
|
|
|
} |
48
|
|
|
/** @var array $response */ |
49
|
|
|
$response = $this->requestArray('risk/getcertficates', $params); |
50
|
|
|
|
51
|
|
|
if ('SUCCESS' !== $response['return_code']) { |
52
|
|
|
throw new InvalidArgumentException(sprintf('Failed to get certificate. return_code_msg: "%s" .', $response['return_code'].'('.$response['return_msg'].')')); |
53
|
|
|
} |
54
|
|
|
if ('SUCCESS' !== $response['result_code']) { |
55
|
|
|
throw new InvalidArgumentException(sprintf('Failed to get certificate. result_err_code_desc: "%s" .', $response['result_code'].'('.$response['err_code'].'['.$response['err_code_desc'].'])')); |
56
|
|
|
} |
57
|
|
|
$certificates = \GuzzleHttp\json_decode($response['certificates'], true)['data'][0]; |
|
|
|
|
58
|
|
|
$ciphertext = $this->decrypt($certificates['encrypt_certificate']); |
59
|
|
|
unset($certificates['encrypt_certificate']); |
60
|
|
|
$certificates['certificates'] = $ciphertext; |
61
|
|
|
|
62
|
|
|
return $certificates; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* decrypt ciphertext. |
67
|
|
|
* |
68
|
|
|
* @param array $encryptCertificate |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
* |
72
|
|
|
* @throws \EasyWeChat\MicroMerchant\Kernel\Exceptions\InvalidExtensionException |
73
|
|
|
*/ |
74
|
|
|
public function decrypt(array $encryptCertificate) |
75
|
|
|
{ |
76
|
|
|
if (false === extension_loaded('sodium')) { |
77
|
|
|
throw new InvalidExtensionException('sodium extension is not installed,Reference link https://php.net/manual/zh/book.sodium.php'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (false === sodium_crypto_aead_aes256gcm_is_available()) { |
81
|
|
|
throw new InvalidExtensionException('aes256gcm is not currently supported'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// sodium_crypto_aead_aes256gcm_decrypt function needs to open libsodium extension. |
85
|
|
|
// https://www.php.net/manual/zh/function.sodium-crypto-aead-aes256gcm-decrypt.php |
86
|
|
|
return sodium_crypto_aead_aes256gcm_decrypt( |
87
|
|
|
base64_decode($encryptCertificate['ciphertext'], true), |
88
|
|
|
$encryptCertificate['associated_data'], |
89
|
|
|
$encryptCertificate['nonce'], |
90
|
|
|
$this->app['config']->apiv3_key |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.