Api::getApiEndpoint()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
1
<?php
2
3
namespace PayumTW\Collect;
4
5
use Http\Message\MessageFactory;
6
use Payum\Core\HttpClientInterface;
7
use Payum\Core\Exception\Http\HttpException;
8
9
abstract class Api
10
{
11
    /**
12
     * TIMEZONE.
13
     *
14
     * @var string
15
     */
16
    const TIMEZONE = 'Asia/Taipei';
17
18
    /**
19
     * @var \Payum\Core\HttpClientInterface
20
     */
21
    protected $client;
22
23
    /**
24
     * @var \Http\Message\MessageFactory
25
     */
26
    protected $messageFactory;
27
28
    /**
29
     * @var array
30
     */
31
    protected $options = [];
32
33
    /**
34
     * $encrypter.
35
     *
36
     * @var Encrypter
37
     */
38
    protected $encrypter;
39
40
    /**
41
     * @param array $options
42
     * @param \Payum\Core\HttpClientInterface $client
43
     * @param \Http\Message\MessageFactory $messageFactory
44
     * @param Encrypter $encrypter
45
     *
46
     * @throws \Payum\Core\Exception\InvalidArgumentException if an option is invalid
47
     */
48 12
    public function __construct(array $options, HttpClientInterface $client, MessageFactory $messageFactory, Encrypter $encrypter = null)
49
    {
50 12
        $this->options = $options;
51 12
        $this->client = $client;
52 12
        $this->messageFactory = $messageFactory;
53 12
        $this->encrypter = $encrypter ?: new Encrypter();
54 12
    }
55
56
    /**
57
     * @return string
58
     */
59
    abstract public function getApiEndpoint($type = 'capture');
60
61
    /**
62
     * createTransaction.
63
     *
64
     * @param array $params
65
     * @return array
66
     */
67
    abstract public function createTransaction(array $params);
68
69
    /**
70
     * Verify if the hash of the given parameter is correct.
71
     *
72
     * @param array $params
73
     * @return bool
74
     */
75 3
    public function verifyHash(array $params)
76
    {
77
        $filters = [
78 3
            'ok' => ['order_amount', 'send_time', 'ret', 'acquire_time', 'auth_code', 'card_no', 'notify_time', 'cust_order_no'],
79 3
            'fail' => ['order_amount', 'send_time', 'ret', 'notify_time', 'cust_order_no'],
80 3
            'status' => ['api_id', 'trans_id', 'amount', 'status', 'nonce'],
81 3
        ];
82
83 3
        if (isset($params['status']) === true) {
84 1
            $hashKey = 'checksum';
85 1
            $filterKeys = $filters['status'];
86 1
        } else {
87 2
            $hashKey = 'chk';
88 2
            $filterKeys = $filters[strtolower($params['ret'])];
89
        }
90
91 3
        return $params[$hashKey] === $this->calculateHash($params, $filterKeys);
92
    }
93
94
    /**
95
     * @param string $method
96
     * @param array|string $params
97
     * @param string $type
98
     * @param bool $isJson
99
     * @return string
100
     */
101 4
    protected function doRequest($method, $params, $type = 'cancel', $isJson = true)
102
    {
103 4
        $request = $this->messageFactory->createRequest($method, $this->getApiEndpoint($type), [
104 4
            'Content-Type' => 'application/x-www-form-urlencoded',
105 4
        ], is_array($params) === true ? http_build_query($params) : $params);
106
107 4
        $response = $this->client->send($request);
108
109 4
        $statusCode = $response->getStatusCode();
110 4
        if (false === ($statusCode >= 200 && $statusCode < 300)) {
111
            throw HttpException::factory($request, $response);
112
        }
113
114 4
        $contents = $response->getBody()->getContents();
115
116
        return $isJson === true
117 4
            ? json_decode($contents, true)
118 4
            : $contents;
119
    }
120
121
    /**
122
     * calculateHash.
123
     *
124
     * @param array $params
125
     * @return string
126
     */
127 6
    protected function calculateHash($params, $filterKeys = [])
128
    {
129 6
        return $this->encrypter
130 6
            ->setKey($this->options['hash_base'])
131 6
            ->encrypt($params, $filterKeys);
132
    }
133
}
134