Skip to content

Commit

Permalink
PLUG-106: Place orders upfront, refactor webhook and return
Browse files Browse the repository at this point in the history
  • Loading branch information
lighe committed May 8, 2024
1 parent ae9e56a commit 0a6d24b
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 221 deletions.
2 changes: 1 addition & 1 deletion Api/Transaction/RepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function getByOrderId(int $orderId): DataInterface;
* @throws InputException
* @throws NoSuchEntityException
*/
public function getByUuid(string $uuid): DataInterface;
public function getByPaymentUuid(string $uuid): DataInterface;

/**
* @param string $token
Expand Down
14 changes: 7 additions & 7 deletions Controller/Adminhtml/Credentials/Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Magento\Framework\Filesystem\DirectoryList;
use Magento\Framework\Filesystem\Io\File;
use TrueLayer\Connect\Api\Config\RepositoryInterface as ConfigRepository;
use TrueLayer\Connect\Service\Api\GetClient;
use TrueLayer\Connect\Service\Api\ClientFactory;
use TrueLayer\Interfaces\Client\ClientInterface;

/**
Expand All @@ -31,9 +31,9 @@ class Check extends Action implements HttpPostActionInterface
*/
private $directoryList;
/**
* @var GetClient
* @var ClientFactory
*/
private $getClient;
private $clientFactory;
/**
* @var Json
*/
Expand All @@ -52,20 +52,20 @@ class Check extends Action implements HttpPostActionInterface
*
* @param Action\Context $context
* @param JsonFactory $resultJsonFactory
* @param GetClient $getClient
* @param ClientFactory $clientFactory
* @param ConfigRepository $configProvider
* @param File $file
* @param DirectoryList $directoryList
*/
public function __construct(
Action\Context $context,
JsonFactory $resultJsonFactory,
GetClient $getClient,
ClientFactory $clientFactory,
ConfigRepository $configProvider,
File $file,
DirectoryList $directoryList
) {
$this->getClient = $getClient;
$this->clientFactory = $clientFactory;
$this->resultJson = $resultJsonFactory->create();
$this->configProvider = $configProvider;
$this->file = $file;
Expand Down Expand Up @@ -106,7 +106,7 @@ private function testCredentials(): ?ClientInterface
throw new LocalizedException(__('No Client Secret set!'));
}

