diff --git a/Block/Express/Button.php b/Block/Express/Button.php index 02f402c0..5def4a8e 100644 --- a/Block/Express/Button.php +++ b/Block/Express/Button.php @@ -61,18 +61,16 @@ class Button extends Template implements ShortcutInterface * @param Template\Context $context * @param Random $mathRandom * @param ConfigInterface $config - * @param Repository $assetRepo * @param array $data */ public function __construct( Template\Context $context, Random $mathRandom, ConfigInterface $config, - Repository $assetRepo, array $data = [] ) { $this->config = $config; - $this->assetRepo = $assetRepo; + $this->assetRepo = $context->getAssetRepository(); $this->mathRandom = $mathRandom; parent::__construct($context, $data); } diff --git a/Gateway/Transaction/ShippingDetails.php b/Gateway/Transaction/ShippingDetails.php index 31a11121..679686d1 100644 --- a/Gateway/Transaction/ShippingDetails.php +++ b/Gateway/Transaction/ShippingDetails.php @@ -163,6 +163,6 @@ public function getAddressLine1() */ public function getAddressLine2() { - return $this->getData(self::ADDRESS)[self::ADDRESS_LINE_2]; + return $this->getData(self::ADDRESS)[self::ADDRESS_LINE_2] ?? ''; } } diff --git a/Gateway/Transaction/Transaction.php b/Gateway/Transaction/Transaction.php index 5fb5c791..f2694bee 100644 --- a/Gateway/Transaction/Transaction.php +++ b/Gateway/Transaction/Transaction.php @@ -26,6 +26,11 @@ class Transaction */ const TRANSACTION_STATUS_INITIATE = 'initiate'; + /** + * @var string + */ + const TRANSACTION_STATUS_INITIATED = 'initiated'; + /** * @var string */ diff --git a/Gateway/Transaction/TransactionLogHistory.php b/Gateway/Transaction/TransactionLogHistory.php index c34adb65..bc170252 100644 --- a/Gateway/Transaction/TransactionLogHistory.php +++ b/Gateway/Transaction/TransactionLogHistory.php @@ -27,7 +27,7 @@ class TransactionLogHistory extends DataObject /** * @var string */ - private $lastOrderStatus = Transaction::TRANSACTION_OPERATION_CANCEL; + private $lastTransactionStatus = Transaction::TRANSACTION_OPERATION_CANCEL; /** * @var string @@ -54,9 +54,9 @@ public function getItems() public function getLastTransactionStatus() { if ($this->getLastItem()) { - $this->lastOrderStatus = $this->getLastItem()->getOperation(); + $this->lastTransactionStatus = $this->getLastItem()->getOperation(); } - return $this->lastOrderStatus; + return $this->lastTransactionStatus; } /** @@ -84,7 +84,7 @@ public function getLastItem() $items = $this->getItems(); $lastTransactionTime = 0; foreach ($items as $item) { - if ($item->getTimeStamp() > $lastTransactionTime) { + if ($item->getTimeStamp() >= $lastTransactionTime) { $lastTransactionTime = $item->getTimeStamp(); $this->lastItem = $item; } diff --git a/Model/Helper/OrderPlace.php b/Model/Helper/OrderPlace.php index 51551659..0c297b48 100644 --- a/Model/Helper/OrderPlace.php +++ b/Model/Helper/OrderPlace.php @@ -16,9 +16,10 @@ namespace Vipps\Payment\Model\Helper; use Magento\Quote\Model\Quote; -use Magento\Checkout\{Helper\Data, Model\Type\Onepage}; use Magento\Framework\Session\SessionManagerInterface; use Magento\Quote\Api\{CartRepositoryInterface, CartManagementInterface}; +use Magento\Framework\Exception\CouldNotSaveException; +use Magento\Framework\Exception\NoSuchEntityException; /** * Class OrderPlace @@ -36,11 +37,6 @@ class OrderPlace */ private $customerSession; - /** - * @var Data - */ - private $checkoutHelper; - /** * @var CartRepositoryInterface */ @@ -52,17 +48,14 @@ class OrderPlace * @param CartRepositoryInterface $quoteRepository * @param CartManagementInterface $cartManagement * @param SessionManagerInterface $customerSession - * @param Data $checkoutHelper */ public function __construct( CartRepositoryInterface $quoteRepository, CartManagementInterface $cartManagement, - SessionManagerInterface $customerSession, - Data $checkoutHelper + SessionManagerInterface $customerSession ) { $this->cartManagement = $cartManagement; $this->customerSession = $customerSession; - $this->checkoutHelper = $checkoutHelper; $this->quoteRepository = $quoteRepository; } @@ -70,30 +63,24 @@ public function __construct( * @param Quote $quote * * @return int + * @throws CouldNotSaveException + * @throws NoSuchEntityException */ public function execute(Quote $quote) { - $this->updateCheckoutMethod($quote); - return $this->cartManagement->placeOrder($quote->getId()); - } + // Here we need to active a quote + // this active flag present only during this current request (does not stored in DB) + // We should do this because when we called $this->quoteRepository->save() + // the next request to repository e.g. $this->quoteRepository->getActive() - return quote + // from DB with "isActive" = false; - /** - * Update quote checkout method. - * - * @param Quote $quote - */ - private function updateCheckoutMethod(Quote $quote) - { - if (!$quote->getCheckoutMethod()) { - if ($this->checkoutHelper->isAllowedGuestCheckout($quote)) { - $quote->setCheckoutMethod(Onepage::METHOD_GUEST); - } else { - $quote->setCheckoutMethod(Onepage::METHOD_REGISTER); - } - } - $this->quoteRepository->save($quote); - //We need to load Quote and activate it + /** @var Quote $quote */ $quote = $this->quoteRepository->get($quote->getId()); $quote->setIsActive(true); + + // collect totals before place order + $quote->collectTotals(); + + return $this->cartManagement->placeOrder($quote->getId()); } } diff --git a/Model/Helper/QuoteUpdater.php b/Model/Helper/QuoteUpdater.php index 9d8afe80..728473ba 100644 --- a/Model/Helper/QuoteUpdater.php +++ b/Model/Helper/QuoteUpdater.php @@ -15,6 +15,7 @@ */ namespace Vipps\Payment\Model\Helper; +use Magento\Checkout\{Helper\Data, Model\Type\Onepage}; use Magento\Quote\{Api\CartRepositoryInterface, Model\Quote, Model\Quote\Address}; use Magento\Braintree\Model\Paypal\Helper\AbstractHelper; use Vipps\Payment\Gateway\Transaction\{ShippingDetails, Transaction}; @@ -31,14 +32,22 @@ class QuoteUpdater extends AbstractHelper private $quoteRepository; /** - * Constructor + * @var Data + */ + private $checkoutHelper; + + /** + * QuoteUpdater constructor. * * @param CartRepositoryInterface $quoteRepository + * @param Data $checkoutHelper */ public function __construct( - CartRepositoryInterface $quoteRepository + CartRepositoryInterface $quoteRepository, + Data $checkoutHelper ) { $this->quoteRepository = $quoteRepository; + $this->checkoutHelper = $checkoutHelper; } /** @@ -49,37 +58,44 @@ public function __construct( */ public function execute(Quote $quote, Transaction $transaction) { - if (!$transaction->isExpressCheckout()) { - return; + $this->updateCheckoutMethod($quote); + + if ($transaction->isExpressCheckout()) { + $payment = $quote->getPayment(); + $payment->setMethod('vipps'); + + $quote->setMayEditShippingAddress(false); + $quote->setMayEditShippingMethod(true); + + $this->updateQuoteAddress($quote, $transaction); + $this->disabledQuoteAddressValidation($quote); + + /** + * Unset shipping assignment to prevent from saving / applying outdated data + * @see \Magento\Quote\Model\QuoteRepository\SaveHandler::processShippingAssignment + */ + if ($quote->getExtensionAttributes()) { + $quote->getExtensionAttributes()->setShippingAssignments(null); + } } - $payment = $quote->getPayment(); - $payment->setMethod('vipps'); - $this->updateQuote($quote, $transaction); + + $this->quoteRepository->save($quote); } /** + * Update checkout method + * * @param Quote $quote - * @param Transaction $transaction */ - private function updateQuote(Quote $quote, Transaction $transaction) + private function updateCheckoutMethod(Quote $quote) { - $quote->setMayEditShippingAddress(false); - $quote->setMayEditShippingMethod(true); - - $this->updateQuoteAddress($quote, $transaction); - $this->disabledQuoteAddressValidation($quote); - - $quote->collectTotals(); - - /** - * Unset shipping assignment to prevent from saving / applying outdated data - * @see \Magento\Quote\Model\QuoteRepository\SaveHandler::processShippingAssignment - */ - if ($quote->getExtensionAttributes()) { - $quote->getExtensionAttributes()->setShippingAssignments(null); + if (!$quote->getCheckoutMethod()) { + if ($this->checkoutHelper->isAllowedGuestCheckout($quote)) { + $quote->setCheckoutMethod(Onepage::METHOD_GUEST); + } else { + $quote->setCheckoutMethod(Onepage::METHOD_REGISTER); + } } - - $this->quoteRepository->save($quote); } /** diff --git a/Model/OrderManagement.php b/Model/OrderManagement.php index ecdd4a38..5322468d 100644 --- a/Model/OrderManagement.php +++ b/Model/OrderManagement.php @@ -331,21 +331,26 @@ private function notify($order) } /** - * @param $orderStatus + * @param $transactionStatus * * @throws LocalizedException */ - private function validateTransactionStatus($orderStatus) + private function validateTransactionStatus($transactionStatus) { - if ($orderStatus == Transaction::TRANSACTION_STATUS_INITIATE) { - throw new LocalizedException(__('Your order was not approved in Vipps.')); - } + $initiatedStatuses = [ + Transaction::TRANSACTION_STATUS_INITIATE, + Transaction::TRANSACTION_STATUS_INITIATED + ]; $canceledStatuses = [ Transaction::TRANSACTION_OPERATION_CANCEL, Transaction::TRANSACTION_STATUS_AUTOCANCEL, Transaction::TRANSACTION_STATUS_CANCELLED ]; - if (in_array($orderStatus, $canceledStatuses)) { + + if (in_array($transactionStatus, $initiatedStatuses)) { + throw new LocalizedException(__('Your order was not approved in Vipps.')); + } + if (in_array($transactionStatus, $canceledStatuses)) { throw new LocalizedException(__('Your order was canceled in Vipps.')); } } diff --git a/Model/Profiling/Profiler.php b/Model/Profiling/Profiler.php index 48507880..dfb7e209 100644 --- a/Model/Profiling/Profiler.php +++ b/Model/Profiling/Profiler.php @@ -79,13 +79,18 @@ public function save(TransferInterface $transfer, Response $response) /** @var ItemInterface $itemDO */ $itemDO = $this->dataItemFactory->create(); - $itemDO->setRequestType($this->parseRequestType($transfer)); + $data = $this->parseDataFromTransferObject($transfer); + + $requestType = $data['type'] ?? 'undefined'; + $orderId = $data['order_id'] ?? $this->parseOrderId($response); + + $itemDO->setRequestType($requestType); $itemDO->setRequest($this->packArray( array_merge(['headers' => $transfer->getHeaders()], ['body' => $transfer->getBody()]) )); $itemDO->setStatusCode($response->getStatusCode()); - $itemDO->setIncrementId($this->parseOrderId($response)); + $itemDO->setIncrementId($orderId); $itemDO->setResponse($this->packArray($this->parseResponse($response))); $item = $this->itemRepository->save($itemDO); @@ -106,19 +111,20 @@ private function parseOrderId(Response $response) } /** - * Parse request type from url + * Parse data from transfer object * * @param TransferInterface $transfer * - * @return string + * @return array */ - private function parseRequestType(TransferInterface $transfer) + private function parseDataFromTransferObject(TransferInterface $transfer) { + $result = []; if (preg_match('/payments(\/([^\/]+)\/([a-z]+))?$/', $transfer->getUri(), $matches)) { - $status = $matches[3] ?? TypeInterface::INITIATE_PAYMENT; - return $status; + $result['order_id'] = $matches[2] ?? ($transfer->getBody()['transaction']['orderId'] ?? null); + $result['type'] = $matches[3] ?? TypeInterface::INITIATE_PAYMENT; } - return 'undefined'; + return $result; } /** diff --git a/etc/config.xml b/etc/config.xml index eb5a50cb..1d33cb19 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -33,11 +33,6 @@ 1 1 1 - AE,VI,MC,DI,JCB,CUP,DN,MI - 2 - 1 - - develop