ReturnAdvancePlugin   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 33
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A assembly() 0 26 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\V3\Marketing\ECommerceRefund;
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
17
use function Yansongda\Pay\get_wechat_config;
18
19
/**
20
 * @see https://pay.weixin.qq.com/docs/partner/apis/ecommerce-refund/refunds/create-return-advance.html
21
 */
22
class ReturnAdvancePlugin implements PluginInterface
23
{
24
    /**
25
     * @throws ContainerException
26
     * @throws InvalidParamsException
27
     * @throws ServiceNotFoundException
28
     */
29
    public function assembly(Rocket $rocket, Closure $next): Rocket
30
    {
31
        Logger::debug('[Wechat][V3][Marketing][ECommerceRefund][ReturnAdvancePlugin] 插件开始装载', ['rocket' => $rocket]);
32
33
        $params = $rocket->getParams();
34
        $payload = $rocket->getPayload();
35
        $config = get_wechat_config($params);
36
        $refundId = $payload?->get('refund_id') ?? null;
37
38
        if (Pay::MODE_NORMAL === ($config['mode'] ?? Pay::MODE_NORMAL)) {
39
            throw new InvalidParamsException(Exception::PARAMS_PLUGIN_ONLY_SUPPORT_SERVICE_MODE, '参数异常: 平台收付通(退款)-垫付退款回补,只支持服务商模式,当前配置为普通商户模式');
40
        }
41
42
        if (is_null($refundId)) {
43
            throw new InvalidParamsException(Exception::PARAMS_NECESSARY_PARAMS_MISSING, '参数异常: 平台收付通(退款)-垫付退款回补,缺少必要参数 `refund_id`');
44
        }
45
46
        $rocket->setPayload([
47
            '_method' => 'POST',
48
            '_service_url' => 'v3/ecommerce/refunds/'.$refundId.'/return-advance',
49
            'sub_mchid' => $payload->get('sub_mchid', $config['sub_mch_id'] ?? ''),
50
        ]);
51
52
        Logger::info('[Wechat][V3][Marketing][ECommerceRefund][ReturnAdvancePlugin] 插件装载完毕', ['rocket' => $rocket]);
53
54
        return $next($rocket);
55
    }
56
}
57