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?
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.
$transactions->getTotalCount()
I get 0
for all orders, even those that have associated transactions.
$transactions->getItems()
to get a list of transaction object from this.
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);