QueryByWxPlugin   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 43
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A service() 0 5 1
A normal() 0 4 1
A assembly() 0 22 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\V3\Pay\Jsapi;
6
7
use Closure;
8
use Yansongda\Artful\Contract\PluginInterface;
9
use Yansongda\Artful\Exception\ContainerException;
10
use Yansongda\Artful\Exception\InvalidParamsException;
11
use Yansongda\Artful\Exception\ServiceNotFoundException;
12
use Yansongda\Artful\Logger;
13
use Yansongda\Artful\Rocket;
14
use Yansongda\Pay\Exception\Exception;
15
use Yansongda\Supports\Collection;
16
17
use function Yansongda\Pay\get_provider_config;
18
19
/**
20
 * @see https://pay.weixin.qq.com/docs/merchant/apis/jsapi-payment/query-by-wx-trade-no.html
21
 * @see https://pay.weixin.qq.com/docs/partner/apis/partner-jsapi-payment/query-by-wx-trade-no.html
22
 */
23
class QueryByWxPlugin implements PluginInterface
24
{
25
    /**
26
     * @throws ContainerException
27
     * @throws InvalidParamsException
28
     * @throws ServiceNotFoundException
29
     */
30
    public function assembly(Rocket $rocket, Closure $next): Rocket
31
    {
32
        Logger::debug('[Wechat][Pay][Jsapi][QueryByWxPlugin] 插件开始装载', ['rocket' => $rocket]);
33
34
        $params = $rocket->getParams();
35
        $config = get_provider_config('wechat', $params);
36
        $payload = $rocket->getPayload();
37
        $transactionId = $payload?->get('transaction_id') ?? null;
38
39
        if (empty($transactionId)) {
40
            throw new InvalidParamsException(Exception::PARAMS_NECESSARY_PARAMS_MISSING, '参数异常: Jsapi 通过微信订单号查询订单,参数缺少 `transaction_id`');
41
        }
42
43
        $rocket->setPayload([
44
            '_method' => 'GET',
45
            '_url' => 'v3/pay/transactions/id/'.$transactionId.'?'.$this->normal($config),
46
            '_service_url' => 'v3/pay/partner/transactions/id/'.$transactionId.'?'.$this->service($payload, $config),
0 ignored issues
show
Bug introduced by
It seems like $payload can also be of type null; however, parameter $payload of Yansongda\Pay\Plugin\Wec...ryByWxPlugin::service() does only seem to accept Yansongda\Supports\Collection, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
            '_service_url' => 'v3/pay/partner/transactions/id/'.$transactionId.'?'.$this->service(/** @scrutinizer ignore-type */ $payload, $config),
Loading history...
47
        ]);
48
49
        Logger::info('[Wechat][Pay][Jsapi][QueryByWxPlugin] 插件装载完毕', ['rocket' => $rocket]);
50
51
        return $next($rocket);
52
    }
53
54
    protected function normal(array $config): string
55
    {
56
        return http_build_query([
57
            'mchid' => $config['mch_id'] ?? 'null',
58
        ]);
59
    }
60
61
    protected function service(Collection $payload, array $config): string
62
    {
63
        return http_build_query([
64
            'sp_mchid' => $config['mch_id'] ?? 'null',
65
            'sub_mchid' => $payload->get('sub_mchid', $config['sub_mch_id'] ?? 'null'),
66
        ]);
67
    }
68
}
69