store transaction Id into magento order payment table

store transaction Id into magento order payment table

August 18, 2026 · Illustrated author avatarBy Stefan Bogdanescu
store transaction Id  into magento order payment table

Storing Payment Gateway Transaction IDs in Magento Order Payment Table

As an experienced developer working with e-commerce platforms like Magento, I often receive questions about customizing payment methods to store specific data related to transactions. In this post, I’ll guide you through the process of storing your payment gateway’s transaction ID directly into the sales_order_payment table when a successful payment action occurs.

Understanding the Payment Method Lifecycle

Before diving into the code, let’s briefly recap how Magento handles payment methods and order payments. When a customer places an order and chooses your custom payment method, Magento triggers the appropriate callback functions based on the status of the payment:

  • before_order_place_after: Triggered before the order is placed.
  • after_order_place: Executes after the order has been created but before any further actions like sending emails or updating stock levels.
  • after_place_order (deprecated in Magento 2.3 onwards): Similar to after_order_place.

The key to storing your transaction ID lies within these callback functions, specifically the after_order_place function.

Implementing the Transaction ID Storage

Let’s assume you have already created a custom payment method extending \Magento\Payment\Gateway\Command\ResultInterface. To store the transaction ID:

  1. Create a Magento setup script to add the new column ‘transaction_id’ to the sales_order_payment table if it doesn’t exist.
namespace Magento\Setup\Patch\Data;

use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\Data PatchInterface;
use Magento\Framework\Setup\Patch\PatchRevertableInterface;

class AddTransactionIdToSalesOrderPayment implements DataPatchInterface, PatchRevertableInterface
{
    private $moduleDataSetup;

    public function __construct(ModuleDataSetupInterface $moduleDataSetup)
    {
        $this->moduleDataSetup = $moduleDataSetup;
    }

    public function apply()
    {
        $this->moduleDataSetup->startSetup();
        $this->moduleDataSetup->getConnection()->addColumn(
            $this->moduleDataSetup->getTable('sales_order_payment'),
            'transaction_id',
            [
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                'length' => 255,
                'comment' => 'Payment gateway transaction ID'
            ]
        );
        $this->moduleDataSetup->endSetup();
    }

    public function revert()
    {
        $this->moduleDataSetup->startSetup();
        $this->moduleDataSetup->getConnection()->dropColumn(
            $this->moduleDataSetup->getTable('sales_order_payment'),
            'transaction_id'
        );
        $this->moduleDataSetup->endSetup();
    }
}
  1. Override the after_order_place method in your custom payment method to store the transaction ID.
class YourPaymentMethod extends \Magento\Payment\Gateway\Command\ResultInterface
{
    public function afterOrderPlace($paymentAction, $stateObject)
    {
        if ($paymentAction === 'authorize_capture') {
            // Assuming you have a response object with the transaction ID
            $response = $this->getLastResponse();
            $transactionId = $response['transaction_id'];

            $resource = $this->_objectManager->get('Magento\Framework\App\ResourceConnection');
            $connection = $resource->getConnection();

            $paymentId = $stateObject->getPayment()->getEntityId();
            $tableName = $resource->getTableName('sales_order_payment');

            $connection->beginTransaction();
            try {
                $where = [
                    'entity_id' => (int)$paymentId
                ];
                $bind = ['transaction_id' => $transactionId];
                $connection->update($tableName, $bind, $where);
                $connection->commit();
            } catch (\Exception $e) {
                $connection->rollBack();
                throw $e;
            }
        }

        return $this;
    }
}

Conclusion

Storing payment gateway transaction IDs in the sales_order_payment table is a common requirement for many custom payment integrations. By following this guide, you can seamlessly store your transaction ID within Magento’s core database structure.

Remember to thoroughly test your implementation, especially when dealing with database schema changes and financial data. Always have backups ready and perform extensive testing before deploying these changes in production environments.

For more advanced customizations or if you encounter any issues, feel free to explore the comprehensive resources available at Laravel Company. We specialize in providing expert guidance on Magento development and e-commerce solutions tailored to your business needs.

Magento 2 Database Email Orders

Our network