InvokePlugin::getSign()   A
last analyzed

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
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\V3\Pay\Jsapi;
6
7
use Closure;
8
use Throwable;
9
use Yansongda\Artful\Contract\PluginInterface;
10
use Yansongda\Artful\Exception\ContainerException;
11
use Yansongda\Artful\Exception\InvalidConfigException;
12
use Yansongda\Artful\Exception\InvalidResponseException;
13
use Yansongda\Artful\Exception\ServiceNotFoundException;
14
use Yansongda\Artful\Logger;
15
use Yansongda\Artful\Rocket;
16
use Yansongda\Pay\Exception\Exception;
17
use Yansongda\Pay\Pay;
18
use Yansongda\Supports\Collection;
19
use Yansongda\Supports\Config;
20
use Yansongda\Supports\Str;
21
22
use function Yansongda\Pay\get_provider_config;
23
use function Yansongda\Pay\get_wechat_sign;
24
use function Yansongda\Pay\get_wechat_type_key;
25
26
/**
27
 * @see https://pay.weixin.qq.com/docs/merchant/apis/jsapi-payment/jsapi-transfer-payment.html
28
 * @see https://pay.weixin.qq.com/docs/partner/apis/partner-jsapi-payment/jsapi-transfer-payment.html
29
 */
30
class InvokePlugin implements PluginInterface
31
{
32
    /**
33
     * @throws ContainerException
34
     * @throws InvalidConfigException
35
     * @throws InvalidResponseException
36
     * @throws ServiceNotFoundException
37
     * @throws Throwable                生成随机串失败
38
     */
39
    public function assembly(Rocket $rocket, Closure $next): Rocket
40
    {
41
        /* @var Rocket $rocket */
42
        $rocket = $next($rocket);
43
44
        Logger::debug('[Wechat][V3][Pay][Jsapi][InvokePlugin] 插件开始装载', ['rocket' => $rocket]);
45
46
        $destination = $rocket->getDestination();
47
        $prepayId = $destination?->get('prepay_id');
0 ignored issues
show
Bug introduced by
The method get() 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

47
        /** @scrutinizer ignore-call */ 
48
        $prepayId = $destination?->get('prepay_id');

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

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() ?? null);
53
        }
54
55
        $params = $rocket->getParams();
56
        $config = get_provider_config('wechat', $params);
57
        $payload = $rocket->getPayload();
58
59
        $rocket->setDestination($this->getInvokeConfig($payload, $params, $config, $prepayId));
60
61
        Logger::info('[Wechat][V3][Pay][Jsapi][InvokePlugin] 插件装载完毕', ['rocket' => $rocket]);
62
63
        return $rocket;
64
    }
65
66
    /**
67
     * @throws InvalidConfigException
68
     * @throws Throwable              生成随机串失败
69
     */
70
    protected function getInvokeConfig(?Collection $payload, array $params, array $config, string $prepayId): Config
71
    {
72
        $invokeConfig = new Config([
73
            'appId' => $this->getAppId($payload, $config, $params),
74
            'timeStamp' => time().'',
75
            'nonceStr' => Str::random(32),
76
            'package' => 'prepay_id='.$prepayId,
77
            'signType' => 'RSA',
78
        ]);
79
80
        $invokeConfig->set('paySign', $this->getSign($invokeConfig, $config));
81
82
        return $invokeConfig;
83
    }
84
85
    /**
86
     * @throws InvalidConfigException
87
     */
88
    protected function getSign(Collection $invokeConfig, array $config): string
89
    {
90
        $contents = $invokeConfig->get('appId', '')."\n"
91
            .$invokeConfig->get('timeStamp', '')."\n"
92
            .$invokeConfig->get('nonceStr', '')."\n"
93
            .$invokeConfig->get('package', '')."\n";
94
95
        return get_wechat_sign($config, $contents);
96
    }
97
98
    protected function getAppId(?Collection $payload, array $config, array $params): string
99
    {
100
        if (Pay::MODE_SERVICE === ($config['mode'] ?? Pay::MODE_NORMAL)) {
101
            return $payload?->get('_invoke_appid') ?? $config['sub_'.get_wechat_type_key($params)] ?? '';
102
        }
103
104
        return $payload?->get('_invoke_appid') ?? $config[get_wechat_type_key($params)] ?? '';
105
    }
106
}
107