CaptureStrategyCommand.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Braintree\Gateway\Command;
  7. use Braintree\Transaction;
  8. use Magento\Braintree\Gateway\SubjectReader;
  9. use Magento\Braintree\Model\Adapter\BraintreeAdapterFactory;
  10. use Magento\Braintree\Model\Adapter\BraintreeSearchAdapter;
  11. use Magento\Framework\Api\FilterBuilder;
  12. use Magento\Framework\Api\SearchCriteriaBuilder;
  13. use Magento\Payment\Gateway\Command\CommandPoolInterface;
  14. use Magento\Payment\Gateway\CommandInterface;
  15. use Magento\Payment\Gateway\Data\OrderAdapterInterface;
  16. use Magento\Payment\Gateway\Helper\ContextHelper;
  17. use Magento\Sales\Api\Data\OrderPaymentInterface;
  18. use Magento\Sales\Api\Data\TransactionInterface;
  19. use Magento\Sales\Api\TransactionRepositoryInterface;
  20. use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
  21. /**
  22. * Class CaptureStrategyCommand
  23. * @SuppressWarnings(PHPMD)
  24. */
  25. class CaptureStrategyCommand implements CommandInterface
  26. {
  27. /**
  28. * Braintree authorize and capture command
  29. */
  30. const SALE = 'sale';
  31. /**
  32. * Braintree capture command
  33. */
  34. const CAPTURE = 'settlement';
  35. /**
  36. * Braintree vault capture command
  37. */
  38. const VAULT_CAPTURE = 'vault_capture';
  39. /**
  40. * @var CommandPoolInterface
  41. */
  42. private $commandPool;
  43. /**
  44. * @var TransactionRepositoryInterface
  45. */
  46. private $transactionRepository;
  47. /**
  48. * @var FilterBuilder
  49. */
  50. private $filterBuilder;
  51. /**
  52. * @var SearchCriteriaBuilder
  53. */
  54. private $searchCriteriaBuilder;
  55. /**
  56. * @var SubjectReader
  57. */
  58. private $subjectReader;
  59. /**
  60. * @var BraintreeAdapterFactory
  61. */
  62. private $braintreeAdapterFactory;
  63. /**
  64. * @var BraintreeSearchAdapter
  65. */
  66. private $braintreeSearchAdapter;
  67. /**
  68. * Constructor
  69. *
  70. * @param CommandPoolInterface $commandPool
  71. * @param TransactionRepositoryInterface $repository
  72. * @param FilterBuilder $filterBuilder
  73. * @param SearchCriteriaBuilder $searchCriteriaBuilder
  74. * @param SubjectReader $subjectReader
  75. * @param BraintreeAdapterFactory $braintreeAdapterFactory,
  76. * @param BraintreeSearchAdapter $braintreeSearchAdapter
  77. */
  78. public function __construct(
  79. CommandPoolInterface $commandPool,
  80. TransactionRepositoryInterface $repository,
  81. FilterBuilder $filterBuilder,
  82. SearchCriteriaBuilder $searchCriteriaBuilder,
  83. SubjectReader $subjectReader,
  84. BraintreeAdapterFactory $braintreeAdapterFactory,
  85. BraintreeSearchAdapter $braintreeSearchAdapter
  86. ) {
  87. $this->commandPool = $commandPool;
  88. $this->transactionRepository = $repository;
  89. $this->filterBuilder = $filterBuilder;
  90. $this->searchCriteriaBuilder = $searchCriteriaBuilder;
  91. $this->subjectReader = $subjectReader;
  92. $this->braintreeAdapterFactory = $braintreeAdapterFactory;
  93. $this->braintreeSearchAdapter = $braintreeSearchAdapter;
  94. }
  95. /**
  96. * @inheritdoc
  97. */
  98. public function execute(array $commandSubject)
  99. {
  100. /** @var \Magento\Payment\Gateway\Data\PaymentDataObjectInterface $paymentDO */
  101. $paymentDO = $this->subjectReader->readPayment($commandSubject);
  102. $command = $this->getCommand($paymentDO);
  103. $this->commandPool->get($command)->execute($commandSubject);
  104. }
  105. /**
  106. * Get execution command name.
  107. *
  108. * @param PaymentDataObjectInterface $paymentDO
  109. * @return string
  110. */
  111. private function getCommand(PaymentDataObjectInterface $paymentDO)
  112. {
  113. $payment = $paymentDO->getPayment();
  114. ContextHelper::assertOrderPayment($payment);
  115. // if auth transaction does not exist then execute authorize&capture command
  116. $existsCapture = $this->isExistsCaptureTransaction($payment);
  117. if (!$payment->getAuthorizationTransaction() && !$existsCapture) {
  118. return self::SALE;
  119. }
  120. // do capture for authorization transaction
  121. if (!$existsCapture && !$this->isExpiredAuthorization($payment, $paymentDO->getOrder())) {
  122. return self::CAPTURE;
  123. }
  124. // process capture for payment via Vault
  125. return self::VAULT_CAPTURE;
  126. }
  127. /**
  128. * Checks if authorization transaction does not expired yet.
  129. *
  130. * @param OrderPaymentInterface $payment
  131. * @param OrderAdapterInterface $orderAdapter
  132. * @return bool
  133. */
  134. private function isExpiredAuthorization(OrderPaymentInterface $payment, OrderAdapterInterface $orderAdapter)
  135. {
  136. $adapter = $this->braintreeAdapterFactory->create($orderAdapter->getStoreId());
  137. $collection = $adapter->search(
  138. [
  139. $this->braintreeSearchAdapter->id()->is($payment->getLastTransId()),
  140. $this->braintreeSearchAdapter->status()->is(Transaction::AUTHORIZATION_EXPIRED)
  141. ]
  142. );
  143. return $collection->maximumCount() > 0;
  144. }
  145. /**
  146. * Check if capture transaction already exists
  147. *
  148. * @param OrderPaymentInterface $payment
  149. * @return bool
  150. */
  151. private function isExistsCaptureTransaction(OrderPaymentInterface $payment)
  152. {
  153. $this->searchCriteriaBuilder->addFilters(
  154. [
  155. $this->filterBuilder
  156. ->setField('payment_id')
  157. ->setValue($payment->getId())
  158. ->create(),
  159. ]
  160. );
  161. $this->searchCriteriaBuilder->addFilters(
  162. [
  163. $this->filterBuilder
  164. ->setField('txn_type')
  165. ->setValue(TransactionInterface::TYPE_CAPTURE)
  166. ->create(),
  167. ]
  168. );
  169. $searchCriteria = $this->searchCriteriaBuilder->create();
  170. $count = $this->transactionRepository->getList($searchCriteria)->getTotalCount();
  171. return (boolean) $count;
  172. }
  173. }