Skip to content

Commit

Permalink
Merge pull request #2 from vippsas/publication-1.0.0
Browse files Browse the repository at this point in the history
VIPPS-142: Publication v.1.0.0
  • Loading branch information
voleye authored Jul 14, 2018
2 parents c5ae8ee + fa5867f commit 9b28446
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 81 deletions.
4 changes: 1 addition & 3 deletions Block/Express/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion Gateway/Transaction/ShippingDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -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] ?? '';
}
}
5 changes: 5 additions & 0 deletions Gateway/Transaction/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class Transaction
*/
const TRANSACTION_STATUS_INITIATE = 'initiate';

/**
* @var string
*/
const TRANSACTION_STATUS_INITIATED = 'initiated';

/**
* @var string
*/
Expand Down
8 changes: 4 additions & 4 deletions Gateway/Transaction/TransactionLogHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TransactionLogHistory extends DataObject
/**
* @var string
*/
private $lastOrderStatus = Transaction::TRANSACTION_OPERATION_CANCEL;
private $lastTransactionStatus = Transaction::TRANSACTION_OPERATION_CANCEL;

/**
* @var string
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}
Expand Down
45 changes: 16 additions & 29 deletions Model/Helper/OrderPlace.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,11 +37,6 @@ class OrderPlace
*/
private $customerSession;

/**
* @var Data
*/
private $checkoutHelper;

/**
* @var CartRepositoryInterface
*/
Expand All @@ -52,48 +48,39 @@ 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;
}

/**
* @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());
}
}
66 changes: 41 additions & 25 deletions Model/Helper/QuoteUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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;
}

/**
Expand All @@ -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);
}

/**
Expand Down
17 changes: 11 additions & 6 deletions Model/OrderManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.'));
}
}
Expand Down
22 changes: 14 additions & 8 deletions Model/Profiling/Profiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}

/**
Expand Down
5 changes: 0 additions & 5 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@
<can_void>1</can_void>
<can_cancel>1</can_cancel>
<can_use_internal>1</can_use_internal>
<cctypes>AE,VI,MC,DI,JCB,CUP,DN,MI</cctypes>
<cc_year_length>2</cc_year_length>
<useccv>1</useccv>
<privateInfoKeys></privateInfoKeys>
<paymentInfoKeys></paymentInfoKeys>
<environment>develop</environment>
<client_secret backend_model="Magento\Config\Model\Config\Backend\Encrypted" />
<subscription_key1 backend_model="Magento\Config\Model\Config\Backend\Encrypted" />
Expand Down

0 comments on commit 9b28446

Please sign in to comment.