Passed
Pull Request — master (#909)
by Songda
04:52 queued 02:52
created

QueryAmountsPlugin::assembly()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 21
rs 9.9332
c 1
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\Extend\ProfitSharing;
6
7
use Closure;
8
use Yansongda\Pay\Contract\PluginInterface;
9
use Yansongda\Pay\Exception\Exception;
10
use Yansongda\Pay\Exception\InvalidParamsException;
11
use Yansongda\Pay\Logger;
12
use Yansongda\Pay\Rocket;
13
14
/**
15
 * @see https://pay.weixin.qq.com/docs/merchant/apis/profit-sharing/transactions/query-order-amount.html
16
 * @see https://pay.weixin.qq.com/docs/partner/apis/profit-sharing/transactions/query-order-amount.html
17
 */
18
class QueryAmountsPlugin implements PluginInterface
19
{
20
    /**
21
     * @throws InvalidParamsException
22
     */
23
    public function assembly(Rocket $rocket, Closure $next): Rocket
24
    {
25
        Logger::debug('[Wechat][Extend][ProfitSharing][QueryAmountsPlugin] 插件开始装载', ['rocket' => $rocket]);
26
27
        $transactionId = $rocket->getPayload()?->get('transaction_id');
28
29
        if (is_null($transactionId)) {
30
            throw new InvalidParamsException(Exception::PARAMS_NECESSARY_PARAMS_MISSING, '参数异常: 缺少分账参数');
31
        }
32
33
        $rocket->setPayload(array_merge(
34
            [
35
                '_method' => 'GET',
36
                '_url' => 'v3/profitsharing/transactions/'.$transactionId.'/amounts',
37
                '_service_url' => 'v3/profitsharing/transactions/'.$transactionId.'/amounts',
38
            ],
39
        ));
40
41
        Logger::info('[Wechat][Extend][ProfitSharing][QueryAmountsPlugin] 插件装载完毕', ['rocket' => $rocket]);
42
43
        return $next($rocket);
44
    }
45
}
46