Skip to content

Commit

Permalink
Epayment api + Mobilepay (#185)
Browse files Browse the repository at this point in the history
* Migrate to ecomm api.
* Switcher from ecomm api to epayment.
* Update labels for mobile ecomm, allow NOK, EUR currencies.
* Extend graphql for pwa mobilepay.
* Add license.
* Update mobilepay logo.
* Update unit tests.
* Update phpcs config.
* Always capture when mobilepay is selected.
* Update links and notes at configuration.
* Unlock authorize and capture for mobilepay.
* Fix implicit convert to int.

---------

Co-authored-by: Oleg Malichenko <[email protected]>
  • Loading branch information
aluminium1989 and olegmalichenkoatvaimo authored Jun 11, 2024
1 parent 9be9e1a commit 0a59dcb
Show file tree
Hide file tree
Showing 122 changed files with 7,975 additions and 387 deletions.
27 changes: 27 additions & 0 deletions Api/Fallback/AuthoriseInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types=1);

/**
* Copyright 2023 Vipps
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
namespace Vipps\Payment\Api\Fallback;

use Magento\Framework\App\RequestInterface;
use Vipps\Payment\Api\Data\QuoteInterface;

interface AuthoriseInterface
{
public function do(RequestInterface $request, QuoteInterface $vippsQuote): void;

public function getOrderId(RequestInterface $request): string;
}
77 changes: 77 additions & 0 deletions Api/Payment/CommandManagerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php declare(strict_types=1);

/**
* Copyright 2023 Vipps
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

namespace Vipps\Payment\Api\Payment;

use Magento\Payment\Gateway\Command\ResultInterface;
use Magento\Payment\Model\InfoInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Vipps\Payment\Gateway\Exception\VippsException;

/**
* Interface PaymentCommandManagerInterface
* @package Vipps\Payment\Api
* @api
*/
interface CommandManagerInterface
{
/**
* @param string $reference
* @param array $arguments
*
* @return mixed
*/
public function getPayment($reference, $arguments = []);

/**
* @param string $orderId
* @param array $arguments
*
* @return mixed
*/
public function getPaymentEventLog($orderId, $arguments = []);

/**
* Send Receipt.
*
* @param array $arguments
*
* @return mixed
* @throws VippsException
*/
public function sendReceipt(OrderInterface $order, $arguments = []);

/**
* Method to execute cancel Command.
*
* @param InfoInterface $payment
* @param array $arguments
*
* @return mixed
*/
public function cancel(InfoInterface $payment, $arguments = []);

/**
* Initiate payment action.
*
* @param InfoInterface $payment
* @param array $arguments
*
* @return ResultInterface|null
*/
public function initiatePayment(InfoInterface $payment, $arguments);
}
10 changes: 10 additions & 0 deletions Api/Transaction/PaymentDetailsInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Vipps\Payment\Api\Transaction;

interface PaymentDetailsInterface
{
public function get(string $orderId);
}
12 changes: 8 additions & 4 deletions Block/Express/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

namespace Vipps\Payment\Block\Express;

use Magento\Framework\View\Element\Template;
Expand All @@ -21,6 +22,7 @@
use Magento\Framework\Math\Random;
use Magento\Payment\Gateway\ConfigInterface;
use Magento\Catalog\Block\ShortcutInterface;
use Vipps\Payment\Model\Config\Source\Version;

