How is \Magento\Sales\Model\Order\Payment\Operations\CaptureOperation::capture() implemented and used?

Context:

Implementation

ffea3cd/app/code/Magento/Sales/Model/Order/Payment/Operations/CaptureOperation.php#L16-L113

/**
 * Captures payment.
 *
 * @param OrderPaymentInterface $payment
 * @param InvoiceInterface|null $invoice
 * @return OrderPaymentInterface
 * @throws \Magento\Framework\Exception\LocalizedException
 */
public function capture(OrderPaymentInterface $payment, $invoice)
{
	/**
	 * @var $payment Payment
	 */
	if (null === $invoice) {
		$invoice = $this->invoice($payment);
		$payment->setCreatedInvoice($invoice);
		if ($payment->getIsFraudDetected()) {
			$payment->getOrder()->setStatus(Order::STATUS_FRAUD);
		}
		return $payment;
	}
	$amountToCapture = $payment->formatAmount($invoice->getBaseGrandTotal());
	$order = $payment->getOrder();
	$payment->setTransactionId(
		$this->transactionManager->generateTransactionId(
			$payment,
			Transaction::TYPE_CAPTURE,
			$payment->getAuthorizationTransaction()
		)
	);
	$this->eventManager->dispatch(
		'sales_order_payment_capture',
		['payment' => $payment, 'invoice' => $invoice]
	);
	/**
	 * Fetch an update about existing transaction. It can determine whether the transaction can be paid
	 * Capture attempt will happen only when invoice is not yet paid and the transaction can be paid
	 */
	if ($invoice->getTransactionId()) {
		$method = $payment->getMethodInstance();
		$method->setStore(
			$order->getStoreId()
		);
		if ($method->canFetchTransactionInfo()) {
			$method->fetchTransactionInfo(
				$payment,
				$invoice->getTransactionId()
			);
		}
	}
	if ($invoice->getIsPaid()) {
		throw new \Magento\Framework\Exception\LocalizedException(
			__('The transaction "%1" cannot be captured yet.', $invoice->getTransactionId())
		);
	}
	// attempt to capture: this can trigger "is_transaction_pending"
	$method = $payment->getMethodInstance();
	$method->setStore(
		$order->getStoreId()
	);
	//TODO replace for sale usage
	$method->capture($payment, $amountToCapture);
	// prepare parent transaction and its amount
	$paidWorkaround = 0;
	if (!$invoice->wasPayCalled()) {
		$paidWorkaround = (double)$amountToCapture;
	}
	if ($payment->isCaptureFinal($paidWorkaround)) {
		$payment->setShouldCloseParentTransaction(true);
	}
	$transactionBuilder = $this->transactionBuilder->setPayment($payment);
	$transactionBuilder->setOrder($order);
	$transactionBuilder->setFailSafe(true);
	$transactionBuilder->setTransactionId($payment->getTransactionId());
	$transactionBuilder->setAdditionalInformation($payment->getTransactionAdditionalInfo());
	$transactionBuilder->setSalesDocument($invoice);
	$transaction = $transactionBuilder->build(Transaction::TYPE_CAPTURE);
	$message = $this->stateCommand->execute($payment, $amountToCapture, $order);
	if ($payment->getIsTransactionPending()) {
		$invoice->setIsPaid(false);
	} else {
		$invoice->setIsPaid(true);
		$this->updateTotals($payment, ['base_amount_paid_online' => $amountToCapture]);
	}
	$message = $payment->prependMessage($message);
	$payment->addTransactionCommentsToOrder($transaction, $message);
	$invoice->setTransactionId($payment->getLastTransId());
	return $payment;
}

Details:

The single usage

\Magento\Sales\Model\Order\Payment\Processor::capture()

Details: How is \Magento\Sales\Model\Order\Payment\Processor::capture() implemented and used?

A post was split to a new topic: How to set the amount to trigger error ‘payment declined’?