TransferGateway   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 75
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A pay() 0 28 4
A find() 0 8 2
A getTradeType() 0 4 1
1
<?php
2
3
namespace Yansongda\Pay\Gateways\Wechat;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Yansongda\Pay\Events;
7
use Yansongda\Pay\Exceptions\GatewayException;
8
use Yansongda\Pay\Exceptions\InvalidArgumentException;
9
use Yansongda\Pay\Exceptions\InvalidSignException;
10
use Yansongda\Pay\Gateways\Wechat;
11
use Yansongda\Supports\Collection;
12
13
class TransferGateway extends Gateway
14
{
15
    /**
16
     * Pay an order.
17
     *
18
     * @author yansongda <[email protected]>
19
     *
20
     * @param string $endpoint
21
     * @param array  $payload
22
     *
23
     * @throws GatewayException
24
     * @throws InvalidArgumentException
25
     * @throws InvalidSignException
26
     *
27
     * @return Collection
28
     */
29
    public function pay($endpoint, array $payload): Collection
30
    {
31
        if (Wechat::MODE_SERVICE === $this->mode) {
32
            unset($payload['sub_mch_id'], $payload['sub_appid']);
33
        }
34
35
        $type = Support::getTypeName($payload['type'] ?? '');
36
37
        $payload['mch_appid'] = Support::getInstance()->getConfig($type, '');
38
        $payload['mchid'] = $payload['mch_id'];
39
40
        if ('cli' !== php_sapi_name() && !isset($payload['spbill_create_ip'])) {
41
            $payload['spbill_create_ip'] = Request::createFromGlobals()->server->get('SERVER_ADDR');
42
        }
43
44
        unset($payload['appid'], $payload['mch_id'], $payload['trade_type'],
45
            $payload['notify_url'], $payload['type']);
46
47
        $payload['sign'] = Support::generateSign($payload);
48
49
        Events::dispatch(new Events\PayStarted('Wechat', 'Transfer', $endpoint, $payload));
50
51
        return Support::requestApi(
52
            'mmpaymkttransfers/promotion/transfers',
53
            $payload,
54
            true
55
        );
56
    }
57
58
    /**
59
     * Find.
60
     *
61
     * @author yansongda <[email protected]>
62
     *
63
     * @param $order
64
     *
65
     * @return array
66
     */
67
    public function find($order): array
68
    {
69
        return [
70
            'endpoint' => 'mmpaymkttransfers/gettransferinfo',
71
            'order' => is_array($order) ? $order : ['partner_trade_no' => $order],
72
            'cert' => true,
73
        ];
74
    }
75
76
    /**
77
     * Get trade type config.
78
     *
79
     * @author yansongda <[email protected]>
80
     *
81
     * @return string
82
     */
83
    protected function getTradeType(): string
84
    {
85
        return '';
86
    }
87
}
88