Address.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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\Helper;
  17. use Amazon\Core\Api\Data\AmazonAddressInterface;
  18. use Amazon\Core\Domain\AmazonAddress;
  19. use Magento\Customer\Api\Data\AddressInterface;
  20. use Magento\Customer\Api\Data\AddressInterfaceFactory;
  21. use Magento\Customer\Api\Data\RegionInterfaceFactory;
  22. use Magento\Directory\Model\RegionFactory;
  23. use Magento\Framework\App\Config\ScopeConfigInterface;
  24. class Address
  25. {
  26. /**
  27. * @var AddressInterfaceFactory
  28. */
  29. private $addressFactory;
  30. /**
  31. * @var RegionFactory
  32. */
  33. private $regionFactory;
  34. /**
  35. * @var RegionInterfaceFactory
  36. */
  37. private $regionDataFactory;
  38. /**
  39. * @var ScopeConfigInterface
  40. */
  41. private $scopeConfig;
  42. public function __construct(
  43. AddressInterfaceFactory $addressFactory,
  44. RegionFactory $regionFactory,
  45. RegionInterfaceFactory $regionDataFactory,
  46. ScopeConfigInterface $config
  47. ) {
  48. $this->addressFactory = $addressFactory;
  49. $this->regionFactory = $regionFactory;
  50. $this->regionDataFactory = $regionDataFactory;
  51. $this->scopeConfig = $config;
  52. }
  53. /**
  54. * Convert Amazon Address to Magento Address
  55. *
  56. * @param AmazonAddressInterface $amazonAddress
  57. *
  58. * @return AddressInterface
  59. */
  60. public function convertToMagentoEntity(AmazonAddressInterface $amazonAddress)
  61. {
  62. $addressLinesAllowed = (int)$this->scopeConfig->getValue(
  63. 'customer/address/street_lines',
  64. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  65. );
  66. $address = $this->addressFactory->create();
  67. $address->setFirstname($amazonAddress->getFirstName());
  68. $address->setLastname($amazonAddress->getLastName());
  69. $address->setCity($amazonAddress->getCity());
  70. $address->setPostcode($amazonAddress->getPostCode());
  71. $address->setTelephone($amazonAddress->getTelephone());
  72. $address->setCountryId($this->getCountryId($amazonAddress));
  73. /*
  74. * The number of lines in a street address is configurable via 'customer/address/street_lines'.
  75. * To avoid discarding information, we'll concatenate additional lines so that they fit within the configured
  76. * address length.
  77. */
  78. $lines = [];
  79. for ($i = 1; $i <= 4; $i++) {
  80. $line = (string) $amazonAddress->getLine($i);
  81. if ($i <= $addressLinesAllowed) {
  82. $lines[] = $line;
  83. } else {
  84. $lines[count($lines)-1] = trim($lines[count($lines)-1] . ' ' . $line);
  85. }
  86. }
  87. $address->setStreet(array_values($lines));
  88. $company = !empty($amazonAddress->getCompany()) ? $amazonAddress->getCompany() : '';
  89. $address->setCompany($company);
  90. if ($amazonAddress->getState()) {
  91. $address->setRegion($this->getRegionData($amazonAddress, $address->getCountryId()));
  92. }
  93. return $address;
  94. }
  95. protected function getCountryId(AmazonAddressInterface $amazonAddress)
  96. {
  97. return strtoupper($amazonAddress->getCountryCode());
  98. }
  99. protected function getRegionData(AmazonAddressInterface $amazonAddress, $countryId)
  100. {
  101. $region = $this->regionFactory->create();
  102. $regionData = $this->regionDataFactory->create();
  103. $region->loadByCode($amazonAddress->getState(), $countryId);
  104. if (! $region->getId()) {
  105. $region->loadByName($amazonAddress->getState(), $countryId);
  106. }
  107. if ($region->getId()) {
  108. $regionData
  109. ->setRegionId($region->getId())
  110. ->setRegionCode($region->getCode())
  111. ->setRegion($region->getDefaultName());
  112. } else {
  113. $regionData->setRegion($amazonAddress->getState());
  114. }
  115. return $regionData;
  116. }
  117. /**
  118. * Convert Magento address to array for json encode
  119. *
  120. * @param AddressInterface $address
  121. *
  122. * @return array
  123. */
  124. public function convertToArray(AddressInterface $address)
  125. {
  126. $data = [
  127. AddressInterface::CITY => $address->getCity(),
  128. AddressInterface::FIRSTNAME => $address->getFirstname(),
  129. AddressInterface::LASTNAME => $address->getLastname(),
  130. AddressInterface::COUNTRY_ID => $address->getCountryId(),
  131. AddressInterface::STREET => $address->getStreet(),
  132. AddressInterface::POSTCODE => $address->getPostcode(),
  133. AddressInterface::COMPANY => $address->getCompany(),
  134. AddressInterface::TELEPHONE => null,
  135. AddressInterface::REGION => null,
  136. AddressInterface::REGION_ID => null,
  137. 'region_code' => null
  138. ];
  139. if ($address->getTelephone()) {
  140. $data[AddressInterface::TELEPHONE] = $address->getTelephone();
  141. }
  142. if ($address->getRegion()) {
  143. $data[AddressInterface::REGION] = $address->getRegion()->getRegion();
  144. if ($address->getRegion()->getRegionId()) {
  145. $data[AddressInterface::REGION_ID] = $address->getRegion()->getRegionId();
  146. $data['region_code'] = $address->getRegion()->getRegionCode();
  147. }
  148. }
  149. return $data;
  150. }
  151. }