1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace CommerceLeague\ActiveCampaign\Gateway\Request; |
7
|
|
|
|
8
|
|
|
use CommerceLeague\ActiveCampaign\Api\CustomerRepositoryInterface; |
9
|
|
|
use CommerceLeague\ActiveCampaign\Helper\Config as ConfigHelper; |
10
|
|
|
use Magento\Framework\Stdlib\DateTime\TimezoneInterface; |
11
|
|
|
use Magento\Sales\Api\Data\OrderInterface as MagentoOrderInterface; |
12
|
|
|
use Magento\Sales\Model\Order as MagentoOrder; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class OrderBuilder |
16
|
|
|
*/ |
17
|
|
|
class OrderBuilder |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var ConfigHelper |
21
|
|
|
*/ |
22
|
|
|
private $configHelper; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var CustomerRepositoryInterface |
26
|
|
|
*/ |
27
|
|
|
private $customerRepository; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var TimezoneInterface |
31
|
|
|
*/ |
32
|
|
|
private $timezone; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param ConfigHelper $configHelper |
36
|
|
|
* @param CustomerRepositoryInterface $customerRepository |
37
|
|
|
* @param TimezoneInterface $timezone |
38
|
|
|
*/ |
39
|
|
|
public function __construct( |
40
|
|
|
ConfigHelper $configHelper, |
41
|
|
|
CustomerRepositoryInterface $customerRepository, |
42
|
|
|
TimezoneInterface $timezone |
43
|
|
|
) { |
44
|
|
|
$this->configHelper = $configHelper; |
45
|
|
|
$this->customerRepository = $customerRepository; |
46
|
|
|
$this->timezone = $timezone; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param MagentoOrderInterface|MagentoOrder $magentoOrder |
51
|
|
|
* @return array |
52
|
|
|
* @throws \Exception |
53
|
|
|
*/ |
54
|
|
|
public function build(MagentoOrderInterface $magentoOrder): array |
55
|
|
|
{ |
56
|
|
|
$customer = $this->customerRepository->getByMagentoCustomerId($magentoOrder->getCustomerId()); |
57
|
|
|
|
58
|
|
|
$request = [ |
59
|
|
|
'externalid' => $magentoOrder->getId(), |
60
|
|
|
'source' => 1, |
61
|
|
|
'email' => $magentoOrder->getCustomerEmail(), |
62
|
|
|
'orderNumber' => $magentoOrder->getIncrementId(), |
63
|
|
|
'orderDate' => $this->formatDateTime($magentoOrder->getCreatedAt()), |
|
|
|
|
64
|
|
|
'shippingMethod' => $magentoOrder->getShippingMethod(), |
|
|
|
|
65
|
|
|
'totalPrice' => $this->convertToCent((float)$magentoOrder->getGrandTotal()), |
66
|
|
|
'currency' => $magentoOrder->getBaseCurrencyCode(), |
67
|
|
|
'connectionid' => $this->configHelper->getConnectionId(), |
68
|
|
|
'customerid' => $customer->getActiveCampaignId(), |
69
|
|
|
'orderProducts' => [] |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
/** @var MagentoOrder\Item $magentoOrderItem */ |
73
|
|
|
foreach ($magentoOrder->getAllVisibleItems() as $magentoOrderItem) { |
74
|
|
|
$request['orderProducts'][] = [ |
75
|
|
|
'externalid' => $magentoOrderItem->getSku(), |
76
|
|
|
'name' => $magentoOrderItem->getName(), |
77
|
|
|
'price' => $this->convertToCent((float)$magentoOrderItem->getPriceInclTax()), |
78
|
|
|
'quantity' => (int)$magentoOrderItem->getQtyOrdered() |
79
|
|
|
]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $request; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param float $amount |
87
|
|
|
* @return int |
88
|
|
|
*/ |
89
|
|
|
private function convertToCent(float $amount): int |
90
|
|
|
{ |
91
|
|
|
return (int)($amount * 100); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $date |
96
|
|
|
* @return string |
97
|
|
|
* @throws \Exception |
98
|
|
|
*/ |
99
|
|
|
private function formatDateTime(string $date): string |
100
|
|
|
{ |
101
|
|
|
return (new \DateTime($date))->format(\DateTime::W3C); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|