-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.chip.api.php
More file actions
199 lines (155 loc) · 4.51 KB
/
Copy pathclass.chip.api.php
File metadata and controls
199 lines (155 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
define("CHIP_API_ROOT_URL", "https://gate.chip-in.asia");
class ChipApi
{
private static $_instance;
private $require_empty_string_encoding = false;
private $brand_id;
private $private_key;
public static function get_instance($secret_key, $brand_id)
{
if (self::$_instance == null) {
self::$_instance = new self($secret_key, $brand_id);
}
return self::$_instance;
}
public function __construct($private_key, $brand_id)
{
$this->private_key = $private_key;
$this->brand_id = $brand_id;
}
public function create_payment($params)
{
return $this->call('POST', '/purchases/', $params);
}
public function charge_payment($payment_id, $params)
{
return $this->call('POST', "/purchases/{$payment_id}/charge/", $params);
}
public function payment_methods($currency)
{
return $this->call(
'GET',
"/payment_methods/?brand_id={$this->brand_id}¤cy={$currency}&amount=200"
);
}
public function payment_recurring_methods($currency)
{
return $this->call(
'GET',
"/payment_methods/?brand_id={$this->brand_id}¤cy={$currency}&amount=200&recurring=true"
);
}
public function get_payment($payment_id)
{
$result = $this->call('GET', "/purchases/{$payment_id}/");
return $result;
}
public function was_payment_successful($payment_id)
{
$result = $this->get_payment($payment_id);
return $result && $result['status'] == 'paid';
}
public function create_client($params)
{
return $this->call('POST', "/clients/", $params);
}
// this is secret feature
public function get_client_by_email($email)
{
$email_encoded = urlencode($email);
return $this->call('GET', "/clients/?q={$email_encoded}");
}
public function patch_client($client_id, $params)
{
return $this->call('PATCH', "/clients/{$client_id}/", $params);
}
public function delete_token($purchase_id)
{
return $this->call('POST', "/purchases/$purchase_id/delete_recurring_token/");
}
public function retrieve_token($client_id)
{
return $this->call('GET', "/clients/{$client_id}/recurring_tokens/{$client_id}/");
}
public function list_tokens($client_id)
{
return $this->call('GET', "/clients/{$client_id}/recurring_tokens/");
}
public function refund_payment($payment_id, $params)
{
$result = $this->call('POST', "/purchases/{$payment_id}/refund/", $params);
return $result;
}
public function public_key()
{
$result = $this->call('GET', "/public_key/");
return $result;
}
public function account_balance()
{
$params = array(
'brand_id' => $this->brand_id
);
// get initial state prior
$initial_state = $this->require_empty_string_encoding;
// set to true as it requires empty encoding
$this->require_empty_string_encoding = true;
$result = $this->call('GET', '/account/json/balance/?' . http_build_query($params));
// restore initial state
$this->require_empty_string_encoding = $initial_state;
return $result;
}
private function call($method, $route, $params = [])
{
$private_key = $this->private_key;
if (!empty($params)) {
$params = json_encode($params);
}
$response = $this->request(
$method,
sprintf("%s/api/v1%s", CHIP_API_ROOT_URL, $route),
$params,
[
'Content-type: application/json',
'Authorization: ' . "Bearer " . $private_key,
]
);
$result = json_decode($response, true);
if (!$result) {
return null;
}
if (!empty($result['errors'])) {
return null;
}
return $result;
}
private function request($method, $url, $params = [], $headers = [])
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
if ($method == 'POST') {
curl_setopt($ch, CURLOPT_POST, 1);
}
if ($method == 'PUT') {
curl_setopt($ch, CURLOPT_PUT, 1);
}
if ($method == 'PUT' or $method == 'POST' or $method == 'PATCH') {
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
}
if ($method == 'PATCH') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// this to prevent error when account balance called
if ($this->require_empty_string_encoding) {
curl_setopt($ch, CURLOPT_ENCODING, '');
}
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
}