|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Novalnet payment extension |
| 4 | + * |
| 5 | + * NOTICE OF LICENSE |
| 6 | + * |
| 7 | + * This source file is subject to the Novalnet End User License Agreement |
| 8 | + * that is bundled with this package in the file LICENSE.txt |
| 9 | + * |
| 10 | + * DISCLAIMER |
| 11 | + * |
| 12 | + * If you wish to customize Novalnet payment extension for your needs, |
| 13 | + * please contact [email protected] for more information. |
| 14 | + * |
| 15 | + * @category Novalnet |
| 16 | + * @package Novalnet_Payment |
| 17 | + * @copyright Copyright (c) Novalnet AG |
| 18 | + * @license https://www.novalnet.de/payment-plugins/kostenlos/lizenz |
| 19 | + */ |
| 20 | +namespace Novalnet\Payment\Block\Adminhtml\Button; |
| 21 | + |
| 22 | +use Magento\Backend\Block\Widget\Button\Toolbar as ToolbarContext; |
| 23 | +use Magento\Framework\View\Element\AbstractBlock; |
| 24 | +use Magento\Backend\Block\Widget\Button\ButtonList; |
| 25 | + |
| 26 | +class Control extends \Magento\Sales\Block\Adminhtml\Order\View |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @var \Magento\Framework\Registry |
| 30 | + */ |
| 31 | + protected $coreRegistry = null; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var \Novalnet\Payment\Model\NNConfig |
| 35 | + */ |
| 36 | + protected $novalnetConfig; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var \Novalnet\Payment\Helper\Data |
| 40 | + */ |
| 41 | + protected $novalnetHelper; |
| 42 | + |
| 43 | + /** |
| 44 | + * @param \Magento\Framework\Registry $registry |
| 45 | + * @param \Novalnet\Payment\Model\NNConfig $novalnetConfig |
| 46 | + * @param \Novalnet\Payment\Helper\Data $novalnetHelper |
| 47 | + */ |
| 48 | + public function __construct( |
| 49 | + \Magento\Framework\Registry $registry, |
| 50 | + \Novalnet\Payment\Model\NNConfig $novalnetConfig, |
| 51 | + \Novalnet\Payment\Helper\Data $novalnetHelper |
| 52 | + ) { |
| 53 | + $this->coreRegistry = $registry; |
| 54 | + $this->novalnetConfig = $novalnetConfig; |
| 55 | + $this->novalnetHelper = $novalnetHelper; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Before push buttons |
| 60 | + * |
| 61 | + * @param ToolbarContext $toolbar |
| 62 | + * @param AbstractBlock $context |
| 63 | + * @param ButtonList $buttonList |
| 64 | + * @return mixed |
| 65 | + */ |
| 66 | + public function beforePushButtons( |
| 67 | + ToolbarContext $toolbar, |
| 68 | + \Magento\Framework\View\Element\AbstractBlock $context, |
| 69 | + \Magento\Backend\Block\Widget\Button\ButtonList $buttonList |
| 70 | + ) { |
| 71 | + if (!$context instanceof \Magento\Sales\Block\Adminhtml\Order\View) { |
| 72 | + return [$context, $buttonList]; |
| 73 | + } |
| 74 | + |
| 75 | + $order = $this->getOrder(); |
| 76 | + $payment = $order->getPayment(); |
| 77 | + $paymentMethodCode = $payment->getMethodInstance()->getCode(); |
| 78 | + $additionalData = $this->novalnetHelper->getPaymentAdditionalData($payment->getAdditionalData()); |
| 79 | + $paymentType = !empty($additionalData['NnPaymentType']) ? $additionalData['NnPaymentType'] : ''; |
| 80 | + |
| 81 | + if (!empty($paymentMethodCode) && preg_match('/novalnet/i', $paymentMethodCode)) { |
| 82 | + if ($context instanceof \Magento\Sales\Block\Adminhtml\Order\Invoice\View) { |
| 83 | + if ($paymentType == "INVOICE") { |
| 84 | + $buttonList->remove('capture'); |
| 85 | + } |
| 86 | + return [$context, $buttonList]; |
| 87 | + } |
| 88 | + |
| 89 | + $transactionStatus = !empty($additionalData['NnStatus']) |
| 90 | + ? $this->novalnetHelper->getStatus($additionalData['NnStatus'], $order, $paymentType) : ''; |
| 91 | + |
| 92 | + if ($transactionStatus) { |
| 93 | + // remove Capture button |
| 94 | + $buttonList->update('order_invoice', 'label', __('Capture')); |
| 95 | + |
| 96 | + if (in_array($transactionStatus, ['PENDING', 'DEACTIVATED']) || !empty($additionalData['NnZeroAmountBooking'])) { |
| 97 | + $buttonList->remove('order_invoice'); |
| 98 | + } |
| 99 | + |
| 100 | + if ($order->canInvoice() && $transactionStatus == 'ON_HOLD' && |
| 101 | + $paymentType == "INVOICE" |
| 102 | + ) { |
| 103 | + $buttonList->remove('order_invoice'); |
| 104 | + $message = __('Are you sure you want to capture the payment?'); |
| 105 | + $capturePaymentUrl = $context->getUrl( |
| 106 | + 'novalnetpayment/sales/ordercapture', |
| 107 | + ['order_id' => $order->getId()] |
| 108 | + ); |
| 109 | + |
| 110 | + $buttonList->add( |
| 111 | + 'novalnet_confirm', |
| 112 | + [ |
| 113 | + 'label' => __('Novalnet Capture'), |
| 114 | + 'onclick' => "confirmSetLocation('{$message}', '{$capturePaymentUrl}')" |
| 115 | + ] |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + if ($transactionStatus == 'ON_HOLD') { |
| 120 | + $buttonList->remove('void_payment'); |
| 121 | + $message = __('Are you sure you want to cancel the payment?'); |
| 122 | + $voidPaymentUrl = $context->getUrl( |
| 123 | + 'sales/*/voidPayment', |
| 124 | + ['order_id' => $order->getId()] |
| 125 | + ); |
| 126 | + $buttonList->add( |
| 127 | + 'void_payment', |
| 128 | + [ |
| 129 | + 'label' => __('Void'), |
| 130 | + 'onclick' => "confirmSetLocation('{$message}', '{$voidPaymentUrl}')" |
| 131 | + ] |
| 132 | + ); |
| 133 | + } |
| 134 | + |
| 135 | + $processMode = !empty($additionalData['NnPaymentProcessMode']) ? $additionalData['NnPaymentProcessMode'] : ''; |
| 136 | + |
| 137 | + if ($transactionStatus == 'PENDING' && ($processMode == 'redirect' && !in_array($paymentType, ['GOOGLEPAY', 'APPLEPAY', 'CREDITCARD'])) || |
| 138 | + in_array( |
| 139 | + $paymentType, |
| 140 | + [ |
| 141 | + 'PREPAYMENT', |
| 142 | + 'CASHPAYMENT', |
| 143 | + 'MULTIBANCO' |
| 144 | + ] |
| 145 | + ) |
| 146 | + ) { |
| 147 | + $buttonList->remove('order_cancel'); // remove Cancel button |
| 148 | + $buttonList->remove('void_payment'); // remove Void button |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + return [$context, $buttonList]; |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Retrieve order model object |
| 158 | + * |
| 159 | + * @return \Magento\Sales\Model\Order |
| 160 | + */ |
| 161 | + public function getOrder() |
| 162 | + { |
| 163 | + return $this->coreRegistry->registry('sales_order'); |
| 164 | + } |
| 165 | +} |
0 commit comments