1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pagantis\OrdersApiClient; |
4
|
|
|
|
5
|
|
|
use Httpful\Exception\ConnectionErrorException; |
6
|
|
|
use Pagantis\OrdersApiClient\Method\ConfirmOrderMethod; |
7
|
|
|
use Pagantis\OrdersApiClient\Method\CreateOrderMethod; |
8
|
|
|
use Pagantis\OrdersApiClient\Method\GetOrderMethod; |
9
|
|
|
use Pagantis\OrdersApiClient\Method\ListOrdersMethod; |
10
|
|
|
use Pagantis\OrdersApiClient\Method\RefundOrderMethod; |
11
|
|
|
use Pagantis\OrdersApiClient\Model\ApiConfiguration; |
12
|
|
|
use Pagantis\OrdersApiClient\Model\Order; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Client |
16
|
|
|
* |
17
|
|
|
* @package Pagantis/OrdersApiClient |
18
|
|
|
*/ |
19
|
|
|
class Client |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var ApiConfiguration |
23
|
|
|
*/ |
24
|
|
|
protected $apiConfiguration; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Client constructor. |
28
|
|
|
* |
29
|
|
|
* @param $publicKey |
30
|
|
|
* @param $privateKey |
31
|
|
|
* @param null $baseUri |
|
|
|
|
32
|
|
|
* @param array $headers |
33
|
|
|
* |
34
|
|
|
* @throws ConnectionErrorException |
35
|
|
|
* @throws Exception\ClientException |
36
|
|
|
*/ |
37
|
|
|
public function __construct($publicKey, $privateKey, $baseUri = null, $headers = array()) |
38
|
|
|
{ |
39
|
|
|
if (!function_exists("curl_init")) { |
40
|
|
|
throw new ConnectionErrorException("Curl module is not available on this system"); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$apiConfiguration = new ApiConfiguration(); |
44
|
|
|
$apiConfiguration |
45
|
|
|
->setBaseUri($baseUri ? $baseUri : ApiConfiguration::BASE_URI) |
|
|
|
|
46
|
|
|
->setPrivateKey($privateKey) |
47
|
|
|
->setPublicKey($publicKey) |
48
|
|
|
->setHeaders($headers) |
49
|
|
|
; |
50
|
|
|
$this->apiConfiguration = $apiConfiguration; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param Order $order |
55
|
|
|
* @param bool $asJson |
56
|
|
|
* |
57
|
|
|
* @return bool|Order|string |
58
|
|
|
* |
59
|
|
|
* @throws \Exception |
60
|
|
|
*/ |
61
|
|
|
public function createOrder(Order $order, $asJson = false) |
62
|
|
|
{ |
63
|
|
|
$createOrderMethod = new CreateOrderMethod($this->apiConfiguration); |
64
|
|
|
$createOrderMethod->setOrder($order); |
65
|
|
|
if ($asJson) { |
66
|
|
|
return $createOrderMethod->call()->getResponseAsJson(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $createOrderMethod->call()->getOrder(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param $orderId |
74
|
|
|
* @param bool $asJson |
75
|
|
|
* |
76
|
|
|
* @return bool|Order|string |
77
|
|
|
* |
78
|
|
|
* @throws \Exception |
79
|
|
|
*/ |
80
|
|
|
public function getOrder($orderId, $asJson = false) |
81
|
|
|
{ |
82
|
|
|
$getOrderMethod = new GetOrderMethod($this->apiConfiguration); |
83
|
|
|
$getOrderMethod->setOrderId($orderId); |
84
|
|
|
if ($asJson) { |
85
|
|
|
return $getOrderMethod->call()->getResponseAsJson(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $getOrderMethod->call()->getOrder(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* listOrders |
93
|
|
|
* |
94
|
|
|
* @param array|null $queryString |
95
|
|
|
* @param bool $asJson |
96
|
|
|
* |
97
|
|
|
* @return array|bool|string |
98
|
|
|
* |
99
|
|
|
* @throws \Exception |
100
|
|
|
*/ |
101
|
|
|
public function listOrders(array $queryString = null, $asJson = false) |
102
|
|
|
{ |
103
|
|
|
$listOrdersMethod = new ListOrdersMethod($this->apiConfiguration); |
104
|
|
|
$listOrdersMethod->setQueryParameters($queryString); |
|
|
|
|
105
|
|
|
if ($asJson) { |
106
|
|
|
return $listOrdersMethod->call()->getResponseAsJson(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $listOrdersMethod->call()->getOrders(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param $orderId |
114
|
|
|
* @param bool $asJson |
115
|
|
|
* |
116
|
|
|
* @return bool|false|Order|string |
117
|
|
|
* |
118
|
|
|
* @throws \Exception |
119
|
|
|
*/ |
120
|
|
|
public function confirmOrder($orderId, $asJson = false) |
121
|
|
|
{ |
122
|
|
|
$confirmOrderMethod = new ConfirmOrderMethod($this->apiConfiguration); |
123
|
|
|
$confirmOrderMethod->setOrderId($orderId); |
124
|
|
|
if ($asJson) { |
125
|
|
|
return $confirmOrderMethod->call()->getResponseAsJson(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $confirmOrderMethod->call()->getOrder(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param $orderId |
133
|
|
|
* @param Order\Refund $refund |
134
|
|
|
* @param bool $asJson |
135
|
|
|
* |
136
|
|
|
* @return bool|false|Order\Refund|string |
137
|
|
|
* |
138
|
|
|
* @throws \Exception |
139
|
|
|
*/ |
140
|
|
|
public function refundOrder($orderId, Order\Refund $refund, $asJson = false) |
141
|
|
|
{ |
142
|
|
|
$refundOrderMethod = new RefundOrderMethod($this->apiConfiguration); |
143
|
|
|
$refundOrderMethod->setOrderId($orderId); |
144
|
|
|
$refundOrderMethod->setRefund($refund); |
145
|
|
|
if ($asJson) { |
146
|
|
|
return $refundOrderMethod->call()->getResponseAsJson(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $refundOrderMethod->call()->getRefund(); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|