In this article we learn how to create transactions in Magento 2 . A transaction is proof that payment has been done.
In Magento, a transaction is automatically created when an invoice is generated, so developers usually don’t need to handle it manually.
However, this changes when implementing an online payment method that redirects customers to an external payment gateway.
When the customer completes the payment and is redirected back to the merchant’s website with payment data, the transaction may need to be created manually.
In such cases, developers must use a code snippet to create the transaction programmatically:
public function createTransaction($order = null, $paymentData = array())
{
try {
//get payment object from order object
$payment = $order->getPayment();
$payment->setLastTransId($paymentData['id']);
$payment->setTransactionId($paymentData['id']);
$payment->setAdditionalInformation(
[\Magento\Sales\Model\Order\Payment\Transaction::RAW_DETAILS => (array) $paymentData]
);
$formatedPrice = $order->getBaseCurrency()->formatTxt(
$order->getGrandTotal()
);
$message = __('The authorized amount is %1.', $formatedPrice);
//get the object of builder class
$trans = $this->_transactionBuilder;
$transaction = $trans->setPayment($payment)
->setOrder($order)
->setTransactionId($paymentData['id'])
->setAdditionalInformation(
[\Magento\Sales\Model\Order\Payment\Transaction::RAW_DETAILS => (array) $paymentData]
)
->setFailSafe(true)
//build method creates the transaction and returns the object
->build(\Magento\Sales\Model\Order\Payment\Transaction::TYPE_CAPTURE);
$payment->addTransactionCommentsToOrder(
$transaction,
$message
);
$payment->setParentTransactionId(null);
$payment->save();
$order->save();
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 :
Magento\Sales\Model\Order\Payment\Transaction\BuilderInterface,
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 🙂 .

9 comments
How to create builder object.
MagentoSalesModelOrderPaymentTransactionBuilderInterface
you can instantiate this class using object manager or constructor injection
//get payment object from order object
$payment = $order->getPayment();
$payment->setLastTransId($paymentData[‘id’]);
$payment->setParentTransactionId($parentTransactionId);
And the rest of the code need to be the same,
$parentTransactionId is the transaction id that you want to make parent transaction.