1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Odiseo\SyliusMailchimpPlugin\Handler; |
6
|
|
|
|
7
|
|
|
use Odiseo\SyliusMailchimpPlugin\Api\EcommerceInterface; |
8
|
|
|
use Sylius\Component\Core\Model\AddressInterface; |
9
|
|
|
use Sylius\Component\Core\Model\CustomerInterface; |
10
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
11
|
|
|
use Sylius\Component\Core\Model\PaymentInterface; |
12
|
|
|
use Symfony\Component\Routing\RouterInterface; |
13
|
|
|
|
14
|
|
|
final class OrderRegisterHandler implements OrderRegisterHandlerInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var EcommerceInterface |
18
|
|
|
*/ |
19
|
|
|
private $ecommerceApi; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var CustomerRegisterHandlerInterface |
23
|
|
|
*/ |
24
|
|
|
private $customerRegisterHandler; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var RouterInterface |
28
|
|
|
*/ |
29
|
|
|
private $router; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param EcommerceInterface $ecommerceApi |
33
|
|
|
* @param CustomerRegisterHandlerInterface $customerRegisterHandler |
34
|
|
|
* @param RouterInterface $router |
35
|
|
|
*/ |
36
|
|
|
public function __construct( |
37
|
|
|
EcommerceInterface $ecommerceApi, |
38
|
|
|
CustomerRegisterHandlerInterface $customerRegisterHandler, |
39
|
|
|
RouterInterface $router |
40
|
|
|
) { |
41
|
|
|
$this->ecommerceApi = $ecommerceApi; |
42
|
|
|
$this->customerRegisterHandler = $customerRegisterHandler; |
43
|
|
|
$this->router = $router; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
public function register(OrderInterface $order) |
50
|
|
|
{ |
51
|
|
|
/** @var CustomerInterface $customer */ |
52
|
|
|
if ((null === $customer = $order->getCustomer()) || (count($order->getItems()) == 0)) { |
53
|
|
|
return false; |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$channel = $order->getChannel(); |
57
|
|
|
$storeId = $channel->getCode(); |
58
|
|
|
$orderId = (string) $order->getId(); |
59
|
|
|
|
60
|
|
|
// Registering the customer to ensure that exist on Mailchimp |
61
|
|
|
$response = $this->customerRegisterHandler->register($customer, $channel); |
|
|
|
|
62
|
|
|
|
63
|
|
|
if (!isset($response['id']) && $response !== false) { |
|
|
|
|
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Creating order show url |
68
|
|
|
$context = $this->router->getContext(); |
69
|
|
|
$context->setHost($channel->getHostname()); |
70
|
|
|
$orderShowUrl = $this->router->generate('sylius_shop_order_show', [ |
71
|
|
|
'_locale' => $order->getLocaleCode() ?: 'en', |
72
|
|
|
'tokenValue' => $order->getTokenValue(), |
73
|
|
|
], RouterInterface::ABSOLUTE_URL); |
74
|
|
|
|
75
|
|
|
$response = $this->ecommerceApi->getOrder($storeId, $orderId); |
|
|
|
|
76
|
|
|
$isNew = !isset($response['id']); |
77
|
|
|
|
78
|
|
|
$lastCompletedPayment = $order->getLastPayment(PaymentInterface::STATE_COMPLETED); |
79
|
|
|
$orderCompletedDate = $lastCompletedPayment?$lastCompletedPayment->getUpdatedAt():$order->getUpdatedAt(); |
80
|
|
|
|
81
|
|
|
$data = [ |
82
|
|
|
'id' => $orderId, |
83
|
|
|
'customer' => [ |
84
|
|
|
'id' => (string) $customer->getId(), |
85
|
|
|
], |
86
|
|
|
'financial_status' => 'paid', |
87
|
|
|
'currency_code' => $order->getCurrencyCode() ?: 'USD', |
88
|
|
|
'order_total' => $order->getTotal() / 100, |
89
|
|
|
'order_url' => $orderShowUrl, |
90
|
|
|
'discount_total' => $order->getOrderPromotionTotal() / 100, |
91
|
|
|
'tax_total' => $order->getTaxTotal() / 100, |
92
|
|
|
'shipping_total' => $order->getShippingTotal() / 100, |
93
|
|
|
'processed_at_foreign' => $orderCompletedDate->format('c'), |
|
|
|
|
94
|
|
|
'shipping_address' => $this->getAddressData($order->getShippingAddress()), |
|
|
|
|
95
|
|
|
'billing_address' => $this->getAddressData($order->getBillingAddress()), |
96
|
|
|
'lines' => [], |
97
|
|
|
]; |
98
|
|
|
|
99
|
|
|
foreach ($order->getItems() as $item) { |
100
|
|
|
$data['lines'][] = [ |
101
|
|
|
'id' => (string) $item->getId(), |
102
|
|
|
'product_id' => (string) $item->getProduct()->getId(), |
103
|
|
|
'product_variant_id' => (string) $item->getVariant()->getId(), |
104
|
|
|
'quantity' => $item->getQuantity(), |
105
|
|
|
'price' => $item->getTotal() / 100, |
106
|
|
|
]; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if ($isNew) { |
110
|
|
|
$response = $this->ecommerceApi->addOrder($storeId, $data); |
|
|
|
|
111
|
|
|
|
112
|
|
|
// Unregister abandoned cart after order create |
113
|
|
|
$this->removeCart($order); |
114
|
|
|
} else { |
115
|
|
|
$response = $this->ecommerceApi->updateOrder($storeId, $orderId, $data); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $response; |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* {@inheritdoc} |
123
|
|
|
*/ |
124
|
|
|
public function unregister(OrderInterface $order) |
125
|
|
|
{ |
126
|
|
|
$orderId = (string) $order->getId(); |
127
|
|
|
$storeId = $order->getChannel()->getCode(); |
128
|
|
|
|
129
|
|
|
$response = $this->ecommerceApi->getOrder($storeId, $orderId); |
|
|
|
|
130
|
|
|
$isNew = !isset($response['id']); |
131
|
|
|
|
132
|
|
|
if (!$isNew) { |
133
|
|
|
return $this->ecommerceApi->removeOrder($storeId, $orderId); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return false; |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param AddressInterface $address |
141
|
|
|
* |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
|
|
private function getAddressData(AddressInterface $address): array |
145
|
|
|
{ |
146
|
|
|
return [ |
147
|
|
|
'company' => $address->getCompany() ?: '', |
148
|
|
|
'address1' => $address->getStreet() ?: '', |
149
|
|
|
'city' => $address->getCity() ?: '', |
150
|
|
|
'province' => $address->getProvinceName() ?: '', |
151
|
|
|
'province_code' => $address->getProvinceCode() ?: '', |
152
|
|
|
'postal_code' => $address->getPostcode() ?: '', |
153
|
|
|
'country_code' => $address->getCountryCode() ?: '', |
154
|
|
|
'phone' => $address->getPhoneNumber() ?: '', |
155
|
|
|
]; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param OrderInterface $order |
160
|
|
|
*/ |
161
|
|
|
private function removeCart(OrderInterface $order): void |
162
|
|
|
{ |
163
|
|
|
$cartId = (string) $order->getId(); |
164
|
|
|
$storeId = $order->getChannel()->getCode(); |
165
|
|
|
|
166
|
|
|
$this->ecommerceApi->removeCart($storeId, $cartId); |
|
|
|
|
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: