OrderAdapter.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License").
  6. * You may not use this file except in compliance with the License.
  7. * A copy of the License is located at
  8. *
  9. * http://aws.amazon.com/apache2.0
  10. *
  11. * or in the "license" file accompanying this file. This file is distributed
  12. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied. See the License for the specific language governing
  14. * permissions and limitations under the License.
  15. */
  16. namespace Amazon\Payment\Gateway\Data\Order;
  17. use Magento\Payment\Gateway\Data\Order\AddressAdapterFactory;
  18. use Magento\Payment\Gateway\Data\AddressAdapterInterface;
  19. use Magento\Payment\Gateway\Data\OrderAdapterInterface;
  20. use Magento\Sales\Model\Order;
  21. use Amazon\Core\Model\AmazonConfig;
  22. use Amazon\Core\Helper\Data;
  23. /**
  24. * Class OrderAdapter
  25. */
  26. class OrderAdapter implements OrderAdapterInterface
  27. {
  28. /**
  29. * @var Order
  30. */
  31. private $order;
  32. /**
  33. * @var AddressAdapter
  34. */
  35. private $addressAdapterFactory;
  36. /**
  37. * @var Data
  38. */
  39. private $coreHelper;
  40. /**
  41. * @var AmazonConfig
  42. */
  43. private $config;
  44. /**
  45. * OrderAdapter constructor.
  46. *
  47. * @param Order $order
  48. * @param AddressAdapterFactory $addressAdapterFactory
  49. * @param Data $coreHelper
  50. * @param \Amazon\Core\Model\AmazonConfig $config
  51. */
  52. public function __construct(
  53. Order $order,
  54. \Magento\Payment\Gateway\Data\Order\AddressAdapterFactory $addressAdapterFactory,
  55. Data $coreHelper,
  56. AmazonConfig $config
  57. ) {
  58. $this->order = $order;
  59. $this->addressAdapterFactory = $addressAdapterFactory;
  60. $this->coreHelper = $coreHelper;
  61. $this->config = $config;
  62. }
  63. /**
  64. * Returns currency code
  65. *
  66. * @return string
  67. */
  68. public function getCurrencyCode()
  69. {
  70. return $this->order->getBaseCurrencyCode();
  71. }
  72. /**
  73. * Returns order increment id
  74. *
  75. * @return string
  76. */
  77. public function getOrderIncrementId()
  78. {
  79. return $this->order->getIncrementId();
  80. }
  81. /**
  82. * Returns customer ID
  83. *
  84. * @return int|null
  85. */
  86. public function getCustomerId()
  87. {
  88. return $this->order->getCustomerId();
  89. }
  90. /**
  91. * Returns billing address
  92. *
  93. * @return AddressAdapterInterface|null
  94. */
  95. public function getBillingAddress()
  96. {
  97. if ($this->order->getBillingAddress()) {
  98. return $this->addressAdapterFactory->create(
  99. ['address' => $this->order->getBillingAddress()]
  100. );
  101. }
  102. return null;
  103. }
  104. /**
  105. * Returns shipping address
  106. *
  107. * @return AddressAdapterInterface|null
  108. */
  109. public function getShippingAddress()
  110. {
  111. if ($this->order->getShippingAddress()) {
  112. return $this->addressAdapterFactory->create(
  113. ['address' => $this->order->getShippingAddress()]
  114. );
  115. }
  116. return null;
  117. }
  118. /**
  119. * Returns order store id
  120. *
  121. * @return int
  122. */
  123. public function getStoreId()
  124. {
  125. return $this->order->getStoreId();
  126. }
  127. /**
  128. * Returns order id
  129. *
  130. * @return int
  131. */
  132. public function getId()
  133. {
  134. return $this->order->getEntityId();
  135. }
  136. /**
  137. * Returns order grand total amount
  138. *
  139. * @return float|null
  140. */
  141. public function getGrandTotalAmount()
  142. {
  143. return $this->order->getBaseGrandTotal();
  144. }
  145. /**
  146. * Returns list of line items in the cart
  147. *
  148. * @return \Magento\Sales\Api\Data\OrderItemInterface[]
  149. */
  150. public function getItems()
  151. {
  152. return $this->order->getItems();
  153. }
  154. /**
  155. * Gets the remote IP address for the order.
  156. *
  157. * @return string|null Remote IP address.
  158. */
  159. public function getRemoteIp()
  160. {
  161. return $this->order->getRemoteIp();
  162. }
  163. /**
  164. * Gets order currency code and amount if Amazon multi-currency was used.
  165. * @param $amount
  166. * @return array
  167. */
  168. public function getMulticurrencyDetails($amount)
  169. {
  170. $values = ['multicurrency' => false];
  171. if ($this->config->useMultiCurrency()) {
  172. $invoices = $this->order->getInvoiceCollection();
  173. foreach ($invoices->getItems() as $key => $invoice) {
  174. $baseTotal = $invoice->getBaseGrandTotal();
  175. // compare numeric values to make sure we have the right invoice
  176. // (could have an invoice for each item during partial capture).
  177. if (bccomp($baseTotal, (float)$amount) == 0) {
  178. $values = [
  179. 'multicurrency' => true,
  180. 'order_currency' => $invoice->getOrderCurrencyCode(),
  181. 'total' => $invoice->getGrandTotal()
  182. ];
  183. break;
  184. }
  185. }
  186. }
  187. $values['store_name'] = $this->order->getStoreName();
  188. $values['store_id'] = $this->order->getStoreId();
  189. return $values;
  190. }
  191. /**
  192. * Returns current Amazon Order Reference ID
  193. * @return string
  194. */
  195. public function getAmazonOrderID()
  196. {
  197. $orderID = '';
  198. if (!empty($this->order->getExtensionAttributes()->getAmazonOrderReferenceId())) {
  199. $orderID = $this->order->getExtensionAttributes()->getAmazonOrderReferenceId()->getAmazonOrderReferenceId();
  200. }
  201. return $orderID;
  202. }
  203. }