-
Notifications
You must be signed in to change notification settings - Fork 10
/
OrdersController.php
381 lines (328 loc) · 12.9 KB
/
OrdersController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?php
namespace fostercommerce\shipstationconnect\controllers;
use Craft;
use craft\web\Controller;
use craft\elements\MatrixBlock;
use craft\helpers\ElementHelper;
use craft\commerce\Plugin as CommercePlugin;
use craft\commerce\elements\Order;
use craft\db\Query;
use craft\db\Table;
use craft\helpers\App;
use craft\web\Response;
use craft\models\MatrixBlockType;
use yii\web\HttpException;
use yii\base\ErrorException;
use yii\base\Event;
use yii\web\Response as YiiResponse;
use fostercommerce\shipstationconnect\Plugin;
use fostercommerce\shipstationconnect\events\FindOrderEvent;
class OrdersController extends Controller
{
const FIND_ORDER_EVENT = 'findOrderEvent';
// Disable CSRF validation for the entire controller
public $enableCsrfValidation = false;
protected array|int|bool $allowAnonymous = true;
/**
* @inheritDoc
*/
public function init(): void
{
// Allow anonymous access only when this plugin is handling basic
// authentication, otherwise require auth so that Craft doesn't let
// unauthenticated requests go through.
$isUsingCraftAuth = Plugin::getInstance()->isAuthHandledByCraft();
$this->allowAnonymous = !$isUsingCraftAuth;
if ($isUsingCraftAuth) {
$this->requirePermission('shipstationconnect-processOrders');
}
parent::init();
}
/**
* ShipStation will hit this action for processing orders, both POSTing and GETting.
* ShipStation will send a GET param 'action' of either shipnotify or export.
* If this is not found or is any other string, this will throw a 400 exception.
*
* @param array $variables, containing key 'fulfillmentService'
* @throws HttpException for malformed requests
*/
public function actionProcess($store = null, $action = null): null|string|Response|YiiResponse|SimpleXMLElement
{
$request = Craft::$app->request;
try {
if (!$this->authenticate()) {
throw new HttpException(401, 'Invalid ShipStation username or password.');
}
switch ($action) {
case 'export':
return $this->getOrders($store);
case 'shipnotify':
return $this->postShipment();
default:
throw new HttpException(400, 'No action set. Set the ?action= parameter as `export` or `shipnotify`.');
}
} catch (ErrorException $e) {
$this->logException('Error processing action {action}', ['action' => $action], $e);
return $this->asErrorJson($e->getMessage())->setStatusCode(500);
} catch (HttpException $e) {
if ($action) {
$this->logException('Error processing action {action}', ['action' => $action], $e);
} else {
$this->logException('An action is required. Supported actions: export, shipnotify.');
}
return $this->asErrorJson($e->getMessage())->setStatusCode($e->statusCode);
} catch (\Exception $e) {
$this->logException('Error processing action {action}', ['action' => $action], $e);
return $this->asErrorJson($e->getMessage())->setStatusCode(500);
}
}
private function logException($msg, $params = [], $e = null): void
{
Craft::error(
Craft::t('shipstationconnect', $msg, $params),
__METHOD__
);
if ($e) {
Craft::$app->getErrorHandler()->logException($e);
}
}
/**
* Authenticate the user using HTTP Basic auth. This is *not* using Craft's sessions/authentication.
*
* @return bool, true if successfully authenticated or false otherwise
*/
protected function authenticate(): bool
{
$plugin = Plugin::getInstance();
if ($plugin->isAuthHandledByCraft()) {
return true;
}
$settings = $plugin->settings;
$expectedUsername = App::parseEnv($settings->shipstationUsername);
$expectedPassword = App::parseEnv($settings->shipstationPassword);
list($username, $password) = Craft::$app->getRequest()->getAuthCredentials();
return $expectedUsername === $username && $expectedPassword === $password;
}
/**
* Returns a big XML object of all orders in a format described by ShipStation
*
* @return SimpleXMLElement Orders XML
*/
protected function getOrders($store = null): SimpleXMLElement
{
$query = Order::find();
$start_date = $this->parseDate('start_date');
$end_date = $this->parseDate('end_date');
if ($start_date && $end_date) {
$query->dateUpdated(array('and', '> '.$start_date, '< '.$end_date));
}
$query->isCompleted(true);
$storeFieldHandle = Plugin::getInstance()->settings->storesFieldHandle;
if ($store !== null || $storeFieldHandle !== '') {
$field = Craft::$app->getFields()->getFieldByHandle($storeFieldHandle);
$fieldColumnName = ElementHelper::fieldColumnFromField($field);
$query->andWhere([$fieldColumnName => $store]);
}
$query->orderBy('dateUpdated asc');
$num_pages = $this->paginateOrders($query);
$parent_xml = new \SimpleXMLElement('<Orders />');
$parent_xml->addAttribute('pages', $num_pages);
Plugin::getInstance()->xml->orders($parent_xml, $query->all());
$this->returnXML($parent_xml);
}
/**
* For a Criteria instance of Orders, return the number of total pages and apply a corresponding offset and limit
*
* @param ElementCriteriaModel, a REFERENCE to the criteria instance
* @return Int total number of pages
*/
protected function paginateOrders(&$query): int
{
$pageSize = Plugin::getInstance()->settings->ordersPageSize;
if (!is_numeric($pageSize) || $pageSize < 1) {
$pageSize = 25;
}
$numPages = ceil($query->count() / $pageSize);
$pageNum = Craft::$app->getRequest()->getParam('page');
if (!is_numeric($pageNum) || $pageNum < 1) {
$pageNum = 1;
}
$query->limit($pageSize);
$query->offset(($pageNum - 1) * $pageSize);
return $numPages;
}
/**
* For a given date field, parse and return its date as a string
*
* @param String $field_name, the name of the field in GET params
* @return String|null the formatted date string
*/
protected function parseDate($field_name): ?string
{
$request = Craft::$app->getRequest();
if ($date_raw = $request->getParam($field_name)) {
$date = strtotime($date_raw);
if ($date !== false) {
if ($field_name === 'start_date') {
return date('Y-m-d H:i:s', $date);
} else {
return date('Y-m-d H:i:59', $date);
}
}
}
return null;
}
private function getBlockTypeByHandle($fieldId, $handle): ?MatrixBlockType
{
$result = (new Query())
->select([
'id',
'fieldId',
'fieldLayoutId',
'name',
'handle',
'sortOrder',
'uid'
])
->from([Table::MATRIXBLOCKTYPES])
->where(['fieldId' => $fieldId])
->andWhere(['handle' => $handle])
->orderBy(['sortOrder' => SORT_ASC])
->one();
if ($result) {
return new MatrixBlockType($result);
}
return null;
}
/**
* Updates order status for a given order. This is called by ShipStation.
* The order is found using the query param `order_number`.
*
* TODO: This assumes there is a "shipped" handle for an order status
*
* See craft/plugins/commerce/controllers/Commerce_OrdersController.php#actionUpdateStatus() for details
*
* @throws ErrorException if the order fails to save
*/
protected function postShipment(): null|string|Response
{
$order = $this->orderFromParams();
$settings = Plugin::getInstance()->settings;
$status = CommercePlugin::getInstance()
->orderStatuses
->getOrderStatusByHandle($settings->shippedStatusHandle);
if (!$status) {
throw new ErrorException("Failed to find shipped order status");
}
$order->orderStatusId = $status->id;
$order->message = 'Marking order as shipped. Adding shipping information.';
$shippingInformation = $this->getShippingInformationFromParams();
$matrix = Craft::$app->fields->getFieldByHandle($settings->matrixFieldHandle);
// If the field exists
if ($matrix) {
$blockType = $this->getBlockTypeByHandle($matrix->id, $settings->blockTypeHandle);
if ($blockType && $order && $this->validateShippingInformation($shippingInformation)) {
$block = new MatrixBlock([
'ownerId' => $order->id,
'fieldId' => $matrix->id,
'typeId' => $blockType->id,
]);
$block->setFieldValue($settings->carrierFieldHandle, $shippingInformation['carrier']);
$block->setFieldValue($settings->serviceFieldHandle, $shippingInformation['service']);
$block->setFieldValue($settings->trackingNumberFieldHandle, $shippingInformation['trackingNumber']);
if (!Craft::$app->elements->saveElement($block)) {
Craft::warning(
Craft::t(
'shipstationconnect',
'Unable to save shipping information.'
),
__METHOD__
);
}
}
} else {
Craft::warning(
Craft::t(
'shipstationconnect',
'Missing shippingInfo Matrix field. Ignoring.'
),
__METHOD__
);
}
if (Craft::$app->elements->saveElement($order, false)) {
return $this->asJson(['success' => true]);
} else {
throw new ErrorException('Failed to save order with id ' . $order->id);
}
}
private function validateShippingInformation($info): bool
{
// Requires at least one value
foreach ($info as $key => $value) {
if ($value && !empty(trim($value))) {
return true;
}
}
return false;
}
/**
* Parse parameters POSTed from ShipStation for fields available to us on the Order's shippingInfo matrix field
*
* Note: only fields that exist in the matrix block will be set.
* ShipStation posts, in XML, many more fields than these, but for now we disregard.
* https://help.shipstation.com/hc/en-us/articles/205928478-ShipStation-Custom-Store-Development-Guide#2ai
*
* @return array
*/
protected function getShippingInformationFromParams(): array
{
$request = Craft::$app->getRequest();
return [
'carrier' => $request->getParam('carrier'),
'service' => $request->getParam('service'),
'trackingNumber' => $request->getParam('tracking_number'),
];
}
/**
* Find the order model given the order_number passed to us from ShipStation.
*
* Note: the order_number value from ShipStation corresponds to $order->number that we
* return to ShipStation as part of the getOrders() method above.
*
* @throws HttpException, 404 if not found, 406 if order number is invalid
* @return Order
*/
protected function orderFromParams(): Order
{
$request = Craft::$app->getRequest();
if ($orderNumber = $request->getParam('order_number')) {
$findOrderEvent = new FindOrderEvent(['orderNumber' => $orderNumber]);
Event::trigger(static::class, self::FIND_ORDER_EVENT, $findOrderEvent);
$order = $findOrderEvent->order;
if (!$order) {
if ($order = Order::find()->reference($orderNumber)->one()) {
return $order;
}
throw new HttpException(404, "Order with number '{$orderNumber}' not found");
}
return $order;
}
throw new HttpException(406, 'Order number must be set');
}
/**
* Responds to the request with XML.
*
* See craft/app/controllers/BaseController.php#returnJson() for comparisons
*
* @param SimpleXMLElement $xml
* @return null
*/
protected function returnXML(\SimpleXMLElement $xml): void
{
header("Content-type: text/xml");
// Output it into a buffer, in case TasksService wants to close the connection prematurely
ob_start();
echo $xml->asXML();
exit(0);
}
}