|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Mondu\Mondu\Plugin; |
| 4 | + |
| 5 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 6 | +use Magento\Store\Model\ScopeInterface; |
| 7 | +use Mondu\Mondu\Helpers\Log as MonduLogger; |
| 8 | +use Mondu\Mondu\Helpers\Logger\Logger as MonduFileLogger; |
| 9 | + |
| 10 | +class AddTemplateVariable |
| 11 | +{ |
| 12 | + const MONDU_UK_SORT_CODE = '185008'; |
| 13 | + const MONDU_EN_ACCOUNT_HOLDER = 'Mondu Capital S.à r.l.'; |
| 14 | + const MONDU_FR_DE_ACCOUNT_HOLDER = 'Mondu Capital Sàrl'; |
| 15 | + const MONDU_NL_ACCOUNT_HOLDER = 'Mondu Capital S.à r.l'; |
| 16 | + const MONDU_EN_BANK_NAME = 'Citibank N.A.'; |
| 17 | + const MONDU_EN_BIC = 'CITINL2X'; |
| 18 | + const MONDU_DE_NL_BIC = 'HYVEDEMME40'; |
| 19 | + const MONDU_FR_BIC = 'CITIFRPP'; |
| 20 | + const UK_COUNTRY_CODE = 'UK'; |
| 21 | + const DE_COUNTRY_CODE = 'DE'; |
| 22 | + const FR_COUNTRY_CODE = 'FR'; |
| 23 | + const NL_COUNTRY_CODE = 'NL'; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var MonduLogger |
| 27 | + */ |
| 28 | + private MonduLogger $monduLogger; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var MonduFileLogger |
| 32 | + */ |
| 33 | + private MonduFileLogger $monduFileLogger; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var ScopeConfigInterface |
| 37 | + */ |
| 38 | + private ScopeConfigInterface $scopeConfig; |
| 39 | + |
| 40 | + /** |
| 41 | + * @param MonduLogger $monduLogger |
| 42 | + * @param MonduFileLogger $monduFileLogger |
| 43 | + * @param ScopeConfigInterface $scopeConfig |
| 44 | + */ |
| 45 | + public function __construct( |
| 46 | + MonduLogger $monduLogger, |
| 47 | + MonduFileLogger $monduFileLogger, |
| 48 | + ScopeConfigInterface $scopeConfig |
| 49 | + ) { |
| 50 | + $this->monduLogger = $monduLogger; |
| 51 | + $this->monduFileLogger = $monduFileLogger; |
| 52 | + $this->scopeConfig = $scopeConfig; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @param \Magento\Email\Model\Template $subject |
| 57 | + * @param array $vars |
| 58 | + * |
| 59 | + * @return array[] |
| 60 | + */ |
| 61 | + public function beforeSetVars( |
| 62 | + \Magento\Email\Model\Template $subject, |
| 63 | + array $vars |
| 64 | + ) { |
| 65 | + if (!$vars['order'] || !$vars['order']->getMonduReferenceId()) { |
| 66 | + return [$vars]; |
| 67 | + } |
| 68 | + |
| 69 | + try { |
| 70 | + $vars['monduDetails'] = ''; |
| 71 | + $monduReferenceId = $vars['order']->getMonduReferenceId(); |
| 72 | + $monduLog = $this->monduLogger->getLogCollection($monduReferenceId); |
| 73 | + |
| 74 | + if (!$monduLog) { |
| 75 | + return [$vars]; |
| 76 | + } |
| 77 | + |
| 78 | + if (!$monduLog['external_data'] || !is_string($monduLog['external_data'])) { |
| 79 | + return [$vars]; |
| 80 | + } |
| 81 | + |
| 82 | + $externalData = json_decode($monduLog['external_data'] , true); |
| 83 | + if (json_last_error() !== JSON_ERROR_NONE) { |
| 84 | + return [$vars]; |
| 85 | + } |
| 86 | + |
| 87 | + $billingAddress = $vars['order']->getBillingAddress(); |
| 88 | + $vars['monduDetails'] = $this->getInvoiceDetails([ |
| 89 | + 'countryId' => $externalData['buyer_country_code'] ?: $billingAddress->getCountryId(), |
| 90 | + 'merchant_company_name' => $externalData['merchant_company_name'], |
| 91 | + 'bank_account' => $externalData['bank_account'], |
| 92 | + 'invoiceId' => $vars['invoice']->getIncrementId(), |
| 93 | + 'iban' => $monduLog['invoice_iban'], |
| 94 | + 'paymentMethod' => $vars['order']->getPayment()->getMethodInstance()->getTitle(), |
| 95 | + 'netTerms' => isset($monduLog['authorized_net_term']) ? $monduLog['authorized_net_term'] : '' |
| 96 | + ]); |
| 97 | + } catch (\Exception $e) { |
| 98 | + $this->monduFileLogger->critical($e->getMessage()); |
| 99 | + } |
| 100 | + |
| 101 | + return [$vars]; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @param $invoiceDetails |
| 106 | + * |
| 107 | + * @return string |
| 108 | + */ |
| 109 | + protected function getInvoiceDetails($invoiceData): string |
| 110 | + { |
| 111 | + switch ($invoiceData['paymentMethod']) { |
| 112 | + case $this->scopeConfig->getValue('payment/mondu/title', ScopeInterface::SCOPE_STORE): |
| 113 | + $invoiceDetails = $this->getPayLaterViaBankTransferDetails($invoiceData); |
| 114 | + break; |
| 115 | + case $this->scopeConfig->getValue('payment/mondusepa/title', ScopeInterface::SCOPE_STORE): |
| 116 | + $invoiceDetails = __('This invoice was created in accordance with the general terms and conditions of <strong>%1</strong> and <strong>Mondu GmbH</strong> for the purchase on account payment model.', $invoiceData['merchant_company_name']) . '<br/>'; |
| 117 | + $invoiceDetails .= __('Since you have chosen the payment method to purchase on account with payment via SEPA direct debit through Mondu, the invoice amount will be debited from your bank account on the due date.') . '<br/>'; |
| 118 | + $invoiceDetails .= __('Before the amount is debited from your account, you will receive notice of the direct debit. Kindly make sure you have sufficient funds in your account.') . '<br/>'; |
| 119 | + break; |
| 120 | + default: |
| 121 | + $invoiceDetails = __('This invoice was created in accordance with the general terms and conditions of <strong>%1</strong> and <strong>Mondu GmbH</strong> for the instalment payment model.', $invoiceData['merchant_company_name']) . '<br/>'; |
| 122 | + $invoiceDetails .= __('Since you have chosen the instalment payment method via SEPA direct debit through Mondu, the individual installments will be debited from your bank account on the due date.') . '<br/>'; |
| 123 | + $invoiceDetails .= __('Before the amounts are debited from your account, you will receive notice regarding the direct debit. Kindly make sure you have sufficient funds in your account. In the event of changes to your order, the instalment plan will be adjusted to reflect the new order total.') . '<br/>'; |
| 124 | + break; |
| 125 | + } |
| 126 | + |
| 127 | + return $invoiceDetails; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @param $invoiceData |
| 132 | + * |
| 133 | + * @return string |
| 134 | + */ |
| 135 | + protected function getPayLaterViaBankTransferDetails($invoiceData) |
| 136 | + { |
| 137 | + $invoiceDetails = __('This invoice is created in accordance with the terms and conditions of <strong>%1</strong> modified by <strong>Mondu GmbH</strong> payment terms. Please pay to the following account:', $invoiceData['merchant_company_name']) . '<br/>'; |
| 138 | + |
| 139 | + switch ($invoiceData['countryId']) { |
| 140 | + case self::UK_COUNTRY_CODE: |
| 141 | + $invoiceDetails .= __('<strong>Account holder:</strong> %1', $invoiceData['bank_account']['account_holder'] ?: self::MONDU_EN_ACCOUNT_HOLDER) . '<br/>'; |
| 142 | + $invoiceDetails .= __('<strong>Bank:</strong> %1', $invoiceData['bank_account']['bank'] ?: self::MONDU_EN_BANK_NAME) . '<br/>'; |
| 143 | + $invoiceDetails .= __('<strong>Sort Code:</strong> %1', $invoiceData['bank_account']['sort_code'] ?: self::MONDU_UK_SORT_CODE) . '<br/>'; |
| 144 | + $invoiceDetails .= __('<strong>Account Number:</strong> %1', $invoiceData['bank_account']['account_number']) . '<br/>'; |
| 145 | + $invoiceDetails .= __('<strong>IBAN:</strong> %1', $invoiceData['iban']) . '<br/>'; |
| 146 | + $invoiceDetails .= __('<strong>BIC:</strong> %1', $invoiceData['bank_account']['bic'] ?: self::MONDU_EN_BIC) . '<br/>'; |
| 147 | + break; |
| 148 | + case self::DE_COUNTRY_CODE: |
| 149 | + $invoiceDetails .= __('<strong>Account holder:</strong> %1', self::MONDU_FR_DE_ACCOUNT_HOLDER) . '<br/>'; |
| 150 | + $invoiceDetails .= __('<strong>IBAN:</strong> %1', $invoiceData['iban']) . '<br/>'; |
| 151 | + $invoiceDetails .= __('<strong>BIC:</strong> %1', $invoiceData['bank_account']['bic'] ?: self::MONDU_DE_NL_BIC) . '<br/>'; |
| 152 | + break; |
| 153 | + case self::FR_COUNTRY_CODE: |
| 154 | + $invoiceDetails .= __('<strong>Account holder:</strong> %1', self::MONDU_FR_DE_ACCOUNT_HOLDER) . '<br/>'; |
| 155 | + $invoiceDetails .= __('<strong>IBAN:</strong> %1', $invoiceData['iban']) . '<br/>'; |
| 156 | + $invoiceDetails .= __('<strong>BIC:</strong> %1', $invoiceData['bank_account']['bic'] ?: self::MONDU_FR_BIC) . '<br/>'; |
| 157 | + break; |
| 158 | + case self::NL_COUNTRY_CODE: |
| 159 | + $invoiceDetails .= __('<strong>Account holder:</strong> %1', self::MONDU_NL_ACCOUNT_HOLDER) . '<br/>'; |
| 160 | + $invoiceDetails .= __('<strong>IBAN:</strong> %1', $invoiceData['iban']) . '<br/>'; |
| 161 | + $invoiceDetails .= __('<strong>BIC:</strong> %1', $invoiceData['bank_account']['bic'] ?: self::MONDU_DE_NL_BIC) . '<br/>'; |
| 162 | + break; |
| 163 | + default: |
| 164 | + $invoiceDetails .= __('<strong>Account holder:</strong> %1', $invoiceData['bank_account']['account_holder'] ?: self::MONDU_EN_ACCOUNT_HOLDER) . '<br/>'; |
| 165 | + $invoiceDetails .= __('<strong>Bank:</strong> %1', $invoiceData['bank_account']['bank'] ?: self::MONDU_EN_BANK_NAME) . '<br/>'; |
| 166 | + $invoiceDetails .= __('<strong>IBAN:</strong> %1', $invoiceData['iban']) . '<br/>'; |
| 167 | + $invoiceDetails .= __('<strong>BIC:</strong> %1', $invoiceData['bank_account']['bic'] ?: self::MONDU_EN_BIC) . '<br/>'; |
| 168 | + break; |
| 169 | + } |
| 170 | + |
| 171 | + $invoiceDetails .= __('<strong>Payment reference:</strong> %1', $invoiceData['invoiceId']) . '<br/>'; |
| 172 | + $invoiceDetails .= __('<strong>Payment term:</strong> %1 days', $invoiceData['netTerms']) . '<br/>'; |
| 173 | + |
| 174 | + return $invoiceDetails; |
| 175 | + } |
| 176 | +} |
0 commit comments