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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Signifyd\Model\SignifydGateway\Request;
use Magento\Framework\App\Area;
use Magento\Framework\Config\ScopeInterface;
use Magento\Framework\Exception\ConfigurationMismatchException;
use Magento\Framework\Intl\DateTimeFactory;
use Magento\Sales\Api\Data\OrderPaymentInterface;
use Magento\Sales\Model\Order;
use Magento\Signifyd\Model\PaymentMethodMapper\PaymentMethodMapper;
use Magento\Signifyd\Model\PaymentVerificationFactory;
use Magento\Signifyd\Model\SignifydOrderSessionId;
/**
* Prepare data related to purchase event represented in case creation request.
*/
class PurchaseBuilder
{
/**
* @var DateTimeFactory
*/
private $dateTimeFactory;
/**
* @var ScopeInterface
*/
private $scope;
/**
* @var SignifydOrderSessionId
*/
private $signifydOrderSessionId;
/**
* @var PaymentVerificationFactory
*/
private $paymentVerificationFactory;
/**
* @var PaymentMethodMapper
*/
private $paymentMethodMapper;
/**
* PurchaseBuilder constructor.
*
* @param DateTimeFactory $dateTimeFactory
* @param ScopeInterface $scope
* @param SignifydOrderSessionId $signifydOrderSessionId
* @param PaymentVerificationFactory $paymentVerificationFactory
* @param PaymentMethodMapper $paymentMethodMapper
*/
public function __construct(
DateTimeFactory $dateTimeFactory,
ScopeInterface $scope,
SignifydOrderSessionId $signifydOrderSessionId,
PaymentVerificationFactory $paymentVerificationFactory,
PaymentMethodMapper $paymentMethodMapper
) {
$this->dateTimeFactory = $dateTimeFactory;
$this->scope = $scope;
$this->signifydOrderSessionId = $signifydOrderSessionId;
$this->paymentVerificationFactory = $paymentVerificationFactory;
$this->paymentMethodMapper = $paymentMethodMapper;
}
/**
* Returns purchase data params
*
* @param Order $order
* @return array
* @throws ConfigurationMismatchException
*/
public function build(Order $order)
{
$orderPayment = $order->getPayment();
$createdAt = $this->dateTimeFactory->create(
$order->getCreatedAt(),
new \DateTimeZone('UTC')
);
$result = [
'purchase' => [
'orderSessionId' => $this->signifydOrderSessionId->get($order->getQuoteId()),
'browserIpAddress' => $order->getRemoteIp(),
'orderId' => $order->getIncrementId(),
'createdAt' => $createdAt->format(\DateTime::ATOM),
'paymentGateway' => $this->getPaymentGateway($orderPayment->getMethod()),
'transactionId' => $orderPayment->getLastTransId(),
'currency' => $order->getOrderCurrencyCode(),
'avsResponseCode' => $this->getAvsCode($orderPayment),
'cvvResponseCode' => $this->getCvvCode($orderPayment),
'orderChannel' => $this->getOrderChannel(),
'totalPrice' => $order->getGrandTotal(),
'paymentMethod' => $this->paymentMethodMapper
->getSignifydPaymentMethodCode($orderPayment->getMethod())
],
];
$shippingDescription = $order->getShippingDescription();
if ($shippingDescription !== null) {
$result['purchase']['shipments'] = [
[
'shipper' => $this->getShipper($order->getShippingDescription()),
'shippingMethod' => $this->getShippingMethod($order->getShippingDescription()),
'shippingPrice' => $order->getShippingAmount()
]
];
}
$products = $this->getProducts($order);
if (!empty($products)) {
$result['purchase']['products'] = $products;
}
return $result;
}
/**
* Returns the products purchased in the transaction.
*
* @param Order $order
* @return array
*/
private function getProducts(Order $order)
{
$result = [];
foreach ($order->getAllItems() as $orderItem) {
$result[] = [
'itemId' => $orderItem->getSku(),
'itemName' => $orderItem->getName(),
'itemPrice' => $orderItem->getPrice(),
'itemQuantity' => (int)$orderItem->getQtyOrdered(),
'itemUrl' => $orderItem->getProduct()->getProductUrl(),
'itemWeight' => $orderItem->getProduct()->getWeight()
];
}
return $result;
}
/**
* Returns the name of the shipper
*
* @param string $shippingDescription
* @return string
*/
private function getShipper($shippingDescription)
{
$result = explode(' - ', $shippingDescription, 2);
return count($result) == 2 ? $result[0] : '';
}
/**
* Returns the type of the shipment method used
*
* @param string $shippingDescription
* @return string
*/
private function getShippingMethod($shippingDescription)
{
$result = explode(' - ', $shippingDescription, 2);
return count($result) == 2 ? $result[1] : '';
}
/**
* Returns the gateway that processed the transaction. For PayPal orders should be paypal_account.
*
* @param string $gatewayCode
* @return string
*/
private function getPaymentGateway($gatewayCode)
{
$payPalCodeList = [
'paypal_express',
'braintree_paypal',
'payflowpro',
'payflow_express',
'payflow_link',
'payflow_advanced',
'hosted_pro',
];
return in_array($gatewayCode, $payPalCodeList) ? 'paypal_account' : $gatewayCode;
}
/**
* Returns WEB for web-orders, PHONE for orders created by Admin
*
* @return string
*/
private function getOrderChannel()
{
return $this->scope->getCurrentScope() === Area::AREA_ADMINHTML ? 'PHONE' : 'WEB';
}
/**
* Gets AVS code for order payment method.
*
* @param OrderPaymentInterface $orderPayment
* @return string
* @throws ConfigurationMismatchException
*/
private function getAvsCode(OrderPaymentInterface $orderPayment)
{
$avsAdapter = $this->paymentVerificationFactory->createPaymentAvs($orderPayment->getMethod());
return $avsAdapter->getCode($orderPayment);
}
/**
* Gets CVV code for order payment method.
*
* @param OrderPaymentInterface $orderPayment
* @return string
* @throws ConfigurationMismatchException
*/
private function getCvvCode(OrderPaymentInterface $orderPayment)
{
$cvvAdapter = $this->paymentVerificationFactory->createPaymentCvv($orderPayment->getMethod());
return $cvvAdapter->getCode($orderPayment);
}
}