From f17d62c9e53af5d21c440f5e9a5f4085a83e1ca6 Mon Sep 17 00:00:00 2001 From: panariga Date: Wed, 2 Apr 2025 22:15:22 +0300 Subject: [PATCH] first commit --- b2bpayments.php | 360 ++++++++++++++++++ controllers/front/switch.php | 50 +++ controllers/front/validation.php | 76 ++++ views/templates/front/error.tpl | 5 + views/templates/hook/b2b_switch.tpl | 132 +++++++ views/templates/hook/breadcrumb.tpl | 74 ++++ .../hook/displayOrderConfirmation.tpl | 24 ++ .../hook/postpaidAdditionalInformation.tpl | 22 ++ .../hook/prepaidAdditionalInformation.tpl | 22 ++ 9 files changed, 765 insertions(+) create mode 100644 b2bpayments.php create mode 100644 controllers/front/switch.php create mode 100644 controllers/front/validation.php create mode 100644 views/templates/front/error.tpl create mode 100644 views/templates/hook/b2b_switch.tpl create mode 100644 views/templates/hook/breadcrumb.tpl create mode 100644 views/templates/hook/displayOrderConfirmation.tpl create mode 100644 views/templates/hook/postpaidAdditionalInformation.tpl create mode 100644 views/templates/hook/prepaidAdditionalInformation.tpl diff --git a/b2bpayments.php b/b2bpayments.php new file mode 100644 index 0000000..9bde0f8 --- /dev/null +++ b/b2bpayments.php @@ -0,0 +1,360 @@ +name = 'b2bpayments'; + $this->tab = 'payments_gateways'; + $this->version = '1.0.0'; + $this->author = 'panariga'; + $this->need_instance = 0; + $this->bootstrap = true; + + parent::__construct(); + $this->bootstrap = true; + $this->displayName = $this->trans('B2B Payments', [], 'Modules.B2bpayments.Admin'); + $this->description = $this->trans('Provides B2B payment options based on customer group.', [], 'Modules.B2bpayments.Admin'); + + $this->confirmUninstall = $this->trans('Are you sure you want to uninstall?', [], 'Modules.B2bpayments.Admin'); + + $this->b2b_prepaid_payment_name = $this->trans('B2B Prepaid Payment', [], 'Modules.B2bpayments.PaymentMethodName'); + $this->b2b_postpaid_payment_name = $this->trans('B2B Postpaid Payment', [], 'Modules.B2bpayments.PaymentMethodName'); + + if (!Configuration::get('B2BPAYMENTS_POSTPAID_GROUP') || !Configuration::get('B2BPAYMENTS_PREPAID_GROUP')) { + $this->warning = $this->trans('Please configure the B2B Postpaid and Prepaid customer groups.', [], 'Modules.B2bpayments.Admin'); + } + } + + public function install() + { + if ( + !parent::install() || + !$this->registerHook('paymentOptions') + // || !$this->registerHook('displayCustomerAccount') + // || !$this->registerHook('displayBeforeShoppingCartBlock') + || !$this->registerHook('DisplayOrderConfirmation') + ) { + return false; + } + + return true; + } + + public function uninstall() + { + if ( + !Configuration::deleteByName('B2BPAYMENTS_POSTPAID_GROUP') || + !Configuration::deleteByName('B2BPAYMENTS_PREPAID_GROUP') || + !parent::uninstall() + ) { + return false; + } + + return true; + } + + /** + * Load the configuration form + */ + public function getContent() + { + /** + * If values have been submitted in the form, process. + */ + if (((bool)Tools::isSubmit('submitB2BPaymentsModule')) == true) { + $this->postProcess(); + } + + + + return $this->renderForm(); + } + + /** + * Create the form that will be displayed in the configuration of your module. + */ + protected function renderForm() + { + $helper = new HelperForm(); + + $helper->show_toolbar = false; + $helper->table = $this->table; + $helper->module = $this; + $helper->default_form_language = $this->context->language->id; + $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0); + + $helper->identifier = $this->identifier; + $helper->submit_action = 'submitB2BPaymentsModule'; + $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false) + . '&configure=' . $this->name . '&module_name=' . $this->name; + $helper->token = Tools::getAdminTokenLite('AdminModules'); + + $helper->tpl_vars = array( + 'fields_value' => $this->getConfigFormValues(), /* Add values for your inputs */ + 'languages' => $this->context->controller->getLanguages(), + 'id_language' => $this->context->language->id, + ); + + return $helper->generateForm(array($this->getConfigForm())); + } + + /** + * Create the structure of the form. + */ + protected function getConfigForm() + { + $groups = Group::getGroups($this->context->language->id); + $groupOptions = array(); + foreach ($groups as $group) { + $groupOptions[] = array( + 'id_option' => $group['id_group'], + 'name' => $group['name'] + ); + } + + return array( + 'form' => array( + 'legend' => array( + 'title' => $this->trans('Settings', [], 'Modules.B2bpayments.Admin'), + 'icon' => 'icon-cogs', + ), + 'input' => array( + array( + 'type' => 'select', + 'label' => $this->trans('B2B Postpaid Customer Group', [], 'Modules.B2bpayments.Admin'), + 'name' => 'B2BPAYMENTS_POSTPAID_GROUP', + 'options' => array( + 'query' => $groupOptions, + 'id' => 'id_option', + 'name' => 'name' + ), + 'desc' => $this->trans('Select the customer group to be treated as B2B Postpaid.', [], 'Modules.B2bpayments.Admin'), + ), + array( + 'type' => 'select', + 'label' => $this->trans('B2B Prepaid Customer Group', [], 'Modules.B2bpayments.Admin'), + 'name' => 'B2BPAYMENTS_PREPAID_GROUP', + 'options' => array( + 'query' => $groupOptions, + 'id' => 'id_option', + 'name' => 'name' + ), + 'desc' => $this->trans('Select the customer group to be treated as B2B Prepaid.', [], 'Modules.B2bpayments.Admin'), + ), + ), + 'submit' => array( + 'title' => $this->trans('Save', [], 'Modules.B2bpayments.Admin'), + ), + ), + ); + } + + /** + * Set values for the inputs. + */ + protected function getConfigFormValues() + { + return array( + 'B2BPAYMENTS_POSTPAID_GROUP' => Configuration::get('B2BPAYMENTS_POSTPAID_GROUP', null), + 'B2BPAYMENTS_PREPAID_GROUP' => Configuration::get('B2BPAYMENTS_PREPAID_GROUP', null), + ); + } + + /** + * Save form data. + */ + protected function postProcess() + { + $form_values = $this->getConfigFormValues(); + + foreach (array_keys($form_values) as $key) { + Configuration::updateValue($key, Tools::getValue($key)); + } + + $this->b2b_postpaid_group_id = Configuration::get('B2BPAYMENTS_POSTPAID_GROUP'); + $this->b2b_prepaid_group_id = Configuration::get('B2BPAYMENTS_PREPAID_GROUP'); + } + + + /** + * Hook to display payment options + */ + public function hookPaymentOptions($params) + { + if (!$this->active) { + return; + } + + if (empty($params['cart']) || !$params['cart']->id_customer) { + return []; + } + $customer = $this->context->customer; + + $payment_options = array(); + + $postpaid_group_id = (int)Configuration::get('B2BPAYMENTS_POSTPAID_GROUP'); + $prepaid_group_id = (int)Configuration::get('B2BPAYMENTS_PREPAID_GROUP'); + + if ($customer->isLogged() && ($this->isCustomerInGroup((int)$customer->id, $postpaid_group_id)) && $this->isDefaultCustomerGroup((int)$customer->id, $postpaid_group_id)) { + $postpaidOption = new PaymentOption(); + $postpaidOption->setCallToActionText($this->b2b_postpaid_payment_name); + $postpaidOption->setAction($this->context->link->getModuleLink($this->name, 'validation', array(), true)); + $postpaidOption->setAdditionalInformation($this->fetch('module:b2bpayments/views/templates/hook/postpaidAdditionalInformation.tpl')); + $payment_options[] = $postpaidOption; + } + + if ($customer->isLogged() && ($this->isCustomerInGroup((int)$customer->id, $prepaid_group_id)) && $this->isDefaultCustomerGroup((int)$customer->id, $prepaid_group_id)) { + $prepaidOption = new PaymentOption(); + $prepaidOption->setCallToActionText($this->b2b_prepaid_payment_name); + $prepaidOption->setAction($this->context->link->getModuleLink($this->name, 'validation', array(), true)); + $prepaidOption->setAdditionalInformation($this->fetch('module:b2bpayments/views/templates/hook/prepaidAdditionalInformation.tpl')); + $payment_options[] = $prepaidOption; + } + return $payment_options; + } + + + + /** + * Check if a customer is in a specific group. + */ + public function isCustomerInGroup($customer_id, $group_id) + { + $groups = Customer::getGroupsStatic($customer_id); + return in_array($group_id, $groups); + } + + /** + * Check if customer default group is the specified group + */ + + public function isDefaultCustomerGroup($customer_id, $group_id) + { + $customer = new Customer($customer_id); + return (int)$customer->id_default_group === (int)$group_id; + } + + + /** + * Function used to toggle the customer group + */ + public function toggleCustomerGroup($customer_id) + { + $postpaid_group_id = (int)Configuration::get('B2BPAYMENTS_POSTPAID_GROUP'); + $prepaid_group_id = (int)Configuration::get('B2BPAYMENTS_PREPAID_GROUP'); + + $customer = new Customer($customer_id); + + // Check if customer is in both groups + if (!$this->isCustomerInGroup($customer_id, $postpaid_group_id) || !$this->isCustomerInGroup($customer_id, $prepaid_group_id)) { + return false; // Or throw an exception, depending on your needs. + } + + //Check customer default group + if ($this->isDefaultCustomerGroup($customer_id, $postpaid_group_id)) { + $customer->id_default_group = $prepaid_group_id; + } else { + $customer->id_default_group = $postpaid_group_id; + } + return $customer->update(); + } + + /** + * Widget methods + */ + public function renderWidget($hookName, array $configuration) + { + $this->smarty->assign($this->getWidgetVariables($hookName, $configuration)); + if ($hookName == 'breadcrumb') { + return $this->fetch('module:' . $this->name . '/views/templates/hook/breadcrumb.tpl'); + } + return $this->fetch('module:' . $this->name . '/views/templates/hook/b2b_switch.tpl'); + } + + public function getWidgetVariables($hookName, array $configuration) + { + $customer = $this->context->customer; + + $postpaid_group_id = (int)Configuration::get('B2BPAYMENTS_POSTPAID_GROUP'); + $prepaid_group_id = (int)Configuration::get('B2BPAYMENTS_PREPAID_GROUP'); + + if ($customer->isLogged() && $this->isCustomerInGroup($customer->id, $postpaid_group_id) && $this->isCustomerInGroup($customer->id, $prepaid_group_id)) { + return array( + 'show_switch' => true, + 'postpaid_group_id' => $postpaid_group_id, + 'prepaid_group_id' => $prepaid_group_id, + 'current_group_is_postpaid' => $this->isDefaultCustomerGroup($customer->id, $postpaid_group_id), + 'switch_url' => $this->context->link->getModuleLink($this->name, 'switch', array(), true) + ); + } + + return array( + 'show_switch' => false, + ); + } + + public function hookDisplayBeforeShoppingCartBlock($params) + { + return $this->renderWidget('displayBeforeShoppingCartBlock', $params); + } + + public function hookDisplayCustomerAccount($params) + { + return $this->renderWidget('displayCustomerAccount', $params); + } + + public function hookDisplayProductPriceBlock($params) + { + if ($params['type'] == 'after_price') { + if (isset($params['smarty'])) { + $this->smarty = $params['smarty']; + } + return $this->renderWidget('displayProductPriceBlock', $params); + } + } + public function isUsingNewTranslationSystem() + { + return true; + } + + /** + * @param array{cookie: Cookie, cart: Cart, altern: int, order: Order, objOrder: Order} $params + * + * @return string + */ + public function hookDisplayOrderConfirmation(array $params) + { + /** @var Order $order */ + $order = (isset($params['objOrder'])) ? $params['objOrder'] : $params['order']; + + if (!Validate::isLoadedObject($order) || $order->module !== $this->name) { + return ''; + } + $messages = [ + $this->b2b_postpaid_payment_name => $this->trans('B2B Postpaid Payment Order Confirmation Page Message', [], 'Modules.B2bpayments.OrderConfirmationPage'), + $this->b2b_prepaid_payment_name => $this->trans('B2B Prepaid Payment Order Confirmation Page Message', [], 'Modules.B2bpayments.OrderConfirmationPage') + ]; + $b2b_order_confirmation_message = $messages[$order->payment] ?? false; + $this->context->smarty->assign([ + 'b2b_order_confirmation_message' => $b2b_order_confirmation_message, + + ]); + + return $this->fetch('module:b2bpayments/views/templates/hook/displayOrderConfirmation.tpl'); + } +} diff --git a/controllers/front/switch.php b/controllers/front/switch.php new file mode 100644 index 0000000..a3c0391 --- /dev/null +++ b/controllers/front/switch.php @@ -0,0 +1,50 @@ +context->customer->id; + + if (!$customer_id) { + // Handle unauthenticated access, maybe redirect to login + Tools::redirect($this->context->link->getPageLink('authentication', true, $this->context->language->id, [ + 'back' => $_SERVER['HTTP_REFERER'] + ])); + } + + + if (Tools::isSubmit('ajax')) { + + $success = $this->module->toggleCustomerGroup($customer_id); + + if ($success) { + $response = array('success' => true); + } else { + $response = array('success' => false, 'error' => $this->module->trans('Failed to switch group.', [], 'Modules.B2bpayments.SwitchController')); + } + + (new JsonResponse($response))->send(); + exit; + } else { + $success = $this->module->toggleCustomerGroup($customer_id); + + if ($success) { + Tools::redirect($this->context->link->getPageLink('my-account', true, $this->context->language->id)); // Redirect back to account + } else { + // Handle the error + $this->context->smarty->assign( + 'errors', + array($this->module->trans('Failed to switch group.', [], 'Modules.B2bpayments.SwitchController')) + ); + $this->setTemplate('module:b2bpayments/views/templates/front/error.tpl'); + } + } + } +} diff --git a/controllers/front/validation.php b/controllers/front/validation.php new file mode 100644 index 0000000..4dc1481 --- /dev/null +++ b/controllers/front/validation.php @@ -0,0 +1,76 @@ +context->cart->id_customer == 0 || $this->context->cart->id_address_delivery == 0 || $this->context->cart->id_address_invoice == 0 || !$this->module->active) { + Tools::redirect($this->context->link->getPageLink('order', true, $this->context->language->id, [ + 'step' => 1 + ])); + return false; + } + + + if (!$this->context->customer->id) { + // Handle unauthenticated access, maybe redirect to login + Tools::redirect($this->context->link->getPageLink('authentication', true, $this->context->language->id, [ + 'back' => $_SERVER['HTTP_REFERER'] + ])); + } + + + $total = $this->context->cart->getOrderTotal(true, Cart::BOTH); + + + // Refresh cart prices based on the customer's default group. Crucial step! + $this->context->cart->getProducts(true); // Force refresh of product prices. + $this->context->cart->update(); + Cart::resetStaticCache(); + + // Re-calculate the total + $total = $this->context->cart->getOrderTotal(true, Cart::BOTH); + + // Determine payment status based on customer group (for example): + $this->context->customer->id; + $b2b_postpaid_group_id = (int)Configuration::get('B2BPAYMENTS_POSTPAID_GROUP'); + $b2b_prepaid_group_id = (int)Configuration::get('B2BPAYMENTS_PREPAID_GROUP'); + $payment_status = Configuration::get('PS_OS_PAYMENT'); // Default: Payment accepted + if ($this->module->isDefaultCustomerGroup($this->context->customer->id, $b2b_postpaid_group_id) && $this->module->isCustomerInGroup($this->context->customer->id, $b2b_postpaid_group_id)) { + $payment_status = Configuration::get('PS_OS_PREPARATION'); // Order processing in progress. Adjust as needed. + $payment_method = $this->module->b2b_postpaid_payment_name; + } else if ($this->module->isDefaultCustomerGroup($this->context->customer->id, $b2b_prepaid_group_id) && $this->module->isCustomerInGroup($this->context->customer->id, $b2b_prepaid_group_id)) { + //Payment Status Already fine. + $payment_method = $this->module->b2b_prepaid_payment_name; + } else { + // Handle the case where the customer is not in either B2B group, or it's their default group + Tools::redirect($this->context->link->getPageLink('order', true, $this->context->language->id, [ + 'step' => 1 + ])); + return false; + } + + + $this->module->validateOrder( + (int)$this->context->cart->id, + $payment_status, + $total, + $payment_method, + null, + [], + null, + false, + $this->context->customer->secure_key + ); + + Tools::redirect($this->context->link->getPageLink('order-confirmation', true, $this->context->language->id, [ + 'id_cart' => $this->context->cart->id, + 'id_module' => $this->module->id, + 'id_order' => $this->module->currentOrder, + 'key' => $this->context->customer->secure_key, + ])); + + return true; + } +} diff --git a/views/templates/front/error.tpl b/views/templates/front/error.tpl new file mode 100644 index 0000000..68cb245 --- /dev/null +++ b/views/templates/front/error.tpl @@ -0,0 +1,5 @@ +
+ {foreach from=$errors item=error} +

{$error}

+ {/foreach} +
\ No newline at end of file diff --git a/views/templates/hook/b2b_switch.tpl b/views/templates/hook/b2b_switch.tpl new file mode 100644 index 0000000..40e9404 --- /dev/null +++ b/views/templates/hook/b2b_switch.tpl @@ -0,0 +1,132 @@ +
+ {if $show_switch} + {*

{l s='Switch to:' mod='b2bpayments'}

*} + + {if $current_group_is_postpaid} + {l s='Postpaid' d='Modules.B2bpayments.ShopSwitch'} + {else} + {l s='Prepaid' d='Modules.B2bpayments.ShopSwitch'} + {/if} + {/if} +
+ + + + \ No newline at end of file diff --git a/views/templates/hook/breadcrumb.tpl b/views/templates/hook/breadcrumb.tpl new file mode 100644 index 0000000..1faa9f2 --- /dev/null +++ b/views/templates/hook/breadcrumb.tpl @@ -0,0 +1,74 @@ +{if $show_switch} + +
+
+ + + {* The select dropdown *} + +
+ + +
+ + + +{/if} \ No newline at end of file diff --git a/views/templates/hook/displayOrderConfirmation.tpl b/views/templates/hook/displayOrderConfirmation.tpl new file mode 100644 index 0000000..b44ba87 --- /dev/null +++ b/views/templates/hook/displayOrderConfirmation.tpl @@ -0,0 +1,24 @@ +{** + * Copyright since 2007 PrestaShop SA and Contributors + * PrestaShop is an International Registered Trademark & Property of PrestaShop SA + * + * NOTICE OF LICENSE + * + * This source file is subject to the Academic Free License version 3.0 + * that is bundled with this package in the file LICENSE.md. + * It is also available through the world-wide-web at this URL: + * https://opensource.org/licenses/AFL-3.0 + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@prestashop.com so we can send you a copy immediately. + * + * @author PrestaShop SA and Contributors + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + *} + +
+ {if $b2b_order_confirmation_message} +

{$b2b_order_confirmation_message}

+ {/if} + \ No newline at end of file diff --git a/views/templates/hook/postpaidAdditionalInformation.tpl b/views/templates/hook/postpaidAdditionalInformation.tpl new file mode 100644 index 0000000..f55814b --- /dev/null +++ b/views/templates/hook/postpaidAdditionalInformation.tpl @@ -0,0 +1,22 @@ +{** + * Copyright since 2007 PrestaShop SA and Contributors + * PrestaShop is an International Registered Trademark & Property of PrestaShop SA + * + * NOTICE OF LICENSE + * + * This source file is subject to the Academic Free License version 3.0 + * that is bundled with this package in the file LICENSE.md. + * It is also available through the world-wide-web at this URL: + * https://opensource.org/licenses/AFL-3.0 + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@prestashop.com so we can send you a copy immediately. + * + * @author PrestaShop SA and Contributors + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + *} + +
+

{l s='You pay after 5 days' d='Modules.B2bpayments.PaymentAdditionalInformation'}

+
diff --git a/views/templates/hook/prepaidAdditionalInformation.tpl b/views/templates/hook/prepaidAdditionalInformation.tpl new file mode 100644 index 0000000..2493be5 --- /dev/null +++ b/views/templates/hook/prepaidAdditionalInformation.tpl @@ -0,0 +1,22 @@ +{** + * Copyright since 2007 PrestaShop SA and Contributors + * PrestaShop is an International Registered Trademark & Property of PrestaShop SA + * + * NOTICE OF LICENSE + * + * This source file is subject to the Academic Free License version 3.0 + * that is bundled with this package in the file LICENSE.md. + * It is also available through the world-wide-web at this URL: + * https://opensource.org/licenses/AFL-3.0 + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@prestashop.com so we can send you a copy immediately. + * + * @author PrestaShop SA and Contributors + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + *} + +
+

{l s='You pay in advance' d='Modules.B2bpayments.PaymentAdditionalInformation'}

+