<?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; } } }
Search
Dec 7, 2023
Get transaction (profile id, payment id) from order in magento 2
Sep 28, 2023
Magento 2: get discount, gift card amount, subtotal, grand total
<?php
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\GiftCardAccount\Api\GiftCardAccountManagementInterface;
use Magento\GiftCardAccount\Api\Data\GiftCardAccountInterface;
class Price {
/** @var CheckoutSession */
protected $checkoutSession;
/** @var GiftCardAccountManagementInterface */
private $giftCardAccountManagementInterface;
public function __construct(
CheckoutSession $checkoutSession,
GiftCardAccountManagementInterface $giftCardAccountManagementInterface
)
{
$this->checkoutSession = $checkoutSession;
$this->giftCardAccountManagementInterface = $giftCardAccountManagementInterface;
}
public function execute()
{
$quote = $this->getCurrentQuote();
if ($quote) {
$shippingAddress = $quote->getShippingAddress();
$subTotal = round((float) $quote->getSubtotal(), 2);
$grandTotal = round((float) $quote->getGrandTotal(), 2);
$taxAmount = round((float) $shippingAddress->getTaxAmount(), 2);
$giftCardAmountAvailableTotal = round((float) $quote->getGiftCardsAmount(), 2);
// Get giftcard used
$giftCardAccount = $this->getGiftCardAccount();
$giftCardUsedAmount = $giftCardAccount->getGiftCardsAmountUsed();
$giftCardAmount = round($giftCardUsedAmount, 2);
// Get discount
$items = $quote->getAllItems();
$discountAmount = 0;
foreach ($items as $item) {
$discountAmount += $item->getDiscountAmount();
}
$discountAmount = round((float) $discountAmount, 2);
}
}
/**
* @param $quoteId
* @return GiftCardAccountInterface
*/
protected function getGiftCardAccount($quoteId = null)
{
if (!$quoteId) {
$quoteId = $this->getCurrentQuote()->getId();
}
return $this->giftCardAccountManagementInterface->getListByQuoteId($quoteId);
}
/**
* @return CartInterface|Quote
*/
public function getCurrentQuote()
{
return $this->checkoutSession->getQuote();
}
}
Subscribe to:
Posts (Atom)