Passed
Pull Request — master (#513)
by
unknown
02:09
created

RefundPlugin   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 18
c 1
b 0
f 0
dl 0
loc 38
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getUri() 0 3 1
A doSomething() 0 14 2
B getPartnerUri() 0 15 8
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\Ecommerce\Refund;
6
7
use Yansongda\Pay\Exception\Exception;
8
use Yansongda\Pay\Exception\InvalidParamsException;
9
use Yansongda\Pay\Plugin\Wechat\GeneralPlugin;
10
use Yansongda\Pay\Rocket;
11
12
class RefundPlugin extends GeneralPlugin
13
{
14
    protected function getUri(Rocket $rocket): string
15
    {
16
        throw new InvalidParamsException(Exception::SERVICE_NOT_FOUND_ERROR);
17
    }
18
19
    protected function getPartnerUri(Rocket $rocket): string
20
    {
21
        $payload = $rocket->getPayload();
22
23
        if (is_null($payload->get('transaction_id')) && is_null($payload->get('out_trade_no'))) {
24
            throw new InvalidParamsException(Exception::MISSING_NECESSARY_PARAMS);
25
        } elseif (is_null($payload->get('out_refund_no'))) {
26
            throw new InvalidParamsException(Exception::MISSING_NECESSARY_PARAMS);
27
        }
28
        $amount = $payload->get('amount');
29
        if (is_null($amount) || !isset($amount['refund']) || !isset($amount['total']) || !isset($amount['currency'])) {
30
            throw new InvalidParamsException(Exception::MISSING_NECESSARY_PARAMS);
31
        }
32
33
        return 'v3/ecommerce/refunds/apply';
34
    }
35
36
    protected function doSomething(Rocket $rocket): void
37
    {
38
        $config = get_wechat_config($rocket->getParams());
39
40
        $wechatId = [
41
            'sub_mchid' => $rocket->getPayload()->get('sub_mchid', $config->get('sub_mch_id', '')),
42
            'sp_appid' => $config->get('mini_app_id', $config->get('mp_app_id', '')),
43
        ];
44
45
        if (!$rocket->getPayload()->has('notify_url')) {
46
            $wechatId['notify_url'] = $config->get('notify_url');
47
        }
48
49
        $rocket->mergePayload($wechatId);
50
    }
51
}
52