Read More
Menu Close

    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 :

    public function createTransaction($order = null, $paymentData = array())
            try {
                //get payment object from order object
                $payment = $order->getPayment();
                    [\Magento\Sales\Model\Order\Payment\Transaction::RAW_DETAILS => (array) $paymentData]
                $formatedPrice = $order->getBaseCurrency()->formatTxt(
                $message = __('The authorized amount is %1.', $formatedPrice);
                //get the object of builder class
                $trans = $this->_transactionBuilder;
                $transaction = $trans->setPayment($payment)
                    [\Magento\Sales\Model\Order\Payment\Transaction::RAW_DETAILS => (array) $paymentData]
                //build method creates the transaction and returns the object
                return  $transaction->save()->getTransactionId();
            } catch (Exception $e) {
                //log errors here

    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.

    Searching for an experienced
    Magento 2 Company ?
    Read More

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

    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


  • 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