Skip to content

Commit

Permalink
Merge pull request #145 from paynl/feature/PLUG-423
Browse files Browse the repository at this point in the history
Add partial payments to exchange
  • Loading branch information
woutse authored Jul 1, 2021
2 parents 69ce84b + 30d3b91 commit dfa98e6
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 23 deletions.
112 changes: 89 additions & 23 deletions Controller/Checkout/Exchange.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@
use Magento\Sales\Model\OrderRepository;
use Paynl\Payment\Controller\CsrfAwareActionInterface;
use Paynl\Payment\Controller\PayAction;
use Paynl\Result\Transaction\Status;
use Paynl\Result\Transaction\Transaction;



/**
* Description of Index
*
Expand Down Expand Up @@ -138,39 +135,36 @@ public function execute()
}

try {
$transaction = \Paynl\Transaction::status($payOrderId);
$transaction = \Paynl\Transaction::get($payOrderId);
} catch (\Exception $e) {
$this->logger->critical($e, $params);

return $this->result->setContents('FALSE| Error fetching transaction. ' . $e->getMessage());
}

if(method_exists($transaction, 'isPartialPayment')) {
if($transaction->isPartialPayment()) {
return $this->result->setContents("TRUE| Partial payment");
}
}

if ($transaction->isPending()) {
if ($action == 'new_ppt') {
return $this->result->setContents("FALSE| Payment is pending");
}
return $this->result->setContents("TRUE| Ignoring pending");
}

$orderNumber = $transaction->getOrderNumber();
$order = $this->orderRepository->get($orderEntityId);

if (method_exists($transaction, 'isPartialPayment')) {
if ($transaction->isPartialPayment()) {
if ($this->config->registerPartialPayments()) {
return $this->processPartiallyPaidOrder($order, $payOrderId);
}
return $this->result->setContents("TRUE| Partial payment");
}
}

if (empty($order)) {
$this->logger->critical('Cannot load order: ' . $orderEntityId);
return $this->result->setContents('FALSE| Cannot load order');
}

if ($order->getIncrementId() != $orderNumber) {
$this->logger->critical('Ordernumber not equal' . $orderEntityId);
return $this->result->setContents('TRUE| Failed. Ordernumber not found.');
}

$payment = $order->getPayment();
$info = $payment->getAdditionalInformation();
if (!empty($info['transactionId'])) {
Expand All @@ -185,7 +179,9 @@ public function execute()

if ($order->getTotalDue() <= 0) {
$this->logger->debug($action . '. Ignoring - already paid: ' . $orderEntityId);
return $this->result->setContents('TRUE| Ignoring: order has already been paid');
if (!$this->config->registerPartialPayments()) {
return $this->result->setContents('TRUE| Ignoring: order has already been paid');
}
}

if ($action == 'capture') {
Expand All @@ -194,7 +190,7 @@ public function execute()
return $this->result->setContents('TRUE| Already captured.');
}
}

if ($transaction->isPaid() || $transaction->isAuthorized()) {
return $this->processPaidOrder($transaction, $order);
} elseif ($transaction->isCanceled()) {
Expand Down Expand Up @@ -278,7 +274,7 @@ private function uncancelOrder(Order $order)
* @param Order $order
* @return \Magento\Framework\Controller\Result\Raw
*/
private function processPaidOrder(Status $transaction, Order $order)
private function processPaidOrder(Transaction $transaction, Order $order)
{
if ($transaction->isPaid()) {
$message = "PAID";
Expand All @@ -294,12 +290,10 @@ private function processPaidOrder(Status $transaction, Order $order)
}
$message .= " order was uncanceled";
}

/** @var Interceptor $payment */
$payment = $order->getPayment();
$payment->setTransactionId(
$transaction->getId()
);

$payment->setTransactionId($transaction->getId());
$payment->setPreparedMessage('PAY. - ');
$payment->setIsTransactionClosed(0);

Expand All @@ -312,6 +306,16 @@ private function processPaidOrder(Status $transaction, Order $order)
}
}

# Multipayments finish
if ($this->config->registerPartialPayments()) {
$payments = $order->getAllPayments();
if (count($payments) > 1) {
if ($transaction->isPaid() && $order->getTotalDue() == 0) {
$paidAmount = $order->getBaseGrandTotal();
}
}
}

