Completed
Push — master ( c25059...aa3601 )
by i
04:27
created

AbstractWechat   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
dl 0
loc 48
ccs 6
cts 9
cp 0.6667
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getClientIp() 0 3 1
A prepare() 0 6 1
1
<?php
2
3
namespace Nilnice\Payment\Wechat;
4
5
use Illuminate\Config\Repository;
6
use Illuminate\Support\Collection;
7
use Nilnice\Payment\PaymentInterface;
8
use Nilnice\Payment\Wechat\Traits\RequestTrait;
9
use Nilnice\Payment\Wechat\Traits\SecurityTrait;
10
use Symfony\Component\HttpFoundation\Request;
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
     * @return mixed
34
     */
35
    public function getClientIp()
36
    {
37
        return Request::createFromGlobals()->server->get('SERVER_ADDR');
38
    }
39
40
    /**
41
     * Pregenerating order.
42
     *
43
     * @param string $gateway
44
     * @param array  $payload
45
     *
46
     * @return \Illuminate\Support\Collection
47
     *
48
     * @throws \Nilnice\Payment\Exception\GatewayException
49
     * @throws \InvalidArgumentException
50
     * @throws \Nilnice\Payment\Exception\InvalidKeyException
51
     * @throws \Nilnice\Payment\Exception\InvalidSignException
52
     * @throws \RuntimeException
53
     */
54 3
    protected function prepare(string $gateway, array $payload) : Collection
55
    {
56 3
        $key = $this->config->get('key');
57 3
        $payload['sign'] = self::generateSign($payload, $key);
58
59
        return $this->send($gateway, $payload, $key);
60
    }
61
}
62