forked from chargeover/chargeover_php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChargeOverAPI.php
More file actions
executable file
·551 lines (443 loc) · 11.9 KB
/
Copy pathChargeOverAPI.php
File metadata and controls
executable file
·551 lines (443 loc) · 11.9 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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
<?php
define('CHARGEOVERAPI_BASEDIR', dirname(__FILE__));
require_once CHARGEOVERAPI_BASEDIR . '/ChargeOverAPI/Loader.php';
ChargeOverAPI_Loader::load('/ChargeOverAPI/Object.php');
ChargeOverAPI_Loader::load('/ChargeOverAPI/Aggregate.php');
ChargeOverAPI_Loader::load('/ChargeOverAPI/Bulk.php');
ChargeOverAPI_Loader::import('/ChargeOverAPI/Object/');
class ChargeOverAPI
{
const ERROR_UNKNOWN = -1;
const ERROR_RESPONSE = -2;
const AUTHMODE_SIGNATURE_V1 = 'signature-v1';
const AUTHMODE_HTTP_BASIC = 'http-basic';
const METHOD_CREATE = 'create';
const METHOD_MODIFY = 'modify';
const METHOD_DELETE = 'delete';
const METHOD_GET = 'get';
const METHOD_FIND = 'find';
const METHOD_ACTION = 'action';
const METHOD_AGGREGATE = 'aggregate';
const METHOD_BULK = 'bulk';
const STATUS_OK = 'OK';
const STATUS_ERROR = 'Error';
const FLAG_QUICKBOOKS = '_flag_quickbooks';
const FLAG_EMAILS = '_flag_emails';
const FLAG_WEBHOOKS = '_flag_webhooks';
const FLAG_EVENTS = '_flag_events';
protected $_url;
protected $_authmode;
protected $_username;
protected $_password;
protected $_last_request;
protected $_last_response;
protected $_last_error;
protected $_flags;
public function __construct($url, $authmode, $username, $password, $flags = array())
{
$this->_url = rtrim($url, '/');
$this->_authmode = $authmode;
$this->_username = $username;
$this->_password = $password;
$this->_last_request = null;
$this->_last_response = null;
$this->_last_error = null;
$this->_flags = (array) $flags;
}
protected function _signature($public, $private, $url, $data)
{
$tmp = array_merge(range('a', 'z'), range(0, 9));
shuffle($tmp);
$nonce = implode('', array_slice($tmp, 0, 8));
$time = time();
$str = $public . '||' . strtolower($url) . '||' . $nonce . '||' . $time . '||' . $data;
$signature = hash_hmac('sha256', $str, $private);
//print('lib { ' . $str . ' }' . "\n\n");
return 'Authorization: ChargeOver co_public_key="' . $public . '" co_nonce="' . $nonce . '" co_timestamp="' . $time . '" co_signature_method="HMAC-SHA256" co_version="1.0" co_signature="' . $signature . '" ';
}
protected function _request($http_method, $uri, $data = null)
{
$public = $this->_username;
$private = $this->_password;
$endpoint = $this->_url . '/' . ltrim($uri, '/');
if (count($this->_flags))
{
if (false === strpos($endpoint, '?'))
{
$endpoint .= '?' . http_build_query($this->_flags);
}
else
{
$endpoint .= '&' . http_build_query($this->_flags);
}
}
/*
if (false === strpos($endpoint, '?'))
{
$endpoint .= '?debug=1';
}
else
{
$endpoint .= '&debug=1';
}
*/
$headers = array();
$headers[] = 'Content-Type: application/json';
// create a new cURL resource
$ch = curl_init();
if ($data)
{
$data = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
if ($this->_authmode == ChargeOverAPI::AUTHMODE_SIGNATURE_V1)
{
// Signed requests
$signature = $this->_signature($public, $private, $endpoint, $data);
$headers[] = $signature;
}
else if ($this->_authmode == ChargeOverAPI::AUTHMODE_HTTP_BASIC)
{
// HTTP basic
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $public . ':' . $private);
}
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json' ));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $http_method);
// Build last request string
$this->_last_request = $http_method . ' ' . $endpoint . "\r\n\r\n";
if ($data)
{
$this->_last_request .= json_encode($data);
}
$out = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
// Log last response
$this->_last_response = $out;
if (!$out)
{
$err = 'Problem hitting URL [' . $endpoint . ']: ' . print_r(curl_getinfo($ch), true);
return $this->_error($err, ChargeOverAPI::ERROR_UNKNOWN);
}
$data = json_decode($out);
if (json_last_error() == JSON_ERROR_NONE)
{
// We at least got back a valid JSON object
if ($data->status != ChargeOverAPI::STATUS_OK)
{
return $this->_error($data->message, $data->code);
}
return $data;
}
$err = 'Server returned an invalid JSON response: ' . $out . ', JSON parser returned error: ' . json_last_error();
return $this->_error($err, ChargeOverAPI::ERROR_RESPONSE);
}
protected function _error($err, $code = 400)
{
$this->_last_error = $err;
// The response we got back wasn't valid JSON...?
return json_decode(json_encode(array(
'code' => $code, // let's force this to a 400 error instead, it's non-recoverable $info['http_code'],
'status' => ChargeOverAPI::STATUS_ERROR,
'message' => $err,
'response' => null,
)));
}
public function flag($flag, $value)
{
$this->_flags[$flag] = (int) $value;
}
public function lastRequest()
{
return $this->_last_request;
}
public function lastResponse()
{
return $this->_last_response;
}
public function lastError()
{
return $this->_last_error;
}
public function isError($Object)
{
//print_r($Object);
if (!is_object($Object))
{
return true;
}
else if ($Object->status != ChargeOverAPI::STATUS_OK)
{
return true;
}
return false;
}
/**
* Map a method/id/object type to an API URL
*
* @param string $method
* @param integer $id
* @param varies $Object_or_obj_type Either an object, or an object type constant
* @return string The URL for the API
*/
protected function _map($method, $id, $Object_or_obj_type)
{
if ($method == ChargeOverAPI::METHOD_AGGREGATE)
{
return '_aggregate';
}
else if ($method == ChargeOverAPI::METHOD_BULK)
{
return '_bulk';
}
if (is_object($Object_or_obj_type))
{
$obj_type = $this->classToType($Object_or_obj_type);
}
else
{
$obj_type = $Object_or_obj_type;
}
if ($method == ChargeOverAPI::METHOD_CREATE)
{
$id = null;
}
if ($id)
{
return $obj_type . '/' . $id;
}
else
{
return $obj_type;
}
}
public function typeToClass($type)
{
$map = $this->_typeClassMap();
if (isset($map[$type]))
{
return $map[$type];
}
return null;
}
public function classToType($Object_or_class)
{
if (is_object($Object_or_class))
{
$class = get_class($Object_or_class);
}
else
{
$class = $Object_or_class;
}
$map = array_flip($this->_typeClassMap());
if (isset($map[$class]))
{
return $map[$class];
}
return null;
}
protected function _typeClassMap()
{
return array(
ChargeOverAPI_Object::TYPE_CUSTOMER => 'ChargeOverAPI_Object_Customer',
ChargeOverAPI_Object::TYPE_USER => 'ChargeOverAPI_Object_User',
ChargeOverAPI_Object::TYPE_BILLINGPACKAGE => 'ChargeOverAPI_Object_BillingPackage',
ChargeOverAPI_Object::TYPE_PACKAGE => 'ChargeOverAPI_Object_Package',
ChargeOverAPI_Object::TYPE_PROJECT => 'ChargeOverAPI_Object_Project',
ChargeOverAPI_Object::TYPE_CREDITCARD => 'ChargeOverAPI_Object_CreditCard',
ChargeOverAPI_Object::TYPE_INVOICE => 'ChargeOverAPI_Object_Invoice',
ChargeOverAPI_Object::TYPE_TRANSACTION => 'ChargeOverAPI_Object_Transaction',
ChargeOverAPI_Object::TYPE_ACH => 'ChargeOverAPI_Object_Ach',
ChargeOverAPI_Object::TYPE_USAGE => 'ChargeOverAPI_Object_Usage',
ChargeOverAPI_Object::TYPE_ITEM => 'ChargeOverAPI_Object_Item',
ChargeOverAPI_Object::TYPE_ITEMCATEGORY => 'ChargeOverAPI_Object_ItemCategory',
ChargeOverAPI_Object::TYPE_NOTE => 'ChargeOverAPI_Object_Note',
ChargeOverAPI_Object::TYPE_COUNTRY => 'ChargeOverAPI_Object_Country',
);
}
public function rawRequest($method, $uri, $data)
{
}
public function bulk($Bulk)
{
$uri = $this->_map(ChargeOverAPI::METHOD_BULK, null, null);
return $this->_request('POST', $uri, $Bulk->toArray());
}
public function aggregate($Aggregate)
{
$uri = $this->_map(ChargeOverAPI::METHOD_AGGREGATE, null, null);
return $this->_request('POST', $uri, $Aggregate->toArray());
}
/**
* Perform an action (e.g. upgrade/downgrade, cancel, void, set payment method, etc.)
*
* @param string $type
* @param integer $id
* @param string $action
* @param array $data
* @return array
*/
public function action($type, $id, $action, $data = array())
{
$uri = $this->_map(ChargeOverAPI::METHOD_ACTION, $id, $type);
if (is_object($data))
{
$data = $data->toArray();
}
$uri .= '?action=' . $action;
return $this->_request('POST', $uri, $data);
}
public function create($Object)
{
$uri = $this->_map(ChargeOverAPI::METHOD_CREATE, null, $Object);
/*
print('[');
print_r($Object);
print(']');
*/
return $this->_request('POST', $uri, $Object->toArray());
}
public function modify($id, $Object)
{
$uri = $this->_map(ChargeOverAPI::METHOD_MODIFY, $id, $Object);
return $this->_request('PUT', $uri, $Object->toArray());
}
public function find($type, $where = array(), $sort = array(), $offset = 0, $limit = null)
{
$uri = $this->_map(ChargeOverAPI::METHOD_FIND, null, $type);
$uri .= '?_dummy=1';
// WHERE
if (is_array($where) and
count($where))
{
foreach ($where as $key => $value)
{
$where[$key] = urlencode($value);
}
}
$uri .= '&where=' . implode(',', $where);
// ORDER
if (is_array($sort) and
count($sort))
{
foreach ($sort as $key => $value)
{
$sort[$key] = urlencode($value);
}
}
$uri .= '&order=' . implode(',', $sort);
if ($offset or $limit)
{
$uri .= '&offset=' . ((int) $offset) . '&limit=' . ((int) $limit);
}
$resp = $this->_request('GET', $uri);
if (!$this->isError($resp))
{
//print_r($resp);
//print("\n\n\n\n");
$class = $this->typeToClass($type);
// Let's try to transform the array we got back into a list of objects
foreach ($resp->response as $key => $obj)
{
$resp->response[$key] = $this->_createObject($class, $obj);
}
}
return $resp;
}
public function delete($type, $id)
{
$uri = $this->_map(ChargeOverAPI::METHOD_DELETE, $id, $type);
return $this->_request('DELETE', $uri);
}
public function findById($type, $id)
{
if (!$id)
{
return $this->_error('You must provide a valid id value to findById($type, $id)');
}
$uri = $this->_map(ChargeOverAPI::METHOD_FIND, $id, $type);
$resp = $this->_request('GET', $uri);
if (!$this->isError($resp))
{
$class = $this->typeToClass($type);
$resp->response = $this->_createObject($class, $resp->response);
}
return $resp;
}
protected function _createObject($class, $arr_or_obj)
{
if (is_object($arr_or_obj))
{
$arr_or_obj = get_object_vars($arr_or_obj);
}
// Find any children
foreach ($arr_or_obj as $key => $value)
{
if (is_object($value))
{
$value = get_object_vars($value);
}
if (is_array($value))
{
// This is a child
switch ($key)
{
case 'line_items':
$sclass = 'ChargeOverAPI_Object_LineItem';
$is_list = true;
break;
case 'tierset':
$sclass = 'ChargeOverAPI_Object_Tierset';
$is_list = false;
break;
case 'tiers':
$sclass = 'ChargeOverAPI_Object_Tier';
$is_list = true;
break;
default:
$sclass = null;
$is_list = false;
break;
}
if ($sclass)
{
if ($is_list)
{
foreach ($value as $skey => $obj)
{
$arr_or_obj[$key][$skey] = $this->_createObject($sclass, $obj);
}
}
else
{
$arr_or_obj[$key] = $this->_createObject($sclass, $value);
}
}
/*
foreach ($value as $skey => $obj)
{
if ($sclass)
{
if (is_object($obj))
{
$obj = get_object_vars($obj);
}
$arr[$key][$skey] = new $sclass($obj);
}
}
*/
}
else
{
}
}
return new $class($arr_or_obj);
}
}