Menu Close
    Start a Project Request quote Reading list Switch to dark mode

    How to programmatically create transaction in Magento2

    In this article we learn how to create transactions in Magento2 . A transaction is proof that payment has been done. In Magento transactions are created when invoice is created, although magento creates transaction by itself you don’t need to worry about that but in case you want to create an online payment method that requires you to redirect to different website for payment, and when it redirects back to the merchant website with the payment data, you will have to create the transaction manually, here is the code snippet that will create a transaction :

    In the above code, I have created a function that will accept two params,
    the first parameter is $order which is of type Magento\Sales\Model\Order,
    you need to load the order with order id, I suppose you know about this .

    And the second param $paymentData is assumed to be an array that is returned by the
    payment gateway after successful pament.

    We assume that the $paymmentData[‘id’] holds the id of the successful payment.
    $this->_transactionBuilder is of type :
    you don’t need to worry about the Interface magento2 already defines it in
    sales-module di.xml, preference class for the interface.

    When the build method is called on the builder class after setting the required
    params, it automatically creates the transaction object and saves the transaction
    within the order.

    Thats all in this, hope it will help you, try the above code and
    if you have any issue just comment below 🙂 .

    . . .

    Comments (9)

    Add Your Comment

  • Lucas (Brazil)
    Hi, i tryed implement your code, a transaction was created, but it is not appearing in the Magento order grid. In fact, magento creates a transaction record, but does not appear in the list of transactions. I don’t understand this behavior :-(. Thanks for the article, very usefull!
  • gaurav
    useful some how..
  • Ömür Yanıkoğlu
    It was very useful. Thank you 🙂
    • Webkul Support
      Your most welcome 🙂
  • Shailendra Kumar
    Hi ,

    How to create builder object.

    • Ashutosh Srivastava
      this is the builder class:
      you can instantiate this class using object manager or constructor injection
      • Shailendra Kumar
        Thank you.
  • shivraj
    How to set ParentTransactionId ? in sales_payment_transaction table ? while creating the transaction
    • Ashutosh Srivastava
      HI sorry for the late reply, if you are still searching the solution you can find it here, in the above code you just need to setParentTransactionId in the payment object like below :
      //get payment object from order object
      $payment = $order->getPayment();

      And the rest of the code need to be the same,
      $parentTransactionId is the transaction id that you want to make parent transaction.

  • Back to Top
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again