RefundPlugin::assembly()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 16
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 29
rs 9.7333
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\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/jsapi-payment/create.html
22
 * @see https://pay.weixin.qq.com/docs/partner/apis/partner-jsapi-payment/create.html
23
 */
24
class RefundPlugin 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][Pay][Jsapi][RefundPlugin] 插件开始装载', ['rocket' => $rocket]);
34
35
        $params = $rocket->getParams();
36
        $config = get_wechat_config($params);
37
        $payload = $rocket->getPayload();
38
39
        if (is_null($payload)) {
40
            throw new InvalidParamsException(Exception::PARAMS_NECESSARY_PARAMS_MISSING, '参数异常: Jsapi 退款申请,参数为空');
41
        }
42
43
        if (Pay::MODE_SERVICE === $config['mode']) {
44
            $data = $this->service($payload, $config);
45
        }
46
47
        $rocket->mergePayload(array_merge(
48
            [
49
                '_method' => 'POST',
50
                '_url' => 'v3/refund/domestic/refunds',
51
                '_service_url' => 'v3/refund/domestic/refunds',
52
                'notify_url' => $payload->get('notify_url', $config['notify_url'] ?? null),
53
            ],
54
            $data ?? $this->normal()
55
        ));
56
57
        Logger::info('[Wechat][Pay][Jsapi][RefundPlugin] 插件装载完毕', ['rocket' => $rocket]);
58
59
        return $next($rocket);
60
    }
61
62
    protected function normal(): array
63
    {
64
        return [];
65
    }
66
67
    protected function service(Collection $payload, array $config): array
68
    {
69
        return [
70
            'sub_mchid' => $payload->get('sub_mchid', $config['sub_mch_id'] ?? ''),
71
        ];
72
    }
73
}
74