CancelPlugin::assembly()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
nc 1
nop 2
dl 0
loc 20
rs 9.8333
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\V2\Pay\Pos;
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/micropay.php?chapter=9_11&index=3
22
 */
23
class CancelPlugin 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][Pay][Pos][CancelPlugin] 插件开始装载', ['rocket' => $rocket]);
33
34
        $params = $rocket->getParams();
35
        $config = get_provider_config('wechat', $params);
36
37
        $rocket->setPacker(XmlPacker::class)
38
            ->mergePayload([
39
                '_url' => 'secapi/pay/reverse',
40
                '_content_type' => 'application/xml',
41
                'appid' => $config[get_wechat_type_key($params)] ?? '',
42
                'mch_id' => $config['mch_id'] ?? '',
43
                'nonce_str' => Str::random(32),
44
                'sign_type' => 'MD5',
45
            ]);
46
47
        Logger::info('[Wechat][V2][Pay][Pos][CancelPlugin] 插件装载完毕', ['rocket' => $rocket]);
48
49
        return $next($rocket);
50
    }
51
}
52