Passed
Pull Request — master (#909)
by Songda
04:52 queued 02:52
created

ApplyPlugin   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A service() 0 6 1
A assembly() 0 23 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\Marketing\ECommerceRefund;
6
7
use Closure;
8
use Yansongda\Pay\Contract\PluginInterface;
9
use Yansongda\Pay\Exception\ContainerException;
10
use Yansongda\Pay\Exception\Exception;
11
use Yansongda\Pay\Exception\InvalidParamsException;
12
use Yansongda\Pay\Exception\ServiceNotFoundException;
13
use Yansongda\Pay\Logger;
14
use Yansongda\Pay\Rocket;
15
use Yansongda\Supports\Collection;
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-refund.html
21
 */
22
class ApplyPlugin 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][Marketing][ECommerceRefund][ApplyPlugin] 插件开始装载', ['rocket' => $rocket]);
32
33
        $params = $rocket->getParams();
34
        $payload = $rocket->getPayload();
35
        $config = get_wechat_config($params);
36
37
        if (is_null($payload)) {
38
            throw new InvalidParamsException(Exception::PARAMS_NECESSARY_PARAMS_MISSING, '参数异常: 平台收付通(退款)-申请退款,缺少必要参数');
39
        }
40
41
        $rocket->mergePayload(array_merge(
42
            [
43
                '_method' => 'POST',
44
                '_service_url' => 'v3/ecommerce/refunds/apply',
45
            ],
46
            $this->service($payload, $config)
47
        ));
48
49
        Logger::info('[Wechat][Marketing][ECommerceRefund][ApplyPlugin] 插件装载完毕', ['rocket' => $rocket]);
50
51
        return $next($rocket);
52
    }
53
54
    protected function service(Collection $payload, array $config): array
55
    {
56
        return [
57
            'sub_mchid' => $payload->get('sub_mchid', $config['sub_mch_id']),
58
            'sp_appid' => $payload->get('sp_appid', $config['app_id']),
59
            'notify_url' => $payload->get('notify_url', $config['notify_url'] ?? null),
60
        ];
61
    }
62
}
63