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

QueryByWxPlugin::normal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\Pay\Native;
6
7
use Closure;
8
use Yansongda\Pay\Contract\PluginInterface;
9
use Yansongda\Pay\Exception\ContainerException;
10
use Yansongda\Pay\Exception\Exception;
11
use Yansongda\Pay\Exception\InvalidParamsException;
12
use Yansongda\Pay\Exception\ServiceNotFoundException;
13
use Yansongda\Pay\Logger;
14
use Yansongda\Pay\Rocket;
15
16
use function Yansongda\Pay\get_wechat_config;
17
18
/**
19
 * @see https://pay.weixin.qq.com/docs/merchant/apis/native-payment/query-by-wx-trade-no.html
20
 * @see https://pay.weixin.qq.com/docs/partner/apis/partner-native-payment/query-by-wx-trade-no.html
21
 */
22
class QueryByWxPlugin implements PluginInterface
23
{
24
    /**
25
     * @throws ContainerException
26
     * @throws InvalidParamsException
27
     * @throws ServiceNotFoundException
28
     */
29
    public function assembly(Rocket $rocket, Closure $next): Rocket
30
    {
31
        Logger::debug('[Wechat][Pay][Native][QueryByWxPlugin] 插件开始装载', ['rocket' => $rocket]);
32
33
        $params = $rocket->getParams();
34
        $config = get_wechat_config($params);
35
        $transactionId = $rocket->getPayload()?->get('transaction_id') ?? null;
36
37
        if (empty($transactionId)) {
38
            throw new InvalidParamsException(Exception::PARAMS_NECESSARY_PARAMS_MISSING, '参数异常: Native 通过微信订单号查询订单,参数缺少 `transaction_id`');
39
        }
40
41
        $rocket->setPayload([
42
            '_method' => 'GET',
43
            '_url' => 'v3/pay/transactions/id/'.$transactionId.'?'.$this->normal($config),
44
            '_service_url' => 'v3/pay/partner/transactions/id/'.$transactionId.'?'.$this->service($config),
45
        ]);
46
47
        Logger::info('[Wechat][Pay][Native][QueryByWxPlugin] 插件装载完毕', ['rocket' => $rocket]);
48
49
        return $next($rocket);
50
    }
51
52
    protected function normal(array $config): string
53
    {
54
        return http_build_query([
55
            'mchid' => $config['mch_id'] ?? '',
56
        ]);
57
    }
58
59
    protected function service(array $config): string
60
    {
61
        return http_build_query([
62
            'sp_mchid' => $config['mch_id'] ?? '',
63
            'sub_mchid' => $config['sub_mch_id'] ?? '',
64
        ]);
65
    }
66
}
67