ClosePlugin::service()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\V3\Pay\Jsapi;
6
7
use Closure;
8
use Yansongda\Artful\Contract\PluginInterface;
9
use Yansongda\Artful\Direction\OriginResponseDirection;
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
21
/**
22
 * @see https://pay.weixin.qq.com/docs/merchant/apis/jsapi-payment/close-order.html
23
 * @see https://pay.weixin.qq.com/docs/partner/apis/partner-jsapi-payment/close-order.html
24
 */
25
class ClosePlugin implements PluginInterface
26
{
27
    /**
28
     * @throws ContainerException
29
     * @throws InvalidParamsException
30
     * @throws ServiceNotFoundException
31
     */
32
    public function assembly(Rocket $rocket, Closure $next): Rocket
33
    {
34
        Logger::debug('[Wechat][Pay][Jsapi][ClosePlugin] 插件开始装载', ['rocket' => $rocket]);
35
36
        $params = $rocket->getParams();
37
        $config = get_provider_config('wechat', $params);
38
        $payload = $rocket->getPayload();
39
        $outTradeNo = $payload?->get('out_trade_no') ?? null;
40
41
        if (empty($outTradeNo)) {
42
            throw new InvalidParamsException(Exception::PARAMS_NECESSARY_PARAMS_MISSING, '参数异常: Jsapi 关闭订单,参数缺少 `out_trade_no`');
43
        }
44
45
        if (Pay::MODE_SERVICE === ($config['mode'] ?? Pay::MODE_NORMAL)) {
46
            $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

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