Passed
Pull Request — master (#909)
by Songda
02:12
created

RefundAbnormalPlugin::assembly()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 29
rs 9.7333
c 2
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\Pay\Refund;
6
7
use Closure;
8
use Yansongda\Pay\Contract\PluginInterface;
9
use Yansongda\Pay\Exception\ContainerException;
10
use Yansongda\Pay\Exception\DecryptException;
11
use Yansongda\Pay\Exception\Exception;
12
use Yansongda\Pay\Exception\InvalidConfigException;
13
use Yansongda\Pay\Exception\InvalidParamsException;
14
use Yansongda\Pay\Exception\ServiceNotFoundException;
15
use Yansongda\Pay\Logger;
16
use Yansongda\Pay\Pay;
17
use Yansongda\Pay\Rocket;
18
use Yansongda\Supports\Collection;
19
20
use function Yansongda\Pay\encrypt_wechat_contents;
21
use function Yansongda\Pay\get_wechat_config;
22
use function Yansongda\Pay\get_wechat_public_key;
23
use function Yansongda\Pay\get_wechat_serial_no;
24
25
/**
26
 * @see https://pay.weixin.qq.com/docs/merchant/apis/refund/refunds/create-abnormal-refund.html
27
 * @see https://pay.weixin.qq.com/docs/partner/apis/refund/refunds/create-abnormal-refund.html
28
 */
29
class RefundAbnormalPlugin implements PluginInterface
30
{
31
    /**
32
     * @throws ContainerException
33
     * @throws DecryptException
34
     * @throws InvalidConfigException
35
     * @throws InvalidParamsException
36
     * @throws ServiceNotFoundException
37
     */
38
    public function assembly(Rocket $rocket, Closure $next): Rocket
39
    {
40
        Logger::debug('[Wechat][Pay][Refund][RefundAbnormalPlugin] 插件开始装载', ['rocket' => $rocket]);
41
42
        $params = $rocket->getParams();
43
        $payload = $rocket->getPayload();
44
        $config = get_wechat_config($params);
45
        $refundId = $payload?->get('refund_id') ?? null;
46
47
        if (empty($refundId)) {
48
            throw new InvalidParamsException(Exception::PARAMS_NECESSARY_PARAMS_MISSING, '参数异常: 发起异常退款,参数缺少 `refund_id`');
49
        }
50
51
        if (Pay::MODE_SERVICE === $config['mode']) {
52
            $data = $this->service($params, $config, $payload);
53
        }
54
55
        $rocket->mergePayload(array_merge(
56
            [
57
                '_method' => 'POST',
58
                '_url' => 'v3/refund/domestic/refunds/'.$refundId.'/apply-abnormal-refund',
59
                '_service_url' => 'v3/refund/domestic/refunds/'.$refundId.'/apply-abnormal-refund',
60
            ],
61
            $data ?? $this->normal($params, $config, $payload)
62
        ));
63
64
        Logger::info('[Wechat][Pay][Refund][RefundAbnormalPlugin] 插件装载完毕', ['rocket' => $rocket]);
65
66
        return $next($rocket);
67
    }
68
69
    /**
70
     * @throws ContainerException
71
     * @throws InvalidParamsException
72
     * @throws ServiceNotFoundException
73
     * @throws DecryptException
74
     * @throws InvalidConfigException
75
     */
76
    protected function normal(array $params, array $config, ?Collection $payload): array
77
    {
78
        if (is_null($payload)) {
79
            return [];
80
        }
81
82
        return $this->encryptSensitiveData($params, $config, $payload);
83
    }
84
85
    /**
86
     * @throws ContainerException
87
     * @throws DecryptException
88
     * @throws InvalidConfigException
89
     * @throws InvalidParamsException
90
     * @throws ServiceNotFoundException
91
     */
92
    protected function service(array $params, array $config, ?Collection $payload): array
93
    {
94
        $data = [
95
            'sub_mchid' => $config['sub_mch_id'] ?? '',
96
        ];
97
98
        if (is_null($payload)) {
99
            return $data;
100
        }
101
102
        return array_merge($data, $this->encryptSensitiveData($params, $config, $payload));
103
    }
104
105
    /**
106
     * @throws ContainerException
107
     * @throws DecryptException
108
     * @throws InvalidConfigException
109
     * @throws InvalidParamsException
110
     * @throws ServiceNotFoundException
111
     */
112
    protected function encryptSensitiveData(array $params, array $config, ?Collection $payload): array
113
    {
114
        if ($payload->has('bank_account') && $payload->has('real_name')) {
0 ignored issues
show
Bug introduced by
The method has() does not exist on null. ( Ignorable by Annotation )

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

114
        if ($payload->/** @scrutinizer ignore-call */ has('bank_account') && $payload->has('real_name')) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
115
            $data['_serial_no'] = get_wechat_serial_no($params);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
116
            $publicKey = get_wechat_public_key($config, $data['_serial_no']);
117
118
            $data['real_name'] = encrypt_wechat_contents($payload->get('real_name'), $publicKey);
119
            $data['bank_account'] = encrypt_wechat_contents($payload->get('bank_account'), $publicKey);
120
        }
121
122
        return $data ?? [];
123
    }
124
}
125