Skip to content

Commit

Permalink
Restructured the logic of starting a transaction.
Browse files Browse the repository at this point in the history
Moved the logic to the model.
Removed the set-payment-method javascript action, now using the default placeOrder logic from magento
  • Loading branch information
Andy Pieters committed Jan 7, 2016
1 parent da1cf8b commit 426e112
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 222 deletions.
12 changes: 2 additions & 10 deletions Controller/Checkout/Finish.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,8 @@ public function execute()
} else {
//canceled, re-activate quote
try {
// if there is an order - cancel it
/** @var \Magento\Sales\Model\Order $order */
$order = $this->_getCheckoutSession()->getLastRealOrder();
if ($order && $order->getId() && $order->getQuoteId() == $this->_getCheckoutSession()->getQuoteId()) {
$order->cancel()->save();
$this->_getCheckoutSession()->restoreQuote();
$this->messageManager->addNotice(__('Payment canceled'));
} else {
$this->messageManager->addNotice(__('Payment canceled, but unable to cancel order'));
}
$this->_getCheckoutSession()->restoreQuote();
$this->messageManager->addNotice(__('Payment canceled'));
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$this->_logger->error($e);
$this->messageManager->addExceptionMessage($e, $e->getMessage());
Expand Down
165 changes: 45 additions & 120 deletions Controller/Checkout/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

namespace Paynl\Payment\Controller\Checkout;

use Magento\Payment\Helper\Data as PaymentHelper;
use Paynl\Error\Error;

/**
* Description of Redirect
*
Expand All @@ -22,150 +25,72 @@ class Redirect extends \Magento\Framework\App\Action\Action
*/
protected $messageManager;

/**
* @var \Magento\Checkout\Model\Session
*/
protected $_checkoutSession;

/**
* @var \Psr\Log\LoggerInterface
*/
protected $_logger;

/**
* @var PaymentHelper
*/
protected $_paymentHelper;

/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Paynl\Payment\Model\Config $config
* @param \Magento\Framework\Message\ManagerInterface $messageManager
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Paynl\Payment\Model\Config $config
\Paynl\Payment\Model\Config $config,
\Magento\Checkout\Model\Session $checkoutSession,
\Psr\Log\LoggerInterface $logger,
PaymentHelper $paymentHelper
)
{
$this->_config = $config; // Pay.nl config helper
$this->_checkoutSession = $checkoutSession;
$this->_logger = $logger;
$this->_paymentHelper = $paymentHelper;

parent::__construct($context);
}

public function execute()
{
try {
/** @var \Magento\Checkout\Model\Type\Onepage $onepage */
$onepage = $this->_objectManager->get('Magento\Checkout\Model\Type\Onepage');

/** @var \Magento\Quote\Model\Quote $quote */
$quote = $onepage->getQuote();

$quote->collectTotals();

$quote->reserveOrderId();

$orderId = $quote->getReservedOrderId();

$payment = $quote->getPayment()->getMethodInstance();

$total = $quote->getGrandTotal();
$items = $quote->getAllVisibleItems();

$currency = $quote->getQuoteCurrencyCode();

$returnUrl = $this->_url->getUrl('paynl/checkout/finish/');
$exchangeUrl = $this->_url->getUrl('paynl/checkout/exchange/');

$paymentOptionId = $payment->getPaymentOptionId();

$order = $this->_getCheckoutSession()->getLastRealOrder();

$arrBillingAddress = $quote->getBillingAddress()->toArray();
$method = $order->getPayment()->getMethod();

$arrShippingAddress = $quote->getShippingAddress()->toArray();

$enduser = array(
'initials' => substr($arrBillingAddress['firstname'], 0, 1),
'lastName' => $arrBillingAddress['lastname'],
'phoneNumber' => $arrBillingAddress['telephone'],
'emailAddress' => $arrBillingAddress['email'],
);

$address = array();
$arrAddress = \Paynl\Helper::splitAddress($arrBillingAddress['street']);
$address['streetName'] = $arrAddress[0];
$address['houseNumber'] = $arrAddress[1];
$address['zipCode'] = $arrBillingAddress['postcode'];
$address['city'] = $arrBillingAddress['city'];
$address['country'] = $arrBillingAddress['country_id'];

$shippingAddress = array();
$arrAddress2 = \Paynl\Helper::splitAddress($arrShippingAddress['street']);
$shippingAddress['streetName'] = $arrAddress2[0];
$shippingAddress['houseNumber'] = $arrAddress2[1];
$shippingAddress['zipCode'] = $arrShippingAddress['postcode'];
$shippingAddress['city'] = $arrShippingAddress['city'];
$shippingAddress['country'] = $arrShippingAddress['country_id'];

$data = array(
'amount' => $total,
'returnUrl' => $returnUrl,
'paymentMethod' => $paymentOptionId,
'description' => $orderId,
'extra1' => $orderId,
'extra1' => $quote->getId(),
'exchangeUrl' => $exchangeUrl,
'currency' => $currency,
);
$data['address'] = $address;
$data['shippingAddress'] = $shippingAddress;

$data['enduser'] = $enduser;
$arrProducts = array();
foreach ($items as $item) {
$arrItem = $item->toArray();
if ($arrItem['price_incl_tax'] != null) {
$product = array(
'id' => $arrItem['product_id'],
'name' => $arrItem['name'],
'price' => $arrItem['price_incl_tax'],
'qty' => $arrItem['qty'],
'tax' => $arrItem['tax_amount'],
);
}
$arrProducts[] = $product;
}

//shipping
$shippingCost = $quote->getShippingAddress()->getShippingInclTax();
$shippingTax = $quote->getShippingAddress()->getShippingTaxAmount();
$shippingDescription = $quote->getShippingAddress()->getShippingDescription();

$arrProducts[] = array(
'id' => 'shipping',
'name' => $shippingDescription,
'price' => $shippingCost,
'qty' => 1,
'tax' => $shippingTax
);

// kortingen
$discount = $quote->getSubtotal() - $quote->getSubtotalWithDiscount();


if ($discount > 0) {
$arrProducts[] = array(
'id' => 'discount',
'name' => __('Discount'),
'price' => $discount * -1,
'qty' => 1,
'tax' => 0
);
}

$data['products'] = $arrProducts;
if ($this->_config->isTestMode()) {
$data['testmode'] = 1;
$methodInstance = $this->_paymentHelper->getMethodInstance($method);
if ($methodInstance instanceof \Paynl\Payment\Model\Paymentmethod\Paymentmethod) {
$redirectUrl = $methodInstance->startTransaction($order, $this->_url);
$this->_redirect($redirectUrl);
} else {
throw new Error('Method is not a paynl payment method');
}
$data['ipaddress'] = $quote->getRemoteIp();

\Paynl\Config::setApiToken($this->_config->getApiToken());
\Paynl\Config::setServiceId($this->_config->getServiceId());

$transaction = \Paynl\Transaction::start($data);

$onepage->saveOrder();

$this->_redirect($transaction->getRedirectUrl());
} catch(\Exception $e){
} catch (\Exception $e) {
$this->messageManager->addException($e, __('Something went wrong, please try again later'));
$this->_logger->critical($e);
$this->_getCheckoutSession()->restoreQuote();
$this->_redirect('checkout/cart');
}
}

/**
* Return checkout session object
*
* @return \Magento\Checkout\Model\Session
*/
protected function _getCheckoutSession()
{
return $this->_checkoutSession;
}
}
134 changes: 116 additions & 18 deletions Model/Paymentmethod/PaymentMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

namespace Paynl\Payment\Model\Paymentmethod;

use Magento\Framework\UrlInterface;
use \Magento\Payment\Model\Method\AbstractMethod;
use Magento\Sales\Model\Order;
use Paynl\Payment\Model\Config;

/**
* Description of AbstractPaymentMethod
*
Expand All @@ -26,20 +30,6 @@ public function getInstructions()
return trim($this->getConfigData('instructions'));
}

public function initSettings()
{

$storeId = $this->getStore();

$apitoken = $this->_scopeConfig->getValue('payment/paynl/apitoken',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId);
$serviceId = $this->_scopeConfig->getValue('payment/paynl/serviceid',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId);

\Paynl\Config::setApitoken($apitoken);
\Paynl\Config::setServiceId($serviceId);
}

public function getPaymentOptionId(){
return $this->getConfigData('payment_option_id');
}
Expand All @@ -52,9 +42,117 @@ public function initialize($paymentAction, $stateObject)
$stateObject->setIsNotified(false);
}

public function getOrderPlaceRedirectUrl()
{
return true;
}

public function startTransaction(Order $order, UrlInterface $url){
$config = new Config($this->_scopeConfig);

$config->configureSDK();

$total = $order->getGrandTotal();
$items = $order->getAllVisibleItems();

$orderId = $order->getIncrementId();
$quoteId = $order->getQuoteId();

$currency = $order->getOrderCurrencyCode();

$returnUrl = $url->getUrl('paynl/checkout/finish/');
$exchangeUrl = $url->getUrl('paynl/checkout/exchange/');

$paymentOptionId = $this->getPaymentOptionId();

$arrBillingAddress = $order->getBillingAddress()->toArray();

$arrShippingAddress = $order->getShippingAddress()->toArray();

$enduser = array(
'initials' => substr($arrBillingAddress['firstname'], 0, 1),
'lastName' => $arrBillingAddress['lastname'],
'phoneNumber' => $arrBillingAddress['telephone'],
'emailAddress' => $arrBillingAddress['email'],
);

$address = array();
$arrAddress = \Paynl\Helper::splitAddress($arrBillingAddress['street']);
$address['streetName'] = $arrAddress[0];
$address['houseNumber'] = $arrAddress[1];
$address['zipCode'] = $arrBillingAddress['postcode'];
$address['city'] = $arrBillingAddress['city'];
$address['country'] = $arrBillingAddress['country_id'];

$shippingAddress = array();
$arrAddress2 = \Paynl\Helper::splitAddress($arrShippingAddress['street']);
$shippingAddress['streetName'] = $arrAddress2[0];
$shippingAddress['houseNumber'] = $arrAddress2[1];
$shippingAddress['zipCode'] = $arrShippingAddress['postcode'];
$shippingAddress['city'] = $arrShippingAddress['city'];
$shippingAddress['country'] = $arrShippingAddress['country_id'];

$data = array(
'amount' => $total,
'returnUrl' => $returnUrl,
'paymentMethod' => $paymentOptionId,
'description' => $orderId,
'extra1' => $orderId,
'extra2' => $quoteId,
'exchangeUrl' => $exchangeUrl,
'currency' => $currency,
);
$data['address'] = $address;
$data['shippingAddress'] = $shippingAddress;

$data['enduser'] = $enduser;
$arrProducts = array();
foreach ($items as $item) {
$arrItem = $item->toArray();
if ($arrItem['price_incl_tax'] != null) {
$product = array(
'id' => $arrItem['product_id'],
'name' => $arrItem['name'],
'price' => $arrItem['price_incl_tax'],
'qty' => $arrItem['qty_ordered'],
'tax' => $arrItem['tax_amount'],
);
}
$arrProducts[] = $product;
}

//shipping
$shippingCost = $order->getShippingAddress()->getShippingInclTax();
$shippingTax = $order->getShippingAddress()->getShippingTaxAmount();
$shippingDescription = $order->getShippingAddress()->getShippingDescription();

$arrProducts[] = array(
'id' => 'shipping',
'name' => $shippingDescription,
'price' => $shippingCost,
'qty' => 1,
'tax' => $shippingTax
);

// kortingen
$discount = $order->getSubtotal() - $order->getSubtotalWithDiscount();


if ($discount > 0) {
$arrProducts[] = array(
'id' => 'discount',
'name' => __('Discount'),
'price' => $discount * -1,
'qty' => 1,
'tax' => 0
);
}

$data['products'] = $arrProducts;

if ($config->isTestMode()) {
$data['testmode'] = 1;
}
$data['ipaddress'] = $order->getRemoteIp();

$transaction = \Paynl\Transaction::start($data);

return $transaction->getRedirectUrl();
}
}
Loading

0 comments on commit 426e112

Please sign in to comment.