/**
* Class Button
Expand Down Expand Up @@ -67,9 +69,9 @@ class Button extends Template implements ShortcutInterface
*/
public function __construct(
Template\Context $context,
Random $mathRandom,
ConfigInterface $config,
array $data = []
Random $mathRandom,
ConfigInterface $config,
array $data = []
) {
$this->config = $config;
$this->assetRepo = $context->getAssetRepository();
Expand All @@ -87,7 +89,9 @@ public function __construct(
protected function _toHtml() //@codingStandardsIgnoreLine
{
if (!$this->config->getValue('active')
|| !$this->config->getValue('express_checkout')) {
|| !$this->config->getValue('express_checkout')
|| $this->config->getValue('version') !== Version::CONFIG_VIPPS
) {
return '';
}
if (!$this->getIsInCatalogProduct() &&
Expand Down
73 changes: 46 additions & 27 deletions Controller/Payment/Fallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@
use Psr\Log\LoggerInterface;
use Vipps\Payment\Api\Data\QuoteInterface;
use Vipps\Payment\Api\QuoteRepositoryInterface;
use Vipps\Payment\Gateway\Config\Config;
use Vipps\Payment\Gateway\Transaction\Transaction;
use Vipps\Payment\GatewayEpayment\Data\Payment;
use Vipps\Payment\Model\Fallback\AuthoriseProxy;
use Vipps\Payment\Model\Gdpr\Compliance;
use Vipps\Payment\Model\OrderLocator;
use Vipps\Payment\Model\Transaction\StatusVisitor;
use Vipps\Payment\Model\TransactionProcessor;

/**
Expand Down Expand Up @@ -106,7 +110,7 @@ class Fallback implements ActionInterface, CsrfAwareActionInterface
private $resultFactory;

/**
* @var ConfigInterface
* @var Config
*/
private $config;

Expand All @@ -119,6 +123,8 @@ class Fallback implements ActionInterface, CsrfAwareActionInterface
* @var OrderInterface|null
*/
private $order;
private StatusVisitor $statusVisitor;
private AuthoriseProxy $authoriseProxy;

/**
* Fallback constructor.
Expand All @@ -134,23 +140,25 @@ class Fallback implements ActionInterface, CsrfAwareActionInterface
* @param OrderLocator $orderLocator
* @param Compliance $compliance
* @param LoggerInterface $logger
* @param ConfigInterface $config
* @param Config $config
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
ResultFactory $resultFactory,
RequestInterface $request,
SessionManagerInterface $checkoutSession,
TransactionProcessor $transactionProcessor,
CartRepositoryInterface $cartRepository,
ManagerInterface $messageManager,
ResultFactory $resultFactory,
RequestInterface $request,
SessionManagerInterface $checkoutSession,
TransactionProcessor $transactionProcessor,
CartRepositoryInterface $cartRepository,
ManagerInterface $messageManager,
QuoteRepositoryInterface $vippsQuoteRepository,
OrderManagementInterface $orderManagement,
OrderLocator $orderLocator,
Compliance $compliance,
LoggerInterface $logger,
ConfigInterface $config
OrderLocator $orderLocator,
Compliance $compliance,
LoggerInterface $logger,
Config $config,
StatusVisitor $statusVisitor,
AuthoriseProxy $authoriseProxy
) {
$this->resultFactory = $resultFactory;
$this->request = $request;
Expand All @@ -164,6 +172,8 @@ public function __construct(
$this->orderManagement = $orderManagement;
$this->logger = $logger;
$this->config = $config;
$this->statusVisitor = $statusVisitor;
$this->authoriseProxy = $authoriseProxy;
}

/**
Expand All @@ -176,9 +186,10 @@ public function execute()
/** @var Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
try {
$this->authorize();

$vippsQuote = $this->getVippsQuote();

$this->authoriseProxy->do($this->request, $vippsQuote);

$transaction = $this->transactionProcessor->process($vippsQuote);

$resultRedirect = $this->prepareResponse($resultRedirect, $transaction);
Expand All @@ -196,7 +207,7 @@ public function execute()
$cartPersistence = $this->config->getValue('cancellation_cart_persistence');

$quoteCouldBeRestored = $transaction
&& ($transaction->transactionWasCancelled() || $transaction->isTransactionExpired());
&& ($this->statusVisitor->isCanceled($transaction) || $this->statusVisitor->isExpired($transaction));
$order = $this->getOrder();

if ($quoteCouldBeRestored && $cartPersistence) {
Expand Down Expand Up @@ -285,20 +296,20 @@ private function getVippsQuote($forceReload = false): QuoteInterface
{
if (null === $this->vippsQuote || $forceReload) {
$this->vippsQuote = $this->vippsQuoteRepository
->loadByOrderId($this->request->getParam('order_id'));
->loadByOrderId($this->authoriseProxy->getOrderId($this->request));
}

return $this->vippsQuote;
}

/**
* @param Redirect $resultRedirect
* @param Transaction $transaction
* @param Transaction|Payment $transaction
*
* @return Redirect
* @throws \Exception
*/
private function prepareResponse(Redirect $resultRedirect, Transaction $transaction)
private function prepareResponse(Redirect $resultRedirect, $transaction)
{
$this->defineMessage($transaction);
$this->defineRedirectPath($resultRedirect, $transaction);
Expand Down Expand Up @@ -344,13 +355,19 @@ private function isCartPersistent()
return $this->config->getValue('cancellation_cart_persistence');
}

private function defineMessage(Transaction $transaction): void
/**
* @param Transaction|Payment $transaction
* @return void
*/
private function defineMessage($transaction): void
{
if ($transaction->transactionWasCancelled()) {
$this->messageManager->addWarningMessage(__('Your order was cancelled in Vipps.'));
} elseif ($transaction->isTransactionReserved() || $transaction->isTransactionCaptured()) {
//$this->messageManager->addWarningMessage(__('Your order was successfully placed.'));
} elseif ($transaction->isTransactionExpired()) {
if ($this->statusVisitor->isCanceled($transaction)) {
$this->messageManager->addWarningMessage(__('Your order was cancelled in %1.', $this->config->getTitle()));
} elseif (
$this->statusVisitor->isReserved($transaction)
|| $this->statusVisitor->isCaptured($transaction)) {
$this->messageManager->addWarningMessage(__('Your order was successfully placed.'));
} elseif ($this->statusVisitor->isExpired($transaction)) {
$this->messageManager->addErrorMessage(
__('Transaction was expired. Please, place your order again')
);
Expand All @@ -363,13 +380,15 @@ private function defineMessage(Transaction $transaction): void

/**
* @param Redirect $resultRedirect
* @param Transaction $transaction
* @param Transaction|Payment $transaction
*
* @throws NoSuchEntityException
*/
private function defineRedirectPath(Redirect $resultRedirect, Transaction $transaction): void
private function defineRedirectPath(Redirect $resultRedirect, $transaction): void
{
if ($transaction->isTransactionReserved() || $transaction->isTransactionCaptured()) {
if ($this->statusVisitor->isReserved($transaction)
|| $this->statusVisitor->isCaptured($transaction)
) {
$resultRedirect->setPath('checkout/onepage/success', ['_secure' => true]);
} else {
$orderId = $this->getOrder() ? $this->getOrder()->getEntityId() : null;
Expand Down
10 changes: 5 additions & 5 deletions Controller/Payment/InitExpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\Session\SessionManagerInterface;
use Magento\Payment\Gateway\ConfigInterface;
use Magento\Payment\Gateway\Command\ResultInterface;
use Magento\Quote\Api\CartRepositoryInterface;
use Psr\Log\LoggerInterface;
use Vipps\Payment\Api\CommandManagerInterface;
use Vipps\Payment\Gateway\Config\Config;
use Vipps\Payment\Gateway\Request\Initiate\InitiateBuilderInterface;
use Vipps\Payment\Model\Method\Vipps;

Expand Down Expand Up @@ -74,7 +74,7 @@ class InitExpress implements ActionInterface
private $frameworkExceptionFactory;

/**
* @var ConfigInterface
* @var Config
*/
private $config;

Expand Down Expand Up @@ -102,7 +102,7 @@ class InitExpress implements ActionInterface
* @param CommandManagerInterface $commandManager
* @param CartRepositoryInterface $cartRepository
* @param LocalizedExceptionFactory $frameworkExceptionFactory
* @param ConfigInterface $config
* @param Config $config
* @param ManagerInterface $messageManager
* @param CheckoutHelper $checkoutHelper
* @param LoggerInterface $logger
Expand All @@ -116,7 +116,7 @@ public function __construct(
CommandManagerInterface $commandManager,
CartRepositoryInterface $cartRepository,
LocalizedExceptionFactory $frameworkExceptionFactory,
ConfigInterface $config,
Config $config,
ManagerInterface $messageManager,
CheckoutHelper $checkoutHelper,
LoggerInterface $logger
Expand Down Expand Up @@ -155,7 +155,7 @@ public function execute()
} catch (\Exception $e) {
$this->logger->critical($this->enlargeMessage($e));
$this->messageManager->addErrorMessage(
__('An error occurred during request to Vipps. Please try again later.')
__('An error occurred during request to %1. Please try again later.', $this->config->getTitle())
);
$resultRedirect->setPath('checkout/cart', ['_secure' => true]);
}
Expand Down
Loading

0 comments on commit 0a59dcb

Please sign in to comment.