diff --git a/Controller/Checkout/Exchange.php b/Controller/Checkout/Exchange.php index f9b213df..f3759cde 100644 --- a/Controller/Checkout/Exchange.php +++ b/Controller/Checkout/Exchange.php @@ -63,6 +63,12 @@ class Exchange extends PayAction implements CsrfAwareActionInterface */ private $orderRepository; + /** + * + * @var Magento\Sales\Model\Order\Payment\Transaction\BuilderInterface + */ + private $builderInterface; + private $paynlConfig; public function createCsrfValidationException(RequestInterface $request): ?InvalidRequestException @@ -95,7 +101,8 @@ public function __construct( \Psr\Log\LoggerInterface $logger, \Magento\Framework\Controller\Result\Raw $result, OrderRepository $orderRepository, - \Paynl\Payment\Model\Config $paynlConfig + \Paynl\Payment\Model\Config $paynlConfig, + \Magento\Sales\Model\Order\Payment\Transaction\BuilderInterface $builderInterface ) { $this->result = $result; @@ -106,6 +113,7 @@ public function __construct( $this->logger = $logger; $this->orderRepository = $orderRepository; $this->paynlConfig = $paynlConfig; + $this->builderInterface = $builderInterface; parent::__construct($context); } @@ -168,7 +176,7 @@ public function execute() $this->logger->debug('Already captured.'); return $this->result->setContents('TRUE| Already captured.'); - } + } } if ($transaction->isPaid() || $transaction->isAuthorized()) { @@ -275,65 +283,71 @@ private function processPaidOrder(Transaction $transaction, Order $order) ); $payment->setPreparedMessage('PAY. - '); - $payment->setIsTransactionClosed( - 0 - ); + $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 + # We can only register the payment in the base currency $paidAmount = $order->getBaseGrandTotal(); } } + # Force order state to processing + $order->setState(Order::STATE_PROCESSING); + $paymentMethod = $order->getPayment()->getMethod(); + if ($transaction->isAuthorized()) { - $paidAmount = $transaction->getCurrencyAmount(); - $payment->registerAuthorizationNotification($paidAmount); + $statusAuthorized = $this->config->getAuthorizedStatus($paymentMethod); + $order->setStatus(!empty($statusAuthorized) ? $statusAuthorized : Order::STATE_PROCESSING); } else { - $payment->registerCaptureNotification( - $paidAmount, $this->config->isSkipFraudDetection() - ); + $statusPaid = $this->config->getPaidStatus($paymentMethod); + $order->setStatus(!empty($statusPaid) ? $statusPaid : Order::STATE_PROCESSING); } - // Force order state/status to processing - $order->setState(Order::STATE_PROCESSING); + # Notify customer + if ($order && !$order->getEmailSent()) { + $this->orderSender->send($order); + $order->addStatusHistoryComment(__('New order email sent'))->setIsCustomerNotified(true)->save(); + } - $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; + # Skip creation of invoice for B2B if enabled + if ($this->config->ignoreB2BInvoice($paymentMethod)) { + $orderCompany = $order->getBillingAddress()->getCompany(); + if(!empty($orderCompany)) { + # Create transaction + $formatedPrice = $order->getBaseCurrency()->formatTxt($order->getGrandTotal()); + $transactionMessage = __('PAY. - Captured amount of %1.', $formatedPrice); + $transactionBuilder = $this->builderInterface->setPayment($payment)->setOrder($order)->setTransactionId($transaction->getId())->setFailSafe(true)->build('capture'); + $payment->addTransactionCommentsToOrder($transactionBuilder, $transactionMessage); + $payment->setParentTransactionId(null); + $payment->save(); + $transactionBuilder->save(); + $order->addStatusHistoryComment(__('B2B Setting: Skipped creating invoice')); + $this->orderRepository->save($order); + return $this->result->setContents("TRUE| " . $message . " (B2B: No invoice created)"); + } + } - if($transaction->isAuthorized()){ - $order->setStatus($statusAuthorized); + # Make the invoice and send it to the customer + if ($transaction->isAuthorized()) { + $paidAmount = $transaction->getCurrencyAmount(); + $payment->registerAuthorizationNotification($paidAmount); } else { - $order->setStatus($statusPaid); + $payment->registerCaptureNotification( + $paidAmount, $this->config->isSkipFraudDetection() + ); } $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(); - + $order->addStatusHistoryComment(__('You notified customer about invoice #%1.', $invoice->getIncrementId())) + ->setIsCustomerNotified(true) + ->save(); } return $this->result->setContents("TRUE| " . $message); diff --git a/Model/Config.php b/Model/Config.php index 2416262c..7db4b401 100644 --- a/Model/Config.php +++ b/Model/Config.php @@ -125,6 +125,12 @@ public function getPaidStatus($methodCode){ return $this->store->getConfig('payment/' . $methodCode . '/order_status_processing'); } + public function ignoreB2BInvoice($methodCode) + { + return $this->store->getConfig('payment/' . $methodCode . '/turn_off_invoices_b2b') == 1; + } + + /** * @param $methodCode string * @return string diff --git a/composer.json b/composer.json index a69cbcbe..ccb6c2c7 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "paynl/magento2-plugin", "description": "PAY. Magento2 Payment methods", "type": "magento2-module", - "version": "1.7.2", + "version": "1.7.3", "require": { "magento/module-sales": "100 - 103", "magento/module-payment": "100 - 103", diff --git a/etc/adminhtml/paymentmethods/billink.xml b/etc/adminhtml/paymentmethods/billink.xml index f33e70d4..6e2caf7c 100644 --- a/etc/adminhtml/paymentmethods/billink.xml +++ b/etc/adminhtml/paymentmethods/billink.xml @@ -144,6 +144,15 @@ payment/paynl_payment_billink/showforcompany + + + Magento\Config\Model\Config\Source\Yesno + + 1 + + payment/paynl_payment_billink/turn_off_invoices_b2b + + - \ No newline at end of file diff --git a/etc/config.xml b/etc/config.xml index 1ec94392..7fa5d176 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -94,6 +94,7 @@ In order to pay for your order click the link below Paynl\Payment\Model\Paymentmethod\Billink paynl_payment after_payment + No 0