InvokePlugin::getAppId()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 7
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\V2\Pay\App;
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_v2;
24
25
/**
26
 * @see https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_12&index=2
27
 */
28
class InvokePlugin implements PluginInterface
29
{
30
    /**
31
     * @throws ContainerException
32
     * @throws Exception
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][V2][Pay][App][InvokePlugin] 插件开始装载', ['rocket' => $rocket]);
43
44
        $destination = $rocket->getDestination();
45
        $prepayId = $destination?->get('prepay_id') ?? null;
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

45
        $prepayId = $destination?->/** @scrutinizer ignore-call */ get('prepay_id') ?? 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...
46
47
        if (is_null($prepayId)) {
48
            Logger::error('[Wechat][V2][Pay][App][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

48
            Logger::error('[Wechat][V2][Pay][App][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...
49
50
            throw new InvalidResponseException(Exception::RESPONSE_MISSING_NECESSARY_PARAMS, $destination?->get('message') ?? '预下单失败:响应缺少 `prepay_id` 参数,请自行检查参数是否符合微信要求', $destination?->all() ?? null);
51
        }
52
53
        $params = $rocket->getParams();
54
        $config = get_provider_config('wechat', $params);
55
        $payload = $rocket->getPayload();
56
57
        $rocket->setDestination($this->getInvokeConfig($payload, $config, $prepayId));
58
59
        Logger::info('[Wechat][V2][Pay][App][InvokePlugin] 插件装载完毕', ['rocket' => $rocket]);
60
61
        return $rocket;
62
    }
63
64
    /**
65
     * @throws InvalidConfigException
66
     * @throws Throwable              生成随机串失败
67
     */
68
    protected function getInvokeConfig(?Collection $payload, array $config, string $prepayId): Config
69
    {
70
        $invokeConfig = new Config([
71
            'appid' => $this->getAppId($payload, $config),
72
            'partnerid' => $this->getPartnerId($payload, $config),
73
            'prepayid' => $prepayId,
74
            'package' => 'Sign=WXPay',
75
            'noncestr' => Str::random(32),
76
            'timestamp' => time().'',
77
        ]);
78
79
        $invokeConfig->set('sign', get_wechat_sign_v2($config, $invokeConfig->all()));
80
81
        return $invokeConfig;
82
    }
83
84
    protected function getAppId(?Collection $payload, array $config): string
85
    {
86
        if (Pay::MODE_SERVICE === ($config['mode'] ?? Pay::MODE_NORMAL)) {
87
            return $payload?->get('_invoke_appid') ?? $config['sub_app_id'] ?? '';
88
        }
89
90
        return $payload?->get('_invoke_appid') ?? $config['app_id'] ?? '';
91
    }
92
93
    protected function getPartnerId(?Collection $payload, array $config): string
94
    {
95
        return $payload?->get('_invoke_partnerid') ?? $config['mch_id'] ?? '';
96
    }
97
}
98