1

I am developing for magento 2. I have an instance of \Magento\Sales\Model\Order and I want to get a list of all of the transactions associated with the order.

How can I do this?

2 Answers 2

0

Edit:

This is a not good answer, you can follow the @adjco answer. Try to use Service Contract.

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.

5
  • I have just tried each of the options you suggest and found that neither works. if I call $transactions->getTotalCount() I get 0 for all orders, even those that have associated transactions. Nov 9, 2016 at 16:25
  • Ah, I was using the order increment ID, not the order entity ID. I now get the transactions I expected. Nov 9, 2016 at 16:31
  • How you get by using the order increment Id? Nov 9, 2016 at 16:35
  • No, it didn't work with the increment ID, it did work with the entity ID Nov 9, 2016 at 16:51
  • 1
    Note: use $transactions->getItems() to get a list of transaction object from this. Nov 9, 2016 at 16:51
2

Inside your class needing the transaction list inject the following (and assign them to appropriate class properties):

Magento\Sales\Model\Order\Payment\Transaction\Repository, \Magento\Framework\Api\SearchCriteriaBuilder and \Magento\Framework\Api\FilterBuilder

Then use the following code to retrieve all transactions for that order and its associated payment:

    $filters[] = $this->filterBuilder->setField('payment_id')
        ->setValue($yourOrder->getPayment()->getId())
        ->create();

    $filters[] = $this->filterBuilder->setField('order_id')
        ->setValue($yourOrder->getId())
        ->create();

    $searchCriteria = $this->searchCriteriaBuilder->addFilters($filters)
        ->create();

    $transactionList = $this->transactionRepository->getList($searchCriteria);
1
  • Can you explain your filters[] approach more?
    – camdixon
    Nov 7, 2016 at 4:47

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.