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

InvokePlugin::getSign()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
dl 0
loc 8
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\Pay\Jsapi;
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_config_type_key;
23
use function Yansongda\Pay\get_wechat_sign;
24
25
/**
26
 * @see https://pay.weixin.qq.com/docs/merchant/apis/jsapi-payment/jsapi-transfer-payment.html
27
 * @see https://pay.weixin.qq.com/docs/partner/apis/partner-jsapi-payment/jsapi-transfer-payment.html
28
 */
29
class InvokePlugin implements PluginInterface
30
{
31
    /**
32
     * @throws ContainerException
33
     * @throws InvalidConfigException
34
     * @throws InvalidResponseException
35
     * @throws ServiceNotFoundException
36
     * @throws Throwable                生成随机串失败
37
     */
38
    public function assembly(Rocket $rocket, Closure $next): Rocket
39
    {
40
        /* @var Rocket $rocket */
41
        $rocket = $next($rocket);
42
43
        Logger::debug('[Wechat][Pay][Jsapi][InvokePlugin] 插件开始装载', ['rocket' => $rocket]);
44
45
        $destination = $rocket->getDestination();
46
47
        $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

47
        /** @scrutinizer ignore-call */ 
48
        $prepayId = $destination->get('prepay_id');
Loading history...
48
49
        if (is_null($prepayId)) {
50
            Logger::error('[Wechat][Pay][Jsapi][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

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