9

How to capture payment for the created invoice. In Magento1.x it is like

$invoice = Mage::getModel('sales/Service_Order', $this->getOrder())->prepareInvoice();
$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
$invoice->register();

I have the order increment id which has order status pending. Invoice needs to be generated for that order by loading increment id and do invoice capture operation. I am expecting the invoice capture operation will set transaction id(Ex. 2121) and move the order status to completed

1

1 Answer 1

19
+50

It's pretty similar to M1 in Magento 2, you need to inject the Magento\Sales\Model\Service\InvoiceService in your class:

protected $_invoiceService;

public function __construct(
    ...
    \Magento\Sales\Model\Service\InvoiceService $invoiceService
    ...
) {
    ...
    $this->_invoiceService = $invoiceService;
    ...
}

Then, assuming you have the order you can do:

$invoice = $this->_invoiceService->prepareInvoice($order);
$invoice->setRequestedCaptureCase(\Magento\Sales\Model\Order\Invoice::CAPTURE_ONLINE);
$invoice->register();

To set a transaction ID on an invoice you can simply call setTransactionId() method on the invoice object.

7
  • is it possible to set custom transaction id in invoice capture
    – blakcaps
    Commented Sep 9, 2016 at 9:03
  • @blakcaps see my update Commented Sep 9, 2016 at 9:10
  • Good It helped me Commented Oct 7, 2017 at 11:49
  • can we capture the payment after generate the shipment instead of invoice? Commented May 22, 2018 at 9:58
  • i tried this but shipping fees are not included in the paid amount. any advice ?
    – Zorox
    Commented Mar 16, 2020 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.