Passed
Pull Request — master (#1010)
by ma
02:08
created

CancelPlugin::service()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 7
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\V3\Pay\Pos;
6
7
use Closure;
8
use Throwable;
9
use Yansongda\Artful\Contract\PluginInterface;
10
use Yansongda\Artful\Exception\ContainerException;
11
use Yansongda\Artful\Exception\InvalidParamsException;
12
use Yansongda\Artful\Exception\ServiceNotFoundException;
13
use Yansongda\Artful\Logger;
14
use Yansongda\Artful\Rocket;
15
use Yansongda\Pay\Exception\Exception;
16
17
use function Yansongda\Pay\get_provider_config;
18
use function Yansongda\Pay\get_wechat_type_key;
19
20
/**
21
 * @see https://pay.weixin.qq.com/docs/merchant/apis/code-payment-v3/direct/reverse.html
22
 */
23
class CancelPlugin implements PluginInterface
24
{
25
    /**
26
     * @throws ContainerException
27
     * @throws ServiceNotFoundException
28
     * @throws Throwable                随机字符串生成失败
29
     */
30
    public function assembly(Rocket $rocket, Closure $next): Rocket
31
    {
32
        Logger::debug('[Wechat][V3][Pay][Pos][CancelPlugin] 插件开始装载', ['rocket' => $rocket]);
33
34
        $params = $rocket->getParams();
35
        $config = get_provider_config('wechat', $params);
36
        $payload = $rocket->getPayload();
37
38
        $outTradeNo = $payload?->get('out_trade_no') ?? null;
39
40
        if (empty($outTradeNo)) {
41
            throw new InvalidParamsException(Exception::PARAMS_NECESSARY_PARAMS_MISSING, '参数异常: 付款码支付撤销订单,参数缺少 `out_trade_no`');
42
        }
43
        if (Pay::MODE_SERVICE === ($config['mode'] ?? Pay::MODE_NORMAL)) {
44
            $rocket->mergePayload(array_merge(
45
                [
46
                    '_method' => 'POST',
47
                    '_url' => 'v3/pay/partner/transactions/out-trade-no/'.$outTradeNo.'/reverse',
48
                ],
49
                $this->service($payload, $params, $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...CancelPlugin::service() does only seem to accept Yansongda\Pay\Plugin\Wechat\V3\Pay\Pos\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

49
                $this->service(/** @scrutinizer ignore-type */ $payload, $params, $config)
Loading history...
50
            ));
51
        }else{
52
            $rocket->setPayload(array_merge(
53
                [
54
                    '_method' => 'POST',
55
                    '_url' => 'v3/pay/transactions/out-trade-no/'.$outTradeNo.'/reverse',
56
                ],
57
                $this->normal($params, $config)
58
            ));
59
        }
60
61
        Logger::info('[Wechat][V3][Pay][Pos][CancelPlugin] 插件装载完毕', ['rocket' => $rocket]);
62
63
        return $next($rocket);
64
    }
65
66
    protected function normal(array $params, array $config): array
67
    {
68
        return [
69
            'appid' => $config[get_wechat_type_key($params)] ?? '',
70
            'mchid' => $config['mch_id'] ?? '',
71
        ];
72
    }
73
74
    protected function service(Collection $payload, array $params, array $config): array
0 ignored issues
show
Bug introduced by
The type Yansongda\Pay\Plugin\Wechat\V3\Pay\Pos\Collection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
75
    {
76
        $data = [
77
            'sp_mchid' => $config['mch_id'] ?? '',
78
            'sub_mchid' => $payload->get('sub_mchid', $config['sub_mch_id'] ?? ''),
79
        ];
80
        return $data;
81
    }
82
}
83