# Force order state to processing
$order->setState(Order::STATE_PROCESSING);
$paymentMethod = $order->getPayment()->getMethod();
Expand Down Expand Up @@ -372,4 +376,66 @@ private function processPaidOrder(Status $transaction, Order $order)

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

/**
* @param Order $order
* @param $payOrderId
* @return \Magento\Framework\Controller\Result\Raw
*/
private function processPartiallyPaidOrder(Order $order, $payOrderId)
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$orderPaymentFactory = $objectManager->get(\Magento\Sales\Model\Order\PaymentFactory::class);
$returnMessage = "TRUE| Partial payment processed";

try {
$details = \Paynl\Transaction::details($payOrderId);

$paymentDetails = $details->getPaymentDetails();
$transactionDetails = $paymentDetails['transactionDetails'];
$firstPayment = count($transactionDetails) == 1;
$totalpaid =0;
foreach ($transactionDetails as $_dt) {
$totalpaid += $_dt['amount']['value'];
}
$_detail = end($transactionDetails);

$subProfile = $_detail['orderId'];
$profileId = $_detail['paymentProfileId'];
$method = $_detail['paymentProfileName'];
$amount = $_detail['amount']['value'] / 100;
$currency = $_detail['amount']['currency'];
$methodCode = $this->config->getPaymentmethodCode($profileId);

/** @var Interceptor $orderPayment */
if (!$firstPayment) {
$orderPayment = $orderPaymentFactory->create();
} else {
$orderPayment = $order->getPayment();
}
$orderPayment->setMethod($methodCode);
$orderPayment->setOrder($order);
$orderPayment->setBaseAmountPaid($amount);
$orderPayment->save();

$transactionBuilder = $this->builderInterface->setPayment($orderPayment)
->setOrder($order)
->setTransactionId($subProfile)
->setFailSafe(true)
->build('capture')
->setAdditionalInformation(\Magento\Sales\Model\Order\Payment\Transaction::RAW_DETAILS,
array("Paymentmethod" => $method, "Amount" => $amount, "Currency" => $currency));
$transactionBuilder->save();

$order->addStatusHistoryComment(__('PAY.: Partial payment received: '.$subProfile.' - Amount ' . $currency . ' ' . $amount . ' Method: ' . $method));
$order->setTotalPaid($totalpaid / 100);

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

} catch (\Exception $e) {
$returnMessage = 'TRUE| Failed processing partial payment'. $e->getMessage();
}

return $this->result->setContents($returnMessage);
}
}
22 changes: 22 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,26 @@ public function getDefaultPaymentOption()
{
return $this->store->getConfig('payment/paynl/default_payment_option');
}

public function registerPartialPayments()
{
return $this->store->getConfig('payment/paynl/register_partial_payments');
}

public function getPaymentmethodCode($paymentProfileId){

//Get all PAY. methods
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$paymentHelper = $objectManager->get('Magento\Payment\Helper\Data');
$paymentMethodList = $paymentHelper->getPaymentMethods();
$pay_methods = array();
foreach ($paymentMethodList as $key => $value) {
if (strpos($key, 'paynl_') !== false && $key != 'paynl_payment_paylink') {
$code = $this->store->getConfig('payment/' . $key . '/payment_option_id');
if($code == $paymentProfileId){
return $key;
}
}
}
}
}
5 changes: 5 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@
<comment><![CDATA[Automatically capture authorisation-transaction when creating the shipment for the order.]]></comment>
<source_model>Paynl\Payment\Model\Config\Source\OffOn</source_model>
</field>
<field id="register_partial_payments" translate="label" type="select" sortOrder="106" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Register partial payments</label>
<comment><![CDATA[Register partial payments]]></comment>
<source_model>Paynl\Payment\Model\Config\Source\OffOn</source_model>
</field>
<group id="paynl_paymentmethods" translate="label" sortOrder="110" showInDefault="1" showInWebsite="1" showInStore="1">
<frontend_model>Paynl\Payment\Block\Adminhtml\System\Config\Fieldset\Expanded</frontend_model>
<label>Paymentmethods</label>
Expand Down

0 comments on commit dfa98e6

Please sign in to comment.