$result = $this->getClient->execute(
$result = $this->clientFactory->create(
(int)$config['store_id'],
['credentials' => $config['credentials']]
);
Expand Down
1 change: 1 addition & 0 deletions Controller/Checkout/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function __construct(
public function execute(): Redirect
{
$resultRedirect = $this->resultRedirectFactory->create();

if (!$transactionId = $this->getRequest()->getParam('payment_id')) {
$this->messageManager->addErrorMessage(__('Error in return data from TrueLayer'));
$resultRedirect->setPath('checkout/cart/index');
Expand Down
2 changes: 1 addition & 1 deletion Model/Transaction/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public function getByOrderId(int $orderId): DataInterface
/**
* @inheritDoc
*/
public function getByUuid(string $uuid): DataInterface
public function getByPaymentUuid(string $uuid): DataInterface
{
if (!$uuid) {
$errorMsg = static::INPUT_EXCEPTION;
Expand Down
3 changes: 2 additions & 1 deletion Model/Webapi/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace TrueLayer\Connect\Model\Webapi;

use Magento\Framework\Math\Random;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Model\Order;
use Magento\Checkout\Model\Session;
use TrueLayer\Connect\Api\Log\RepositoryInterface as LogRepository;
Expand Down Expand Up @@ -88,6 +87,7 @@ public function orderRequest()
* @throws \TrueLayer\Exceptions\ApiResponseUnsuccessfulException
* @throws \TrueLayer\Exceptions\InvalidArgumentException
* @throws \TrueLayer\Exceptions\SignerException
* @throws \Magento\Framework\Exception\LocalizedException
*/
private function getHppUrl(): string
{
Expand Down Expand Up @@ -195,6 +195,7 @@ private function getMerchantAccountId(ClientInterface $client): string

/**
* @return DataInterface
* @throws \Magento\Framework\Exception\LocalizedException
*/
private function getTransaction(): DataInterface
{
Expand Down
2 changes: 1 addition & 1 deletion Model/Webapi/Pending.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct(
public function checkOrderPlaced(string $token): bool
{
try {
$transaction = $this->transactionRepository->getByUuid($token);
$transaction = $this->transactionRepository->getByPaymentUuid($token);
return (bool)$transaction->getOrderId();
} catch (InputException|NoSuchEntityException $e) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion Model/Webapi/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private function getStoreId(): int
return 0;
}

$transaction = $this->transactionRepository->getByUuid($postArray['payment_id']);
$transaction = $this->transactionRepository->getByPaymentUuid($postArray['payment_id']);
if (!$quoteId = $transaction->getQuoteId()) {
return 0;
}
Expand Down
97 changes: 18 additions & 79 deletions Service/Order/ProcessReturn.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,12 @@

namespace TrueLayer\Connect\Service\Order;

use Magento\Checkout\Model\Session;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\Order;
use TrueLayer\Connect\Api\Log\RepositoryInterface as LogRepository;
use TrueLayer\Connect\Api\Transaction\RepositoryInterface as TransactionRepository;
use TrueLayer\Connect\Service\Api\GetClient;
use TrueLayer\Connect\Service\Api\ClientFactory;

/**
* Class ProcessReturn
Expand All @@ -31,17 +26,9 @@ class ProcessReturn
public const UNKNOWN_MSG = 'Unknown error, please try again.';

/**
* @var GetClient
* @var ClientFactory
*/
private $getClient;
/**
* @var Session
*/
private $checkoutSession;
/**
* @var CartRepositoryInterface
*/
private $quoteRepository;
private $clientFactory;
/**
* @var OrderInterface
*/
Expand All @@ -50,10 +37,6 @@ class ProcessReturn
* @var TransactionRepository
*/
private $transactionRepository;
/**
* @var OrderRepositoryInterface
*/
private $orderRepository;
/**
* @var LogRepository
*/
Expand All @@ -62,28 +45,19 @@ class ProcessReturn
/**
* ProcessReturn constructor.
*
* @param Session $checkoutSession
* @param GetClient $getClient
* @param CartRepositoryInterface $quoteRepository
* @param ClientFactory $clientFactory
* @param OrderInterface $orderInterface
* @param OrderRepositoryInterface $orderRepository
* @param TransactionRepository $transactionRepository
* @param LogRepository $logger
*/
public function __construct(
Session $checkoutSession,
GetClient $getClient,
CartRepositoryInterface $quoteRepository,
ClientFactory $clientFactory,
OrderInterface $orderInterface,
OrderRepositoryInterface $orderRepository,
TransactionRepository $transactionRepository,
LogRepository $logger
) {
$this->checkoutSession = $checkoutSession;
$this->getClient = $getClient;
$this->quoteRepository = $quoteRepository;
$this->clientFactory = $clientFactory;
$this->orderInterface = $orderInterface;
$this->orderRepository = $orderRepository;
$this->transactionRepository = $transactionRepository;
$this->logger = $logger;
}
Expand All @@ -96,68 +70,33 @@ public function __construct(
* @throws \TrueLayer\Exceptions\ApiRequestJsonSerializationException
* @throws \TrueLayer\Exceptions\ApiResponseUnsuccessfulException
* @throws \TrueLayer\Exceptions\SignerException
* @throws \TrueLayer\Exceptions\ValidationException
*/
public function execute(string $transactionId): array
{
$transaction = $this->transactionRepository->getByUuid($transactionId);
$quote = $this->quoteRepository->get($transaction->getQuoteId());
$this->checkoutSession->setLoadInactive(true)->replaceQuote($quote);
$transaction = $this->transactionRepository->getByPaymentUuid($transactionId);
$order = $this->orderInterface->loadByAttribute('quote_id', $transaction->getQuoteId());

$order = $this->orderInterface->loadByAttribute('quote_id', $quote->getId());

$client = $this->getClient->execute($quote->getStoreId());
$payment = $client->getPayment($transactionId);
$transactionStatus = $payment->getStatus();
$client = $this->clientFactory->create((int) $order->getStoreId());
$paymentStatus = $client->getPayment($transactionId)->getStatus();

if (!$order->getEntityId()) {
if ($transactionStatus == 'settled' || $transactionStatus == 'executed') {
return ['success' => false, 'status' => $transactionStatus];
if ($paymentStatus == 'settled' || $paymentStatus == 'executed') {
return ['success' => false, 'status' => $paymentStatus];
}
}

switch ($transactionStatus) {
switch ($paymentStatus) {
case 'executed':
case 'settled':
$this->updateCheckoutSession($quote, $order);
return ['success' => true, 'status' => $transactionStatus];
return ['success' => true, 'status' => $paymentStatus];
case 'cancelled':
$message = (string)self::CANCELLED_MSG;
return ['success' => false, 'status' => $transactionStatus, 'msg' => __($message)];
return ['success' => false, 'status' => $paymentStatus, 'msg' => __(self::CANCELLED_MSG)];
case 'failed':
$message = (string)self::FAILED_MSG;
return ['success' => false, 'status' => $transactionStatus, 'msg' => __($message)];
return ['success' => false, 'status' => $paymentStatus, 'msg' => __(self::FAILED_MSG)];
case 'rejected':
$message = (string)self::REJECTED_MSG;
return ['success' => false, 'status' => $transactionStatus, 'msg' => __($message)];
return ['success' => false, 'status' => $paymentStatus, 'msg' => __(self::REJECTED_MSG)];
default:
$message = (string)self::UNKNOWN_MSG;
return ['success' => false, 'status' => $transactionStatus, 'msg' => __($message)];
return ['success' => false, 'status' => $paymentStatus, 'msg' => __(self::UNKNOWN_MSG)];
}
}

/**
* @param CartInterface $quote
* @param Order $order
* @return void
*/
private function updateCheckoutSession(CartInterface $quote, Order $order): void
{
$this->orderRepository->save($order);

// Remove additional quote for customer
if ($customerId = $quote->getCustomer()->getId()) {
try {
$activeQuote = $this->quoteRepository->getActiveForCustomer($customerId);
$this->quoteRepository->delete($activeQuote);
} catch (NoSuchEntityException $e) {
$this->logger->addErrorLog('Remove customer quote', $e->getMessage());
}
}

$this->checkoutSession->setLastQuoteId($quote->getEntityId())
->setLastSuccessQuoteId($quote->getEntityId())
->setLastRealOrderId($order->getIncrementId())
->setLastOrderId($order->getId());
}
}
Loading

0 comments on commit 0a6d24b

Please sign in to comment.