Skip to content

Commit

Permalink
Merge pull request #4 from vippsas/publication-1.0.1
Browse files Browse the repository at this point in the history
VIPPS-144: Publication v.1.0.1
  • Loading branch information
voleye authored Jul 20, 2018
2 parents 9b28446 + 6df92db commit 54ddebe
Show file tree
Hide file tree
Showing 31 changed files with 1,019 additions and 1,003 deletions.
3 changes: 3 additions & 0 deletions Api/CommandManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
namespace Vipps\Payment\Api;

use Magento\Payment\{Gateway\Command\ResultInterface, Model\InfoInterface};
use Vipps\Payment\Gateway\Exception\VippsException;

/**
* Interface CommandManagerInterface
Expand All @@ -40,6 +41,7 @@ public function initiatePayment(InfoInterface $payment, $arguments);
* @param array $arguments
*
* @return mixed
* @throws VippsException
*/
public function getPaymentDetails($arguments = []);

Expand All @@ -49,6 +51,7 @@ public function getPaymentDetails($arguments = []);
* @param string $orderId
*
* @return ResultInterface|null
* @throws VippsException
*/
public function getOrderStatus($orderId);
}
123 changes: 60 additions & 63 deletions Controller/Payment/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,22 @@
namespace Vipps\Payment\Controller\Payment;

use Magento\Framework\{
Controller\ResultFactory, App\Action\Action, App\Action\Context, Serialize\Serializer\Json,
Session\SessionManagerInterface, Controller\ResultInterface, App\ResponseInterface
Controller\ResultFactory,
App\Action\Action,
App\Action\Context,
Controller\ResultInterface,
App\ResponseInterface,
Serialize\Serializer\Json
};
use Vipps\Payment\{
Api\CommandManagerInterface, Gateway\Request\Initiate\MerchantDataBuilder, Gateway\Transaction\TransactionBuilder,
Model\OrderManagement
Gateway\Request\Initiate\MerchantDataBuilder,
Gateway\Transaction\TransactionBuilder,
Model\OrderPlace,
Model\QuoteLocator
};
use Magento\Quote\{
Api\Data\CartInterface, Model\Quote
};
use Magento\Quote\{Api\CartRepositoryInterface, Model\Quote};
use Zend\Http\Response as ZendResponse;
use Psr\Log\LoggerInterface;

