1 | <?php |
||
2 | namespace W3design\Qapla; |
||
3 | |||
4 | use GuzzleHttp; |
||
5 | |||
6 | class Qapla |
||
7 | { |
||
8 | /** |
||
9 | * Private API key to connect a Qapla channel |
||
10 | * @var string |
||
11 | */ |
||
12 | protected $privateApiKey; |
||
13 | |||
14 | /** |
||
15 | * Public API key to connect a Qapla channel |
||
16 | * @var string |
||
17 | */ |
||
18 | protected $publicApiKey; |
||
19 | |||
20 | /** |
||
21 | * Qapla constructor. |
||
22 | * |
||
23 | * @param $privateApiKey |
||
24 | * @param $publicApiKey |
||
25 | */ |
||
26 | public function __construct($privateApiKey, $publicApiKey) |
||
27 | { |
||
28 | $this->privateApiKey = $privateApiKey; |
||
29 | $this->publicApiKey = $publicApiKey; |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * Return the status of a shipment using the tracking number. |
||
34 | * @param string $tracking_or_reference ['trackingNumber' or 'reference'] |
||
35 | * @param $value |
||
36 | * @param string $lang |
||
37 | * |
||
38 | * @return mixed |
||
39 | */ |
||
40 | public function getTrack($tracking_or_reference = 'trackingNumber', $value, $lang = 'ita') |
||
41 | { |
||
42 | $client = new GuzzleHttp\Client(); |
||
43 | $res = $client->get(config('qapla.url').'getTrack/', ['query' => [ |
||
44 | 'apiKey' => $this->privateApiKey, |
||
45 | $tracking_or_reference => $value, |
||
46 | 'lang' => $lang] |
||
47 | ]); |
||
48 | $result = GuzzleHttp\json_decode($res->getBody()); |
||
49 | if($result->getTrack->result == 'KO') return $result->getTrack->error; |
||
50 | return $result->getTrack; |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Allows one or more shipments to be loaded via a POST request in JSON format. |
||
55 | * @param $data |
||
56 | * |
||
57 | * @return mixed |
||
58 | */ |
||
59 | public function pushTrack($data) |
||
60 | { |
||
61 | $json ='{ |
||
62 | "apiKey": "'.$this->privateApiKey.'", |
||
63 | "pushTrack": '.json_encode($data).' |
||
64 | }'; |
||
65 | |||
66 | $client = new GuzzleHttp\Client(); |
||
67 | $res = $client->post(config('qapla.url').'pushTrack/', [ |
||
68 | 'headers' => ['content-type' => 'application/json'], |
||
69 | 'body' => $json |
||
70 | ]); |
||
71 | $result = GuzzleHttp\json_decode($res->getBody()); |
||
72 | return $result->pushTrack->track; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Return the list of shipments imported from Qapla, with a maximum limit of 100 shipments per call. |
||
77 | * @param null $date_or_days |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
78 | * |
||
79 | * @return mixed |
||
80 | */ |
||
81 | public function getTracks($date_or_days = null) |
||
82 | { |
||
83 | $param_name = 'dateFrom'; |
||
84 | $date_or_days ?: $date_or_days = config('qapla.orders.default_fromDate'); |
||
85 | if(strtotime($date_or_days) === false) $param_name = 'days'; |
||
86 | |||
87 | $client = new GuzzleHttp\Client(); |
||
88 | $res = $client->get(config('qapla.url').'getTracks/', ['query' => [ |
||
89 | 'apiKey' => $this->privateApiKey, |
||
90 | $param_name => $date_or_days] |
||
91 | ]); |
||
92 | $result = GuzzleHttp\json_decode($res->getBody()); |
||
93 | if($result->getTracks->result == 'KO') return $result->getTracks->error; |
||
94 | return $result->getTracks->tracks; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Allows you to delete a shipment. |
||
99 | * @param $trackingNumber |
||
100 | * |
||
101 | * @return bool |
||
102 | */ |
||
103 | public function deleteTrack($trackingNumber) |
||
104 | { |
||
105 | $client = new GuzzleHttp\Client(); |
||
106 | $res = $client->get(config('qapla.url').'deleteTrack/', ['query' => [ |
||
107 | 'apiKey' => $this->privateApiKey, |
||
108 | 'trackingNumber' => $trackingNumber] |
||
109 | ]); |
||
110 | $result = GuzzleHttp\json_decode($res->getBody()); |
||
111 | if($result->deleteTrack->result == 'KO') return $result->deleteTrack->error; |
||
112 | return true; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Return the list of orders imported from Qapla, with a maximum limit of 100 orders per call. |
||
117 | * @param null $date_or_days |
||
0 ignored issues
–
show
|
|||
118 | * |
||
119 | * @return mixed |
||
120 | */ |
||
121 | public function getOrders($date_or_days = null) |
||
122 | { |
||
123 | $param_name = 'dateFrom'; |
||
124 | $date_or_days ?: $date_or_days = config('qapla.orders.default_fromDate'); |
||
125 | if(strtotime($date_or_days) === false) $param_name = 'days'; |
||
126 | |||
127 | $client = new GuzzleHttp\Client(); |
||
128 | $res = $client->get(config('qapla.url').'getOrders/', ['query' => [ |
||
129 | 'apiKey' => $this->privateApiKey, |
||
130 | $param_name => $date_or_days] |
||
131 | ]); |
||
132 | $result = GuzzleHttp\json_decode($res->getBody()); |
||
133 | if($result->getOrders->result == 'KO') return $result->getOrders->error; |
||
134 | return $result->getOrders->orders; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Allows you to load one or more orders via a POST request in JSON format. |
||
139 | * @param $data |
||
140 | * |
||
141 | * @return bool |
||
142 | */ |
||
143 | public function pushOrder($data) |
||
144 | { |
||
145 | $json ='{ |
||
146 | "apiKey": "'.$this->privateApiKey.'", |
||
147 | "pushOrder": '.json_encode($data).' |
||
148 | }'; |
||
149 | |||
150 | $client = new GuzzleHttp\Client(); |
||
151 | $res = $client->post(config('qapla.url').'pushOrder/', [ |
||
152 | 'headers' => ['content-type' => 'application/json'], |
||
153 | 'body' => $json |
||
154 | ]); |
||
155 | $result = GuzzleHttp\json_decode($res->getBody()); |
||
156 | if($result->pushOrder->result == 'KO') return $result->pushOrder->error; |
||
157 | return true; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Return the amount of the remaining credits on your premium account |
||
162 | * @return mixed |
||
163 | */ |
||
164 | public function getCredits() |
||
165 | { |
||
166 | $client = new GuzzleHttp\Client(); |
||
167 | $res = $client->get(config('qapla.url').'getCredits/', ['query' => [ |
||
168 | 'apiKey' => $this->privateApiKey] |
||
169 | ]); |
||
170 | $result = GuzzleHttp\json_decode($res->getBody()); |
||
171 | if($result->getCredits->result == 'KO') return $result->getCredits->error; |
||
172 | return $result->getCredits->credits; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Return the list of couriers either total, or for single country/region. |
||
177 | * @param null $country |
||
0 ignored issues
–
show
|
|||
178 | * |
||
179 | * @return mixed |
||
180 | */ |
||
181 | public function getCouriers($country = null) |
||
182 | { |
||
183 | $client = new GuzzleHttp\Client(); |
||
184 | $res = $client->get(config('qapla.url').'getCouriers/', ['query' => [ |
||
185 | 'apiKey' => $this->privateApiKey, |
||
186 | 'country' => $country ?: config('qapla.couriers.default_country')] |
||
187 | ]); |
||
188 | $result = GuzzleHttp\json_decode($res->getBody()); |
||
189 | if($result->getCouriers->result == 'KO') return $result->getCouriers->error; |
||
190 | return $result->getCouriers->courier; |
||
191 | } |
||
192 | } |