Search

Dec 7, 2023

Get transaction (profile id, payment id) from order in magento 2

<?php
use Magento\Sales\Api\Data\TransactionInterface;
use Magento\Sales\Api\TransactionRepositoryInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\HTTP\Client\Curl;


class ... {
    
    /** @var TransactionRepositoryInterface */
    private $_transactionRepositoryInterface;

    /** @var SearchCriteriaBuilder */
    private $_searchCriteriaBuilder;

    /** @var Curl */
    private $_curl;


    /**
     * ...
     * @param Curl $_curl
     * @param TransactionRepositoryInterface $_transactionRepositoryInterface
     * @param SearchCriteriaBuilder $_searchCriteriaBuilder
     */
    public function __construct(
        ...
        Curl                           $_curl,
        TransactionRepositoryInterface $_transactionRepositoryInterface,
        SearchCriteriaBuilder          $_searchCriteriaBuilder
    )
    {
        parent::__construct(...);
        $this->_curl = $_curl;
        $this->_transactionRepositoryInterface = $_transactionRepositoryInterface;
        $this->_searchCriteriaBuilder = $_searchCriteriaBuilder;
    }


    public function excute() {
        $orderId = 123;
         $trans = $this->_getTransactionFromOrder($orderId);
        $tran = false;
        if (is_array($trans) && count($trans) > 0) {
            $tran = reset($trans);
        }

        if ($tran) {
            /** @var TransactionInterface $trans */
            $transId = $tran->getTxnId();
            echo "<br>Trans ID: " . $transId . "<br>";
            $addInfo = $tran->getAdditionalInformation();

            if (isset($addInfo["raw_details_info"]["profile_id"])) {
                echo "<br>profile_id: " . $addInfo["raw_details_info"]["profile_id"];
            }
            if (isset($addInfo["raw_details_info"]["payment_id"])) {
                echo "<br>payment_id: " . $addInfo["raw_details_info"]["payment_id"];
            }
        }


        // Get Data by Trans ID from Authorize net API
        $transId = 4444;
        $data = $this->_getTransDetail($transId);
    }


    /**
     * @param $orderId
     *
     * @return TransactionInterface[]
     */
    private function _getTransactionFromOrder($orderId) {
        $this->_searchCriteriaBuilder->addFilter('order_id', $orderId);
        $list = $this->_transactionRepositoryInterface->getList(
            $this->_searchCriteriaBuilder->create()
        );
        return $list->getItems();
    }


    /**
     * Get Transaction Detail from authorize net API
     * @param $transId
     *
     * @return false|string
     */
    private function _getTransDetail($transId)
    {
        if (!$transId) {
            return false;
        }

        try {
            // Set the external URL
            $apiUrl = 'https://apitest.authorize.net/xml/v1/request.api';
            $apiUrlProduction = 'https://api.authorize.net/xml/v1/request.api';

            // Set the POST data as JSON
            $postData = [
                'getTransactionDetailsRequest' => [
                    'merchantAuthentication' => [
                        'name' => 'API_USER_ID',
                        'transactionKey' => 'TRANS_KEY',
                    ],
                    'transId' => $transId,
                ],
            ];
            $postDataJson = json_encode($postData);
            // Make the POST request
            $this->_curl->post($apiUrl, $postDataJson);
            $response = $this->_curl->getBody();
            return $response;
        } catch (\Exception $e) {
            return false;
        }
    }

}


No comments:

Post a Comment