Passed
Push — master ( ab7eb6...522f6c )
by Songda
03:48 queued 01:30
created

InvokeJsapiPlugin::getInvokeConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\V3\Marketing\MchTransfer;
6
7
use Closure;
8
use Yansongda\Artful\Contract\PluginInterface;
9
use Yansongda\Artful\Exception\ContainerException;
10
use Yansongda\Artful\Exception\InvalidParamsException;
11
use Yansongda\Artful\Exception\InvalidResponseException;
12
use Yansongda\Artful\Exception\ServiceNotFoundException;
13
use Yansongda\Artful\Logger;
14
use Yansongda\Artful\Rocket;
15
use Yansongda\Pay\Exception\Exception;
16
use Yansongda\Pay\Pay;
17
use Yansongda\Supports\Collection;
18
use Yansongda\Supports\Config;
19
20
use function Yansongda\Pay\get_provider_config;
21
use function Yansongda\Pay\get_wechat_type_key;
22
23
/**
24
 * @see https://pay.weixin.qq.com/doc/v3/merchant/4012716430
25
 */
26
class InvokeJsapiPlugin implements PluginInterface
27
{
28
    /**
29
     * @throws ContainerException
30
     * @throws InvalidResponseException
31
     * @throws ServiceNotFoundException
32
     * @throws InvalidParamsException
33
     */
34
    public function assembly(Rocket $rocket, Closure $next): Rocket
35
    {
36
        /* @var Rocket $rocket */
37
        $rocket = $next($rocket);
38
39
        Logger::debug('[Wechat][V3][Marketing][MchTransfer][InvokeJsapiPlugin] 插件开始装载', ['rocket' => $rocket]);
40
41
        $config = get_provider_config('wechat', $rocket->getParams());
42
        $destination = $rocket->getDestination();
43
        $packageInfo = $destination?->get('package_info');
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

43
        /** @scrutinizer ignore-call */ 
44
        $packageInfo = $destination?->get('package_info');

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...
44
45
        if (Pay::MODE_SERVICE === ($config['mode'] ?? Pay::MODE_NORMAL)) {
46
            throw new InvalidParamsException(Exception::PARAMS_PLUGIN_ONLY_SUPPORT_NORMAL_MODE, '参数异常: JSAPI调起用户确认收款,只支持普通商户模式,当前配置为服务商模式');
47
        }
48
49
        if (is_null($packageInfo)) {
50
            Logger::error('[Wechat][V3][Marketing][MchTransfer][InvokeJsapiPlugin] JSAPI调起用户确认收款失败:响应缺少 `package_info` 参数,请自行检查参数是否符合微信要求', $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][Marketing][MchTransfer][InvokeJsapiPlugin] JSAPI调起用户确认收款失败:响应缺少 `package_info` 参数,请自行检查参数是否符合微信要求', $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('fail_reason') ?? 'JSAPI调起用户确认收款失败:响应缺少 `package_info` 参数,请自行检查参数是否符合微信要求', $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, $packageInfo));
60
61
        Logger::info('[Wechat][V3][Marketing][MchTransfer][InvokeJsapiPlugin] 插件装载完毕', ['rocket' => $rocket]);
62
63
        return $rocket;
64
    }
65
66
    protected function getInvokeConfig(?Collection $payload, array $params, array $config, string $packageInfo): Config
67
    {
68
        return new Config([
69
            'appId' => $payload?->get('_invoke_appId') ?? $config[get_wechat_type_key($params)] ?? '',
70
            'mchId' => $payload?->get('_invoke_mchId') ?? $config['mch_id'] ?? '',
71
            'package' => $packageInfo,
72
        ]);
73
    }
74
}
75