Skip to content

Commit

Permalink
Merge pull request #67 from bold-commerce/CHK-4880
Browse files Browse the repository at this point in the history
Add credit card info to Magento payment from authorization full respo…
  • Loading branch information
NickolasMalovanets authored Sep 13, 2024
2 parents 029d4d9 + ba7a6f3 commit 2e26b47
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions Observer/Order/BeforePlaceObserver.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

declare(strict_types=1);

namespace Bold\CheckoutPaymentBooster\Observer\Order;
Expand All @@ -11,7 +10,8 @@
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Model\Order\Payment\Transaction;

/**
* Authorize Bold payments before placing order.
Expand All @@ -23,11 +23,6 @@ class BeforePlaceObserver implements ObserverInterface
*/
private $authorize;

/**
* @var CartRepositoryInterface
*/
private $cartRepository;

/**
* @var Session
*/
Expand All @@ -40,18 +35,15 @@ class BeforePlaceObserver implements ObserverInterface

/**
* @param Authorize $authorize
* @param CartRepositoryInterface $cartRepository
* @param Session $checkoutSession
* @param CheckPaymentMethod $checkPaymentMethod
*/
public function __construct(
Authorize $authorize,
CartRepositoryInterface $cartRepository,
Session $checkoutSession,
CheckPaymentMethod $checkPaymentMethod
) {
$this->authorize = $authorize;
$this->cartRepository = $cartRepository;
$this->checkoutSession = $checkoutSession;
$this->checkPaymentMethod = $checkPaymentMethod;
}
Expand All @@ -72,6 +64,36 @@ public function execute(Observer $observer): void
}
$publicOrderId = $this->checkoutSession->getBoldCheckoutData()['data']['public_order_id'] ?? '';
$websiteId = (int)$order->getStore()->getWebsiteId();
$this->authorize->execute($publicOrderId, $websiteId);
$response = $this->authorize->execute($publicOrderId, $websiteId);
$this->saveTransaction($order, $response['data'] ?? []);
}

/**
* Add Bold transaction data to order payment.
*
* @param OrderInterface $order
* @param array $transactionData
* @return void
*/
private function saveTransaction(OrderInterface $order, array $transactionData)
{
if (!isset($transactionData['transactions'][0]['transaction_id'])) {
return;
}
$order->getPayment()->setTransactionId($transactionData['transactions'][0]['transaction_id']);
$order->getPayment()->setIsTransactionClosed(0);
$order->getPayment()->addTransaction(Transaction::TYPE_AUTH);
$cardDetails = $transactionData['transactions'][0]['tender_details'] ?? null;
if (!$cardDetails) {
return;
}
$brand = $cardDetails['brand'] ?? '';
$lastFour = $cardDetails['last_four'] ?? '';
if (!$lastFour && isset($cardDetails['line_text'])) {
preg_match('/\b(\d{4})\b(?=\s*\(Transaction ID)/', $cardDetails['line_text'], $matches);
$lastFour = $matches[1] ?? $cardDetails['line_text'];
}
$order->getPayment()->setCcType($brand);
$order->getPayment()->setCcLast4($lastFour);
}
}

0 comments on commit 2e26b47

Please sign in to comment.