Passed
Push — master ( d1dcd3...a849c7 )
by Songda
01:59
created

RefundAbnormalPlugin   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 28
c 1
b 0
f 0
dl 0
loc 86
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A normal() 0 3 1
A encryptSensitiveData() 0 11 3
A service() 0 7 1
A assembly() 0 29 3
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'] ?? Pay::MODE_NORMAL)) {
52
            $data = $this->service($params, $config, $payload);
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...normalPlugin::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

52
            $data = $this->service($params, $config, /** @scrutinizer ignore-type */ $payload);
Loading history...
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)
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...bnormalPlugin::normal() 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

61
            $data ?? $this->normal($params, $config, /** @scrutinizer ignore-type */ $payload)
Loading history...
62
        ))->exceptPayload('refund_id');
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
        return $this->encryptSensitiveData($params, $config, $payload);
79
    }
80
81
    /**
82
     * @throws ContainerException
83
     * @throws DecryptException
84
     * @throws InvalidConfigException
85
     * @throws InvalidParamsException
86
     * @throws ServiceNotFoundException
87
     */
88
    protected function service(array $params, array $config, Collection $payload): array
89
    {
90
        $data = [
91
            'sub_mchid' => $payload->get('sub_mchid', $config['sub_mch_id'] ?? ''),
92
        ];
93
94
        return array_merge($data, $this->encryptSensitiveData($params, $config, $payload));
95
    }
96
97
    /**
98
     * @throws ContainerException
99
     * @throws DecryptException
100
     * @throws InvalidConfigException
101
     * @throws InvalidParamsException
102
     * @throws ServiceNotFoundException
103
     */
104
    protected function encryptSensitiveData(array $params, array $config, Collection $payload): array
105
    {
106
        if ($payload->has('bank_account') && $payload->has('real_name')) {
107
            $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...
108
            $publicKey = get_wechat_public_key($config, $data['_serial_no']);
109
110
            $data['real_name'] = encrypt_wechat_contents($payload->get('real_name'), $publicKey);
111
            $data['bank_account'] = encrypt_wechat_contents($payload->get('bank_account'), $publicKey);
112
        }
113
114
        return $data ?? [];
115
    }
116
}
117