Passed
Pull Request — master (#763)
by
unknown
02:15
created

InvokePrepayV2Plugin::getSign()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 3
b 0
f 0
nc 3
nop 2
dl 0
loc 16
rs 9.9332
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\Pay\Common;
6
7
use Closure;
8
use Yansongda\Pay\Contract\PluginInterface;
9
use Yansongda\Pay\Exception\Exception;
10
use Yansongda\Pay\Exception\InvalidConfigException;
11
use Yansongda\Pay\Exception\InvalidResponseException;
12
13
use function Yansongda\Pay\get_wechat_config;
14
15
use Yansongda\Pay\Logger;
16
use Yansongda\Pay\Pay;
17
use Yansongda\Pay\Rocket;
18
use Yansongda\Supports\Collection;
19
use Yansongda\Supports\Config;
20
use Yansongda\Supports\Str;
21
22
class InvokePrepayV2Plugin implements PluginInterface
23
{
24
    /**
25
     * @throws \Yansongda\Pay\Exception\ContainerException
26
     * @throws \Yansongda\Pay\Exception\InvalidResponseException
27
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
28
     * @throws \Exception
29
     */
30
    public function assembly(Rocket $rocket, Closure $next): Rocket
31
    {
32
        /* @var Rocket $rocket */
33
        $rocket = $next($rocket);
34
35
        Logger::debug('[wechat][InvokePrepayPlugin] 插件开始装载', ['rocket' => $rocket]);
36
37
        $prepayId = $rocket->getDestination()->get('prepay_id');
0 ignored issues
show
Bug introduced by
The method get() does not exist on Psr\Http\Message\MessageInterface. It seems like you code against a sub-type of Psr\Http\Message\MessageInterface such as Yansongda\Pay\Request. ( Ignorable by Annotation )

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

37
        $prepayId = $rocket->getDestination()->/** @scrutinizer ignore-call */ get('prepay_id');
Loading history...
38
39
        if (is_null($prepayId)) {
40
            Logger::error('[wechat][InvokePrepayPlugin] 预下单失败:响应缺少 prepay_id 参数,请自行检查参数是否符合微信要求', $rocket->getDestination()->all());
0 ignored issues
show
Bug introduced by
The method all() does not exist on Psr\Http\Message\MessageInterface. ( Ignorable by Annotation )

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

40
            Logger::error('[wechat][InvokePrepayPlugin] 预下单失败:响应缺少 prepay_id 参数,请自行检查参数是否符合微信要求', $rocket->getDestination()->/** @scrutinizer ignore-call */ all());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
41
42
            throw new InvalidResponseException(Exception::RESPONSE_MISSING_NECESSARY_PARAMS, 'Prepay Response Error: Missing PrepayId', $rocket->getDestination()->all());
43
        }
44
45
        $config = $this->getInvokeConfig($rocket, $prepayId);
46
47
        $rocket->setDestination($config);
48
49
        Logger::info('[wechat][InvokePrepayPlugin] 插件装载完毕', ['rocket' => $rocket]);
50
51
        return $rocket;
52
    }
53
54
    /**
55
     * @throws \Yansongda\Pay\Exception\ContainerException
56
     * @throws \Yansongda\Pay\Exception\InvalidConfigException
57
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
58
     */
59
    protected function getSign(Collection $invokeConfig, array $params): string
60
    {
61
        $secret = get_wechat_config($params)['mch_secret_key_v2'] ?? null;
62
        if (empty($secret)) {
63
            throw new InvalidConfigException(Exception::WECHAT_CONFIG_ERROR, 'Missing Wechat Config -- [mch_secret_key_v2]');
64
        }
65
66
        $data = $invokeConfig->toArray();
67
        ksort($data);
68
        $contents = '';
69
        foreach ($data as $key => $datum) {
70
            $contents .= $key.'='.$datum.'&';
71
        }
72
        $contents .= 'key='.$secret;
73
74
        return md5($contents);
75
    }
76
77
    /**
78
     * @throws \Yansongda\Pay\Exception\ContainerException
79
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
80
     * @throws \Exception
81
     */
82
    protected function getInvokeConfig(Rocket $rocket, string $prepayId): Config
83
    {
84
        $config = new Config([
85
            'appId' => $this->getAppId($rocket),
86
            'timeStamp' => time().'',
87
            'nonceStr' => Str::random(32),
88
            'package' => 'prepay_id='.$prepayId,
89
            'signType' => 'MD5',
90
        ]);
91
92
        $config->set('paySign', $this->getSign($config, $rocket->getParams()));
93
94
        return $config;
95
    }
96
97
    /**
98
     * @throws \Yansongda\Pay\Exception\ContainerException
99
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
100
     */
101
    protected function getAppId(Rocket $rocket): string
102
    {
103
        $config = get_wechat_config($rocket->getParams());
104
        $payload = $rocket->getPayload();
105
106
        if (Pay::MODE_SERVICE === ($config['mode'] ?? null) && $payload->has('sub_appid')) {
107
            return $payload->get('sub_appid', '');
108
        }
109
110
        return $config[$this->getConfigKey($rocket->getParams())] ?? '';
111
    }
112
113
    protected function getConfigKey(array $params): string
114
    {
115
        $type = $params['_type'] ?? 'mp';
116
117
        return $type.'_app_id';
118
    }
119
}
120