Completed
Push — master ( bf6fd0...fe0998 )
by i
04:47
created

AbstractWechat::getGatewayUrl()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 4
nop 1
dl 0
loc 18
ccs 0
cts 14
cp 0
crap 20
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Nilnice\Payment\Wechat;
4
5
use Illuminate\Config\Repository;
6
use Illuminate\Support\Collection;
7
use Nilnice\Payment\Constant;
8
use Nilnice\Payment\PaymentInterface;
9
use Nilnice\Payment\Wechat\Traits\RequestTrait;
10
use Nilnice\Payment\Wechat\Traits\SecurityTrait;
11
12
abstract class AbstractWechat implements PaymentInterface
13
{
14
    use SecurityTrait;
15
    use RequestTrait;
16
17
    /**
18
     * @var \Illuminate\Config\Repository
19
     */
20
    protected $config;
21
22
    /**
23
     * AbstractWechat constructor.
24
     *
25
     * @param \Illuminate\Config\Repository $config
26
     */
27 3
    public function __construct(Repository $config)
28
    {
29 3
        $this->config = $config;
30 3
    }
31
32
    /**
33
     * Pregenerating order.
34
     *
35
     * @param string $gateway
36
     * @param array  $payload
37
     *
38
     * @return \Illuminate\Support\Collection
39
     * @throws \Nilnice\Payment\Exception\GatewayException
40
     * @throws \InvalidArgumentException
41
     * @throws \Nilnice\Payment\Exception\InvalidKeyException
42
     * @throws \Nilnice\Payment\Exception\InvalidSignException
43
     * @throws \RuntimeException
44
     */
45 3
    protected function prepare(string $gateway, array $payload) : Collection
46
    {
47 3
        $env = $this->config->get('env', 'pro');
48 3
        $key = $this->config->get('key');
49 3
        $payload['sign'] = self::generateSign($payload, $key);
50
        $gateway = self::getGatewayUrl($env) . $gateway;
51
52
        return $this->send($gateway, $payload, $key);
53
    }
54
55
    /**
56
     * Get gateway url.
57
     *
58
     * @param string $env
59
     *
60
     * @return string
61
     */
62
    private static function getGatewayUrl($env = '') : string
63
    {
64
        $uri = '';
65
        switch ($env) {
66
            case 'pro':
67
                $uri = Constant::WX_PAY_PRO_URI;
68
                break;
69
            case 'dev':
70
                $uri = Constant::WX_PAY_DEV_URI;
71
                break;
72
            case 'hk':
73
                $uri = Constant::WX_PAY_PRO_HK_URI;
74
                break;
75
            default:
76
                break;
77
        }
78
79
        return $uri;
80
    }
81
}
82