Get a specific Payment transaction details by the given Transaction id in Magento 2.
Retrieve the transaction details information will be an array of order id, txn_id, payment id, txn_type, and additional_information from the sales_payment_transaction database table.
The sales module has API TransactionRepositoryInterface, use get() method from the interface to fetch transactional data.
<?php namespace Jesadiya\Transaction\Model; use Psr\Log\LoggerInterface; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Sales\Api\Data\TransactionInterface; use Magento\Sales\Api\TransactionRepositoryInterface; class TransactionInfo { /** * @var LoggerInterface */ private $logger; /** * @var TransactionRepositoryInterface */ private $transactionRepository; public function __construct( TransactionRepositoryInterface $transactionRepository, LoggerInterface $logger ) { $this->transactionRepository = $transactionRepository; $this->logger = $logger; } /** * Loads a specified transaction by id * * @param int $transactionId * @return TransactionInterface|null */ public function get(int $transactionId) { $transactionData = null; try { $transactionData = $this->transactionRepository->get($transactionId); } catch (NoSuchEntityException $exception) { $this->logger->critical($exception->getMessage()); } return $transactionData; } }
Call method with a valid argument,
$transactionId = 1; $result = $this->get($transactionId); echo $txnId = $result->getTxnId(); echo $parentTxnId = $result->getParentTxnId(); echo $txnType = $result->getTxnType(); echo $orderId = $result->getOrderId();
If you want to delete transaction id, Delete Transaction data from Magento 2