PurchaseBuilder.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Signifyd\Model\SignifydGateway\Request;
  7. use Magento\Framework\App\Area;
  8. use Magento\Framework\Config\ScopeInterface;
  9. use Magento\Framework\Exception\ConfigurationMismatchException;
  10. use Magento\Framework\Intl\DateTimeFactory;
  11. use Magento\Sales\Api\Data\OrderPaymentInterface;
  12. use Magento\Sales\Model\Order;
  13. use Magento\Signifyd\Model\PaymentMethodMapper\PaymentMethodMapper;
  14. use Magento\Signifyd\Model\PaymentVerificationFactory;
  15. use Magento\Signifyd\Model\SignifydOrderSessionId;
  16. /**
  17. * Prepare data related to purchase event represented in case creation request.
  18. */
  19. class PurchaseBuilder
  20. {
  21. /**
  22. * @var DateTimeFactory
  23. */
  24. private $dateTimeFactory;
  25. /**
  26. * @var ScopeInterface
  27. */
  28. private $scope;
  29. /**
  30. * @var SignifydOrderSessionId
  31. */
  32. private $signifydOrderSessionId;
  33. /**
  34. * @var PaymentVerificationFactory
  35. */
  36. private $paymentVerificationFactory;
  37. /**
  38. * @var PaymentMethodMapper
  39. */
  40. private $paymentMethodMapper;
  41. /**
  42. * PurchaseBuilder constructor.
  43. *
  44. * @param DateTimeFactory $dateTimeFactory
  45. * @param ScopeInterface $scope
  46. * @param SignifydOrderSessionId $signifydOrderSessionId
  47. * @param PaymentVerificationFactory $paymentVerificationFactory
  48. * @param PaymentMethodMapper $paymentMethodMapper
  49. */
  50. public function __construct(
  51. DateTimeFactory $dateTimeFactory,
  52. ScopeInterface $scope,
  53. SignifydOrderSessionId $signifydOrderSessionId,
  54. PaymentVerificationFactory $paymentVerificationFactory,
  55. PaymentMethodMapper $paymentMethodMapper
  56. ) {
  57. $this->dateTimeFactory = $dateTimeFactory;
  58. $this->scope = $scope;
  59. $this->signifydOrderSessionId = $signifydOrderSessionId;
  60. $this->paymentVerificationFactory = $paymentVerificationFactory;
  61. $this->paymentMethodMapper = $paymentMethodMapper;
  62. }
  63. /**
  64. * Returns purchase data params
  65. *
  66. * @param Order $order
  67. * @return array
  68. * @throws ConfigurationMismatchException
  69. */
  70. public function build(Order $order)
  71. {
  72. $orderPayment = $order->getPayment();
  73. $createdAt = $this->dateTimeFactory->create(
  74. $order->getCreatedAt(),
  75. new \DateTimeZone('UTC')
  76. );
  77. $result = [
  78. 'purchase' => [
  79. 'orderSessionId' => $this->signifydOrderSessionId->get($order->getQuoteId()),
  80. 'browserIpAddress' => $order->getRemoteIp(),
  81. 'orderId' => $order->getIncrementId(),
  82. 'createdAt' => $createdAt->format(\DateTime::ATOM),
  83. 'paymentGateway' => $this->getPaymentGateway($orderPayment->getMethod()),
  84. 'transactionId' => $orderPayment->getLastTransId(),
  85. 'currency' => $order->getOrderCurrencyCode(),
  86. 'avsResponseCode' => $this->getAvsCode($orderPayment),
  87. 'cvvResponseCode' => $this->getCvvCode($orderPayment),
  88. 'orderChannel' => $this->getOrderChannel(),
  89. 'totalPrice' => $order->getGrandTotal(),
  90. 'paymentMethod' => $this->paymentMethodMapper
  91. ->getSignifydPaymentMethodCode($orderPayment->getMethod())
  92. ],
  93. ];
  94. $shippingDescription = $order->getShippingDescription();
  95. if ($shippingDescription !== null) {
  96. $result['purchase']['shipments'] = [
  97. [
  98. 'shipper' => $this->getShipper($order->getShippingDescription()),
  99. 'shippingMethod' => $this->getShippingMethod($order->getShippingDescription()),
  100. 'shippingPrice' => $order->getShippingAmount()
  101. ]
  102. ];
  103. }
  104. $products = $this->getProducts($order);
  105. if (!empty($products)) {
  106. $result['purchase']['products'] = $products;
  107. }
  108. return $result;
  109. }
  110. /**
  111. * Returns the products purchased in the transaction.
  112. *
  113. * @param Order $order
  114. * @return array
  115. */
  116. private function getProducts(Order $order)
  117. {
  118. $result = [];
  119. foreach ($order->getAllItems() as $orderItem) {
  120. $result[] = [
  121. 'itemId' => $orderItem->getSku(),
  122. 'itemName' => $orderItem->getName(),
  123. 'itemPrice' => $orderItem->getPrice(),
  124. 'itemQuantity' => (int)$orderItem->getQtyOrdered(),
  125. 'itemUrl' => $orderItem->getProduct()->getProductUrl(),
  126. 'itemWeight' => $orderItem->getProduct()->getWeight()
  127. ];
  128. }
  129. return $result;
  130. }
  131. /**
  132. * Returns the name of the shipper
  133. *
  134. * @param string $shippingDescription
  135. * @return string
  136. */
  137. private function getShipper($shippingDescription)
  138. {
  139. $result = explode(' - ', $shippingDescription, 2);
  140. return count($result) == 2 ? $result[0] : '';
  141. }
  142. /**
  143. * Returns the type of the shipment method used
  144. *
  145. * @param string $shippingDescription
  146. * @return string
  147. */
  148. private function getShippingMethod($shippingDescription)
  149. {
  150. $result = explode(' - ', $shippingDescription, 2);
  151. return count($result) == 2 ? $result[1] : '';
  152. }
  153. /**
  154. * Returns the gateway that processed the transaction. For PayPal orders should be paypal_account.
  155. *
  156. * @param string $gatewayCode
  157. * @return string
  158. */
  159. private function getPaymentGateway($gatewayCode)
  160. {
  161. $payPalCodeList = [
  162. 'paypal_express',
  163. 'braintree_paypal',
  164. 'payflowpro',
  165. 'payflow_express',
  166. 'payflow_link',
  167. 'payflow_advanced',
  168. 'hosted_pro',
  169. ];
  170. return in_array($gatewayCode, $payPalCodeList) ? 'paypal_account' : $gatewayCode;
  171. }
  172. /**
  173. * Returns WEB for web-orders, PHONE for orders created by Admin
  174. *
  175. * @return string
  176. */
  177. private function getOrderChannel()
  178. {
  179. return $this->scope->getCurrentScope() === Area::AREA_ADMINHTML ? 'PHONE' : 'WEB';
  180. }
  181. /**
  182. * Gets AVS code for order payment method.
  183. *
  184. * @param OrderPaymentInterface $orderPayment
  185. * @return string
  186. * @throws ConfigurationMismatchException
  187. */
  188. private function getAvsCode(OrderPaymentInterface $orderPayment)
  189. {
  190. $avsAdapter = $this->paymentVerificationFactory->createPaymentAvs($orderPayment->getMethod());
  191. return $avsAdapter->getCode($orderPayment);
  192. }
  193. /**
  194. * Gets CVV code for order payment method.
  195. *
  196. * @param OrderPaymentInterface $orderPayment
  197. * @return string
  198. * @throws ConfigurationMismatchException
  199. */
  200. private function getCvvCode(OrderPaymentInterface $orderPayment)
  201. {
  202. $cvvAdapter = $this->paymentVerificationFactory->createPaymentCvv($orderPayment->getMethod());
  203. return $cvvAdapter->getCode($orderPayment);
  204. }
  205. }