Completed
Pull Request — master (#1556)
by wannanbigpig
07:00 queued 03:07
created

Client   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 80
ccs 0
cts 32
cp 0
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 38 4
A decrypt() 0 17 3
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 \EasyWeChat\MicroMerchant\Kernel\Exceptions\InvalidSignException
37
     */
38
    public function get(bool $returnRaw = false)
39
    {
40
        $params = [
41
            'sign_type' => 'HMAC-SHA256',
42
            'nonce_str' => uniqid('micro'),
43
        ];
44
45
        $responseType = $this->app->config->get('response_type');
46
        if (true === $returnRaw) {
47
            return $this->requestRaw('risk/getcertficates', $params);
48
        }
49
50
        $this->app->config->set('response_type', 'array');
51
        $response = $this->request('risk/getcertficates', $params);
52
        $this->app->config->set('response_type', $responseType);
53
54
        if ('SUCCESS' !== $response['return_code']) {
55
            throw new InvalidArgumentException(
56
                sprintf(
57
                    'Failed to get certificate. return_code_msg: "%s" .',
58
                    $response['return_code'].'('.$response['return_msg'].')'
59
                )
60
            );
61
        }
62
        if ('SUCCESS' !== $response['result_code']) {
63
            throw new InvalidArgumentException(
64
                sprintf(
65
                    'Failed to get certificate. result_err_code_des: "%s" .',
66
                    $response['result_code'].'('.$response['err_code'].'['.$response['err_code_des'].'])'
67
                )
68
            );
69
        }
70
        $certificates = \GuzzleHttp\json_decode($response['certificates'], true)['data'][0];
71
        $ciphertext = $this->decrypt($certificates['encrypt_certificate']);
72
        unset($certificates['encrypt_certificate']);
73
        $certificates['certificates'] = $ciphertext;
74
75
        return $certificates;
76
    }
77
78
    /**
79
     * decrypt ciphertext.
80
     *
81
     * @param array $encryptCertificate
82
     *
83
     * @return string
84
     *
85
     * @throws \EasyWeChat\MicroMerchant\Kernel\Exceptions\InvalidExtensionException
86
     */
87
    public function decrypt(array $encryptCertificate)
88
    {
89
        if (false === extension_loaded('sodium')) {
90
            throw new InvalidExtensionException('sodium extension is not installed,Reference link https://php.net/manual/zh/book.sodium.php');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 140 characters; contains 142 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
91
        }
92
93
        if (false === sodium_crypto_aead_aes256gcm_is_available()) {
94
            throw new InvalidExtensionException('aes256gcm is not currently supported');
95
        }
96
97
        // sodium_crypto_aead_aes256gcm_decrypt function needs to open libsodium extension.
98
        // https://www.php.net/manual/zh/function.sodium-crypto-aead-aes256gcm-decrypt.php
99
        return sodium_crypto_aead_aes256gcm_decrypt(
100
            base64_decode($encryptCertificate['ciphertext'], true),
101
            $encryptCertificate['associated_data'],
102
            $encryptCertificate['nonce'],
103
            $this->app['config']->apiv3_key
104
        );
105
    }
106
}
107