-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstamojo.php
367 lines (322 loc) · 11.4 KB
/
instamojo.php
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<?php
class Instamojo {
const version = '1.1';
protected $curl;
protected $endpoint = 'https://www.instamojo.com/api/1.1/';
protected $api_key = null;
protected $auth_token = null;
/**
* @param string $api_key
* @param string $auth_token is available on the d
* @param string $endpoint can be set if you are working on an alternative server.
* @return array AuthToken object.
*/
public function __construct($api_key, $auth_token=null, $endpoint=null)
{
$this->api_key = (string) $api_key;
$this->auth_token = (string) $auth_token;
if(!is_null($endpoint)){
$this->endpoint = (string) $endpoint;
}
}
public function __destruct()
{
if(!is_null($this->curl)) {
curl_close($this->curl);
}
}
/**
* @return array headers with Authentication tokens added
*/
private function build_curl_headers()
{
$headers = array("X-Api-key: $this->api_key");
if($this->auth_token) {
$headers[] = "X-Auth-Token: $this->auth_token";
}
return $headers;
}
/**
* @param string $path
* @return string adds the path to endpoint with.
*/
private function build_api_call_url($path)
{
if (strpos($path, '/?') === false and strpos($path, '?') === false) {
return $this->endpoint . $path . '/';
}
return $this->endpoint . $path;
}
/**
* @param string $method ('GET', 'POST', 'DELETE', 'PATCH')
* @param string $path whichever API path you want to target.
* @param array $data contains the POST data to be sent to the API.
* @return array decoded json returned by API.
*/
private function api_call($method, $path, array $data=null)
{
$path = (string) $path;
$method = (string) $method;
$data = (array) $data;
$headers = $this->build_curl_headers();
$request_url = $this-> build_api_call_url($path);
$options = array();
$options[CURLOPT_HTTPHEADER] = $headers;
$options[CURLOPT_RETURNTRANSFER] = true;
if($method == 'POST') {
$options[CURLOPT_POST] = 1;
$options[CURLOPT_POSTFIELDS] = http_build_query($data);
} else if($method == 'DELETE') {
$options[CURLOPT_CUSTOMREQUEST] = 'DELETE';
} else if($method == 'PATCH') {
$options[CURLOPT_POST] = 1;
$options[CURLOPT_POSTFIELDS] = http_build_query($data);
$options[CURLOPT_CUSTOMREQUEST] = 'PATCH';
} else if ($method == 'GET' or $method == 'HEAD') {
if (!empty($data)) {
/* Update URL to container Query String of Paramaters */
$request_url .= '?' . http_build_query($data);
}
}
// $options[CURLOPT_VERBOSE] = true;
$options[CURLOPT_URL] = $request_url;
$this->curl = curl_init();
$setopt = curl_setopt_array($this->curl, $options);
$response = curl_exec($this->curl);
$headers = curl_getinfo($this->curl);
$error_number = curl_errno($this->curl);
$error_message = curl_error($this->curl);
$response_obj = json_decode($response, true);
if($error_number != 0){
if($error_number == 60){
throw new Exception("Something went wrong. cURL raised an error with number: $error_number and message: $error_message. " .
"Please check http://stackoverflow.com/a/21114601/846892 for a fix." . PHP_EOL);
}
else{
throw new Exception("Something went wrong. cURL raised an error with number: $error_number and message: $error_message." . PHP_EOL);
}
}
if($response_obj['success'] == false) {
$message = json_encode($response_obj['message']);
throw new Exception($message . PHP_EOL);
}
return $response_obj;
}
/**
* @return string URL to upload file or cover image asynchronously
*/
public function getUploadUrl()
{
$result = $this->api_call('GET', 'links/get_file_upload_url', array());
return $result['upload_url'];
}
/**
* @param string $file_path
* @return string JSON returned when the file upload is complete.
*/
public function uploadFile($file_path)
{
$upload_url = $this->getUploadUrl();
$file_path = realpath($file_path);
$file_name = basename($file_path);
$ch = curl_init();
$data = array('fileUpload' => $this->getCurlValue($file_path, $file_name));
curl_setopt($ch, CURLOPT_URL, $upload_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
return curl_exec($ch);
}
public function getCurlValue($file_path, $file_name, $content_type='')
{
// http://stackoverflow.com/a/21048702/846892
// PHP 5.5 introduced a CurlFile object that deprecates the old @filename syntax
// See: https://wiki.php.net/rfc/curl-file-upload
if (function_exists('curl_file_create')) {
return curl_file_create($file_path, $content_type, $file_name);
}
// Use the old style if using an older version of PHP
$value = "@{$file_path};filename=$file_name";
if ($content_type) {
$value .= ';type=' . $content_type;
}
return $value;
}
/**
* Uploads any file or cover image mentioned in $link and
* updates it with the json required by the API.
* @param array $link
* @return array $link updated with uploaded file information if applicable.
*/
public function uploadMagic(array $link)
{
if(!empty($link['file_upload'])) {
$file_upload_json = $this->uploadFile($link['file_upload']);
$link['file_upload_json'] = $file_upload_json;
unset($link['file_upload']);
}
if(!empty($link['cover_image'])) {
$cover_image_json = $this->uploadFile($link['cover_image']);
$link['cover_image_json'] = $cover_image_json;
unset($link['cover_image']);
}
return $link;
}
/**
* Authenticate using username and password of a user.
* Automatically updates the auth_token value.
* @param array $args contains username=>USERNAME and password=PASSWORD
* @return array AuthToken object.
*/
public function auth(array $args)
{
$response = $this->api_call('POST', 'auth', $args);
$this->auth_token = $response['auth_token']['auth_token'];
return $this->auth_token;
}
/**
* @return array list of Link objects.
*/
public function linksList()
{
$response = $this->api_call('GET', 'links', array());
return $response['links'];
}
/**
* @return array single Link object.
*/
public function linkDetail($slug)
{
$response = $this->api_call('GET', 'links/' . $slug, array());
return $response['link'];
}
/**
* @return array single Link object.
*/
public function linkCreate(array $link)
{
if(empty($link['currency'])){
$link['currency'] = 'INR';
}
$link = $this->uploadMagic($link);
$response = $this->api_call('POST', 'links', $link);
return $response['link'];
}
/**
* @return array single Link object.
*/
public function linkEdit($slug, array $link)
{
$link = $this->uploadMagic($link);
$response = $this->api_call('PATCH', 'links/' . $slug, $link);
return $response['link'];
}
/**
* @return array single Link object.
*/
public function linkDelete($slug)
{
$response = $this->api_call('DELETE', 'links/' . $slug, array());
return $response;
}
/**
* @return array list of Payment objects.
*/
public function paymentsList($limit = null, $page = null)
{
$params = array();
if (!is_null($limit)) {
$params['limit'] = $limit;
}
if (!is_null($page)) {
$params['page'] = $page;
}
$response = $this->api_call('GET', 'payments', $params);
return $response['payments'];
}
/**
* @param string payment_id as provided by paymentsList() or Instamojo's webhook or redirect functions.
* @return array single Payment object.
*/
public function paymentDetail($payment_id)
{
$response = $this->api_call('GET', 'payments/' . $payment_id, array());
return $response['payment'];
}
///// Request a Payment /////
/**
* @param array single PaymentRequest object.
* @return array single PaymentRequest object.
*/
public function paymentRequestCreate(array $payment_request)
{
$response = $this->api_call('POST', 'payment-requests', $payment_request);
return $response['payment_request'];
}
/**
* @param string id as provided by paymentRequestCreate, paymentRequestsList, webhook or redirect.
* @return array single PaymentRequest object.
*/
public function paymentRequestStatus($id)
{
$response = $this->api_call('GET', 'payment-requests/' . $id, array());
return $response['payment_request'];
}
/**
* @param string id as provided by paymentRequestCreate, paymentRequestsList, webhook or redirect.
* @param string payment_id as received with the redirection URL or webhook.
* @return array single PaymentRequest object.
*/
public function paymentRequestPaymentStatus($id, $payment_id)
{
$response = $this->api_call('GET', 'payment-requests/' . $id . '/' . $payment_id, array());
return $response['payment_request'];
}
/**
* @param array datetime_limits containing datetime data with keys 'max_created_at', 'min_created_at',
* 'min_modified_at' and 'max_modified_at' in ISO 8601 format(optional).
* @return array containing list of PaymentRequest objects.
* For more information on the allowed date formats check the
* docs: https://www.instamojo.com/developers/request-a-payment-api/#toc-filtering-payment-requests
*/
public function paymentRequestsList($datetime_limits=null)
{
$endpoint = 'payment-requests';
if(!empty($datetime_limits)){
$query_string = http_build_query($datetime_limits);
if(!empty($query_string)){
$endpoint .= '/?' . $query_string;
}
}
$response = $this->api_call('GET', $endpoint, array());
return $response['payment_requests'];
}
///// Refunds /////
/**
* @param array single Refund object.
* @return array single Refund object.
*/
public function refundCreate(array $refund)
{
$response = $this->api_call('POST', 'refunds', $refund);
return $response['refund'];
}
/**
* @param string id as provided by refundCreate or refundsList.
* @return array single Refund object.
*/
public function refundDetail($id)
{
$response = $this->api_call('GET', 'refunds/' . $id, array());
return $response['refund'];
}
/**
* @return array containing list of Refund objects.
*/
public function refundsList()
{
$response = $this->api_call('GET', 'refunds', array());
return $response['refunds'];
}
}
?>