Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CaptureStrategyCommand.php
Go to the documentation of this file.
1 <?php
7 
8 use Braintree\Transaction;
22 
28 {
32  const SALE = 'sale';
33 
37  const CAPTURE = 'settlement';
38 
42  const VAULT_CAPTURE = 'vault_capture';
43 
47  private $commandPool;
48 
52  private $transactionRepository;
53 
57  private $filterBuilder;
58 
62  private $searchCriteriaBuilder;
63 
67  private $subjectReader;
68 
72  private $braintreeAdapterFactory;
73 
77  private $braintreeSearchAdapter;
78 
90  public function __construct(
91  CommandPoolInterface $commandPool,
93  FilterBuilder $filterBuilder,
94  SearchCriteriaBuilder $searchCriteriaBuilder,
95  SubjectReader $subjectReader,
96  BraintreeAdapterFactory $braintreeAdapterFactory,
97  BraintreeSearchAdapter $braintreeSearchAdapter
98  ) {
99  $this->commandPool = $commandPool;
100  $this->transactionRepository = $repository;
101  $this->filterBuilder = $filterBuilder;
102  $this->searchCriteriaBuilder = $searchCriteriaBuilder;
103  $this->subjectReader = $subjectReader;
104  $this->braintreeAdapterFactory = $braintreeAdapterFactory;
105  $this->braintreeSearchAdapter = $braintreeSearchAdapter;
106  }
107 
111  public function execute(array $commandSubject)
112  {
114  $paymentDO = $this->subjectReader->readPayment($commandSubject);
115 
116  $command = $this->getCommand($paymentDO);
117  $this->commandPool->get($command)->execute($commandSubject);
118  }
119 
126  private function getCommand(PaymentDataObjectInterface $paymentDO)
127  {
128  $payment = $paymentDO->getPayment();
130 
131  // if auth transaction does not exist then execute authorize&capture command
132  $existsCapture = $this->isExistsCaptureTransaction($payment);
133  if (!$payment->getAuthorizationTransaction() && !$existsCapture) {
134  return self::SALE;
135  }
136 
137  // do capture for authorization transaction
138  if (!$existsCapture && !$this->isExpiredAuthorization($payment, $paymentDO->getOrder())) {
139  return self::CAPTURE;
140  }
141 
142  // process capture for payment via Vault
143  return self::VAULT_CAPTURE;
144  }
145 
153  private function isExpiredAuthorization(OrderPaymentInterface $payment, OrderAdapterInterface $orderAdapter)
154  {
155  $adapter = $this->braintreeAdapterFactory->create($orderAdapter->getStoreId());
156  $collection = $adapter->search(
157  [
158  $this->braintreeSearchAdapter->id()->is($payment->getLastTransId()),
159  $this->braintreeSearchAdapter->status()->is(Transaction::AUTHORIZATION_EXPIRED)
160  ]
161  );
162 
163  return $collection->maximumCount() > 0;
164  }
165 
172  private function isExistsCaptureTransaction(OrderPaymentInterface $payment)
173  {
174  $this->searchCriteriaBuilder->addFilters(
175  [
176  $this->filterBuilder
177  ->setField('payment_id')
178  ->setValue($payment->getId())
179  ->create(),
180  ]
181  );
182 
183  $this->searchCriteriaBuilder->addFilters(
184  [
185  $this->filterBuilder
186  ->setField('txn_type')
188  ->create(),
189  ]
190  );
191 
192  $searchCriteria = $this->searchCriteriaBuilder->create();
193 
194  $count = $this->transactionRepository->getList($searchCriteria)->getTotalCount();
195  return (boolean) $count;
196  }
197 }
__construct(CommandPoolInterface $commandPool, TransactionRepositoryInterface $repository, FilterBuilder $filterBuilder, SearchCriteriaBuilder $searchCriteriaBuilder, SubjectReader $subjectReader, BraintreeAdapterFactory $braintreeAdapterFactory, BraintreeSearchAdapter $braintreeSearchAdapter)
$count
Definition: recent.phtml:13
$adapter
Definition: webapi_user.php:16
$searchCriteria
$payment
Definition: order.php:17
$searchCriteriaBuilder
static assertOrderPayment(InfoInterface $paymentInfo)