/
Shipments.php
155 lines (117 loc) · 4.79 KB
/
Shipments.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
<?php
namespace verbb\snipcart\services;
use verbb\snipcart\Snipcart;
use verbb\snipcart\errors\ShippingRateException;
use verbb\snipcart\events\ShippingRateEvent;
use verbb\snipcart\models\snipcart\Order;
use verbb\snipcart\providers\shipstation\ShipStation;
use verbb\snipcart\records\ShippingQuoteLog;
use craft\base\Component;
use yii\db\ActiveRecord;
class Shipments extends Component
{
// Constants
// =========================================================================
public const EVENT_BEFORE_RETURN_SHIPPING_RATES = 'beforeReturnShippingRates';
// Properties
// =========================================================================
private ?ShipStation $_shipStation = null;
// Public Methods
// =========================================================================
public function getShipStation(): ?ShipStation
{
if (!($this->_shipStation instanceof ShipStation)) {
$settings = Snipcart::$plugin->getSettings();
return $this->_shipStation = $settings->getProvider('shipStation');
}
return $this->_shipStation;
}
public function collectRatesForOrder(Order $order): array
{
if ($order->hasShippableItems() === false) {
Snipcart::error('Snipcart order {num} did not contain any shippable items.', [
'num' => $order->invoiceNumber ?? $order->token,
]);
return [];
}
$shipStation = $this->getShipStation();
if (!$shipStation) {
return [];
}
try {
$rates = [];
$package = Snipcart::$plugin->getOrders()->getOrderPackaging($order);
$includeShipStationRates = $shipStation->isConfigured() && $shipStation->getSettings()->enableShippingRates;
if ($includeShipStationRates && $shipStationRates = $shipStation->getRatesForOrder($order, $package)) {
$rates = array_merge($rates, $shipStationRates);
}
if ($this->hasEventHandlers(self::EVENT_BEFORE_RETURN_SHIPPING_RATES)) {
$shippingRateEvent = new ShippingRateEvent([
'rates' => $rates,
'order' => $order,
'package' => $package,
]);
$this->trigger(self::EVENT_BEFORE_RETURN_SHIPPING_RATES, $shippingRateEvent);
if (!$shippingRateEvent->isValid) {
throw new ShippingRateException($shippingRateEvent);
}
$rates = $shippingRateEvent->rates;
}
} catch (ShippingRateException $shippingRateException) {
Snipcart::error('Snipcart plugin returned an error while fetching rates for {num}', [
'num' => $order->invoiceNumber,
]);
return [
'errors' => $shippingRateException->event->getErrors(),
];
}
if (empty($rates)) {
Snipcart::error('Snipcart plugin did not return any rates for {num}', [
'num' => $order->invoiceNumber,
]);
}
return [
'rates' => $rates,
'package' => $package,
];
}
public function handleCompletedOrder(Order $order): object
{
$response = (object) [
'orders' => [],
'errors' => [],
];
$shipStation = $this->getShipStation();
if (!$shipStation) {
return $response;
}
// is the plugin in test mode?
$isTestMode = Snipcart::$plugin->getSettings()->testMode;
// has ShipStation integration been configured?
$shipStationConfigured = $shipStation->isConfigured();
// does ShipStation's settings say we should send completed Snipcart orders?
$shipStationShouldSend = $shipStation->getSettings()->sendCompletedOrders;
// should we seriously do it?
$sendToShipStation = ! $isTestMode && $order->hasShippableItems() && $shipStationConfigured && $shipStationShouldSend;
// send order to ShipStation if we need to
if ($sendToShipStation) {
$shipStationOrder = $shipStation->createOrder($order);
$response->orders['shipStation'] = $shipStationOrder;
if ((is_countable($shipStationOrder->getErrors()) ? count($shipStationOrder->getErrors()) : 0) > 0) {
$response->errors['shipStation'] = $shipStationOrder->getErrors();
}
}
return $response;
}
public function getQuoteLogForOrder($order): ShippingQuoteLog|array|ActiveRecord|null
{
return ShippingQuoteLog::find()
->where([
'token' => $order->token,
])
->orderBy([
'dateCreated' => SORT_DESC,
])
->one();
}
}