AddressDataBuilder.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Braintree\Gateway\Request;
  7. use Magento\Payment\Gateway\Request\BuilderInterface;
  8. use Magento\Braintree\Gateway\SubjectReader;
  9. /**
  10. * Class AddressDataBuilder
  11. */
  12. class AddressDataBuilder implements BuilderInterface
  13. {
  14. /**
  15. * ShippingAddress block name
  16. */
  17. const SHIPPING_ADDRESS = 'shipping';
  18. /**
  19. * BillingAddress block name
  20. */
  21. const BILLING_ADDRESS = 'billing';
  22. /**
  23. * The first name value must be less than or equal to 255 characters.
  24. */
  25. const FIRST_NAME = 'firstName';
  26. /**
  27. * The last name value must be less than or equal to 255 characters.
  28. */
  29. const LAST_NAME = 'lastName';
  30. /**
  31. * The customer’s company. 255 character maximum.
  32. */
  33. const COMPANY = 'company';
  34. /**
  35. * The street address. Maximum 255 characters, and must contain at least 1 digit.
  36. * Required when AVS rules are configured to require street address.
  37. */
  38. const STREET_ADDRESS = 'streetAddress';
  39. /**
  40. * The extended address information—such as apartment or suite number. 255 character maximum.
  41. */
  42. const EXTENDED_ADDRESS = 'extendedAddress';
  43. /**
  44. * The locality/city. 255 character maximum.
  45. */
  46. const LOCALITY = 'locality';
  47. /**
  48. * The state or province. For PayPal addresses, the region must be a 2-letter abbreviation;
  49. * for all other payment methods, it must be less than or equal to 255 characters.
  50. */
  51. const REGION = 'region';
  52. /**
  53. * The postal code. Postal code must be a string of 5 or 9 alphanumeric digits,
  54. * optionally separated by a dash or a space. Spaces, hyphens,
  55. * and all other special characters are ignored.
  56. */
  57. const POSTAL_CODE = 'postalCode';
  58. /**
  59. * The ISO 3166-1 alpha-2 country code specified in an address.
  60. * The gateway only accepts specific alpha-2 values.
  61. *
  62. * @link https://developers.braintreepayments.com/reference/general/countries/php#list-of-countries
  63. */
  64. const COUNTRY_CODE = 'countryCodeAlpha2';
  65. /**
  66. * @var SubjectReader
  67. */
  68. private $subjectReader;
  69. /**
  70. * Constructor
  71. *
  72. * @param SubjectReader $subjectReader
  73. */
  74. public function __construct(SubjectReader $subjectReader)
  75. {
  76. $this->subjectReader = $subjectReader;
  77. }
  78. /**
  79. * @inheritdoc
  80. */
  81. public function build(array $buildSubject)
  82. {
  83. $paymentDO = $this->subjectReader->readPayment($buildSubject);
  84. $order = $paymentDO->getOrder();
  85. $result = [];
  86. $billingAddress = $order->getBillingAddress();
  87. if ($billingAddress) {
  88. $result[self::BILLING_ADDRESS] = [
  89. self::FIRST_NAME => $billingAddress->getFirstname(),
  90. self::LAST_NAME => $billingAddress->getLastname(),
  91. self::COMPANY => $billingAddress->getCompany(),
  92. self::STREET_ADDRESS => $billingAddress->getStreetLine1(),
  93. self::EXTENDED_ADDRESS => $billingAddress->getStreetLine2(),
  94. self::LOCALITY => $billingAddress->getCity(),
  95. self::REGION => $billingAddress->getRegionCode(),
  96. self::POSTAL_CODE => $billingAddress->getPostcode(),
  97. self::COUNTRY_CODE => $billingAddress->getCountryId()
  98. ];
  99. }
  100. $shippingAddress = $order->getShippingAddress();
  101. if ($shippingAddress) {
  102. $result[self::SHIPPING_ADDRESS] = [
  103. self::FIRST_NAME => $shippingAddress->getFirstname(),
  104. self::LAST_NAME => $shippingAddress->getLastname(),
  105. self::COMPANY => $shippingAddress->getCompany(),
  106. self::STREET_ADDRESS => $shippingAddress->getStreetLine1(),
  107. self::EXTENDED_ADDRESS => $shippingAddress->getStreetLine2(),
  108. self::LOCALITY => $shippingAddress->getCity(),
  109. self::REGION => $shippingAddress->getRegionCode(),
  110. self::POSTAL_CODE => $shippingAddress->getPostcode(),
  111. self::COUNTRY_CODE => $shippingAddress->getCountryId()
  112. ];
  113. }
  114. return $result;
  115. }
  116. }