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

InvokePlugin::assembly()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 25
rs 9.8666
c 1
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\Pay\App;
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/in-app-payment/app-transfer-payment.html
27
 * @see https://pay.weixin.qq.com/docs/partner/apis/partner-in-app-payment/app-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][App][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][App][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][App][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][App][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
            'partnerid' => $config['mch_id'] ?? null,
74
            'prepayid' => $prepayId,
75
            'package' => 'Sign=WXPay',
76
            'noncestr' => Str::random(32),
77
            'timestamp' => time().'',
78
        ]);
79
80
        $invokeConfig->set('sign', $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('prepayid', '')."\n";
94
95
        return get_wechat_sign($config, $contents);
96
    }
97
}
98