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
|
|
|
* @param string $method |
58
|
|
|
* @param array $params |
59
|
|
|
* @param string $type |
60
|
|
|
* @param bool $isJson |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
4 |
|
protected function doRequest($method, $params, $type = 'cancel', $isJson = true) |
64
|
|
|
{ |
65
|
4 |
|
$request = $this->messageFactory->createRequest($method, $this->getApiEndpoint($type), [ |
66
|
4 |
|
'Content-Type' => 'application/x-www-form-urlencoded', |
67
|
4 |
|
], is_array($params) === true ? http_build_query($params) : $params); |
68
|
|
|
|
69
|
4 |
|
$response = $this->client->send($request); |
70
|
|
|
|
71
|
4 |
|
$statusCode = $response->getStatusCode(); |
72
|
4 |
|
if (false == ($statusCode >= 200 && $statusCode < 300)) { |
|
|
|
|
73
|
|
|
throw HttpException::factory($request, $response); |
74
|
|
|
} |
75
|
|
|
|
76
|
4 |
|
$contents = $response->getBody()->getContents(); |
77
|
|
|
|
78
|
|
|
return $isJson === true |
79
|
4 |
|
? json_decode($contents, true) |
80
|
4 |
|
: $contents; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
abstract public function getApiEndpoint($type = 'capture'); |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* createTransaction. |
90
|
|
|
* |
91
|
|
|
* @param array $params |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
|
|
abstract public function createTransaction(array $params); |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Verify if the hash of the given parameter is correct. |
98
|
|
|
* |
99
|
|
|
* @param array $params |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
3 |
|
public function verifyHash(array $params) |
103
|
|
|
{ |
104
|
|
|
$filters = [ |
105
|
3 |
|
'ok' => ['order_amount', 'send_time', 'ret', 'acquire_time', 'auth_code', 'card_no', 'notify_time', 'cust_order_no'], |
106
|
3 |
|
'fail' => ['order_amount', 'send_time', 'ret', 'notify_time', 'cust_order_no'], |
107
|
3 |
|
'status' => ['api_id', 'trans_id', 'amount', 'status', 'nonce'], |
108
|
3 |
|
]; |
109
|
|
|
|
110
|
3 |
|
if (isset($params['status']) === true) { |
111
|
1 |
|
$hashKey = 'checksum'; |
112
|
1 |
|
$filterKeys = $filters['status']; |
113
|
1 |
|
} else { |
114
|
2 |
|
$hashKey = 'chk'; |
115
|
2 |
|
$filterKeys = $filters[strtolower($params['ret'])]; |
116
|
|
|
} |
117
|
|
|
|
118
|
3 |
|
return $params[$hashKey] === $this->calculateHash($params, $filterKeys); |
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
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.