Skip to content

Commit

Permalink
Merge pull request #28 from david-fiaty-cko/master
Browse files Browse the repository at this point in the history
Checkout.com Magento 2 Module - v1.0.11
  • Loading branch information
nicolas-maalouf-cko authored Nov 9, 2017
2 parents 304f270 + dc43fe2 commit 90c95ee
Show file tree
Hide file tree
Showing 29 changed files with 1,082 additions and 204 deletions.
9 changes: 7 additions & 2 deletions Block/Customer/CardRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
namespace CheckoutCom\Magento2\Block\Customer;

use Magento\Payment\Model\CcConfigProvider;
use CheckoutCom\Magento2\Model\Ui\ConfigProvider;
use Magento\Framework\View\Element\Template;
use Magento\Vault\Api\Data\PaymentTokenInterface;
use Magento\Vault\Block\AbstractCardRenderer;
use CheckoutCom\Magento2\Model\Ui\ConfigProvider;
use CheckoutCom\Magento2\Gateway\Config\Config as GatewayConfig;

class CardRenderer extends AbstractCardRenderer {
Expand All @@ -31,7 +31,12 @@ class CardRenderer extends AbstractCardRenderer {
* @param GatewayConfig $gatewayConfig
* @param array $data
*/
public function __construct(Template\Context $context, CcConfigProvider $iconsProvider, GatewayConfig $gatewayConfig, array $data) {
public function __construct(
Template\Context $context,
CcConfigProvider $iconsProvider,
GatewayConfig $gatewayConfig,
array $data
) {
parent::__construct($context, $iconsProvider, $data);

$this->gatewayConfig = $gatewayConfig;
Expand Down
119 changes: 96 additions & 23 deletions Controller/Payment/PlaceOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,33 @@
namespace CheckoutCom\Magento2\Controller\Payment;

use Magento\Framework\App\Action\Context;
use Magento\Checkout\Model\Session;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Customer\Model\Session as CustomerSession;
use CheckoutCom\Magento2\Gateway\Config\Config as GatewayConfig;
use CheckoutCom\Magento2\Model\Service\OrderService;
use Magento\Customer\Api\Data\GroupInterface;
use Magento\Sales\Api\Data\OrderInterface;
use CheckoutCom\Magento2\Model\Ui\ConfigProvider;
use Magento\Sales\Model\Order\Payment\Transaction;
use Magento\Sales\Model\Order\Payment\Transaction\BuilderInterface;
use CheckoutCom\Magento2\Model\Service\TokenChargeService;

class PlaceOrder extends AbstractAction {

/**
* @var Session
* @var TokenChargeService
*/
protected $session;
protected $tokenChargeService;

/**
* @var CheckoutSession
*/
protected $checkoutSession;

/**
* @var OrderInterface
*/
protected $orderInterface;

/**
* @var OrderService
Expand All @@ -37,16 +52,29 @@ class PlaceOrder extends AbstractAction {
/**
* PlaceOrder constructor.
* @param Context $context
* @param Session $session
* @param CheckoutSession $checkoutSession
* @param GatewayConfig $gatewayConfig
* @param OrderInterface $orderInterface
* @param OrderService $orderService
* @param Order $orderManager
* @param OrderSender $orderSender
*/
public function __construct(Context $context, Session $session, GatewayConfig $gatewayConfig, OrderService $orderService, CustomerSession $customerSession) {
public function __construct(
Context $context,
CheckoutSession $checkoutSession,
GatewayConfig $gatewayConfig,
OrderService $orderService,
OrderInterface $orderInterface,
CustomerSession $customerSession,
TokenChargeService $tokenChargeService
) {
parent::__construct($context, $gatewayConfig);

$this->session = $session;
$this->customerSession = $customerSession;
$this->orderService = $orderService;
$this->checkoutSession = $checkoutSession;
$this->customerSession = $customerSession;
$this->orderService = $orderService;
$this->orderInterface = $orderInterface;
$this->tokenChargeService = $tokenChargeService;
}

/**
Expand All @@ -55,45 +83,90 @@ public function __construct(Context $context, Session $session, GatewayConfig $g
* @return \Magento\Framework\Controller\Result\Redirect
*/
public function execute() {

// Retrieve the request parameters
$resultRedirect = $this->getResultRedirect();
$cardToken = $this->getRequest()->getParam('cko-card-token');
$email = $this->getRequest()->getParam('cko-context-id');
$agreement = array_keys($this->getRequest()->getPostValue('agreement', []));
$quote = $this->session->getQuote();
$params = array(
'cardToken' => $this->getRequest()->getParam('cko-card-token'),
'email' => $this->getRequest()->getParam('cko-context-id'),
'agreement' => array_keys($this->getRequest()->getPostValue('agreement', [])),
'quote' => $this->checkoutSession->getQuote()
);

$order = null;
if (isset($this->customerSession->getData('checkoutSessionData')['orderTrackId'])) {
$order = $this->orderInterface->loadByIncrementId($this->customerSession->getData('checkoutSessionData')['orderTrackId']);
}

if ($order) {
$this->updateOrder($params, $order);
}
else {
$this->createOrder($params);
}
}

public function updateOrder($params, $order) {

// Create the charge for order already placed
$updateSuccess = $this->tokenChargeService->sendChargeRequest($params['cardToken'], $order);

// Update payment data
$order = $this->updatePaymentData($order);

// 3D Secure redirection if needed
if($this->gatewayConfig->isVerify3DSecure()) {
$this->place3DSecureRedirectUrl();
exit();
}

return $this->_redirect('checkout/onepage/success', ['_secure' => true]);
}

public function createOrder($params) {

// Check for guest email
if ($quote->getCustomerEmail() === null
if ($params['quote']->getCustomerEmail() === null
&& $this->customerSession->isLoggedIn() === false
&& isset($this->customerSession->getData('checkoutSessionData')['customerEmail'])
&& $this->customerSession->getData('checkoutSessionData')['customerEmail'] === $email)
&& $this->customerSession->getData('checkoutSessionData')['customerEmail'] === $params['email'])
{
$quote->setCustomerId(null)
->setCustomerEmail($email)
$params['quote']->setCustomerId(null)
->setCustomerEmail($params['email'])
->setCustomerIsGuest(true)
->setCustomerGroupId(GroupInterface::NOT_LOGGED_IN_ID);
}

// Perform quote and order validation
try {

// Create an order from the quote
$this->validateQuote($quote);
$this->orderService->execute($quote, $cardToken, $agreement);
$this->validateQuote($params['quote']);
$this->orderService->execute($params['quote'], $params['cardToken'], $params['agreement']);

// 3D Secure redirection if needed
if($this->gatewayConfig->isVerify3DSecure()) {
$this->place3DSecureRedirectUrl();
exit();
}

return $resultRedirect->setPath('checkout/onepage/success', ['_secure' => true]);
return $this->_redirect('checkout/onepage/success', ['_secure' => true]);

} catch (\Exception $e) {
$this->messageManager->addExceptionMessage($e, $e->getMessage());
}

return $resultRedirect->setPath('checkout/cart', ['_secure' => true]);
return $this->_redirect('checkout/cart', ['_secure' => true]);
}

public function updatePaymentData($order) {
// Load payment object
$payment = $order->getPayment();

// Set the payment method, previously "substitution" for pre auth order creation
$payment->setMethod(ConfigProvider::CODE);
$payment->save();
$order->save();

return $order;
}

/**
Expand All @@ -104,7 +177,7 @@ public function execute() {
public function place3DSecureRedirectUrl() {
echo '<script type="text/javascript">';
echo 'function waitForElement() {';
echo 'var redirectUrl = "' . $this->session->get3DSRedirect() . '";';
echo 'var redirectUrl = "' . $this->checkoutSession->get3DSRedirect() . '";';
echo 'if (redirectUrl.length != 0){ window.location.replace(redirectUrl); }';
echo 'else { setTimeout(waitForElement, 250); }';
echo '} ';
Expand Down
130 changes: 130 additions & 0 deletions Controller/Payment/PlaceOrderAjax.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php
/**
* Checkout.com Magento 2 Payment module (https://www.checkout.com)
*
* Copyright (c) 2017 Checkout.com (https://www.checkout.com)
* Author: David Fiaty | [email protected]
*
* License GNU/GPL V3 https://www.gnu.org/licenses/gpl-3.0.en.html
*/

namespace CheckoutCom\Magento2\Controller\Payment;

use Magento\Framework\App\Action\Context;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Customer\Model\Session as CustomerSession;
use CheckoutCom\Magento2\Gateway\Config\Config as GatewayConfig;
use CheckoutCom\Magento2\Model\Service\OrderService;
use Magento\Customer\Api\Data\GroupInterface;
use Magento\Quote\Model\QuoteManagement;
use CheckoutCom\Magento2\Model\Ui\ConfigProvider;
use Magento\Framework\Controller\Result\JsonFactory;

class PlaceOrderAjax extends AbstractAction {

/**
* @var QuoteManagement
*/
protected $quoteManagement;

/**
* @var CheckoutSession
*/
protected $checkoutSession;

/**
* @var OrderService
*/
protected $orderService;

/**
* @var CustomerSession
*/
protected $customerSession;

/**
* @var JsonFactory
*/
protected $resultJsonFactory;

/**
* PlaceOrder constructor.
* @param Context $context
* @param CheckoutSession $checkoutSession
* @param GatewayConfig $gatewayConfig
* @param OrderService $orderService
* @param JsonFactory $resultJsonFactory
*/
public function __construct(
Context $context,
CheckoutSession $checkoutSession,
GatewayConfig $gatewayConfig,
OrderService $orderService,
CustomerSession $customerSession,
QuoteManagement $quoteManagement,
JsonFactory $resultJsonFactory
) {
parent::__construct($context, $gatewayConfig);

$this->checkoutSession = $checkoutSession;
$this->customerSession = $customerSession;
$this->orderService = $orderService;
$this->quoteManagement = $quoteManagement;
$this->resultJsonFactory = $resultJsonFactory;
}

/**
* Handles the controller method.
*
* @return \Magento\Framework\Controller\Result\Redirect
*/
public function execute() {
// Prepare the redirection
$resultRedirect = $this->getResultRedirect();

// Load the customer quote
$quote = $this->checkoutSession->getQuote();

// Retrieve the request parameters
$cardToken = $this->getRequest()->getParam('cko-card-token');
$email = $this->getRequest()->getParam('cko-context-id');
$agreement = array_keys($this->getRequest()->getPostValue('agreement', []));

// Check for guest email
if ($quote->getCustomerEmail() === null
&& $this->customerSession->isLoggedIn() === false
&& isset($this->customerSession->getData('checkoutSessionData')['customerEmail'])
)
{
$quote->setCustomerId(null)
->setCustomerEmail($email)
->setCustomerIsGuest(true)
->setCustomerGroupId(GroupInterface::NOT_LOGGED_IN_ID)
->save();
}

// Prepare session quote info for redirection after payment
$this->checkoutSession
->setLastQuoteId($quote->getId())
->setLastSuccessQuoteId($quote->getId())
->clearHelperData();

// Set payment
$quote->getPayment()->setMethod('substitution');
$quote->collectTotals()->save();

// Create the order
$order = $this->quoteManagement->submit($quote);

// Prepare session order info for redirection after payment
if ($order) {
$this->checkoutSession->setLastOrderId($order->getId())
->setLastRealOrderId($order->getIncrementId())
->setLastOrderStatus($order->getStatus());
}

return $this->resultJsonFactory->create()->setData([
'trackId' => $order->getIncrementId()
]);
}
}
Loading

0 comments on commit 90c95ee

Please sign in to comment.