Passed
Pull Request — master (#753)
by Songda
01:51
created

PrepayPlugin::getConfigKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 8
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\Pay\Common;
6
7
use function Yansongda\Pay\get_wechat_config;
8
9
use Yansongda\Pay\Pay;
10
use Yansongda\Pay\Plugin\Wechat\GeneralPlugin;
11
use Yansongda\Pay\Rocket;
12
13
class PrepayPlugin extends GeneralPlugin
14
{
15
    protected function getUri(Rocket $rocket): string
16
    {
17
        return 'v3/pay/transactions/jsapi';
18
    }
19
20
    protected function getPartnerUri(Rocket $rocket): string
21
    {
22
        return 'v3/pay/partner/transactions/jsapi';
23
    }
24
25
    /**
26
     * @throws \Yansongda\Pay\Exception\ContainerException
27
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
28
     */
29
    protected function doSomething(Rocket $rocket): void
30
    {
31
        $config = get_wechat_config($rocket->getParams());
32
33
        $wechatId = $this->getWechatId($config, $rocket);
34
35
        if (!$rocket->getPayload()->has('notify_url')) {
36
            $wechatId['notify_url'] = $config['notify_url'] ?? null;
37
        }
38
39
        $rocket->mergePayload($wechatId);
40
    }
41
42
    protected function getWechatId(array $config, Rocket $rocket): array
43
    {
44
        $payload = $rocket->getPayload();
45
        $configKey = $this->getConfigKey($rocket->getParams());
46
47
        $result = [
48
            'appid' => $config[$configKey] ?? '',
49
            'mchid' => $config['mch_id'] ?? '',
50
        ];
51
52
        if (Pay::MODE_SERVICE === ($config['mode'] ?? null)) {
53
            $result = [
54
                'sp_appid' => $config[$configKey] ?? '',
55
                'sp_mchid' => $config['mch_id'] ?? '',
56
                'sub_mchid' => $payload->get('sub_mchid', $config['sub_mch_id'] ?? null),
57
            ];
58
59
            $subAppId = $payload->get('sub_appid', $config['sub_'.$configKey] ?? null);
60
61
            if (!empty($subAppId)) {
62
                $result['sub_appid'] = $subAppId;
63
            }
64
        }
65
66
        return $result;
67
    }
68
}
69