Skip to content
This repository has been archived by the owner on Feb 18, 2025. It is now read-only.

Commit

Permalink
Improved Vipps order processing flow (#8)
Browse files Browse the repository at this point in the history
* Improved Vipps order processing flow
  • Loading branch information
ed007m authored Oct 30, 2020
1 parent 39f027f commit cd0c854
Show file tree
Hide file tree
Showing 19 changed files with 730 additions and 243 deletions.
6 changes: 0 additions & 6 deletions app/code/community/Vipps/Payment/Controller/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ class Vipps_Payment_Controller_Abstract extends Mage_Core_Controller_Front_Actio
*/
protected $gdprCompliance;

/**
* @var Vipps_Payment_Gateway_Transaction_TransactionBuilder
*/
protected $transactionBuilder;

/**
* @var Vipps_Payment_Helper_Gateway
*/
Expand All @@ -78,7 +73,6 @@ public function preDispatch()
$this->messageManager = Mage::getSingleton('vipps_payment/adapter_messageManager');
$this->serializer = Mage::getSingleton('vipps_payment/adapter_jsonEncoder');
$this->gdprCompliance = Mage::getSingleton('vipps_payment/gdpr_compliance');
$this->transactionBuilder = new Vipps_Payment_Gateway_Transaction_TransactionBuilder;

return $this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ class Vipps_Payment_Gateway_Command_PaymentDetailsProvider
*/
private $commandManager;

/**
* @var Vipps_Payment_Gateway_Transaction_TransactionBuilder
*/
private $transactionBuilder;

public function __construct()
{
$this->transactionBuilder = new Vipps_Payment_Gateway_Transaction_TransactionBuilder;
}

/**
* @param array $arguments
*
Expand All @@ -32,7 +42,9 @@ class Vipps_Payment_Gateway_Command_PaymentDetailsProvider
*/
public function get(array $arguments)
{
return $this->getCommandManager()->getPaymentDetails($arguments);
$responseData = $this->getCommandManager()->getPaymentDetails($arguments);

return $this->transactionBuilder->setData($responseData)->build();
}

/**
Expand All @@ -41,7 +53,8 @@ public function get(array $arguments)
private function getCommandManager()
{
if (!$this->commandManager) {
$this->commandManager = Mage::helper('vipps_payment/gateway')->getSingleton('command_commandManager'); // @TODO: Replace with async singleton loading
$this->commandManager = Mage::helper('vipps_payment/gateway')
->getSingleton('command_commandManager'); // @TODO: Replace with async singleton loading
}

return $this->commandManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private function getHeaders($headers)
self::HEADER_PARAM_X_REQUEST_ID => '',
self::HEADER_PARAM_X_SOURCE_ADDRESS => '',
self::HEADER_PARAM_X_TIMESTAMP => '',
self::HEADER_PARAM_SUBSCRIPTION_KEY => $this->config->getValue('subscription_key2')
self::HEADER_PARAM_SUBSCRIPTION_KEY => $this->config->getValue('subscription_key1')
],
$headers
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ class Vipps_Payment_Gateway_Transaction_Transaction
*/
private $shippingDetails;

/**
* @var string
*/
private $orderId;

/**
* Transaction constructor.
*
Expand All @@ -160,19 +165,26 @@ class Vipps_Payment_Gateway_Transaction_Transaction
* @param \Vipps_Payment_Gateway_Transaction_ShippingDetails|null $shippingDetails
*/
public function __construct(
$orderId,
Vipps_Payment_Gateway_Transaction_TransactionInfo $transactionInfo,
Vipps_Payment_Gateway_Transaction_TransactionSummary $transactionSummary,
Vipps_Payment_Gateway_Transaction_TransactionLogHistory $transactionLogHistory,
\Vipps_Payment_Gateway_Transaction_UserDetails $userDetails = null,
\Vipps_Payment_Gateway_Transaction_ShippingDetails $shippingDetails = null
) {
$this->orderId = $orderId;
$this->transactionInfo = $transactionInfo;
$this->transactionSummary = $transactionSummary;
$this->transactionLogHistory = $transactionLogHistory;
$this->userDetails = $userDetails;
$this->shippingDetails = $shippingDetails;
}

public function getOrderId()
{
return $this->orderId;
}

/**
* @return Vipps_Payment_Gateway_Transaction_TransactionSummary
*/
Expand Down Expand Up @@ -258,7 +270,7 @@ public function isTransactionReserved()

return false;
}

