Skip to content

Commit

Permalink
Added capture and void functionality
Browse files Browse the repository at this point in the history
Plugin now processes the authorized status correctly
  • Loading branch information
andy committed Apr 1, 2019
1 parent 9cf4349 commit 284d669
Show file tree
Hide file tree
Showing 45 changed files with 643 additions and 385 deletions.
190 changes: 113 additions & 77 deletions Controller/Checkout/Exchange.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

use Magento\Framework\Exception\LocalizedException;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Payment\Interceptor;
use Magento\Sales\Model\OrderRepository;
use Paynl\Result\Transaction\Transaction;

/**
* Description of Index
Expand Down Expand Up @@ -94,7 +96,6 @@ public function __construct(

public function execute()
{
$skipFraudDetection = $this->config->isSkipFraudDetection();
\Paynl\Config::setApiToken($this->config->getApiToken());

$params = $this->getRequest()->getParams();
Expand Down Expand Up @@ -131,92 +132,35 @@ public function execute()
return $this->result->setContents('TRUE| Total due <= 0, so iam not touching the status of the order');
}

if ($transaction->isPaid()) {
$message = "PAID";
if ($order->isCanceled()) {
try {
$this->uncancel($order);
} catch (LocalizedException $e) {
return $this->result->setContents('FALSE| Cannot un-cancel order: ' . $e->getMessage());
}
$message .= " order was uncanceled";
}
/** @var \Magento\Sales\Model\Order\Payment\Interceptor $payment */
$payment = $order->getPayment();
$payment->setTransactionId(
$transaction->getId()
);

$payment->setPreparedMessage('Pay.nl - ');
$payment->setIsTransactionClosed(
0
);
$paidAmount = $transaction->getPaidCurrencyAmount();

if (!$this->paynlConfig->isAlwaysBaseCurrency()) {
if ($order->getBaseCurrencyCode() != $order->getOrderCurrencyCode()) {
// we can only register the payment in the base currency
$paidAmount = $order->getBaseGrandTotal();
}
}

$payment->registerCaptureNotification(
$paidAmount, $skipFraudDetection
);

// Force order state/status to processing
$order->setState(Order::STATE_PROCESSING);

$statusProcessing = $this->config->getPaidStatus($order->getPayment()->getMethod());
$order->setStatus(!empty($statusProcessing) ? $statusProcessing : Order::STATE_PROCESSING);

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

// notify customer
$invoice = $payment->getCreatedInvoice();
if ($invoice && !$order->getEmailSent()) {
$this->orderSender->send($order);
$order->addStatusHistoryComment(
__('New order email sent')
)->setIsCustomerNotified(
true
)->save();
}
if ($invoice && !$invoice->getEmailSent()) {
$this->invoiceSender->send($invoice);

$order->addStatusHistoryComment(
__('You notified customer about invoice #%1.',
$invoice->getIncrementId())
)->setIsCustomerNotified(
true
)->save();

}

return $this->result->setContents("TRUE| " . $message);
if ($transaction->isPaid() || $transaction->isAuthorized()) {
return $this->processPaidOrder($transaction, $order);

} elseif ($transaction->isCanceled()) {
if ($this->config->isNeverCancel()) {
return $this->result->setContents("TRUE| Not Canceled because never cancel is enabled");
}
if ($order->getState() == 'holded') {
$order->unhold();
}
return $this->cancelOrder($order);
}

$order->cancel();
$order->addStatusHistoryComment(__('Pay.nl canceled the order'));
$this->orderRepository->save($order);
}

return $this->result->setContents("TRUE| CANCELED");
private function cancelOrder(Order $order)
{
if ($this->config->isNeverCancel()) {
return $this->result->setContents("TRUE| Not Canceled because never cancel is enabled");
}
if ($order->getState() == 'holded') {
$order->unhold();
}

$order->cancel();
$order->addStatusHistoryComment(__('Pay.nl canceled the order'));
$this->orderRepository->save($order);

return $this->result->setContents("TRUE| CANCELED");
}

