Skip to content

Commit

Permalink
New order emails are sent after payment.
Browse files Browse the repository at this point in the history
Also added a setting to each payment method to configure this for every payment method
  • Loading branch information
Andy Pieters committed Nov 8, 2017
1 parent 4616a81 commit 4ae61d6
Show file tree
Hide file tree
Showing 42 changed files with 759 additions and 341 deletions.
422 changes: 214 additions & 208 deletions Controller/Checkout/Exchange.php

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions Controller/Checkout/Finish.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,27 @@ class Finish extends \Magento\Framework\App\Action\Action
*
* @var \Paynl\Payment\Model\Config
*/
protected $config;
private $config;

/**
* @var Session
*/
protected $checkoutSession;
private $checkoutSession;

/**
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
private $logger;

/**
* @var OrderRepository
*/
protected $orderRepository;
private $orderRepository;

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

/**
* Index constructor.
Expand Down
41 changes: 24 additions & 17 deletions Controller/Checkout/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use Magento\Payment\Helper\Data as PaymentHelper;
use Magento\Quote\Model\QuoteRepository;
use Magento\Sales\Model\OrderRepository;
use Paynl\Error\Error;

/**
Expand All @@ -19,28 +20,33 @@ class Redirect extends \Magento\Framework\App\Action\Action
/**
* @var \Paynl\Payment\Model\Config
*/
protected $_config;

private $config;

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

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

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

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


/**
* @var OrderRepository
*/
private $orderRepository;

/**
* @param \Magento\Framework\App\Action\Context $context
Expand All @@ -53,14 +59,16 @@ public function __construct(
\Magento\Checkout\Model\Session $checkoutSession,
\Psr\Log\LoggerInterface $logger,
PaymentHelper $paymentHelper,
QuoteRepository $quoteRepository
QuoteRepository $quoteRepository,
OrderRepository $orderRepository
)
{
$this->_config = $config; // Pay.nl config helper
$this->_checkoutSession = $checkoutSession;
$this->_logger = $logger;
$this->_paymentHelper = $paymentHelper;
$this->_quoteRepository = $quoteRepository;
$this->config = $config; // Pay.nl config helper
$this->checkoutSession = $checkoutSession;
$this->_logger = $logger;
$this->paymentHelper = $paymentHelper;
$this->quoteRepository = $quoteRepository;
$this->orderRepository = $orderRepository;

parent::__construct($context);
}
Expand All @@ -69,15 +77,14 @@ public function execute()
{
try {
$order = $this->_getCheckoutSession()->getLastRealOrder();

$method = $order->getPayment()->getMethod();

// restore the quote
$quote = $this->_quoteRepository->get($order->getQuoteId());
$quote = $this->quoteRepository->get($order->getQuoteId());
$quote->setIsActive(true)->setReservedOrderId(null);
$this->_quoteRepository->save($quote);
$this->quoteRepository->save($quote);

$methodInstance = $this->_paymentHelper->getMethodInstance($method);
$methodInstance = $this->paymentHelper->getMethodInstance($method);
if ($methodInstance instanceof \Paynl\Payment\Model\Paymentmethod\Paymentmethod) {
$redirectUrl = $methodInstance->startTransaction($order);
$this->getResponse()->setNoCacheHeaders();
Expand All @@ -102,6 +109,6 @@ public function execute()
*/
protected function _getCheckoutSession()
{
return $this->_checkoutSession;
return $this->checkoutSession;
}
}
43 changes: 43 additions & 0 deletions Model/Config/Source/SendNewOrderEmail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Copyright © 2015 Pay.nl All rights reserved.
*/

namespace Paynl\Payment\Model\Config\Source;

use \Magento\Framework\Option\ArrayInterface;

class SendNewOrderEmail implements ArrayInterface
{


/**
* Options getter
*
* @return array
*/
public function toOptionArray()
{
$arrOptions = $this->toArray();

$arrResult = [];
foreach ($arrOptions as $value => $label) {
$arrResult[] = ['value' => $value, 'label' => $label];
}
return $arrResult;
}

/**
* Get options in "key-value" format
*
* @return array
*/
public function toArray()
{
return [
'after_payment' => __('After successful payment'),
'before_payment' => __('Before payment')
];
}

}
48 changes: 38 additions & 10 deletions Model/Paymentmethod/PaymentMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

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;
Expand All @@ -21,6 +20,29 @@ abstract class PaymentMethod extends AbstractMethod

protected $_canRefund = true;
protected $_canRefundInvoicePartial = true;
/**
* @var Config
*/
protected $paynlConfig;

public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
\Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
\Magento\Payment\Helper\Data $paymentData,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Payment\Model\Method\Logger $logger,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
) {
parent::__construct(
$context, $registry, $extensionFactory, $customAttributeFactory,
$paymentData, $scopeConfig, $logger, $resource, $resourceCollection, $data );

$this->paynlConfig = new Config($this->_scopeConfig);
}

