Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PurchaseBuilder.php
Go to the documentation of this file.
1 <?php
7 
17 
22 {
26  private $dateTimeFactory;
27 
31  private $scope;
32 
36  private $signifydOrderSessionId;
37 
41  private $paymentVerificationFactory;
42 
46  private $paymentMethodMapper;
47 
57  public function __construct(
58  DateTimeFactory $dateTimeFactory,
59  ScopeInterface $scope,
60  SignifydOrderSessionId $signifydOrderSessionId,
61  PaymentVerificationFactory $paymentVerificationFactory,
62  PaymentMethodMapper $paymentMethodMapper
63  ) {
64  $this->dateTimeFactory = $dateTimeFactory;
65  $this->scope = $scope;
66  $this->signifydOrderSessionId = $signifydOrderSessionId;
67  $this->paymentVerificationFactory = $paymentVerificationFactory;
68  $this->paymentMethodMapper = $paymentMethodMapper;
69  }
70 
78  public function build(Order $order)
79  {
80  $orderPayment = $order->getPayment();
81  $createdAt = $this->dateTimeFactory->create(
82  $order->getCreatedAt(),
83  new \DateTimeZone('UTC')
84  );
85 
86  $result = [
87  'purchase' => [
88  'orderSessionId' => $this->signifydOrderSessionId->get($order->getQuoteId()),
89  'browserIpAddress' => $order->getRemoteIp(),
90  'orderId' => $order->getIncrementId(),
91  'createdAt' => $createdAt->format(\DateTime::ATOM),
92  'paymentGateway' => $this->getPaymentGateway($orderPayment->getMethod()),
93  'transactionId' => $orderPayment->getLastTransId(),
94  'currency' => $order->getOrderCurrencyCode(),
95  'avsResponseCode' => $this->getAvsCode($orderPayment),
96  'cvvResponseCode' => $this->getCvvCode($orderPayment),
97  'orderChannel' => $this->getOrderChannel(),
98  'totalPrice' => $order->getGrandTotal(),
99  'paymentMethod' => $this->paymentMethodMapper
100  ->getSignifydPaymentMethodCode($orderPayment->getMethod())
101  ],
102  ];
103 
104  $shippingDescription = $order->getShippingDescription();
105  if ($shippingDescription !== null) {
106  $result['purchase']['shipments'] = [
107  [
108  'shipper' => $this->getShipper($order->getShippingDescription()),
109  'shippingMethod' => $this->getShippingMethod($order->getShippingDescription()),
110  'shippingPrice' => $order->getShippingAmount()
111  ]
112  ];
113  }
114 
115  $products = $this->getProducts($order);
116  if (!empty($products)) {
117  $result['purchase']['products'] = $products;
118  }
119 
120  return $result;
121  }
122 
129  private function getProducts(Order $order)
130  {
131  $result = [];
132  foreach ($order->getAllItems() as $orderItem) {
133  $result[] = [
134  'itemId' => $orderItem->getSku(),
135  'itemName' => $orderItem->getName(),
136  'itemPrice' => $orderItem->getPrice(),
137  'itemQuantity' => (int)$orderItem->getQtyOrdered(),
138  'itemUrl' => $orderItem->getProduct()->getProductUrl(),
139  'itemWeight' => $orderItem->getProduct()->getWeight()
140  ];
141  }
142 
143  return $result;
144  }
145 
152  private function getShipper($shippingDescription)
153  {
154  $result = explode(' - ', $shippingDescription, 2);
155 
156  return count($result) == 2 ? $result[0] : '';
157  }
158 
165  private function getShippingMethod($shippingDescription)
166  {
167  $result = explode(' - ', $shippingDescription, 2);
168 
169  return count($result) == 2 ? $result[1] : '';
170  }
171 
178  private function getPaymentGateway($gatewayCode)
179  {
180  $payPalCodeList = [
181  'paypal_express',
182  'braintree_paypal',
183  'payflowpro',
184  'payflow_express',
185  'payflow_link',
186  'payflow_advanced',
187  'hosted_pro',
188  ];
189  return in_array($gatewayCode, $payPalCodeList) ? 'paypal_account' : $gatewayCode;
190  }
191 
197  private function getOrderChannel()
198  {
199  return $this->scope->getCurrentScope() === Area::AREA_ADMINHTML ? 'PHONE' : 'WEB';
200  }
201 
209  private function getAvsCode(OrderPaymentInterface $orderPayment)
210  {
211  $avsAdapter = $this->paymentVerificationFactory->createPaymentAvs($orderPayment->getMethod());
212  return $avsAdapter->getCode($orderPayment);
213  }
214 
222  private function getCvvCode(OrderPaymentInterface $orderPayment)
223  {
224  $cvvAdapter = $this->paymentVerificationFactory->createPaymentCvv($orderPayment->getMethod());
225  return $cvvAdapter->getCode($orderPayment);
226  }
227 }
$orderItem
Definition: order.php:30
__construct(DateTimeFactory $dateTimeFactory, ScopeInterface $scope, SignifydOrderSessionId $signifydOrderSessionId, PaymentVerificationFactory $paymentVerificationFactory, PaymentMethodMapper $paymentMethodMapper)
$order
Definition: order.php:55