5

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

13
+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.

  • is it possible to set custom transaction id in invoice capture – blakcaps Sep 9 '16 at 9:03
  • @blakcaps see my update – Raphael at Digital Pianism Sep 9 '16 at 9:10
  • Good It helped me – PMB Oct 7 '17 at 11:49
  • can we capture the payment after generate the shipment instead of invoice? – Sunny Rahevar May 22 '18 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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