InstantPurchase.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\InstantPurchase\CustomerData;
  7. use Magento\Customer\CustomerData\SectionSourceInterface;
  8. use Magento\Customer\Model\Session;
  9. use Magento\InstantPurchase\Model\InstantPurchaseInterface as InstantPurchaseModel;
  10. use Magento\InstantPurchase\Model\Ui\CustomerAddressesFormatter;
  11. use Magento\InstantPurchase\Model\Ui\PaymentTokenFormatter;
  12. use Magento\InstantPurchase\Model\Ui\ShippingMethodFormatter;
  13. use Magento\Store\Model\StoreManagerInterface;
  14. /**
  15. * Instant Purchase private customer data source.
  16. *
  17. * Contains all required data to perform instance purchase:
  18. * - payment method
  19. * - shipping address
  20. * - billing address
  21. * - shipping method
  22. */
  23. class InstantPurchase implements SectionSourceInterface
  24. {
  25. /**
  26. * @var Session
  27. */
  28. private $customerSession;
  29. /**
  30. * @var StoreManagerInterface
  31. */
  32. private $storeManager;
  33. /**
  34. * @var InstantPurchaseModel
  35. */
  36. private $instantPurchase;
  37. /**
  38. * @var PaymentTokenFormatter
  39. */
  40. private $paymentTokenFormatter;
  41. /**
  42. * @var CustomerAddressesFormatter
  43. */
  44. private $customerAddressesFormatter;
  45. /**
  46. * @var ShippingMethodFormatter
  47. */
  48. private $shippingMethodFormatter;
  49. /**
  50. * InstantPurchase constructor.
  51. * @param Session $customerSession
  52. * @param StoreManagerInterface $storeManager
  53. * @param InstantPurchaseModel $instantPurchase
  54. * @param PaymentTokenFormatter $paymentTokenFormatter
  55. * @param CustomerAddressesFormatter $customerAddressesFormatter
  56. * @param ShippingMethodFormatter $shippingMethodFormatter
  57. */
  58. public function __construct(
  59. Session $customerSession,
  60. StoreManagerInterface $storeManager,
  61. InstantPurchaseModel $instantPurchase,
  62. PaymentTokenFormatter $paymentTokenFormatter,
  63. CustomerAddressesFormatter $customerAddressesFormatter,
  64. ShippingMethodFormatter $shippingMethodFormatter
  65. ) {
  66. $this->customerSession = $customerSession;
  67. $this->storeManager = $storeManager;
  68. $this->instantPurchase = $instantPurchase;
  69. $this->paymentTokenFormatter = $paymentTokenFormatter;
  70. $this->customerAddressesFormatter = $customerAddressesFormatter;
  71. $this->shippingMethodFormatter = $shippingMethodFormatter;
  72. }
  73. /**
  74. * @inheritdoc
  75. */
  76. public function getSectionData(): array
  77. {
  78. if (!$this->customerSession->isLoggedIn()) {
  79. return ['available' => false];
  80. }
  81. $store = $this->storeManager->getStore();
  82. $customer = $this->customerSession->getCustomer();
  83. $instantPurchaseOption = $this->instantPurchase->getOption($store, $customer);
  84. $data = [
  85. 'available' => $instantPurchaseOption->isAvailable()
  86. ];
  87. if (!$instantPurchaseOption->isAvailable()) {
  88. return $data;
  89. }
  90. $paymentToken = $instantPurchaseOption->getPaymentToken();
  91. $shippingAddress = $instantPurchaseOption->getShippingAddress();
  92. $billingAddress = $instantPurchaseOption->getBillingAddress();
  93. $shippingMethod = $instantPurchaseOption->getShippingMethod();
  94. $data += [
  95. 'paymentToken' => [
  96. 'publicHash' => $paymentToken->getPublicHash(),
  97. 'summary' => $this->paymentTokenFormatter->format($paymentToken),
  98. ],
  99. 'shippingAddress' => [
  100. 'id' => $shippingAddress->getId(),
  101. 'summary' => $this->customerAddressesFormatter->format($shippingAddress),
  102. ],
  103. 'billingAddress' => [
  104. 'id' => $billingAddress->getId(),
  105. 'summary' => $this->customerAddressesFormatter->format($billingAddress),
  106. ],
  107. 'shippingMethod' => [
  108. 'carrier' => $shippingMethod->getCarrierCode(),
  109. 'method' => $shippingMethod->getMethodCode(),
  110. 'summary' => $this->shippingMethodFormatter->format($shippingMethod),
  111. ]
  112. ];
  113. return $data;
  114. }
  115. }