2

How to get transaction id of order in Magento 2?

5
  • do you have any code that you have tried?
    – fmsthird
    Commented Mar 26, 2019 at 10:09
  • I have tried this $transactionFactory = $objectManager->get('\Magento\Sales\Model\ResourceModel\Order\Payment\Transaction\CollectionFactory'); $transactions = $transactionFactory->create()->addOrderIdFilter($_order->getId()); print_r($transactions->getItems()); but this is not working Commented Mar 26, 2019 at 10:10
  • payment transaction id or order id? Commented Mar 26, 2019 at 11:04
  • I want payment transaction id Commented Mar 26, 2019 at 11:06
  • You shouldn't use objectManager directly in your code Commented Mar 27, 2019 at 4:31

3 Answers 3

2

Edit:

Try to inject Magento\Sales\Api\Data\TransactionSearchResultInterfaceFactory into your constructor:

protected $transactions;

public function __constructor(
  \Magento\Sales\Api\Data\TransactionSearchResultInterfaceFactory $transactions,
  ......
)
{
  $this->transactions = $transactions;
}

In your method:

$transactions = $this->transactions->create()->addOrderIdFilter($orderId);
$transactions->getItems();

If Magento\Sales\Api\Data\TransactionSearchResultInterfaceFactory doesn't work, try to use \Magento\Sales\Model\ResourceModel\Order\Payment\Transaction\CollectionFactory. I'm not test yet both of them.

1
  • This is not working. Commented Mar 26, 2019 at 11:02
0

This will work.

class MyClass
{
    /**
     * @var \Magento\Sales\Api\TransactionRepositoryInterface
     */
    private $repository;

    /**
     * @var \Magento\Framework\Api\SearchCriteriaBuilder
     */
    private $searchCriteriaBuilder;

    /**
     * MyClass constructor.
     *
     * @param \Magento\Sales\Api\TransactionRepositoryInterface $repository
     * @param \Magento\Framework\Api\SearchCriteriaBuilder      $searchCriteriaBuilder
     */
    public function __construct(
        \Magento\Sales\Api\TransactionRepositoryInterface $repository,
        \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
    ) {
        $this->repository            = $repository;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
    }

    /**
     * @param int $id
     *
     * @return \Magento\Sales\Api\Data\TransactionInterface[]
     */
    public function getTransactionByOrderId($id)
    {
        $this->searchCriteriaBuilder->addFilter('order_id', $id);
        $list = $this->repository->getList(
            $this->searchCriteriaBuilder->create()
        );

        return $list->getItems();
    }
}
0

You can use \Magento\Sales\Api\Data\TransactionSearchResultInterface for transaction Id.

$transaction = $objectManager->create('\Magento\Sales\Api\Data\TransactionSearchResultInterfaceFactory')->create()->addOrderIdFilter($block->getOrderId())->getFirstItem();

$transactionId = $transaction->getData('txn_id');

The $transactionId is your required transaction Id.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.