LastCreatedPaymentTokenChooser.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\InstantPurchase\Model\PaymentMethodChoose;
  7. use Magento\Customer\Model\Customer;
  8. use Magento\Framework\Api\SearchCriteriaBuilder;
  9. use Magento\Framework\Api\SearchCriteriaInterface;
  10. use Magento\Framework\Api\SortOrderBuilder;
  11. use Magento\Framework\Intl\DateTimeFactory;
  12. use Magento\InstantPurchase\PaymentMethodIntegration\Integration;
  13. use Magento\InstantPurchase\PaymentMethodIntegration\IntegrationsManager;
  14. use Magento\Store\Model\Store;
  15. use Magento\Vault\Api\Data\PaymentTokenInterface;
  16. use Magento\Vault\Api\PaymentTokenRepositoryInterface;
  17. /**
  18. * Payment token chooser to select most recent token.
  19. *
  20. * Current im
  21. */
  22. class LastCreatedPaymentTokenChooser implements PaymentTokenChooserInterface
  23. {
  24. /**
  25. * @var PaymentTokenRepositoryInterface
  26. */
  27. private $paymentTokenRepository;
  28. /**
  29. * @var IntegrationsManager
  30. */
  31. private $integrationsManager;
  32. /**
  33. * @var SortOrderBuilder
  34. */
  35. private $sortOrderBuilder;
  36. /**
  37. * @var SearchCriteriaBuilder
  38. */
  39. private $searchCriteriaBuilder;
  40. /**
  41. * @var DateTimeFactory
  42. */
  43. private $dateTimeFactory;
  44. /**
  45. * LastCreatedPaymentTokenChooser constructor.
  46. * @param PaymentTokenRepositoryInterface $paymentTokenRepository
  47. * @param IntegrationsManager $integrationsManager
  48. * @param SortOrderBuilder $sortOrderBuilder
  49. * @param SearchCriteriaBuilder $searchCriteriaBuilder
  50. * @param DateTimeFactory $dateTimeFactory]
  51. */
  52. public function __construct(
  53. PaymentTokenRepositoryInterface $paymentTokenRepository,
  54. IntegrationsManager $integrationsManager,
  55. SortOrderBuilder $sortOrderBuilder,
  56. SearchCriteriaBuilder $searchCriteriaBuilder,
  57. DateTimeFactory $dateTimeFactory
  58. ) {
  59. $this->paymentTokenRepository = $paymentTokenRepository;
  60. $this->integrationsManager = $integrationsManager;
  61. $this->sortOrderBuilder = $sortOrderBuilder;
  62. $this->searchCriteriaBuilder = $searchCriteriaBuilder;
  63. $this->dateTimeFactory = $dateTimeFactory;
  64. }
  65. /**
  66. * @inheritdoc
  67. */
  68. public function choose(Store $store, Customer $customer)
  69. {
  70. $searchCriteria = $this->buildSearchCriteria($store->getId(), $customer->getId());
  71. $searchResult = $this->paymentTokenRepository->getList($searchCriteria);
  72. $tokens = $searchResult->getItems();
  73. $lastCreatedToken = array_shift($tokens);
  74. return $lastCreatedToken;
  75. }
  76. /**
  77. * Builds search criteria to find available payment tokens
  78. *
  79. * @param int $storeId
  80. * @param int $customerId
  81. * @return SearchCriteriaInterface
  82. */
  83. private function buildSearchCriteria(int $storeId, int $customerId): SearchCriteriaInterface
  84. {
  85. $this->searchCriteriaBuilder->addFilter(
  86. PaymentTokenInterface::CUSTOMER_ID,
  87. $customerId
  88. );
  89. $this->searchCriteriaBuilder->addFilter(
  90. PaymentTokenInterface::IS_VISIBLE,
  91. 1
  92. );
  93. $this->searchCriteriaBuilder->addFilter(
  94. PaymentTokenInterface::IS_ACTIVE,
  95. 1
  96. );
  97. $this->searchCriteriaBuilder->addFilter(
  98. PaymentTokenInterface::PAYMENT_METHOD_CODE,
  99. $this->getSupportedPaymentMethodCodes($storeId),
  100. 'in'
  101. );
  102. $this->searchCriteriaBuilder->addFilter(
  103. PaymentTokenInterface::EXPIRES_AT,
  104. $this->dateTimeFactory->create('now', new \DateTimeZone('UTC'))
  105. ->format('Y-m-d 00:00:00'),
  106. 'gt'
  107. );
  108. $creationReverseOrder = $this->sortOrderBuilder->setField(PaymentTokenInterface::CREATED_AT)
  109. ->setDescendingDirection()
  110. ->create();
  111. $this->searchCriteriaBuilder->addSortOrder($creationReverseOrder);
  112. $this->searchCriteriaBuilder->setPageSize(1);
  113. $searchCriteria = $this->searchCriteriaBuilder->create();
  114. return $searchCriteria;
  115. }
  116. /**
  117. * Lists supported payment method codes.
  118. *
  119. * @param int $storeId
  120. * @return array
  121. */
  122. private function getSupportedPaymentMethodCodes(int $storeId)
  123. {
  124. $integrations = $this->integrationsManager->getList($storeId);
  125. $integrations = array_filter($integrations, function (Integration $integration) {
  126. return $integration->isAvailable();
  127. });
  128. $paymentMethodCodes = array_map(function (Integration $integration) {
  129. return $integration->getVaultProviderCode();
  130. }, $integrations);
  131. return $paymentMethodCodes;
  132. }
  133. }