PluginProbe
MakeCommerce for WooCommerce / 2.5.4
MakeCommerce for WooCommerce v2.5.4
4.1.0 4.0.8 trunk 1.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2.0 2.2.1 2.2.2 All 96 releases
makecommerce / includes / Api.php

Api.php in MakeCommerce for WooCommerce 2.5.4, at includes/Api.php

807 lines 21.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 require __DIR__ . '/vendor/autoload.php';
4
5 use Httpful\Http;
6 use Httpful\Request;
7
8 class Maksekeskus
9 {
10 const SIGNATURE_TYPE_1 = 'V1';
11 const SIGNATURE_TYPE_2 = 'V2';
12 const SIGNATURE_TYPE_MAC = 'MAC';
13
14 /**
15 * @var str API base URL
16 */
17 private $apiUrl;
18
19
20 /**
21 * @var str Shop ID
22 */
23 private $shopId;
24
25
26 /**
27 * @var str Publishable Key
28 */
29 private $publishableKey;
30
31
32 /**
33 * @var str Secret Key
34 */
35 private $secretKey;
36
37
38 /**
39 * Response object of the last API request
40 *
41 * @var obj
42 */
43 private $lastApiResponse;
44
45
46 /**
47 * API client constructor
48 *
49 * @param str $shopId Shop ID
50 * @param str $publishableKey Publishable API Key, NULL if not provided
51 * @param str $$secretKey Secret API Key, NULL if not provided
52 * @param bool $testEnv TRUE if connecting to API in test environment, FALSE otherwise. Default to FALSE.
53 * @return void
54 */
55 public function __construct ($shopId, $publishableKey = NULL, $secretKey = NULL, $testEnv = FALSE)
56 {
57 $this->setShopId($shopId);
58 $this->setPublishableKey($publishableKey);
59 $this->setSecretKey($secretKey);
60
61 if ($testEnv) {
62 $this->setApiUrl('https://api-test.maksekeskus.ee');
63 } else {
64 $this->setApiUrl('https://api.maksekeskus.ee');
65 }
66 }
67
68
69 /**
70 * Set API base URL
71 *
72 * @param str $value
73 * @return void
74 */
75 public function setApiUrl ($value)
76 {
77 $this->apiUrl = $value;
78 }
79
80
81 /**
82 * Get API base URL
83 *
84 * @return str
85 */
86 public function getApiUrl ()
87 {
88 return $this->apiUrl;
89 }
90
91
92 /**
93 * Set Shop ID
94 *
95 * @param str $value
96 * @return void
97 */
98 public function setShopId ($value)
99 {
100 $this->shopId = $value;
101 }
102
103
104 /**
105 * Get Shop ID
106 *
107 * @return str
108 */
109 public function getShopId ()
110 {
111 return $this->shopId;
112 }
113
114
115 /**
116 * Set Publishable Key
117 *
118 * @param str $value
119 * @return void
120 */
121 public function setPublishableKey ($value)
122 {
123 $this->publishableKey = $value;
124 }
125
126
127 /**
128 * Get Publishable Key
129 *
130 * @return str
131 */
132 public function getPublishableKey ()
133 {
134 return $this->publishableKey;
135 }
136
137
138 /**
139 * Set Secret Key
140 *
141 * @param str $value
142 * @return void
143 */
144 public function setSecretKey ($value)
145 {
146 $this->secretKey = $value;
147 }
148
149
150 /**
151 * Get Secret Key
152 *
153 * @return str
154 */
155 public function getSecretKey ()
156 {
157 return $this->secretKey;
158 }
159
160
161 /**
162 * Extract message data from request
163 *
164 * @param array $request Request data (ie. $_REQUEST)
165 * @param bool $as_object Whether to return the message as an object, defaults to FALSE
166 * @throws Exception if unable to extract message data from request
167 * @return mixed An object or associative array containing the message data
168 */
169 public function extractRequestData ($request, $as_object = FALSE)
170 {
171 if (empty($request['json'])) {
172 throw new Exception("Unable to extract data from request");
173 }
174
175 return json_decode($request['json'], !$as_object);
176 }
177
178
179 /**
180 * Extracts the signature type from request data
181 *
182 * @deprecated Verify message authenticity via MAC instead.
183 * @param array $request Associative array of request data
184 * @return string Returns the signature type, NULL if not present
185 */
186 public function extractRequestSignatureType ($request)
187 {
188 $data = $this->extractRequestData($request);
189
190 if (!empty($data['signature'])) {
191 if (empty($data['transaction'])) {
192 return self::SIGNATURE_TYPE_1;
193 } else {
194 return self::SIGNATURE_TYPE_2;
195 }
196 }
197
198 return null;
199 }
200
201
202 /**
203 * Extracts the signature value from request data
204 *
205 * @deprecated Verify message authenticity via MAC instead.
206 * @param array $request Associative array of request data
207 * @return string Returns the signature, NULL if not present
208 */
209 public function extractRequestSignature ($request)
210 {
211 $data = $this->extractRequestData($request);
212
213 if (!empty($data['signature'])) {
214 return $data['signature'];
215 }
216
217 return null;
218 }
219
220
221 /**
222 * Extracts the MAC value from request data
223 *
224 * @param array $request Associative array of request data
225 * @return string Returns the extracted MAC value, NULL if not present
226 */
227 public function extractRequestMac ($request)
228 {
229 if (!empty($request['mac'])) {
230 return $request['mac'];
231 }
232
233 return null;
234 }
235
236
237 /**
238 * Create a MAC hash for the given string
239 *
240 * @param string $string Input string
241 * @return string MAC value
242 */
243 protected function createMacHash ($string)
244 {
245 return strtoupper(hash('sha512', $string . $this->getSecretKey()));
246 }
247
248
249 /**
250 * Prepares the input string for MAC calculation depending on integration type
251 *
252 * @param str $json JSON message
253 * @param bool $v1 TRUE if REDIRECT, FALSE if API/EMBEDDED. Defaults to FALSE.
254 * @return str
255 */
256 protected function getMacInput ($data, $mac_type)
257 {
258 if (!is_array($data)) {
259 $data = json_decode(is_object($data) ? json_encode($data) : $data, TRUE);
260 }
261
262 if ($mac_type == self::SIGNATURE_TYPE_MAC) {
263 $mac_input = json_encode($data, JSON_UNESCAPED_UNICODE);
264 } else {
265 if ($mac_type == self::SIGNATURE_TYPE_2) {
266 $use_parts = array('amount', 'currency', 'reference', 'transaction', 'status');
267 } else {
268 $use_parts = array('paymentId', 'amount', 'status');
269 }
270
271 $mac_input = '';
272 foreach ($use_parts as $part) {
273 $mac_input .= (is_bool($data[$part]) ? ($data[$part] ? 'true' : 'false') : (string) $data[$part]);
274 }
275 }
276
277 return $mac_input;
278 }
279
280
281 /**
282 * Compose a signature
283 *
284 * @deprecated Used only for testing. Verify message authenticity via MAC instead.
285 * @param mixed $data Transaction data
286 * @param string $signature_type V1 or V2
287 * @return string Signature
288 */
289 public function composeSignature ($data, $signature_type)
290 {
291 $mac_input = $this->getMacInput($data, $signature_type);
292
293 return $this->createMacHash($mac_input);
294 }
295
296
297 /**
298 * Compose a signature for the Embedded Payments snippet
299 *
300 * @param mixed $amount Transaction amount
301 * @param string $currency Transaction currency
302 * @param string $reference An optional reference value
303 * @return string Signature
304 */
305 public function composeEmbeddedSignature ($amount, $currency, $reference = NULL)
306 {
307 $mac_input = (string)$amount . (string)$currency . (string)$reference;
308
309 return $this->createMacHash($mac_input);
310 }
311
312
313 public function composeMac ($data)
314 {
315 $mac_input = $this->getMacInput($data, self::SIGNATURE_TYPE_MAC);
316
317 return $this->createMacHash($mac_input);
318 }
319
320
321 public function explainSignature ($data, $signature_type)
322 {
323 $input = $this->getMacInput($data, $signature_type);
324
325 return 'UPPERCASE(HEX(SHA512('.$input.')))';
326 }
327
328
329 public function explainMac ($data)
330 {
331 $input = $this->getMacInput($data, self::SIGNATURE_TYPE_MAC);
332
333 return 'UPPERCASE(HEX(SHA512('.$input.')))';
334 }
335
336
337 /**
338 * Verify the MAC of the received request
339 *
340 * @param array $request Associative array of request data
341 * @return bool TRUE if MAC verification was successful, FALSE otherwise
342 */
343 public function verifyMac ($request)
344 {
345 try {
346 $received = $this->extractRequestMac($request);
347 $expected = $this->composeMac($this->extractRequestData($request));
348
349 return ($received == $expected);
350 } catch (Exception $e) {
351 return FALSE;
352 }
353 }
354
355
356 /**
357 * Verify the signature of the received request
358 *
359 * @deprecated Verify message authenticity via MAC instead.
360 * @param array $request Associative array of request data
361 * @return bool TRUE if signature verification was successful, FALSE otherwise
362 */
363 public function verifySignature ($request)
364 {
365 try {
366 $received = $this->extractRequestSignature($request);
367 $expected = $this->composeSignature($this->extractRequestData($request), $this->extractRequestSignatureType($request));
368
369 return ($received == $expected);
370 } catch (Exception $e) {
371 return FALSE;
372 }
373 }
374
375
376 /**
377 * Send a GET request to an API endpoint
378 *
379 * @param string $endpoint API endpoint
380 * @param array $params Request parameters
381 * @return obj Response object
382 */
383 public function makeGetRequest ($endpoint, $params = NULL)
384 {
385 return $this->makeApiRequest(Http::GET, $endpoint, $params);
386 }
387
388
389 /**
390 * Send a POST request to an API endpoint
391 *
392 * @param string $endpoint API endpoint
393 * @param string $body Request body
394 * @return obj Response object
395 */
396 public function makePostRequest ($endpoint, $body = NULL)
397 {
398 return $this->makeApiRequest(Http::POST, $endpoint, NULL, $body);
399 }
400
401
402 /**
403 * Send a GET request to an API endpoint
404 *
405 * @param string $endpoint API endpoint
406 * @param array $params Request parameters
407 * @param string $body Request body
408 * @return obj Response object
409 */
410 public function makePutRequest ($endpoint, $params = NULL, $body = NULL)
411 {
412 return $this->makeApiRequest(Http::PUT, $endpoint, $params, $body);
413 }
414
415
416 /**
417 * Send a request to an API endpoint
418 *
419 * @param string $method Request method (Http::GET, Http::POST or Http::PUT)
420 * @param string $endpoint API endpoint
421 * @param array $params Request parameters
422 * @param string $body Request body
423 * @return obj Response object
424 */
425 protected function makeApiRequest ($method, $endpoint, $params = NULL, $body = NULL)
426 {
427 $uri = $this->apiUrl . $endpoint;
428
429 if (isset($params) AND count($params)) {
430 $uri .= '?'.http_build_query($params);
431 }
432
433 $auth_user = $this->getShopId();
434 $auth_pass = $this->getSecretKey();
435
436 if ($method == Http::GET) {
437 $response = Request::get($uri)
438 ->authenticateWith($auth_user, $auth_pass)
439 ->send();
440 } else if ($method == Http::POST) {
441 $response = Request::post($uri)
442 ->authenticateWith($auth_user, $auth_pass)
443 ->sendsJson()
444 ->body(json_encode($body))
445 ->send();
446 } else if ($method == Http::PUT) {
447 $response = Request::put($uri)
448 ->authenticateWith($auth_user, $auth_pass)
449 ->sendsJson()
450 ->body(json_encode($body))
451 ->send();
452 }
453
454 $this->lastApiResponse = $response;
455
456 return $response;
457 }
458
459
460 /**
461 * Returns the Response object of the last API request
462 *
463 * @return obj
464 */
465 public function getLastApiResponse ()
466 {
467 return $this->lastApiResponse;
468 }
469
470
471 /**
472 * Get shop data
473 *
474 * @throws Exception if failed to get shop data
475 * @return obj Shop object
476 */
477 public function getShop ()
478 {
479 $response = $this->makeGetRequest("/v1/shop");
480
481 if (in_array($response->code, array(200))) {
482 return $response->body;
483 } else {
484 throw new Exception('Could not get shop data. Response ('.$response->code.'): '.$response->raw_body);
485 }
486 }
487
488 /**
489 * Get shop config for e-shop integration
490 *
491 * @param string $environment json-encoded key-value pairs describing the e-shop environment
492 * @throws Exception if failed to get shop configuration
493 * @return obj Shop configuration object
494 */
495 public function getShopConfig ($environment)
496 {
497 $response = $this->makeGetRequest("/v1/shop/configuration", $environment);
498
499 if (in_array($response->code, array(200))) {
500 return $response->body;
501 } else {
502 throw new Exception('Could not get shop configuration for the environment. Response ('.$response->code.'): '.$response->raw_body);
503 }
504 }
505
506
507
508 /**
509 * Update shop data
510 *
511 * @param mixed An object or array containing request body
512 * @throws Exception if failed to update shop data
513 * @return obj Shop object
514 */
515 public function updateShop ($request_body)
516 {
517 $response = $this->makePutRequest("/v1/shop", NULL, $request_body);
518
519 if (in_array($response->code, array(200))) {
520 return $response->body;
521 } else {
522 throw new Exception('Could not get shop data. Response ('.$response->code.'): '.$response->raw_body);
523 }
524 }
525
526
527 /**
528 * Create new transaction
529 *
530 * @param mixed An object or array containing request body
531 * @throws Exception if failed to create transaction
532 * @return obj Transaction object
533 */
534 public function createTransaction ($request_body)
535 {
536 $response = $this->makePostRequest('/v1/transactions', $request_body);
537
538 if (in_array($response->code, array(200, 201))) {
539 return $response->body;
540 } else {
541 throw new Exception('Could not create transaction. Response ('.$response->code.'): '.$response->raw_body);
542 }
543 }
544
545
546 /**
547 * Get transaction details
548 *
549 * @param string $transaction_id Transaction ID
550 * @throws Exception if failed to get transaction object
551 * @return obj Transaction object
552 */
553 public function getTransaction ($transaction_id)
554 {
555 $response = $this->makeGetRequest("/v1/transactions/{$transaction_id}");
556
557 if (in_array($response->code, array(200))) {
558 return $response->body;
559 } else {
560 throw new Exception('Could not get transaction. Response ('.$response->code.'): '.$response->raw_body);
561 }
562 }
563
564
565 /**
566 * Get transactions list
567 *
568 * @param array $params Associative array of query parameters
569 * @return obj Transactions list
570 */
571 public function getTransactions ($params = array())
572 {
573 $request_params = array();
574
575 if (!empty($params['since'])) {
576 $request_params['since'] = $params['since'];
577 }
578
579 if (!empty($params['until'])) {
580 $request_params['until'] = $params['until'];
581 }
582
583 if (!empty($params['completed_since'])) {
584 $request_params['completed_since'] = $params['completed_since'];
585 }
586
587 if (!empty($params['completed_until'])) {
588 $request_params['completed_until'] = $params['completed_until'];
589 }
590
591 if (!empty($params['refunded_since'])) {
592 $request_params['refunded_since'] = $params['refunded_since'];
593 }
594
595 if (!empty($params['refunded_until'])) {
596 $request_params['refunded_until'] = $params['refunded_until'];
597 }
598
599 if (!empty($params['status'])) {
600 $request_params['status'] = is_array($params['status']) ? join(',', $params['status']) : $params['status'];
601 }
602
603 if (!empty($params['page'])) {
604 $request_params['page'] = (int) $params['page'];
605 }
606
607 if (!empty($params['per_page'])) {
608 $request_params['per_page'] = (int) $params['per_page'];
609 }
610
611 return $this->makeGetRequest("/v1/transactions", $request_params)->body;
612 }
613
614
615 public function createToken ($request_body)
616 {
617 $response = $this->makePostRequest('/v1/tokens', $request_body);
618
619 if (!in_array($response->code, array(200, 201))) {
620 throw new Exception('Could not create payment token. Response ('.$response->code.'): '.$response->raw_body);
621 }
622
623 return $response->body;
624 }
625
626
627 /**
628 * Get token by email or cookie ID
629 *
630 * @param string $request_params Request parameters
631 * @throws Exception if failed to get token object
632 * @return obj Token object
633 */
634 public function getToken ($request_params)
635 {
636 $response = $this->makeGetRequest('/v1/tokens', $request_params);
637
638 if (in_array($response->code, array(200))) {
639 return $response->body;
640 } else {
641 throw new Exception('Could not get token. Response ('.$response->code.'): '.$response->raw_body);
642 }
643 }
644
645
646 public function createPayment ($transaction_id, $request_body)
647 {
648 $response = $this->makePostRequest("/v1/transactions/{$transaction_id}/payments", $request_body);
649
650 if (!in_array($response->code, array(200, 201))) {
651 throw new Exception('Could not create payment. Response ('.$response->code.'): '.$response->raw_body);
652 }
653
654 return $response->body;
655 }
656
657
658 public function createRefund ($transaction_id, $request_body)
659 {
660 $response = $this->makePostRequest("/v1/transactions/{$transaction_id}/refunds", $request_body);
661
662 if (!in_array($response->code, array(200, 201))) {
663 throw new Exception('Could not create refund. Response ('.$response->code.'): '.$response->raw_body);
664 }
665
666 return $response->body;
667 }
668
669
670 /**
671 * Get refund details
672 *
673 * @param string $refund_id Refund ID
674 * @throws Exception if failed to get refund object
675 * @return obj Refund object
676 */
677 public function getRefund ($refund_id)
678 {
679 $response = $this->makeGetRequest("/v1/refunds/{$refund_id}");
680
681 if (in_array($response->code, array(200))) {
682 return $response->body;
683 } else {
684 throw new Exception('Could not get refund. Response ('.$response->code.'): '.$response->raw_body);
685 }
686 }
687
688
689 /**
690 * Get a list of a transaction's refunds
691 *
692 * @param string $transaction_id Transaction ID
693 * @throws Exception if failed to get refunds list
694 * @return array Refund objects
695 */
696 public function getTransactionRefunds ($transaction_id)
697 {
698 $response = $this->makeGetRequest("/v1/refunds");
699
700 if (in_array($response->code, array(200))) {
701 return $response->body;
702 } else {
703 throw new Exception('Could not get transaction refunds list. Response ('.$response->code.'): '.$response->raw_body);
704 }
705 }
706
707
708 /**
709 * Get a list of refunds
710 *
711 * @throws Exception if failed to get refunds list
712 * @return array Refund objects
713 */
714 public function getRefunds ()
715 {
716 $response = $this->makeGetRequest("/v1/refunds");
717
718 if (in_array($response->code, array(200))) {
719 return $response->body;
720 } else {
721 throw new Exception('Could not get refunds list. Response ('.$response->code.'): '.$response->raw_body);
722 }
723 }
724
725
726 /**
727 * Get payment methods
728 *
729 * @param mixed An object or array containing request parameters
730 * @throws Exception if failed to get payment methods
731 * @return obj An object containing grouped lists of Payment Method objects
732 */
733 public function getPaymentMethods ($request_params)
734 {
735 $response = $this->makeGetRequest('/v1/methods', $request_params);
736
737 if (!in_array($response->code, array(200))) {
738 throw new Exception('Could not get payment methods. Response ('.$response->code.'): '.$response->raw_body);
739 }
740
741 return $response->body;
742 }
743
744
745
746
747
748 /**
749 * Get carrier-specifuc destinations for shipments (list of Automated Parcel Machines)
750 *
751 * @param mixed. An object or array containing request body
752 * @throws Exception if failed to retrieve the listing
753 * @return obj Shop configuration object
754 */
755 public function getDestinations ($request_body)
756 {
757 $response = $this->makePostRequest("/v1/shipments/destinations", $request_body);
758
759 if (in_array($response->code, array(200))) {
760 return $response->body;
761 } else {
762 throw new Exception('Could not retrieve destinations list. Response ('.$response->code.'): '.$response->raw_body);
763 }
764 }
765
766
767
768
769 /**
770 * Create new shipments at carrier systems
771 *
772 * @param mixed An object or array containing request body
773 * @throws Exception if failed to create transaction
774 * @return obj Transaction object
775 */
776 public function createShipments ($request_body)
777 {
778 $response = $this->makePostRequest('/v1/shipments', $request_body);
779
780 if (in_array($response->code, array(200, 201))) {
781 return $response->body;
782 } else {
783 throw new Exception('Could not create shipments. Response ('.$response->code.'): '.$response->raw_body);
784 }
785 }
786
787
788 /**
789 * generate parcel labels for shipments registered at carriers
790 *
791 * @param mixed An object or array containing request body
792 * @throws Exception if failed to create transaction
793 * @return obj Transaction object
794 */
795 public function createLabels ($request_body)
796 {
797 $response = $this->makePostRequest('/v1/shipments/createlabels', $request_body);
798
799 if (in_array($response->code, array(200, 201))) {
800 return $response->body;
801 } else {
802 throw new Exception('Could generate parcel labels. Response ('.$response->code.'): '.$response->raw_body);
803 }
804 }
805
806 }
807