private function uncancel(\Magento\Sales\Model\Order $order)
private function uncancelOrder(Order $order)
{
if ($order->isCanceled()) {
$state = \Magento\Sales\Model\Order::STATE_PENDING_PAYMENT;
$state = Order::STATE_PENDING_PAYMENT;
$productStockQty = [];
foreach ($order->getAllVisibleItems() as $item) {
$productStockQty[$item->getProductId()] = $item->getQtyCanceled();
Expand Down Expand Up @@ -260,4 +204,96 @@ private function uncancel(\Magento\Sales\Model\Order $order)

return $order;
}

/**
* @param Transaction $transaction
* @param Order $order
* @return \Magento\Framework\Controller\Result\Raw
*/
private function processPaidOrder(Transaction $transaction, Order $order)
{
if ($transaction->isPaid()) {
$message = "PAID";
} else {
$message = "AUTHORIZED";
}

if ($order->isCanceled()) {
try {
$this->uncancelOrder($order);
} catch (LocalizedException $e) {
return $this->result->setContents('FALSE| Cannot un-cancel order: ' . $e->getMessage());
}
$message .= " order was uncanceled";
}
/** @var Interceptor $payment */
$payment = $order->getPayment();
$payment->setTransactionId(
$transaction->getId()
);

$payment->setPreparedMessage('Pay.nl - ');
$payment->setIsTransactionClosed(
0
);

$paidAmount = $transaction->getPaidCurrencyAmount();

if (!$this->paynlConfig->isAlwaysBaseCurrency()) {
if ($order->getBaseCurrencyCode() != $order->getOrderCurrencyCode()) {
// we can only register the payment in the base currency
$paidAmount = $order->getBaseGrandTotal();
}
}

if ($transaction->isAuthorized()) {
$paidAmount = $transaction->getCurrencyAmount();
$payment->registerAuthorizationNotification($paidAmount);
} else {
$payment->registerCaptureNotification(
$paidAmount, $this->config->isSkipFraudDetection()
);
}

// Force order state/status to processing
$order->setState(Order::STATE_PROCESSING);

$statusPaid = $this->config->getPaidStatus($order->getPayment()->getMethod());
$statusAuthorized= $this->config->getAuthorizedStatus($order->getPayment()->getMethod());
$statusPaid = !empty($statusPaid) ? $statusPaid : Order::STATE_PROCESSING;
$statusAuthorized = !empty($statusAuthorized) ? $statusAuthorized : Order::STATE_PROCESSING;

if($transaction->isAuthorized()){
$order->setStatus($statusAuthorized);
} else {
$order->setStatus($statusPaid);
}

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

// notify customer
if ($order && !$order->getEmailSent()) {
$this->orderSender->send($order);
$order->addStatusHistoryComment(
__('New order email sent')
)->setIsCustomerNotified(
true
)->save();
}

$invoice = $payment->getCreatedInvoice();
if ($invoice && !$invoice->getEmailSent()) {
$this->invoiceSender->send($invoice);

$order->addStatusHistoryComment(
__('You notified customer about invoice #%1.',
$invoice->getIncrementId())
)->setIsCustomerNotified(
true
)->save();

}

return $this->result->setContents("TRUE| " . $message);
}
}
24 changes: 14 additions & 10 deletions Controller/Checkout/Finish.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,24 @@
namespace Paynl\Payment\Controller\Checkout;

use Magento\Checkout\Model\Session;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Quote\Model\QuoteRepository;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\OrderRepository;
use Paynl\Payment\Model\Config;
use Psr\Log\LoggerInterface;

/**
* Description of Redirect
*
* @author Andy Pieters <[email protected]>
*/
class Finish extends \Magento\Framework\App\Action\Action
class Finish extends Action
{
/**
*
* @var \Paynl\Payment\Model\Config
* @var Config
*/
private $config;

Expand All @@ -29,7 +33,7 @@ class Finish extends \Magento\Framework\App\Action\Action
private $checkoutSession;

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

Expand All @@ -45,17 +49,17 @@ class Finish extends \Magento\Framework\App\Action\Action

/**
* Index constructor.
* @param \Magento\Framework\App\Action\Context $context
* @param \Paynl\Payment\Model\Config $config
* @param Context $context
* @param Config $config
* @param Session $checkoutSession
* @param \Psr\Log\LoggerInterface $logger
* @param LoggerInterface $logger
* @param OrderRepository $orderRepository
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Paynl\Payment\Model\Config $config,
Context $context,
Config $config,
Session $checkoutSession,
\Psr\Log\LoggerInterface $logger,
LoggerInterface $logger,
OrderRepository $orderRepository,
QuoteRepository $quoteRepository
)
Expand Down Expand Up @@ -107,7 +111,7 @@ public function execute()
$pinStatus = $status->getTransactionState();
}

if ($transaction->isPaid() || ($transaction->isPending() && $pinStatus == null)) {
if ($transaction->isPaid() || $transaction->isAuthorized() || ($transaction->isPending() && $pinStatus == null)) {
$successUrl = $this->config->getSuccessPage($payment->getMethod());
$resultRedirect->setPath($successUrl, ['_query' => ['utm_nooverride' => '1']]);

Expand Down
3 changes: 3 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ public function getPaymentOptionId($methodCode)
public function getPendingStatus($methodCode){
return $this->store->getConfig('payment/' . $methodCode . '/order_status');
}
public function getAuthorizedStatus($methodCode){
return $this->store->getConfig('payment/' . $methodCode . '/order_status_authorized');
}

public function getPaidStatus($methodCode){
return $this->store->getConfig('payment/' . $methodCode . '/order_status_processing');
Expand Down
Loading

0 comments on commit 284d669

Please sign in to comment.