/**
* @return string|null
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public function build()
$shippingDetails = new Vipps_Payment_Gateway_Transaction_ShippingDetails($this->response['shippingDetails']);
}

return new Vipps_Payment_Gateway_Transaction_Transaction($info, $summary, $logHistory, $userDetails, $shippingDetails);
$orderId = $this->response['orderId'];

return new Vipps_Payment_Gateway_Transaction_Transaction($orderId, $info, $summary, $logHistory, $userDetails, $shippingDetails);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ class Vipps_Payment_Model_Cron_FetchOrderFromVipps extends Vipps_Payment_Model_C
*/
private $transactionBuilder;

/**
* @var Vipps_Payment_Model_OrderPlace
*/
private $orderPlace;

/**
* @var Vipps_Payment_Model_Quote_AttemptManagement
*/
Expand All @@ -54,6 +49,11 @@ class Vipps_Payment_Model_Cron_FetchOrderFromVipps extends Vipps_Payment_Model_C
*/
private $vippsQuoteRepository;

/**
* @var Vipps_Payment_Model_TransactionProcessor
*/
private $transactionProcessor;

/**
* FetchOrderFromVipps constructor.
*/
Expand All @@ -62,11 +62,11 @@ public function __construct()
parent::__construct();

$this->transactionBuilder = new Vipps_Payment_Gateway_Transaction_TransactionBuilder();
$this->orderPlace = Mage::getSingleton('vipps_payment/orderPlace');
$this->attemptManagement = Mage::getSingleton('vipps_payment/quote_attemptManagement');
$this->quoteRepository = Mage::getSingleton('vipps_payment/adapter_cartRepository');
$this->vippsQuoteRepository = Mage::getSingleton('vipps_payment/quoteRepository');
$this->orderLocator = Mage::getSingleton('vipps_payment/orderRepository');
$this->transactionProcessor = Mage::getSingleton('vipps_payment/transactionProcessor');
}

/**
Expand Down Expand Up @@ -134,21 +134,7 @@ private function processQuote(Vipps_Payment_Model_Quote $vippsQuote)
// Register new attempt.
$attempt = $this->attemptManagement->createAttempt($vippsQuote);

// Get Magento Quote for processing.
$transaction = $this->getTransaction($vippsQuote->getReservedOrderId());
if ($transaction->transactionWasCancelled() || $transaction->transactionWasVoided()) {
$attemptMessage = __('Transaction was cancelled in Vipps');
$vippsQuoteStatus = Vipps_Payment_Model_QuoteStatusInterface::STATUS_CANCELED;
} elseif ($transaction->isTransactionReserved()) {
$order = $this->placeOrder($vippsQuote, $transaction);
if ($order) {
$vippsQuoteStatus = Vipps_Payment_Model_QuoteStatusInterface::STATUS_PLACED;
$attemptMessage = __('Placed');
}
} elseif ($transaction->isTransactionExpired()) {
$vippsQuoteStatus = Vipps_Payment_Model_QuoteStatusInterface::STATUS_EXPIRED;
$attemptMessage = __('Transaction has been expired');
}
$transaction = $this->transactionProcessor->process($vippsQuote);
} catch (\Exception $e) {
$vippsQuoteStatus = $this->isMaxAttemptsReached($vippsQuote)
? Vipps_Payment_Model_QuoteStatusInterface::STATUS_PLACE_FAILED
Expand All @@ -169,68 +155,6 @@ private function processQuote(Vipps_Payment_Model_Quote $vippsQuote)
}
}

/**
* @param $orderId
*
* @return Vipps_Payment_Gateway_Transaction_Transaction
* @throws Vipps_Payment_Gateway_Command_CommandException
*/
private function getTransaction($orderId)
{
$response = $this->commandManager->getPaymentDetails(
['orderId' => $orderId]
);
return $this->transactionBuilder->setData($response)->build();
}

