/** * @param InfoInterface $payment * @param string $amount * @return $this * @throws LocalizedException */ protected function partialCapture($payment, $amount) { $collection = $this->salesTransactionCollectionFactory->create()->addPaymentIdFilter($payment->getId())->addTxnTypeFilter(PaymentTransaction::TYPE_AUTH)->setOrder('created_at', \Magento\Framework\Data\Collection::SORT_ORDER_DESC)->setOrder('transaction_id', \Magento\Framework\Data\Collection::SORT_ORDER_DESC)->setPageSize(1)->setCurPage(1); $authTransaction = $collection->getFirstItem(); if (!$authTransaction->getId()) { throw new LocalizedException(__('Can not find original authorization transaction for partial capture')); } if ($token = $authTransaction->getAdditionalInformation('token')) { //order was placed using saved card or card was saved during checkout token $found = true; try { $this->braintreeCreditCard->find($token); } catch (\Exception $e) { $found = false; } if ($found) { $this->config->initEnvironment($payment->getOrder()->getStoreId()); $this->braintreeAuthorize($payment, $amount, true, $token); } else { // case if payment token is no more applicable. attempt to clone transaction $result = $this->cloneTransaction($amount, $authTransaction->getTxnId()); if ($result && $result->success) { $this->processSuccessResult($payment, $result, $amount); } else { throw new LocalizedException($this->errorHelper->parseBraintreeError($result)); } } } else { // order was placed without saved card and card wasn't saved during checkout $result = $this->cloneTransaction($amount, $authTransaction->getTxnId()); if ($result->success) { $this->processSuccessResult($payment, $result, $amount); } else { throw new LocalizedException($this->errorHelper->parseBraintreeError($result)); } } return $this; }