ClosePlugin   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 50
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A assembly() 0 29 3
A normal() 0 4 1
A service() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\V3\Pay\Native;
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\ServiceNotFoundException;
12
use Yansongda\Artful\Logger;
13
use Yansongda\Artful\Rocket;
14
use Yansongda\Pay\Exception\Exception;
15
use Yansongda\Pay\Pay;
16
use Yansongda\Supports\Collection;
17
18
use function Yansongda\Pay\get_wechat_config;
19
20
/**
21
 * @see https://pay.weixin.qq.com/docs/merchant/apis/native-payment/close-order.html
22
 * @see https://pay.weixin.qq.com/docs/partner/apis/partner-native-payment/close-order.html
23
 */
24
class ClosePlugin implements PluginInterface
25
{
26
    /**
27
     * @throws ContainerException
28
     * @throws InvalidParamsException
29
     * @throws ServiceNotFoundException
30
     */
31
    public function assembly(Rocket $rocket, Closure $next): Rocket
32
    {
33
        Logger::debug('[Wechat][V3][Pay][Native][ClosePlugin] 插件开始装载', ['rocket' => $rocket]);
34
35
        $params = $rocket->getParams();
36
        $config = get_wechat_config($params);
37
        $payload = $rocket->getPayload();
38
        $outTradeNo = $payload?->get('out_trade_no') ?? null;
39
40
        if (empty($outTradeNo)) {
41
            throw new InvalidParamsException(Exception::PARAMS_NECESSARY_PARAMS_MISSING, '参数异常: Native 关闭订单,参数缺少 `out_trade_no`');
42
        }
43
44
        if (Pay::MODE_SERVICE === ($config['mode'] ?? Pay::MODE_NORMAL)) {
45
            $data = $this->service($payload, $config);
0 ignored issues
show
Bug introduced by
It seems like $payload can also be of type null; however, parameter $payload of Yansongda\Pay\Plugin\Wec...\ClosePlugin::service() does only seem to accept Yansongda\Supports\Collection, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
            $data = $this->service(/** @scrutinizer ignore-type */ $payload, $config);
Loading history...
46
        }
47
48
        $rocket->setPayload(array_merge(
49
            [
50
                '_method' => 'POST',
51
                '_url' => 'v3/pay/transactions/out-trade-no/'.$outTradeNo.'/close',
52
                '_service_url' => 'v3/pay/partner/transactions/out-trade-no/'.$outTradeNo.'/close',
53
            ],
54
            $data ?? $this->normal($config)
55
        ));
56
57
        Logger::info('[Wechat][Pay][Native][ClosePlugin] 插件装载完毕', ['rocket' => $rocket]);
58
59
        return $next($rocket);
60
    }
61
62
    protected function normal(array $config): array
63
    {
64
        return [
65
            'mchid' => $config['mch_id'] ?? '',
66
        ];
67
    }
68
69
    protected function service(Collection $payload, array $config): array
70
    {
71
        return [
72
            'sp_mchid' => $config['mch_id'] ?? '',
73
            'sub_mchid' => $payload->get('sub_mchid', $config['sub_mch_id'] ?? ''),
74
        ];
75
    }
76
}
77