/**
* @param Vipps_Payment_Model_Quote $vippsQuote
* @param Vipps_Payment_Gateway_Transaction_Transaction $transaction
*
* @return Mage_Sales_Model_Order|null
* @throws Mage_Core_Exception
* @throws Vipps_Payment_Gateway_Exception_VippsException
* @throws Vipps_Payment_Gateway_Exception_WrongAmountException
* @throws Zend_Db_Adapter_Exception
* @throws Zend_Db_Statement_Exception
*/
private function placeOrder(Vipps_Payment_Model_Quote $vippsQuote, Vipps_Payment_Gateway_Transaction_Transaction $transaction)
{
$quote = $this->quoteRepository->get($vippsQuote->getQuoteId());

$existentOrder = $this->orderLocator->getByIncrement($vippsQuote->getReservedOrderId());

$order = isset($existentOrder) ? $existentOrder : $this->orderPlace->execute($quote, $transaction);
if ($order) {
$this->logger->debug(sprintf('Order placed: "%s"', $order->getIncrementId()));
} else {
$this->logger->critical(sprintf(
'Order has not been placed, quote id: "%s", reserved_order_id: "%s"',
$quote->getId(),
$quote->getReservedOrderId()
));
}
return $order;
}

/**
* Validate Vipps Quote expiration.
*
* @param Vipps_Payment_Model_Quote $vippsQuote
* @return bool
* @throws \Exception
*/
private function isQuoteExpired(Vipps_Payment_Model_Quote $vippsQuote)
{
$createdAt = new DateTime($vippsQuote->getCreatedAt());

$interval = new \DateInterval("PT{$this->cancellationConfig->getInactivityTime()}M"); //@codingStandardsIgnoreLine

$createdAt->add($interval);

return !$createdAt->diff(new DateTime())->invert;
}

/**
* Check for attempts count.
*
Expand Down
12 changes: 12 additions & 0 deletions app/code/community/Vipps/Payment/Model/Exception/AcquireLock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* Copyright © Vaimo Norge AS. All rights reserved.
* See LICENSE.txt for license details.
*/

/**
* Class Vipps_Payment_Model_Exception_TransactionExpired
*/
class Vipps_Payment_Model_Exception_AcquireLock extends \Exception
{
}
2 changes: 1 addition & 1 deletion app/code/community/Vipps/Payment/Model/OrderRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function save(Mage_Sales_Model_Order $order)

/**
* @param string $incrementId
* @return null
* @return Mage_Sales_Model_Order|null
*/
public function getByIncrement($incrementId)
{
Expand Down
3 changes: 3 additions & 0 deletions app/code/community/Vipps/Payment/Model/QuoteUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ public function execute(Mage_Sales_Model_Quote $quote, Vipps_Payment_Gateway_Tra
if (!$transaction->isExpressCheckout()) {
return false;
}

$quote->setMayEditShippingAddress(false);
$quote->setMayEditShippingMethod(true);
$this->updateQuoteAddress($quote, $transaction);
$this->utility->disabledQuoteAddressValidation($quote);
$quote->setTotalsCollectedFlag(false);
$quote->collectTotals();
$this->cartRepository->save($quote);

return $quote;
}

Expand Down Expand Up @@ -100,6 +102,7 @@ private function updateShippingAddress(Mage_Sales_Model_Quote $quote, Vipps_Paym
$shippingAddress->setTelephone($userDetails->getMobileNumber());
$shippingAddress->setShippingMethod($shippingDetails->getShippingMethodId());
$shippingAddress->setShippingAmount($shippingDetails->getShippingCost(), true);
$shippingAddress->setCollectShippingRates(true);

// try to obtain postCode one more time if it is not done before
if (!$shippingAddress->getPostcode() && $shippingDetails->getPostcode()) {
Expand Down
Loading

0 comments on commit cd0c854

Please sign in to comment.