Passed
Pull Request — master (#909)
by Songda
02:12
created

JsapiPayPlugin   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A assembly() 0 20 1
A normal() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\Pay\Combine;
6
7
use Closure;
8
use Yansongda\Pay\Contract\PluginInterface;
9
use Yansongda\Pay\Exception\ContainerException;
10
use Yansongda\Pay\Exception\ServiceNotFoundException;
11
use Yansongda\Pay\Logger;
12
use Yansongda\Pay\Rocket;
13
14
use function Yansongda\Pay\get_wechat_config;
15
use function Yansongda\Pay\get_wechat_config_type_key;
16
17
/**
18
 * @see https://pay.weixin.qq.com/docs/merchant/apis/combine-payment/orders/jsapi-prepay.html
19
 * @see https://pay.weixin.qq.com/docs/partner/apis/combine-payment/orders/jsapi-prepay.html
20
 */
21
class JsapiPayPlugin implements PluginInterface
22
{
23
    /**
24
     * @throws ContainerException
25
     * @throws ServiceNotFoundException
26
     */
27
    public function assembly(Rocket $rocket, Closure $next): Rocket
28
    {
29
        Logger::debug('[Wechat][Pay][Combine][JsapiPayPlugin] 插件开始装载', ['rocket' => $rocket]);
30
31
        $params = $rocket->getParams();
32
        $config = get_wechat_config($params);
33
34
        $rocket->mergePayload(array_merge(
35
            [
36
                '_method' => 'POST',
37
                '_url' => 'v3/combine-transactions/jsapi',
38
                '_service_url' => 'v3/combine-transactions/jsapi',
39
                'notify_url' => $config['notify_url'] ?? '',
40
            ],
41
            $this->normal($params, $config)
42
        ));
43
44
        Logger::info('[Wechat][Pay][Combine][JsapiPayPlugin] 插件装载完毕', ['rocket' => $rocket]);
45
46
        return $next($rocket);
47
    }
48
49
    protected function normal(array $params, array $config): array
50
    {
51
        return [
52
            'combine_appid' => $config[get_wechat_config_type_key($params)] ?? '',
53
            'combine_mchid' => $config['mch_id'] ?? '',
54
        ];
55
    }
56
}
57