Expand All @@ -35,24 +43,14 @@
class Callback extends Action
{
/**
* @var SessionManagerInterface
*/
private $checkoutSession;

/**
* @var CommandManagerInterface
* @var OrderPlace
*/
private $commandManager;
private $orderPlace;

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

/**
* @var OrderManagement
*/
private $orderManagement;
private $quoteLocator;

/**
* @var Json
Expand All @@ -69,33 +67,32 @@ class Callback extends Action
*/
private $logger;

/**
* @var CartInterface
*/
private $quote;

/**
* Callback constructor.
*
* @param Context $context
* @param SessionManagerInterface $checkoutSession
* @param CommandManagerInterface $commandManager
* @param CartRepositoryInterface $cartRepository
* @param OrderManagement $orderManagement
* @param OrderPlace $orderManagement
* @param QuoteLocator $quoteLocator
* @param Json $jsonDecoder
* @param TransactionBuilder $transactionBuilder
* @param LoggerInterface $logger
*/
public function __construct(
Context $context,
SessionManagerInterface $checkoutSession,
CommandManagerInterface $commandManager,
CartRepositoryInterface $cartRepository,
OrderManagement $orderManagement,
OrderPlace $orderManagement,
QuoteLocator $quoteLocator,
Json $jsonDecoder,
TransactionBuilder $transactionBuilder,
LoggerInterface $logger
) {
parent::__construct($context);
$this->checkoutSession = $checkoutSession;
$this->commandManager = $commandManager;
$this->cartRepository = $cartRepository;
$this->orderManagement = $orderManagement;
$this->orderPlace = $orderManagement;
$this->quoteLocator = $quoteLocator;
$this->jsonDecoder = $jsonDecoder;
$this->transactionBuilder = $transactionBuilder;
$this->logger = $logger;
Expand All @@ -110,39 +107,46 @@ public function execute()
{
$result = $this->resultFactory->create(ResultFactory::TYPE_JSON);
try {
$requestContent = $this->getRequest()->getContent();
$requestData = $this->jsonDecoder->unserialize($requestContent);

if (!$this->isValid($requestData)) {
throw new \Exception(__('Invalid request parameters'), 400); //@codingStandardsIgnoreLine
}
$requestData = $this->jsonDecoder->unserialize($this->getRequest()->getContent());

if (!$this->isAuthorized($requestData)) {
throw new \Exception(__('Invalid request'), 401); //@codingStandardsIgnoreLine
}
$this->authorize($requestData);

// create transaction object
$transaction = $this->transactionBuilder->setData($requestData)->build();
$this->orderPlace->execute($this->getQuote($requestData), $transaction);

// place order if not exist in Magento and update order status based on transaction info
$this->orderManagement->place($this->getOrderId($requestData), $transaction);

/** @var Json $result */
$result->setHttpResponseCode(ZendResponse::STATUS_CODE_200);
$result->setData([
'status' => ZendResponse::STATUS_CODE_200,
'message' => 'success'
]);
$result->setData(['status' => ZendResponse::STATUS_CODE_200, 'message' => 'success']);
} catch (\Exception $e) {
$this->logger->critical($e->getMessage());
$result->setHttpResponseCode(ZendResponse::STATUS_CODE_500);
$result->setData([
'status' => ZendResponse::STATUS_CODE_500,
'message' => __('An error occurred during callback processing.')
]);
} finally {
$this->logger->debug($this->getRequest()->getContent());
}
return $result;
}

/**
* @param $requestData
*
* @return bool
* @throws \Exception
*/
private function authorize($requestData)
{
if (!$this->isValid($requestData)) {
throw new \Exception(__('Invalid request parameters'), 400); //@codingStandardsIgnoreLine
}
if (!$this->isAuthorized($requestData)) {
throw new \Exception(__('Invalid request'), 401); //@codingStandardsIgnoreLine
}
return true;
}

/**
* Method to validate request body parameters.
*
Expand Down Expand Up @@ -175,19 +179,20 @@ private function getOrderId($requestData)
*
* @param $requestData
*
* @return Quote|mixed
* @throws \Magento\Framework\Exception\NotFoundException
* @return bool|CartInterface|Quote
*/
private function getQuote($requestData)
{
return $this->orderManagement->getQuoteByReservedOrderId($this->getOrderId($requestData));
if (null === $this->quote) {
$this->quote = $this->quoteLocator->get($this->getOrderId($requestData)) ?: false;
}
return $this->quote;
}

/**
* @param $requestData
* @param array $requestData
*
* @return bool
* @throws \Magento\Framework\Exception\LocalizedException
*/
private function isAuthorized($requestData): bool
{
Expand All @@ -196,18 +201,10 @@ private function isAuthorized($requestData): bool
$additionalInfo = $quote->getPayment()->getAdditionalInformation();
$authToken = $additionalInfo[MerchantDataBuilder::MERCHANT_AUTH_TOKEN] ?? null;

if ($authToken !== $this->getRequest()->getHeader('authorization')) {
return false;
if ($authToken === $this->getRequest()->getHeader('authorization')) {
return true;
}

// clear merchant auth token when success
$additionalInfo[MerchantDataBuilder::MERCHANT_AUTH_TOKEN] = null;
$quote->getPayment()->setAdditionalInformation($additionalInfo);
$this->cartRepository->save($quote);

return true;
}

return false;
}
}
11 changes: 1 addition & 10 deletions Controller/Payment/Express.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
Controller\ResultFactory, Controller\ResultInterface, App\Action\Context, App\Action\Action,
Exception\LocalizedException, App\ResponseInterface, Session\SessionManagerInterface
};
use Magento\Quote\Model\QuoteRepository;
use Magento\Payment\Gateway\ConfigInterface;
use Vipps\Payment\{
Api\CommandManagerInterface ,Gateway\Exception\VippsException, Gateway\Request\Initiate\MerchantDataBuilder
Expand Down Expand Up @@ -53,11 +52,6 @@ class Express extends Action
*/
private $config;

/**
* @var QuoteRepository
*/
private $quoteRepository;

/**
* Express constructor.
*
Expand All @@ -66,22 +60,19 @@ class Express extends Action
* @param SessionManagerInterface $session
* @param LoggerInterface $logger
* @param ConfigInterface $config
* @param QuoteRepository $quoteRepository
*/
public function __construct(
Context $context,
CommandManagerInterface $commandManager,
SessionManagerInterface $session,
LoggerInterface $logger,
ConfigInterface $config,
QuoteRepository $quoteRepository
ConfigInterface $config
) {
parent::__construct($context);
$this->commandManager = $commandManager;
$this->session = $session;
$this->logger = $logger;
$this->config = $config;
$this->quoteRepository = $quoteRepository;
}

/**
Expand Down
Loading

0 comments on commit 54ddebe

Please sign in to comment.