-
Notifications
You must be signed in to change notification settings - Fork 3
/
UpdateOrderProcessor.php
executable file
·193 lines (170 loc) · 6.57 KB
/
UpdateOrderProcessor.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
<?php
/**
* Marello
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is published at http://opensource.org/licenses/osl-3.0.php.
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@marello.com so we can send you a copy immediately
*
* @category Marello
* @package Bridge
* @copyright Copyright Marello (http://www.marello.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
namespace Marello\Bridge\Model\Processor;
use Magento\Sales\Api\OrderRepositoryInterface;
use Marello\Bridge\Api\Data\ConnectorRegistryInterface;
use Marello\Bridge\Api\Data\DataConverterRegistryInterface;
use Marello\Bridge\Api\MarelloTransportInterface;
use Marello\Bridge\Model\Queue\QueueEventTypeInterface;
use Marello\Bridge\Model\Converter\OrderDataConverter;
class UpdateOrderProcessor extends AbstractProcessor
{
const TRANSITION_TYPE_INVOICE = 'invoice';
const TRANSITION_TYPE_PICKPACK = 'prepare_shipping';
const TRANSITION_TYPE_CANCELED = 'cancel';
/** @var OrderRepositoryInterface $orderRepository */
protected $orderRepository;
/**
* UpdateOrderProcessor constructor.
* @param ConnectorRegistryInterface $connectorRegistry
* @param DataConverterRegistryInterface $converterRegistry
* @param MarelloTransportInterface $transport
* @param OrderRepositoryInterface $orderRepository
*/
public function __construct(
ConnectorRegistryInterface $connectorRegistry,
DataConverterRegistryInterface $converterRegistry,
MarelloTransportInterface $transport,
OrderRepositoryInterface $orderRepository
) {
$this->orderRepository = $orderRepository;
parent::__construct($connectorRegistry, $converterRegistry, $transport);
}
/**
* Process updated order data
* @param array $orderData
* @return $this
* @throws \Exception
*/
public function process($orderData)
{
if (!is_array($orderData)) {
return false;
}
$order = $orderData['order'];
$jsonData = unserialize($order->getData('marello_data'));
$data = json_decode($jsonData);
if (!$data || !property_exists($data, 'id')) {
return false;
}
// get all the order info
$marelloOrderId = $data->id;
$this->setTransportConnector('default');
$this->transport->getConnector()->setType('get');
$orderResult = $this->transport->call('/orders', ['id' => $marelloOrderId]);
$fetchResult = $orderResult->getData('body');
$decodedResult = json_decode($fetchResult);
if (!$decodedResult || !property_exists($decodedResult, 'workflowItems')) {
return false;
}
if (is_array($decodedResult->workflowItems)) {
$workFlowItemId = $decodedResult->workflowItems[0]->id;
}
$transition = $this->getWorkflowTransition($orderData['type']);
$transitWorkflowResult = $this->transitOrder($workFlowItemId, $transition);
if (!$transitWorkflowResult->getData('error')) {
$this->saveOrderData($orderResult->getData('body'), $order);
if ($transition === self::TRANSITION_TYPE_CANCELED) {
return true;
}
}
if ($transition === self::TRANSITION_TYPE_INVOICE) {
// update order with payment references etc..
try {
$this->updateOrderData($order, $marelloOrderId);
} catch (\Exception $e) {
throw new \Exception($e->getMessage());
}
// order is invoiced, directly prepare for ship it.
$transitWorkflowResult = $this->transitOrder($workFlowItemId, self::TRANSITION_TYPE_PICKPACK);
if ($transitWorkflowResult->getData('error') && null !== $transitWorkflowResult) {
$this->saveOrderData($orderResult, $order);
return true;
}
}
return false;
}
/**
* @param $order
* @param $marelloOrderId
* @return mixed|null
* @throws \Exception
*/
protected function updateOrderData($order, $marelloOrderId)
{
$converter = $this->getDataConverterByAlias('order');
$converter->setConversionType(OrderDataConverter::ORDER_CONVERSION_TYPE_UPDATE);
$convertedOrder = $converter->convertEntity($order);
// put request only handles certain attributes so get them from the converted order
$orderData = [
'billingAddress' => $convertedOrder['billingAddress'],
'shippingAddress' => $convertedOrder['shippingAddress'],
'paymentReference' => $convertedOrder['paymentReference'],
'invoicedAt' => $convertedOrder['invoicedAt'],
'invoiceReference' => $convertedOrder['invoiceReference'],
];
$this->setTransportConnector('default');
$this->transport->getConnector()->setType('put');
return $this->transport->call(sprintf('/orders/%s', $marelloOrderId), $orderData);
}
/**
* @param $workflowItemId
* @param $transition
* @return mixed|null|void
* @throws \Exception
*/
protected function transitOrder($workflowItemId, $transition)
{
$this->setTransportConnector('default');
$this->transport->getConnector()->setType('get');
return $this->transport->call(sprintf('/workflow/transit/%s/%s', $workflowItemId, $transition));
}
/**
* Set and save Marello data on order
* @param $result
* @param $order
* @throws \Exception
*/
protected function saveOrderData($result, $order)
{
$order->setData('marello_data', serialize($result));
try {
$this->orderRepository->save($order);
} catch (\Exception $e) {
throw new \Exception('Could not save the marello data on the order');
}
}
/**
* Get workflow transition based on type of update
* @param $type
* @return null|string
*/
protected function getWorkflowTransition($type)
{
$transition = null;
switch ($type) :
case QueueEventTypeInterface::QUEUE_EVENT_TYPE_ORDER_INVOICE:
$transition = self::TRANSITION_TYPE_INVOICE;
break;
case QueueEventTypeInterface::QUEUE_EVENT_TYPE_ORDER_CANCEL:
$transition = self::TRANSITION_TYPE_CANCELED;
break;
endswitch;
return $transition;
}
}