Passed
Push — master ( da3bf5...b7cad5 )
by Songda
02:06
created

PayPlugin::service()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\V3\Pay\Pos;
6
7
use Closure;
8
use Throwable;
9
use Yansongda\Artful\Contract\PluginInterface;
10
use Yansongda\Artful\Exception\ContainerException;
11
use Yansongda\Artful\Exception\InvalidParamsException;
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
19
use function Yansongda\Pay\get_provider_config;
20
use function Yansongda\Pay\get_wechat_type_key;
21
22
/**
23
 * @see https://pay.weixin.qq.com/docs/merchant/apis/code-payment-v3/direct/code-pay.html
24
 * @see https://pay.weixin.qq.com/docs/partner/apis/partner-code-payment-v3/partner/partner-code-pay.html
25
 */
26
class PayPlugin implements PluginInterface
27
{
28
    /**
29
     * @throws ContainerException
30
     * @throws ServiceNotFoundException
31
     * @throws Throwable                随机字符串生成失败
32
     */
33
    public function assembly(Rocket $rocket, Closure $next): Rocket
34
    {
35
        Logger::debug('[Wechat][V3][Pay][Pos][PayPlugin] 插件开始装载', ['rocket' => $rocket]);
36
37
        $payload = $rocket->getPayload();
38
        $params = $rocket->getParams();
39
        $config = get_provider_config('wechat', $params);
40
41
        if (is_null($payload)) {
42
            throw new InvalidParamsException(Exception::PARAMS_NECESSARY_PARAMS_MISSING, '参数异常: 付款码支付,参数为空');
43
        }
44
45
        if (Pay::MODE_SERVICE === ($config['mode'] ?? Pay::MODE_NORMAL)) {
46
            $data = $this->service($payload, $params, $config);
47
        }
48
49
        $rocket->mergePayload(array_merge(
50
            [
51
                '_method' => 'POST',
52
                '_url' => 'v3/pay/transactions/codepay',
53
                '_service_url' => 'v3/pay/partner/transactions/codepay',
54
            ],
55
            $data ?? $this->normal($params, $config)
56
        ));
57
58
        Logger::info('[Wechat][V3][Pay][Pos][PayPlugin] 插件装载完毕', ['rocket' => $rocket]);
59
60
        return $next($rocket);
61
    }
62
63
    protected function normal(array $params, array $config): array
64
    {
65
        return [
66
            'appid' => $config[get_wechat_type_key($params)] ?? '',
67
            'mchid' => $config['mch_id'] ?? '',
68
        ];
69
    }
70
71
    protected function service(Collection $payload, array $params, array $config): array
72
    {
73
        $configKey = get_wechat_type_key($params);
74
75
        return [
76
            'sp_appid' => $config[$configKey] ?? '',
77
            'sp_mchid' => $config['mch_id'] ?? '',
78
            'sub_mchid' => $payload->get('sub_mchid', $config['sub_mch_id'] ?? ''),
79
        ];
80
    }
81
}
82