DownloadBillPlugin::assembly()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 11
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 20
rs 9.9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\V3\Pay\Native;
6
7
use Closure;
8
use Yansongda\Artful\Contract\PluginInterface;
9
use Yansongda\Artful\Direction\OriginResponseDirection;
10
use Yansongda\Artful\Exception\InvalidParamsException;
11
use Yansongda\Artful\Logger;
12
use Yansongda\Artful\Rocket;
13
use Yansongda\Pay\Exception\Exception;
14
15
/**
16
 * @see https://pay.weixin.qq.com/docs/merchant/apis/native-payment/download-bill.html
17
 * @see https://pay.weixin.qq.com/docs/partner/apis/partner-native-payment/download-bill.html
18
 */
19
class DownloadBillPlugin implements PluginInterface
20
{
21
    /**
22
     * @throws InvalidParamsException
23
     */
24
    public function assembly(Rocket $rocket, Closure $next): Rocket
25
    {
26
        Logger::debug('[Wechat][V3][Pay][Native][DownloadBillPlugin] 插件开始装载', ['rocket' => $rocket]);
27
28
        $downloadUrl = $rocket->getPayload()?->get('download_url') ?? null;
29
30
        if (empty($downloadUrl)) {
31
            throw new InvalidParamsException(Exception::PARAMS_NECESSARY_PARAMS_MISSING, '参数异常: Native 下载交易对账单,参数缺少 `download_url`');
32
        }
33
34
        $rocket->setDirection(OriginResponseDirection::class)
35
            ->setPayload([
36
                '_method' => 'GET',
37
                '_url' => $downloadUrl,
38
                '_service_url' => $downloadUrl,
39
            ]);
40
41
        Logger::info('[Wechat][Pay][Native][DownloadBillPlugin] 插件装载完毕', ['rocket' => $rocket]);
42
43
        return $next($rocket);
44
    }
45
}
46