/**
* Get payment instructions text from config
Expand All @@ -40,12 +62,20 @@ public function initialize($paymentAction, $stateObject)
$stateObject->setState($state);
$stateObject->setStatus($state);
$stateObject->setIsNotified(false);

$sendEmail = $this->_scopeConfig->getValue('payment/' . $this->_code . '/send_new_order_email', 'store');

if($sendEmail == 'after_payment') {
//prevent sending the order confirmation
$payment = $this->getInfoInstance();
$order = $payment->getOrder();
$order->setCanSendNewEmailFlag( false );
}
}

public function refund(\Magento\Payment\Model\InfoInterface $payment, $amount)
{
$config = new Config($this->_scopeConfig);
$config->configureSDK();
$this->paynlConfig->configureSDK();

$transactionId = $payment->getParentTransactionId();

Expand All @@ -59,9 +89,7 @@ public function startTransaction(Order $order){
}
protected function doStartTransaction(Order $order)
{
$config = new Config($this->_scopeConfig);

$config->configureSDK();
$this->paynlConfig->configureSDK();
$additionalData = $order->getPayment()->getAdditionalInformation();
$bankId = null;
$expireDate = null;
Expand Down Expand Up @@ -96,7 +124,7 @@ protected function doStartTransaction(Order $order)
$strBillingFirstName = substr($arrBillingAddress['firstname'], 0, 1);

// Use full first name for Klarna
if($paymentOptionId == $config->getPaymentOptionId('paynl_payment_klarna'))
if($paymentOptionId == $this->paynlConfig->getPaymentOptionId('paynl_payment_klarna'))
{
$strBillingFirstName = $arrBillingAddress['firstname'];
}
Expand Down Expand Up @@ -130,7 +158,7 @@ protected function doStartTransaction(Order $order)
$strShippingFirstName = substr($arrShippingAddress['firstname'], 0, 1);

// Use full first name for Klarna
if($paymentOptionId == $config->getPaymentOptionId('paynl_payment_klarna'))
if($paymentOptionId == $this->paynlConfig->getPaymentOptionId('paynl_payment_klarna'))
{
$strShippingFirstName = $arrShippingAddress['firstname'];
}
Expand All @@ -151,7 +179,7 @@ protected function doStartTransaction(Order $order)
'amount' => $total,
'returnUrl' => $returnUrl,
'paymentMethod' => $paymentOptionId,
'language' => $config->getLanguage(),
'language' => $this->paynlConfig->getLanguage(),
'bank' => $bankId,
'expireDate' => $expireDate,
'description' => $orderId,
Expand Down Expand Up @@ -218,7 +246,7 @@ protected function doStartTransaction(Order $order)

$data['products'] = $arrProducts;

if ($config->isTestMode()) {
if ($this->paynlConfig->isTestMode()) {
$data['testmode'] = 1;
}
$ipAddress = $order->getRemoteIp();
Expand Down
12 changes: 10 additions & 2 deletions etc/adminhtml/paymentmethods/afterpay.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
<field id="active">1</field>
</depends>
</field>

<field id="sort_order" translate="label" type="text" sortOrder="90" showInDefault="1" showInWebsite="1"
showInStore="1">
<label>Sort Order</label>
Expand All @@ -84,7 +83,16 @@
<field id="active">1</field>
</depends>
</field>

<field id="send_new_order_email" translate="label" type="select" sortOrder="100" showInDefault="1"
showInWebsite="1"
showInStore="1">
<label>Send new order email</label>
<source_model>Paynl\Payment\Model\Config\Source\SendNewOrderEmail</source_model>
<config_path>payment/paynl_payment_afterpay/send_new_order_email</config_path>
<depends>
<field id="active">1</field>
</depends>
</field>
</group>
</include>

12 changes: 10 additions & 2 deletions etc/adminhtml/paymentmethods/amex.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
<field id="active">1</field>
</depends>
</field>

<field id="sort_order" translate="label" type="text" sortOrder="90" showInDefault="1" showInWebsite="1"
showInStore="1">
<label>Sort Order</label>
Expand All @@ -84,7 +83,16 @@
<field id="active">1</field>
</depends>
</field>

<field id="send_new_order_email" translate="label" type="select" sortOrder="100" showInDefault="1"
showInWebsite="1"
showInStore="1">
<label>Send new order email</label>
<source_model>Paynl\Payment\Model\Config\Source\SendNewOrderEmail</source_model>
<config_path>payment/paynl_payment_amex/send_new_order_email</config_path>
<depends>
<field id="active">1</field>
</depends>
</field>
</group>
</include>

12 changes: 10 additions & 2 deletions etc/adminhtml/paymentmethods/billink.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
<field id="active">1</field>
</depends>
</field>

<field id="sort_order" translate="label" type="text" sortOrder="90" showInDefault="1" showInWebsite="1"
showInStore="1">
<label>Sort Order</label>
Expand All @@ -84,7 +83,16 @@
<field id="active">1</field>
</depends>
</field>

<field id="send_new_order_email" translate="label" type="select" sortOrder="100" showInDefault="1"
showInWebsite="1"
showInStore="1">
<label>Send new order email</label>
<source_model>Paynl\Payment\Model\Config\Source\SendNewOrderEmail</source_model>
<config_path>payment/paynl_payment_billink/send_new_order_email</config_path>
<depends>
<field id="active">1</field>
</depends>
</field>
</group>
</include>

12 changes: 10 additions & 2 deletions etc/adminhtml/paymentmethods/capayable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
<field id="active">1</field>
</depends>
</field>

<field id="sort_order" translate="label" type="text" sortOrder="90" showInDefault="1" showInWebsite="1"
showInStore="1">
<label>Sort Order</label>
Expand All @@ -84,7 +83,16 @@
<field id="active">1</field>
</depends>
</field>

<field id="send_new_order_email" translate="label" type="select" sortOrder="100" showInDefault="1"
showInWebsite="1"
showInStore="1">
<label>Send new order email</label>
<source_model>Paynl\Payment\Model\Config\Source\SendNewOrderEmail</source_model>
<config_path>payment/paynl_payment_capayable/send_new_order_email</config_path>
<depends>
<field id="active">1</field>
</depends>
</field>
</group>
</include>

Loading

0 comments on commit 4ae61d6

Please sign in to comment.