Passed
Push — master ( c57d84...4857dc )
by Songda
02:27 queued 27s
created

CancelPlugin::assembly()   A

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
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 20
rs 9.8333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Unipay\Qra\Pos;
6
7
use Closure;
8
use Throwable;
9
use Yansongda\Pay\Contract\PluginInterface;
10
use Yansongda\Pay\Exception\ContainerException;
11
use Yansongda\Pay\Exception\ServiceNotFoundException;
12
use Yansongda\Pay\Logger;
13
use Yansongda\Pay\Packer\XmlPacker;
14
use Yansongda\Pay\Rocket;
15
use Yansongda\Supports\Str;
16
17
use function Yansongda\Pay\get_unipay_config;
18
19
/**
20
 * @see https://up.95516.com/open/openapi/doc?index_1=2&index_2=1&chapter_1=274&chapter_2=294
21
 */
22
class CancelPlugin implements PluginInterface
23
{
24
    /**
25
     * @throws ContainerException
26
     * @throws ServiceNotFoundException
27
     * @throws Throwable                随机数生成失败
28
     */
29
    public function assembly(Rocket $rocket, Closure $next): Rocket
30
    {
31
        Logger::debug('[Unipay][Qra][Pos][CancelPlugin] 插件开始装载', ['rocket' => $rocket]);
32
33
        $params = $rocket->getParams();
34
        $config = get_unipay_config($params);
35
36
        $rocket->setPacker(XmlPacker::class)
37
            ->mergePayload([
38
                '_url' => 'https://qra.95516.com/pay/gateway',
39
                'service' => 'unified.micropay.reverse',
40
                'charset' => 'UTF-8',
41
                'sign_type' => 'MD5',
42
                'mch_id' => $config['mch_id'] ?? '',
43
                'nonce_str' => Str::random(32),
44
            ]);
45
46
        Logger::info('[Unipay][Qra][Pos][CancelPlugin] 插件装载完毕', ['rocket' => $rocket]);
47
48
        return $next($rocket);
49
    }
50
}
51