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

InvokePlugin   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 26
dl 0
loc 66
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A assembly() 0 25 2
A getInvokeConfig() 0 13 1
A getSign() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\Pay\Mini;
6
7
use Closure;
8
use Throwable;
9
use Yansongda\Pay\Contract\PluginInterface;
10
use Yansongda\Pay\Exception\ContainerException;
11
use Yansongda\Pay\Exception\Exception;
12
use Yansongda\Pay\Exception\InvalidConfigException;
13
use Yansongda\Pay\Exception\InvalidResponseException;
14
use Yansongda\Pay\Exception\ServiceNotFoundException;
15
use Yansongda\Pay\Logger;
16
use Yansongda\Pay\Rocket;
17
use Yansongda\Supports\Collection;
18
use Yansongda\Supports\Config;
19
use Yansongda\Supports\Str;
20
21
use function Yansongda\Pay\get_wechat_config;
22
use function Yansongda\Pay\get_wechat_sign;
23
24
/**
25
 * @see https://pay.weixin.qq.com/docs/merchant/apis/mini-program-payment/mini-transfer-payment.html
26
 * @see https://pay.weixin.qq.com/docs/partner/apis/partner-mini-program-payment/mini-transfer-payment.html
27
 */
28
class InvokePlugin implements PluginInterface
29
{
30
    /**
31
     * @throws ContainerException
32
     * @throws InvalidConfigException
33
     * @throws InvalidResponseException
34
     * @throws ServiceNotFoundException
35
     * @throws Throwable                生成随机串失败
36
     */
37
    public function assembly(Rocket $rocket, Closure $next): Rocket
38
    {
39
        /* @var Rocket $rocket */
40
        $rocket = $next($rocket);
41
42
        Logger::debug('[Wechat][Mini][Jsapi][InvokePlugin] 插件开始装载', ['rocket' => $rocket]);
43
44
        $destination = $rocket->getDestination();
45
46
        $prepayId = $destination->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

46
        /** @scrutinizer ignore-call */ 
47
        $prepayId = $destination->get('prepay_id');
Loading history...
47
48
        if (is_null($prepayId)) {
49
            Logger::error('[Wechat][Pay][Mini][InvokePlugin] 预下单失败:响应缺少 `prepay_id` 参数,请自行检查参数是否符合微信要求', $destination->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

49
            Logger::error('[Wechat][Pay][Mini][InvokePlugin] 预下单失败:响应缺少 `prepay_id` 参数,请自行检查参数是否符合微信要求', $destination->/** @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...
50
51
            throw new InvalidResponseException(Exception::RESPONSE_MISSING_NECESSARY_PARAMS, $destination->get('message', '预下单失败:响应缺少 `prepay_id` 参数,请自行检查参数是否符合微信要求'), $destination->all());
52
        }
53
54
        $params = $rocket->getParams();
55
        $config = get_wechat_config($params);
56
57
        $rocket->setDestination($this->getInvokeConfig($config, $prepayId));
58
59
        Logger::info('[Wechat][Pay][Mini][InvokePlugin] 插件装载完毕', ['rocket' => $rocket]);
60
61
        return $rocket;
62
    }
63
64
    /**
65
     * @throws InvalidConfigException
66
     * @throws Throwable              生成随机串失败
67
     */
68
    protected function getInvokeConfig(array $config, string $prepayId): Config
69
    {
70
        $invokeConfig = new Config([
71
            'appId' => $config['mini_app_id'] ?? '',
72
            'timeStamp' => time().'',
73
            'nonceStr' => Str::random(32),
74
            'package' => 'prepay_id='.$prepayId,
75
            'signType' => 'RSA',
76
        ]);
77
78
        $invokeConfig->set('paySign', $this->getSign($invokeConfig, $config));
79
80
        return $invokeConfig;
81
    }
82
83
    /**
84
     * @throws InvalidConfigException
85
     */
86
    protected function getSign(Collection $invokeConfig, array $config): string
87
    {
88
        $contents = $invokeConfig->get('appId', '')."\n".
89
            $invokeConfig->get('timeStamp', '')."\n".
90
            $invokeConfig->get('nonceStr', '')."\n".
91
            $invokeConfig->get('package', '')."\n";
92
93
        return get_wechat_sign($config, $contents);
94
    }
95
}
96