Passed
Push — master ( e73c8b...2ea20b )
by Songda
04:10 queued 02:00
created

PayPlugin::assembly()   A

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
nc 3
nop 2
dl 0
loc 29
rs 9.7333
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Douyin\V1\Pay\Mini;
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_provider_config;
19
20
/**
21
 * @see https://developer.open-douyin.com/docs/resource/zh-CN/mini-app/develop/server/ecpay/pay-list/pay
22
 */
23
class PayPlugin implements PluginInterface
24
{
25
    /**
26
     * @throws ContainerException
27
     * @throws InvalidParamsException
28
     * @throws ServiceNotFoundException
29
     */
30
    public function assembly(Rocket $rocket, Closure $next): Rocket
31
    {
32
        Logger::debug('[Douyin][V1][Pay][Mini][PayPlugin] 插件开始装载', ['rocket' => $rocket]);
33
34
        $payload = $rocket->getPayload();
35
        $params = $rocket->getParams();
36
        $config = get_provider_config('douyin', $params);
37
38
        if (is_null($payload)) {
39
            throw new InvalidParamsException(Exception::PARAMS_NECESSARY_PARAMS_MISSING, '参数异常: 抖音小程序下单,参数为空');
40
        }
41
42
        if (Pay::MODE_SERVICE === ($config['mode'] ?? Pay::MODE_NORMAL)) {
43
            $data = $this->service($payload, $config);
44
        }
45
46
        $rocket->mergePayload(array_merge(
47
            [
48
                '_method' => 'POST',
49
                '_url' => 'api/apps/ecpay/v1/create_order',
50
                'app_id' => $config['mini_app_id'] ?? '',
51
                'notify_url' => $payload->get('notify_url') ?? $this->getNotifyUrl($config),
52
            ],
53
            $data ?? [],
54
        ));
55
56
        Logger::info('[Douyin][V1][Pay][Mini][PayPlugin] 插件装载完毕', ['rocket' => $rocket]);
57
58
        return $next($rocket);
59
    }
60
61
    protected function service(Collection $payload, array $config): array
62
    {
63
        return [
64
            'thirdparty_id' => $payload->get('thirdparty_id', $config['thirdparty_id'] ?? ''),
65
        ];
66
    }
67
68
    protected function getNotifyUrl(array $config): ?string
69
    {
70
        return empty($config['notify_url']) ? null : $config['notify_url'];
71
    }
72
}
73