ApplyPlugin::assembly()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 15
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 22
rs 9.7666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\V2\Papay\Direct;
6
7
use Closure;
8
use Throwable;
9
use Yansongda\Artful\Contract\PluginInterface;
10
use Yansongda\Artful\Exception\ContainerException;
11
use Yansongda\Artful\Exception\ServiceNotFoundException;
12
use Yansongda\Artful\Logger;
13
use Yansongda\Artful\Packer\XmlPacker;
14
use Yansongda\Artful\Rocket;
15
use Yansongda\Supports\Str;
16
17
use function Yansongda\Pay\get_provider_config;
18
use function Yansongda\Pay\get_wechat_type_key;
19
20
/**
21
 * @see https://pay.weixin.qq.com/wiki/doc/api/wxpay_v2/papay/chapter3_8.shtml
22
 */
23
class ApplyPlugin implements PluginInterface
24
{
25
    /**
26
     * @throws ContainerException
27
     * @throws ServiceNotFoundException
28
     * @throws Throwable                随机字符串生成失败
29
     */
30
    public function assembly(Rocket $rocket, Closure $next): Rocket
31
    {
32
        Logger::debug('[Wechat][V2][Papay][Direct][ApplyPlugin] 插件开始装载', ['rocket' => $rocket]);
33
34
        $params = $rocket->getParams();
35
        $config = get_provider_config('wechat', $params);
36
        $payload = $rocket->getPayload();
37
38
        $rocket->setPacker(XmlPacker::class)
39
            ->mergePayload([
40
                '_url' => 'pay/pappayapply',
41
                '_content_type' => 'application/xml',
42
                'appid' => $config[get_wechat_type_key($params)] ?? '',
43
                'mch_id' => $config['mch_id'] ?? '',
44
                'nonce_str' => Str::random(32),
45
                'sign_type' => 'MD5',
46
                'notify_url' => $payload?->get('notify_url') ?? $config['notify_url'] ?? '',
47
            ]);
48
49
        Logger::info('[Wechat][V2][Papay][Direct][ApplyPlugin] 插件装载完毕', ['rocket' => $rocket]);
50
51
        return $next($rocket);
52
    }
53
}
54