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|false|Order|string |
58
|
|
|
* @throws ConnectionErrorException |
59
|
|
|
* @throws Exception\HttpException |
60
|
|
|
* @throws Exception\ClientException |
61
|
|
|
*/ |
62
|
|
|
public function createOrder(Order $order, $asJson = false) |
63
|
|
|
{ |
64
|
|
|
$createOrderMethod = new CreateOrderMethod($this->apiConfiguration); |
65
|
|
|
$createOrderMethod->setOrder($order); |
66
|
|
|
if ($asJson) { |
67
|
|
|
return $createOrderMethod->call()->getResponseAsJson(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $createOrderMethod->call()->getOrder(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param $orderId |
75
|
|
|
* @param bool $asJson |
76
|
|
|
* |
77
|
|
|
* @return bool|false|Order|string |
78
|
|
|
* @throws ConnectionErrorException |
79
|
|
|
* @throws Exception\HttpException |
80
|
|
|
* @throws Exception\ClientException |
81
|
|
|
*/ |
82
|
|
|
public function getOrder($orderId, $asJson = false) |
83
|
|
|
{ |
84
|
|
|
$getOrderMethod = new GetOrderMethod($this->apiConfiguration); |
85
|
|
|
$getOrderMethod->setOrderId($orderId); |
86
|
|
|
if ($asJson) { |
87
|
|
|
return $getOrderMethod->call()->getResponseAsJson(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $getOrderMethod->call()->getOrder(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* listOrders |
95
|
|
|
* |
96
|
|
|
* @param array|null $queryString |
97
|
|
|
* @param bool $asJson |
98
|
|
|
* |
99
|
|
|
* @return array|bool|string |
100
|
|
|
* @throws ConnectionErrorException |
101
|
|
|
* @throws Exception\HttpException |
102
|
|
|
*/ |
103
|
|
|
public function listOrders(array $queryString = null, $asJson = false) |
104
|
|
|
{ |
105
|
|
|
$listOrdersMethod = new ListOrdersMethod($this->apiConfiguration); |
106
|
|
|
$listOrdersMethod->setQueryParameters($queryString); |
|
|
|
|
107
|
|
|
if ($asJson) { |
108
|
|
|
return $listOrdersMethod->call()->getResponseAsJson(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $listOrdersMethod->call()->getOrders(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param $orderId |
116
|
|
|
* @param bool $asJson |
117
|
|
|
* |
118
|
|
|
* @return bool|false|Order|string |
119
|
|
|
* @throws ConnectionErrorException |
120
|
|
|
* @throws Exception\HttpException |
121
|
|
|
* @throws Exception\ClientException |
122
|
|
|
*/ |
123
|
|
|
public function confirmOrder($orderId, $asJson = false) |
124
|
|
|
{ |
125
|
|
|
$confirmOrderMethod = new ConfirmOrderMethod($this->apiConfiguration); |
126
|
|
|
$confirmOrderMethod->setOrderId($orderId); |
127
|
|
|
if ($asJson) { |
128
|
|
|
return $confirmOrderMethod->call()->getResponseAsJson(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $confirmOrderMethod->call()->getOrder(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param $orderId |
136
|
|
|
* @param Order\Refund $refund |
137
|
|
|
* @param bool $asJson |
138
|
|
|
* |
139
|
|
|
* @return bool|false|Order\Refund|string |
140
|
|
|
* @throws ConnectionErrorException |
141
|
|
|
* @throws Exception\HttpException |
142
|
|
|
* @throws Exception\ClientException |
143
|
|
|
*/ |
144
|
|
|
public function refundOrder($orderId, Order\Refund $refund, $asJson = false) |
145
|
|
|
{ |
146
|
|
|
$refundOrderMethod = new RefundOrderMethod($this->apiConfiguration); |
147
|
|
|
$refundOrderMethod->setOrderId($orderId); |
148
|
|
|
$refundOrderMethod->setRefund($refund); |
149
|
|
|
if ($asJson) { |
150
|
|
|
return $refundOrderMethod->call()->getResponseAsJson(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $refundOrderMethod->call()->getRefund(); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|