-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass-gf-chip-api.php
More file actions
212 lines (189 loc) · 4.8 KB
/
class-gf-chip-api.php
File metadata and controls
212 lines (189 loc) · 4.8 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
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
/**
* CHIP API client.
*
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*
* @package GravityFormsCHIP
*/
defined( 'ABSPATH' ) || exit;
// CHIP API URL endpoint as per documented in: https://docs.chip-in.asia.
define( 'GF_CHIP_ROOT_URL', 'https://gate.chip-in.asia' );
/**
* CHIP API client class.
*/
class GF_CHIP_API {
/**
* Singleton instance.
*
* @var GF_CHIP_API
*/
private static $instance;
/**
* Secret key for API auth.
*
* @var string
*/
private $secret_key;
/**
* Brand ID.
*
* @var string
*/
private $brand_id;
/**
* Gets the singleton instance.
*
* @param string $secret_key Secret key.
* @param string $brand_id Brand ID.
* @return GF_CHIP_API
*/
public static function get_instance( $secret_key, $brand_id ) {
if ( null === self::$instance ) {
self::$instance = new self( $secret_key, $brand_id );
}
return self::$instance;
}
/**
* Constructor.
*
* @param string $secret_key Secret key.
* @param string $brand_id Brand ID.
*/
public function __construct( $secret_key, $brand_id ) {
$this->secret_key = $secret_key;
$this->brand_id = $brand_id;
}
/**
* Creates a payment (purchase).
*
* @param array $params Purchase params.
* @return array|null
*/
public function create_payment( $params ) {
// time() is to force fresh instead of cache.
return $this->call( 'POST', '/purchases/?time=' . time(), $params );
}
/**
* Gets a single payment (purchase).
*
* @param string $payment_id Purchase ID.
* @return array|null
*/
public function get_payment( $payment_id ) {
// time() is to force fresh instead of cache.
return $this->call( 'GET', "/purchases/{$payment_id}/?time=" . time() );
}
/**
* Cancels a payment.
*
* @param string $payment_id Purchase ID.
* @return array|null
*/
public function cancel_payment( $payment_id ) {
return $this->call( 'POST', "/purchases/{$payment_id}/cancel/" );
}
/**
* Gets the public key (validates credentials).
*
* @return array|string|null
*/
public function get_public_key() {
$result = $this->call( 'GET', '/public_key/' );
if ( is_string( $result ) ) {
$result = str_replace( '\n', "\n", $result );
}
return $result;
}
/**
* Gets the company UID for the current account (for storing public key by company).
* Calls GET /company_statements/; if results is empty, calls POST /company_statements/ to create one.
*
* @return string|null Company UID or null on failure.
*/
public function get_company_uid() {
$result = $this->call( 'GET', '/company_statements/' );
if ( is_array( $result ) && ! empty( $result['results'] ) && is_array( $result['results'] ) ) {
$first = reset( $result['results'] );
return isset( $first['company_uid'] ) ? (string) $first['company_uid'] : null;
}
if ( is_array( $result ) && ( empty( $result['results'] ) || ! isset( $result['results'] ) ) ) {
$post_result = $this->call(
'POST',
'/company_statements/',
array(
'format' => 'csv',
'timezone' => 'UTC',
)
);
if ( is_array( $post_result ) && isset( $post_result['company_uid'] ) ) {
return (string) $post_result['company_uid'];
}
}
return null;
}
/**
* Refunds a payment.
*
* @param string $payment_id Purchase ID.
* @param array $params Refund params.
* @return array|null
*/
public function refund_payment( $payment_id, $params ) {
return $this->call( 'POST', "/purchases/{$payment_id}/refund/", $params );
}
/**
* Makes an API call.
*
* @param string $method HTTP method.
* @param string $route API route.
* @param array $params Request body (encoded to JSON).
* @return array|null
*/
private function call( $method, $route, $params = array() ) {
$secret_key = $this->secret_key;
if ( ! empty( $params ) ) {
$params = wp_json_encode( $params );
}
$response = $this->request(
$method,
sprintf( '%s/api/v1%s', GF_CHIP_ROOT_URL, $route ),
$params,
array(
'Content-type' => 'application/json',
'Authorization' => 'Bearer ' . $secret_key,
)
);
$result = json_decode( $response, true );
if ( ! $result ) {
return null;
}
if ( ! empty( $result['errors'] ) ) {
return null;
}
return $result;
}
/**
* Sends an HTTP request.
*
* @param string $method HTTP method.
* @param string $url Full URL.
* @param array $params Body.
* @param array $headers Headers.
* @return string Response body.
*/
private function request( $method, $url, $params = array(), $headers = array() ) {
$wp_request = wp_remote_request(
$url,
array(
'method' => $method,
'sslverify' => apply_filters( 'gf_chip_sslverify', true ),
'headers' => $headers,
'body' => $params,
)
);
$response = wp_remote_retrieve_body( $wp_request );
return $response;
}
}