InstantPurchaseChooser.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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\Customer;
  8. use Magento\InstantPurchase\Model\PaymentMethodChoose\PaymentTokenChooserInterface;
  9. use Magento\InstantPurchase\Model\ShippingAddressChoose\ShippingAddressChooserInterface;
  10. use Magento\InstantPurchase\Model\BillingAddressChoose\BillingAddressChooserInterface;
  11. use Magento\InstantPurchase\Model\ShippingMethodChoose\ShippingMethodChooserInterface;
  12. use Magento\Store\Model\Store;
  13. /**
  14. * Choose instant purchase option programmatically based on configured implementation.
  15. *
  16. * Provide implementations of injected chooser interfaces to customize behavior.
  17. */
  18. class InstantPurchaseChooser implements InstantPurchaseInterface
  19. {
  20. /**
  21. * @var InstantPurchaseOptionFactory
  22. */
  23. private $instantPurchaseOptionFactory;
  24. /**
  25. * @var Config
  26. */
  27. private $config;
  28. /**
  29. * @var PaymentTokenChooserInterface
  30. */
  31. private $paymentTokenChooser;
  32. /**
  33. * @var ShippingAddressChooserInterface
  34. */
  35. private $shippingAddressChooser;
  36. /**
  37. * @var BillingAddressChooserInterface
  38. */
  39. private $billingAddressChooser;
  40. /**
  41. * @var ShippingMethodChooserInterface
  42. */
  43. private $shippingMethodChooser;
  44. /**
  45. * InstantPurchase constructor.
  46. * @param InstantPurchaseOptionFactory $instantPurchaseOptionFactory
  47. * @param Config $config
  48. * @param PaymentTokenChooserInterface $paymentTokenChooser
  49. * @param ShippingAddressChooserInterface $shippingAddressChooser
  50. * @param BillingAddressChooserInterface $billingAddressChooser
  51. * @param ShippingMethodChooserInterface $shippingMethodChooser
  52. */
  53. public function __construct(
  54. InstantPurchaseOptionFactory $instantPurchaseOptionFactory,
  55. Config $config,
  56. PaymentTokenChooserInterface $paymentTokenChooser,
  57. ShippingAddressChooserInterface $shippingAddressChooser,
  58. BillingAddressChooserInterface $billingAddressChooser,
  59. ShippingMethodChooserInterface $shippingMethodChooser
  60. ) {
  61. $this->instantPurchaseOptionFactory = $instantPurchaseOptionFactory;
  62. $this->config = $config;
  63. $this->paymentTokenChooser = $paymentTokenChooser;
  64. $this->shippingAddressChooser = $shippingAddressChooser;
  65. $this->billingAddressChooser = $billingAddressChooser;
  66. $this->shippingMethodChooser = $shippingMethodChooser;
  67. }
  68. /**
  69. * @inheritdoc
  70. */
  71. public function getOption(
  72. Store $store,
  73. Customer $customer
  74. ): InstantPurchaseOption {
  75. if (!$this->isInstantPurchaseButtonEnabled($store)) {
  76. return $this->instantPurchaseOptionFactory->createDisabledOption();
  77. }
  78. $paymentToken = $this->paymentTokenChooser->choose($store, $customer);
  79. $shippingAddress = $this->shippingAddressChooser->choose($customer);
  80. $billingAddress = $this->billingAddressChooser->choose($customer);
  81. if ($shippingAddress) {
  82. $shippingMethod = $this->shippingMethodChooser->choose($shippingAddress);
  83. } else {
  84. $shippingMethod = null;
  85. }
  86. $option = $this->instantPurchaseOptionFactory->create(
  87. $paymentToken,
  88. $shippingAddress,
  89. $billingAddress,
  90. $shippingMethod
  91. );
  92. return $option;
  93. }
  94. /**
  95. * Checks if button available.
  96. *
  97. * @param Store $store
  98. * @return bool
  99. */
  100. private function isInstantPurchaseButtonEnabled(Store $store): bool
  101. {
  102. return $this->config->isModuleEnabled($store->getId());
  103. }
  104. }