1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package Shippo |
5
|
|
|
* @author Iurii Makukh <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2015, Iurii Makukh |
7
|
|
|
* @license https://www.gnu.org/licenses/gpl.html GNU/GPLv3 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace gplcart\modules\shippo\models; |
11
|
|
|
|
12
|
|
|
use gplcart\core\Library; |
13
|
|
|
use gplcart\core\Module; |
14
|
|
|
use LogicException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Manages basic behaviors and data related to Shippo API |
18
|
|
|
*/ |
19
|
|
|
class Api |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Module class instance |
24
|
|
|
* @var \gplcart\core\Module $module |
25
|
|
|
*/ |
26
|
|
|
protected $module; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Library class instance |
30
|
|
|
* @var \gplcart\core\Library $library |
31
|
|
|
*/ |
32
|
|
|
protected $library; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param Library $library |
36
|
|
|
* @param Module $module |
37
|
|
|
*/ |
38
|
|
|
public function __construct(Library $library, Module $module) |
39
|
|
|
{ |
40
|
|
|
$this->module = $module; |
41
|
|
|
$this->library = $library; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Returns either live or test token |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
protected function getToken() |
49
|
|
|
{ |
50
|
|
|
$settings = $this->module->getSettings('shippo'); |
51
|
|
|
|
52
|
|
|
if (empty($settings['test'])) { |
53
|
|
|
return $settings['key']['live']; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $settings['key']['test']; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Performs request to Shippo's API to get shipping rates |
61
|
|
|
* @param array $from |
62
|
|
|
* @param array $to |
63
|
|
|
* @param array $parcel |
64
|
|
|
* @return array |
65
|
|
|
* @throws LogicException |
66
|
|
|
*/ |
67
|
|
|
public function getRates(array $from, array $to, array $parcel) |
68
|
|
|
{ |
69
|
|
|
$this->library->load('shippo'); |
70
|
|
|
|
71
|
|
|
if (!class_exists('Shippo') || !class_exists('Shippo_Shipment')) { |
72
|
|
|
throw new LogicException('Failed to load Shippo library'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
\Shippo::setApiKey($this->getToken()); |
76
|
|
|
|
77
|
|
|
$shipment = array( |
78
|
|
|
'async' => false, |
79
|
|
|
'address_to' => $to, |
80
|
|
|
'address_from' => $from, |
81
|
|
|
'parcels' => array($parcel) |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
$result = \Shippo_Shipment::create($shipment); |
85
|
|
|
|
86
|
|
|
if (empty($result['rates'])) { |
87
|
|
|
return array(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$rates = array(); |
91
|
|
|
|
92
|
|
|
foreach ($result['rates'] as $rate) { |
93
|
|
|
$rates[$rate['servicelevel']['token']] = json_decode($rate, true); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $rates; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Creates a Shippo address object |
101
|
|
|
* @param array $address |
102
|
|
|
* @param array $options |
103
|
|
|
* @return array |
104
|
|
|
* @throws LogicException |
105
|
|
|
*/ |
106
|
|
|
public function createAddress(array $address, array $options = array()) |
107
|
|
|
{ |
108
|
|
|
$this->library->load('shippo'); |
109
|
|
|
|
110
|
|
|
if (!class_exists('Shippo') || !class_exists('Shippo_Address')) { |
111
|
|
|
throw new LogicException('Failed to load Shippo library'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
\Shippo::setApiKey($this->getToken()); |
115
|
|
|
|
116
|
|
|
$response = \Shippo_Address::create(array_merge($address, $options)); |
117
|
|
|
|
118
|
|
|
if (!is_array($response)) { |
119
|
|
|
$response = json_decode($response, true); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $response; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Validates an address |
127
|
|
|
* @param array $address |
128
|
|
|
* @return boolean|array |
129
|
|
|
*/ |
130
|
|
|
public function isValidAddress(array $address) |
131
|
|
|
{ |
132
|
|
|
$result = $this->createAddress($address, array('validate' => true)); |
133
|
|
|
|
134
|
|
|
if (empty($result['object_id'])) { |
135
|
|
|
return false; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if (!isset($result['validation_results']['is_valid']) || !empty($result['validation_results']['is_valid'])) { |
139
|
|
|
return true; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if (empty($result['validation_results']['messages'])) { |
143
|
|
|
return false; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$messages = array(); |
147
|
|
|
|
148
|
|
|
foreach ($result['validation_results']['messages'] as $message) { |
149
|
|
|
$messages[] = $message['text']; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $messages; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Request shipping label and tracking number |
157
|
|
|
* @param string $object_id |
158
|
|
|
* @param array $options |
159
|
|
|
* @return array |
160
|
|
|
* @throws LogicException |
161
|
|
|
*/ |
162
|
|
|
public function getLabel($object_id, array $options = array()) |
163
|
|
|
{ |
164
|
|
|
$this->library->load('shippo'); |
165
|
|
|
|
166
|
|
|
if (!class_exists('Shippo') || !class_exists('Shippo_Transaction')) { |
167
|
|
|
throw new LogicException('Failed to load Shippo library'); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
\Shippo::setApiKey($this->getToken()); |
171
|
|
|
|
172
|
|
|
$default = array('rate' => $object_id, 'async' => false); |
173
|
|
|
$response = \Shippo_Transaction::create(array_merge($default, $options)); |
174
|
|
|
|
175
|
|
|
if (!is_array($response)) { |
176
|
|
|
$response = json_decode($response, true); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
if (isset($response['status']) && $response['status'] === 'SUCCESS') { |
180
|
|
|
return $response; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return array(); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
} |
187
|
|
|
|