Passed
Push — master ( ac2972...bd56ba )
by Songda
01:56
created

InvokePrepayPlugin::getAppId()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 10
rs 10
cc 3
nc 2
nop 1
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\InvalidResponseException;
11
use Yansongda\Pay\Logger;
12
use Yansongda\Pay\Pay;
13
use Yansongda\Pay\Rocket;
14
use Yansongda\Supports\Collection;
15
use Yansongda\Supports\Config;
16
use Yansongda\Supports\Str;
17
18
class InvokePrepayPlugin implements PluginInterface
19
{
20
    /**
21
     * @throws \Yansongda\Pay\Exception\ContainerException
22
     * @throws \Yansongda\Pay\Exception\InvalidResponseException
23
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
24
     * @throws \Exception
25
     */
26
    public function assembly(Rocket $rocket, Closure $next): Rocket
27
    {
28
        /* @var Rocket $rocket */
29
        $rocket = $next($rocket);
30
31
        Logger::info('[wechat][InvokePrepayPlugin] 插件开始装载', ['rocket' => $rocket]);
32
33
        $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

33
        $prepayId = $rocket->getDestination()->/** @scrutinizer ignore-call */ get('prepay_id');
Loading history...
34
35
        if (is_null($prepayId)) {
36
            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

36
            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...
37
38
            throw new InvalidResponseException(Exception::RESPONSE_MISSING_NECESSARY_PARAMS, 'Prepay Response Error: Missing PrepayId', $rocket->getDestination()->all());
39
        }
40
41
        $config = $this->getInvokeConfig($rocket, $prepayId);
42
43
        $rocket->setDestination($config);
44
45
        Logger::info('[wechat][InvokePrepayPlugin] 插件装载完毕', ['rocket' => $rocket]);
46
47
        return $rocket;
48
    }
49
50
    /**
51
     * @throws \Yansongda\Pay\Exception\ContainerException
52
     * @throws \Yansongda\Pay\Exception\InvalidConfigException
53
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
54
     */
55
    protected function getSign(Collection $invokeConfig, array $params): string
56
    {
57
        $contents = $invokeConfig->get('appId', '')."\n".
58
            $invokeConfig->get('timeStamp', '')."\n".
59
            $invokeConfig->get('nonceStr', '')."\n".
60
            $invokeConfig->get('package', '')."\n";
61
62
        return get_wechat_sign($params, $contents);
63
    }
64
65
    /**
66
     * @throws \Yansongda\Pay\Exception\ContainerException
67
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
68
     * @throws \Exception
69
     */
70
    protected function getInvokeConfig(Rocket $rocket, string $prepayId): Config
71
    {
72
        $config = new Config([
73
            'appId' => $this->getAppId($rocket),
74
            'timeStamp' => time().'',
75
            'nonceStr' => Str::random(32),
76
            'package' => 'prepay_id='.$prepayId,
77
            'signType' => 'RSA',
78
        ]);
79
80
        $config->set('paySign', $this->getSign($config, $rocket->getParams()));
81
82
        return $config;
83
    }
84
85
    /**
86
     * @throws \Yansongda\Pay\Exception\ContainerException
87
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
88
     */
89
    protected function getAppId(Rocket $rocket): string
90
    {
91
        $config = get_wechat_config($rocket->getParams());
92
        $payload = $rocket->getPayload();
93
94
        if (Pay::MODE_SERVICE == $config->get('mode') && $payload->has('sub_appid')) {
95
            return $payload->get('sub_appid', '');
96
        }
97
98
        return $config->get($this->getConfigKey(), '');
99
    }
100
101
    protected function getConfigKey(): string
102
    {
103
        return 'mp_app_id';
104
    }
105
}
106