Passed
Pull Request — master (#542)
by Songda
02:05
created

CreatePlugin::mergeAppId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 13
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\Fund\Transfer;
6
7
use Yansongda\Pay\Pay;
8
use Yansongda\Pay\Plugin\Wechat\GeneralPlugin;
9
use Yansongda\Pay\Rocket;
10
use Yansongda\Supports\Config;
11
12
class CreatePlugin extends GeneralPlugin
13
{
14
    /**
15
     * @throws \Yansongda\Pay\Exception\ContainerDependencyException
16
     * @throws \Yansongda\Pay\Exception\ContainerException
17
     * @throws \Yansongda\Pay\Exception\InvalidConfigException
18
     * @throws \Yansongda\Pay\Exception\InvalidParamsException
19
     * @throws \Yansongda\Pay\Exception\InvalidResponseException
20
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
21
     */
22
    protected function doSomething(Rocket $rocket): void
23
    {
24
        $params = $rocket->getParams();
25
        $config = get_wechat_config($params);
26
27
        if (empty($config->get('wechat_public_cert_path'))) {
28
            reload_wechat_public_certs($params);
29
        }
30
31
        $certs = $config->get('wechat_public_cert_path');
32
33
        if (empty($params['_serial_no'])) {
34
            mt_srand();
35
            $params['_serial_no'] = array_rand($certs);
0 ignored issues
show
Bug introduced by
It seems like $certs can also be of type null; however, parameter $array of array_rand() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
            $params['_serial_no'] = array_rand(/** @scrutinizer ignore-type */ $certs);
Loading history...
36
            $rocket->setParams($params);
37
        }
38
39
        $rocket->mergePayload($this->mergeAppId($config));
40
        $rocket->mergePayload([
41
            'transfer_detail_list' => $this->mergeEncryptUserName($params, $params['_serial_no'])
42
        ]);
43
    }
44
45
    protected function getUri(Rocket $rocket): string
46
    {
47
        return 'v3/transfer/batches';
48
    }
49
50
    protected function getPartnerUri(Rocket $rocket): string
51
    {
52
        return 'v3/partner-transfer/batches';
53
    }
54
55
    protected function mergeAppId(Config $config): array
56
    {
57
        $appId = [
58
            'appid' => $config->get('mp_app_id'),
59
        ];
60
61
        if (Pay::MODE_SERVICE == $config->get('mode')) {
62
            $appId = [
63
                'sub_mchid' => $config->get('sub_mch_id', ''),
64
            ];
65
        }
66
67
        return $appId;
68
    }
69
70
    /**
71
     * @throws \Yansongda\Pay\Exception\ContainerDependencyException
72
     * @throws \Yansongda\Pay\Exception\ContainerException
73
     * @throws \Yansongda\Pay\Exception\InvalidParamsException
74
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
75
     */
76
    protected function mergeEncryptUserName(array $params, string $serialNo): array
77
    {
78
        $lists = $params['transfer_detail_list'] ?? [];
79
80
        if (empty(reset($lists)['user_name'])) {
81
            return $lists;
82
        }
83
84
        foreach ($lists as $key => $list) {
85
            $lists[$key]['user_name'] = encrypt_wechat_contents($params, $list['user_name'], $serialNo);
86
        }
87
88
        return $lists;
89
    }
90
}
91