Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CaptureOperation.php
Go to the documentation of this file.
1 <?php
7 
13 
15 {
25  {
29  if (null === $invoice) {
30  $invoice = $this->invoice($payment);
31  $payment->setCreatedInvoice($invoice);
32  if ($payment->getIsFraudDetected()) {
33  $payment->getOrder()->setStatus(Order::STATUS_FRAUD);
34  }
35  return $payment;
36  }
37  $amountToCapture = $payment->formatAmount($invoice->getBaseGrandTotal());
38  $order = $payment->getOrder();
39 
40  $payment->setTransactionId(
41  $this->transactionManager->generateTransactionId(
42  $payment,
44  $payment->getAuthorizationTransaction()
45  )
46  );
47 
48  $this->eventManager->dispatch(
49  'sales_order_payment_capture',
50  ['payment' => $payment, 'invoice' => $invoice]
51  );
52 
57  if ($invoice->getTransactionId()) {
58  $method = $payment->getMethodInstance();
59  $method->setStore(
60  $order->getStoreId()
61  );
62  if ($method->canFetchTransactionInfo()) {
63  $method->fetchTransactionInfo(
64  $payment,
65  $invoice->getTransactionId()
66  );
67  }
68  }
69 
70  if ($invoice->getIsPaid()) {
71  throw new \Magento\Framework\Exception\LocalizedException(
72  __('The transaction "%1" cannot be captured yet.', $invoice->getTransactionId())
73  );
74  }
75 
76  // attempt to capture: this can trigger "is_transaction_pending"
77  $method = $payment->getMethodInstance();
78  $method->setStore(
79  $order->getStoreId()
80  );
81  //TODO replace for sale usage
82  $method->capture($payment, $amountToCapture);
83 
84  // prepare parent transaction and its amount
85  $paidWorkaround = 0;
86  if (!$invoice->wasPayCalled()) {
87  $paidWorkaround = (double)$amountToCapture;
88  }
89  if ($payment->isCaptureFinal($paidWorkaround)) {
90  $payment->setShouldCloseParentTransaction(true);
91  }
92 
93  $transactionBuilder = $this->transactionBuilder->setPayment($payment);
94  $transactionBuilder->setOrder($order);
95  $transactionBuilder->setFailSafe(true);
96  $transactionBuilder->setTransactionId($payment->getTransactionId());
97  $transactionBuilder->setAdditionalInformation($payment->getTransactionAdditionalInfo());
98  $transactionBuilder->setSalesDocument($invoice);
100 
101  $message = $this->stateCommand->execute($payment, $amountToCapture, $order);
102  if ($payment->getIsTransactionPending()) {
103  $invoice->setIsPaid(false);
104  } else {
105  $invoice->setIsPaid(true);
106  $this->updateTotals($payment, ['base_amount_paid_online' => $amountToCapture]);
107  }
108  $message = $payment->prependMessage($message);
109  $payment->addTransactionCommentsToOrder($transaction, $message);
110  $invoice->setTransactionId($payment->getLastTransId());
111 
112  return $payment;
113  }
114 }
$transaction
$order
Definition: order.php:55
__()
Definition: __.php:13
$message
$payment
Definition: order.php:17
$invoice
$method
Definition: info.phtml:13