InstantPurchaseOption.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\InstantPurchase\Model;
  7. use Magento\Customer\Model\Address;
  8. use Magento\Framework\Exception\LocalizedException;
  9. use Magento\Quote\Api\Data\ShippingMethodInterface;
  10. use Magento\Vault\Api\Data\PaymentTokenInterface;
  11. use InvalidArgumentException;
  12. /**
  13. * Option to make instant purchase.
  14. *
  15. * @api
  16. * @since 100.2.0
  17. */
  18. class InstantPurchaseOption
  19. {
  20. /**
  21. * @var PaymentTokenInterface|null
  22. */
  23. private $paymentToken;
  24. /**
  25. * @var Address|null
  26. */
  27. private $shippingAddress;
  28. /**
  29. * @var Address|null
  30. */
  31. private $billingAddress;
  32. /**
  33. * @var ShippingMethodInterface|null
  34. */
  35. private $shippingMethod;
  36. /**
  37. * InstantPurchaseOption constructor.
  38. * @param PaymentTokenInterface|null $paymentToken
  39. * @param Address|null $shippingAddress
  40. * @param Address|null $billingAddress
  41. * @param ShippingMethodInterface|null $shippingMethod
  42. * @throws InvalidArgumentException if invalid data provided (implementation error)
  43. *
  44. * @SuppressWarnings(Magento.TypeDuplication)
  45. * Type duplication verified.
  46. * This is not a service class and should not be instantiated directly through Object Manager.
  47. * Use InstantPurchaseOptionFactory instead.
  48. */
  49. public function __construct(
  50. PaymentTokenInterface $paymentToken = null,
  51. Address $shippingAddress = null,
  52. Address $billingAddress = null,
  53. ShippingMethodInterface $shippingMethod = null
  54. ) {
  55. $customers = [];
  56. if ($paymentToken) {
  57. $customers[] = $paymentToken->getCustomerId();
  58. }
  59. if ($shippingAddress) {
  60. $customers[] = $shippingAddress->getCustomerId();
  61. }
  62. if ($billingAddress) {
  63. $customers[] = $billingAddress->getCustomerId();
  64. }
  65. if (count(array_unique($customers)) > 1) {
  66. throw new InvalidArgumentException('Provided data does not belong to same customer.');
  67. }
  68. $this->paymentToken = $paymentToken;
  69. $this->shippingAddress = $shippingAddress;
  70. $this->billingAddress = $billingAddress;
  71. $this->shippingMethod = $shippingMethod;
  72. }
  73. /**
  74. * Checks if option available
  75. *
  76. * @return bool
  77. * @since 100.2.0
  78. */
  79. public function isAvailable(): bool
  80. {
  81. return isset(
  82. $this->paymentToken,
  83. $this->shippingAddress,
  84. $this->billingAddress,
  85. $this->shippingMethod
  86. ) && $this->shippingMethod->getAvailable();
  87. }
  88. /**
  89. * Returns payment token for instant purchase.
  90. *
  91. * @return PaymentTokenInterface
  92. * @throws LocalizedException if payment token is not defined
  93. * @since 100.2.0
  94. */
  95. public function getPaymentToken(): PaymentTokenInterface
  96. {
  97. if (!isset($this->paymentToken)) {
  98. throw new LocalizedException(
  99. __("A payment method isn't defined for instance purchase. Verify and try again.")
  100. );
  101. }
  102. return $this->paymentToken;
  103. }
  104. /**
  105. * Returns shipping address for instant purchase.
  106. *
  107. * @return Address
  108. * @throws LocalizedException if shipping address is not defined
  109. * @since 100.2.0
  110. */
  111. public function getShippingAddress(): Address
  112. {
  113. if (!isset($this->shippingAddress)) {
  114. throw new LocalizedException(__('Shipping address is not defined for instance purchase.'));
  115. }
  116. return $this->shippingAddress;
  117. }
  118. /**
  119. * Returns billing address for instant purchase.
  120. *
  121. * @return Address
  122. * @throws LocalizedException if billing address is not defined
  123. * @since 100.2.0
  124. */
  125. public function getBillingAddress(): Address
  126. {
  127. if (!isset($this->billingAddress)) {
  128. throw new LocalizedException(__('Billing address is not defined for instance purchase.'));
  129. }
  130. return $this->billingAddress;
  131. }
  132. /**
  133. * Returns shipping method for instant purchase.
  134. *
  135. * @return ShippingMethodInterface
  136. * @throws LocalizedException if shipping method is not defined
  137. * @since 100.2.0
  138. */
  139. public function getShippingMethod(): ShippingMethodInterface
  140. {
  141. if (!isset($this->shippingMethod)) {
  142. throw new LocalizedException(__('Shipping method is not defined for instance purchase.'));
  143. }
  144. return $this->shippingMethod;
  145. }
  146. }