Skip to content

Commit

Permalink
Merge pull request #123 from paynl/feature/PLUG-334
Browse files Browse the repository at this point in the history
Feature/plug 334
  • Loading branch information
woutse authored Mar 15, 2021
2 parents b0812cf + 086052b commit eb8269d
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 40 deletions.
90 changes: 52 additions & 38 deletions Controller/Checkout/Exchange.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -106,6 +113,7 @@ public function __construct(
$this->logger = $logger;
$this->orderRepository = $orderRepository;
$this->paynlConfig = $paynlConfig;
$this->builderInterface = $builderInterface;

parent::__construct($context);
}
Expand Down Expand Up @@ -168,7 +176,7 @@ public function execute()
$this->logger->debug('Already captured.');

return $this->result->setContents('TRUE| Already captured.');
}
}
}

if ($transaction->isPaid() || $transaction->isAuthorized()) {
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 9 additions & 1 deletion etc/adminhtml/paymentmethods/billink.xml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,15 @@
<config_path>payment/paynl_payment_billink/showforcompany</config_path>
<comment><![CDATA[Allow payment method to be used for companies, private, or both.]]></comment>
</field>
<field id="turn_off_invoices_b2b" translate="label" type="select" sortOrder="160" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Turn off invoices for B2B</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<depends>
<field id="active">1</field>
</depends>
<config_path>payment/paynl_payment_billink/turn_off_invoices_b2b</config_path>
<comment><![CDATA[Set to `Yes` to disable creating invoices for business orders]]></comment>
</field>
<group id="advanced" translate="label" sortOrder="200" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Advanced</label>
<field id="payment_option_id" translate="label comment" type="text" sortOrder="10" showInDefault="1"
Expand All @@ -156,4 +165,3 @@
</group>
</group>
</include>

1 change: 1 addition & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ In order to pay for your order click the link below
<model>Paynl\Payment\Model\Paymentmethod\Billink</model>
<group>paynl_payment</group>
<send_new_order_email>after_payment</send_new_order_email>
<turn_off_invoices_b2b>No</turn_off_invoices_b2b>
</paynl_payment_billink>
<paynl_payment_capayable>
<active>0</active>
Expand Down

0 comments on commit eb8269d

Please sign in to comment.