Skip to content

Commit

Permalink
All methods now have pending_payment and new as valid states.
Browse files Browse the repository at this point in the history
Added setting for sofort and bank transfer, set on-hold
Quote is reactivated immediately after starting the payment, so the back button works from the payment page.
  • Loading branch information
Andy Pieters committed Dec 6, 2017
1 parent 11e0e43 commit 65d6a9b
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 60 deletions.
7 changes: 4 additions & 3 deletions Controller/Checkout/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,13 @@ public function __construct(
public function execute()
{
try {
$order = $this->_getCheckoutSession()->getLastRealOrder();
$method = $order->getPayment()->getMethod();
$order = $this->checkoutSession->getLastRealOrder();

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

$methodInstance = $this->paymentHelper->getMethodInstance($method);
Expand Down
2 changes: 1 addition & 1 deletion Model/Config/Source/Order/Status/PendingPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ class PendingPayment extends Status
/**
* @var string[]
*/
protected $_stateStatuses = [Order::STATE_PENDING_PAYMENT];
protected $_stateStatuses = [Order::STATE_PENDING_PAYMENT, Order::STATE_NEW];
}
82 changes: 36 additions & 46 deletions Model/Paymentmethod/Paylink.php
Original file line number Diff line number Diff line change
@@ -1,58 +1,48 @@
<?php

namespace Paynl\Payment\Model\Paymentmethod;

use Magento\Sales\Model\Order;
use Magento\Sales\Model\OrderRepository;

/**
*
* @author Andy Pieters <[email protected]>
*/
class Paylink extends PaymentMethod {
protected $_code = 'paynl_payment_paylink';

/**
* Paylink payment block paths
*
* @var string
*/
protected $_formBlockType = \Paynl\Payment\Block\Form\Paylink::class;

/**
* @var OrderRepository
*/
protected $_orderRepository;
class Paylink extends PaymentMethod
{
protected $_code = 'paynl_payment_paylink';

/**
* Paylink payment block paths
*
* @var string
*/
protected $_formBlockType = \Paynl\Payment\Block\Form\Paylink::class;

// this is an admin only method
protected $_canUseCheckout = false;

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,
OrderRepository $orderRepository,
\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->_orderRepository = $orderRepository;
}

public function initialize( $paymentAction, $stateObject ) {
if($paymentAction == 'order') {
/** @var Order $order */
$order = $this->getInfoInstance()->getOrder();
$this->_orderRepository->save($order);
$transaction = $this->doStartTransaction( $order );
$state = $this->getConfigData('order_status');
parent::initialize( $paymentAction, $stateObject );
$order->addStatusHistoryComment('Betaallink: '.$transaction->getRedirectUrl(), $state);
}
}
public function assignData( \Magento\Framework\DataObject $data ) {
$this->getInfoInstance()->setAdditionalInformation('valid_days', $data->getData('additional_data')['valid_days']);
return parent::assignData( $data );
}

public function initialize($paymentAction, $stateObject)
{
if ($paymentAction == 'order') {
/** @var Order $order */
$order = $this->getInfoInstance()->getOrder();
$this->orderRepository->save($order);

$transaction = $this->doStartTransaction($order);

$status = $this->getConfigData('order_status');

$order->addStatusHistoryComment('Betaallink: ' . $transaction->getRedirectUrl(), $status);
parent::initialize($paymentAction, $stateObject);
}
}

public function assignData(\Magento\Framework\DataObject $data)
{
$this->getInfoInstance()->setAdditionalInformation('valid_days', $data->getData('additional_data')['valid_days']);

return parent::assignData($data);
}
}
51 changes: 46 additions & 5 deletions Model/Paymentmethod/PaymentMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ abstract class PaymentMethod extends AbstractMethod
*/
protected $paynlConfig;

/**
* @var \Magento\Sales\Model\OrderRepository
*/
protected $orderRepository;
/**
* @var \Magento\Sales\Model\Order\Config
*/
protected $orderConfig;

public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
Expand All @@ -33,6 +42,8 @@ public function __construct(
\Magento\Payment\Helper\Data $paymentData,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Payment\Model\Method\Logger $logger,
\Magento\Sales\Model\Order\Config $orderConfig,
\Magento\Sales\Model\OrderRepository $orderRepository,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
Expand All @@ -41,9 +52,25 @@ public function __construct(
$context, $registry, $extensionFactory, $customAttributeFactory,
$paymentData, $scopeConfig, $logger, $resource, $resourceCollection, $data );

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

protected function getState($status){
$validStates = [
Order::STATE_NEW,
Order::STATE_PENDING_PAYMENT,
Order::STATE_HOLDED
];

foreach($validStates as $state){
$statusses = $this->orderConfig->getStateStatuses($state, false);
if(in_array($status, $statusses)) return $state;
}
return false;
}

/**
* Get payment instructions text from config
*
Expand All @@ -58,19 +85,26 @@ public function getBanks(){
}
public function initialize($paymentAction, $stateObject)
{
$state = $this->getConfigData('order_status');
$stateObject->setState($state);
$stateObject->setStatus($state);
$status = $this->getConfigData('order_status');

$stateObject->setState($this->getState($status));
$stateObject->setStatus($status);
$stateObject->setIsNotified(false);

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

$payment = $this->getInfoInstance();
/** @var Order $order */
$order = $payment->getOrder();

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

$this->orderRepository->save($order);

return parent::initialize($paymentAction, $stateObject);
}

public function refund(\Magento\Payment\Model\InfoInterface $payment, $amount)
Expand All @@ -85,6 +119,13 @@ public function refund(\Magento\Payment\Model\InfoInterface $payment, $amount)
}
public function startTransaction(Order $order){
$transaction = $this->doStartTransaction($order);

$holded = $this->_scopeConfig->getValue('payment/' . $this->_code . '/holded', 'store');
if($holded){
$order->hold();
}
$this->orderRepository->save($order);

return $transaction->getRedirectUrl();
}
protected function doStartTransaction(Order $order)
Expand Down
12 changes: 11 additions & 1 deletion etc/adminhtml/paymentmethods/overboeking.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,22 @@
<field id="order_status" translate="label" type="select" sortOrder="30" showInDefault="1" showInWebsite="1"
showInStore="1">
<label>New Order Status</label>
<source_model>Paynl\Payment\Model\Config\Source\Order\Status\Holded</source_model>
<source_model>Paynl\Payment\Model\Config\Source\Order\Status\PendingPayment</source_model>
<depends>
<field id="active">1</field>
</depends>
<config_path>payment/paynl_payment_overboeking/order_status</config_path>
</field>
<field id="holded" translate="label" type="select" sortOrder="35" showInDefault="1"
showInWebsite="1"
showInStore="1">
<label>Set order on-hold</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<config_path>payment/paynl_payment_overboeking/holded</config_path>
<depends>
<field id="active">1</field>
</depends>
</field>
<field id="allowspecific" translate="label" type="allowspecific" sortOrder="40" showInDefault="1"
showInWebsite="1" showInStore="1">
<label>Payment from Applicable Countries</label>
Expand Down
12 changes: 11 additions & 1 deletion etc/adminhtml/paymentmethods/sofortbanking.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,22 @@
<field id="order_status" translate="label" type="select" sortOrder="30" showInDefault="1" showInWebsite="1"
showInStore="1">
<label>New Order Status</label>
<source_model>Paynl\Payment\Model\Config\Source\Order\Status\Holded</source_model>
<source_model>Paynl\Payment\Model\Config\Source\Order\Status\PendingPayment</source_model>
<depends>
<field id="active">1</field>
</depends>
<config_path>payment/paynl_payment_sofortbanking/order_status</config_path>
</field>
<field id="holded" translate="label" type="select" sortOrder="35" showInDefault="1"
showInWebsite="1"
showInStore="1">
<label>Set order on-hold</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<config_path>payment/paynl_payment_sofortbanking/holded</config_path>
<depends>
<field id="active">1</field>
</depends>
</field>
<field id="allowspecific" translate="label" type="allowspecific" sortOrder="40" showInDefault="1"
showInWebsite="1" showInStore="1">
<label>Payment from Applicable Countries</label>
Expand Down
8 changes: 5 additions & 3 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<active>1</active>
<title>Pay.nl betaallink</title>
<payment_option_id>961</payment_option_id>
<order_status>pending_payment</order_status>
<order_status>pending</order_status>
<instructions></instructions>
<payment_action>order</payment_action>
<model>Paynl\Payment\Model\Paymentmethod\Paylink</model>
Expand Down Expand Up @@ -232,12 +232,13 @@
<active>0</active>
<title>Overboeking</title>
<payment_option_id>136</payment_option_id>
<order_status>holded</order_status>
<order_status>pending</order_status>
<instructions></instructions>
<payment_action>order</payment_action>
<model>Paynl\Payment\Model\Paymentmethod\Overboeking</model>
<group>paynl_payment</group>
<send_new_order_email>before_payment</send_new_order_email>
<holded>1</holded>
</paynl_payment_overboeking>
<paynl_payment_paypal>
<active>0</active>
Expand Down Expand Up @@ -287,7 +288,8 @@
<active>0</active>
<title>Sofortbanking</title>
<payment_option_id>559</payment_option_id>
<order_status>holded</order_status>
<order_status>pending_payment</order_status>
<holded>1</holded>
<instructions></instructions>
<payment_action>order</payment_action>
<model>Paynl\Payment\Model\Paymentmethod\Sofortbanking</model>
Expand Down

0 comments on commit 65d6a9b

Please sign in to comment.