Passed
Pull Request — master (#763)
by
unknown
04:03 queued 02:05
created

InvokePrepayV2Plugin::getSign()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 2
b 0
f 0
nc 2
nop 2
dl 0
loc 12
rs 10
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 = implode('&', $data).'&key='.$secret;
69
70
        return md5($contents);
71
    }
72
73
    /**
74
     * @throws \Yansongda\Pay\Exception\ContainerException
75
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
76
     * @throws \Exception
77
     */
78
    protected function getInvokeConfig(Rocket $rocket, string $prepayId): Config
79
    {
80
        $config = new Config([
81
            'appId' => $this->getAppId($rocket),
82
            'timeStamp' => time().'',
83
            'nonceStr' => Str::random(32),
84
            'package' => 'prepay_id='.$prepayId,
85
            'signType' => 'MD5',
86
        ]);
87
88
        $config->set('paySign', $this->getSign($config, $rocket->getParams()));
89
90
        return $config;
91
    }
92
93
    /**
94
     * @throws \Yansongda\Pay\Exception\ContainerException
95
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
96
     */
97
    protected function getAppId(Rocket $rocket): string
98
    {
99
        $config = get_wechat_config($rocket->getParams());
100
        $payload = $rocket->getPayload();
101
102
        if (Pay::MODE_SERVICE === ($config['mode'] ?? null) && $payload->has('sub_appid')) {
103
            return $payload->get('sub_appid', '');
104
        }
105
106
        return $config[$this->getConfigKey()] ?? '';
107
    }
108
109
    protected function getConfigKey(): string
110
    {
111
        return 'mp_app_id';
112
    }
113
}
114