first commit

This commit is contained in:
2025-04-02 22:15:22 +03:00
commit f17d62c9e5
9 changed files with 765 additions and 0 deletions

360
b2bpayments.php Normal file
View File

@@ -0,0 +1,360 @@
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
use PrestaShop\PrestaShop\Core\Payment\PaymentOption;
class B2BPayments extends PaymentModule implements PrestaShop\PrestaShop\Core\Module\WidgetInterface
{
public $b2b_postpaid_group_id;
public $b2b_prepaid_group_id;
public $b2b_postpaid_payment_name;
public $b2b_prepaid_payment_name;
public function __construct()
{
$this->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');
}
}

View File

@@ -0,0 +1,50 @@
<?php
use Symfony\Component\HttpFoundation\JsonResponse;
class B2BPaymentsSwitchModuleFrontController extends ModuleFrontController
{
public function postProcess()
{
parent::postProcess();
$customer_id = (int)$this->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');
}
}
}
}

View File

@@ -0,0 +1,76 @@
<?php
class B2BPaymentsValidationModuleFrontController extends ModuleFrontController
{
public function postProcess()
{
if ($this->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;
}
}

View File

@@ -0,0 +1,5 @@
<div class="alert alert-danger">
{foreach from=$errors item=error}
<p>{$error}</p>
{/foreach}
</div>

View File

@@ -0,0 +1,132 @@
<div class="b2b-group-switch">
{if $show_switch}
{* <p>{l s='Switch to:' mod='b2bpayments'}</p> *}
<label class="switch">
<input type="checkbox" id="b2b-switch" {if $current_group_is_postpaid}checked{/if}>
<span class="slider round"></span>
</label>
{if $current_group_is_postpaid}
{l s='Postpaid' d='Modules.B2bpayments.ShopSwitch'}
{else}
{l s='Prepaid' d='Modules.B2bpayments.ShopSwitch'}
{/if}
{/if}
</div>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function() {
const b2bSwitch = document.getElementById('b2b-switch');
if (b2bSwitch) {
b2bSwitch.addEventListener('change', function() {
const xhr = new XMLHttpRequest();
xhr.open('POST', '{$switch_url}');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); // Required for POST requests
xhr.onload = function() {
if (xhr.status >= 200 && xhr.status < 300) {
try {
const response = JSON.parse(xhr.responseText);
if (response.success) {
location.reload(); // Refresh the page
} else {
alert('{$error_message|escape:'javascript'}'); // Use the error message, escaping it
}
} catch (e) {
console.error('JSON Parsing Error:', e);
alert('An error occurred while processing the response. Please try again.');
}
} else {
console.error('HTTP Error:', xhr.status, xhr.statusText);
alert('An error occurred while switching groups. Please try again.');
}
};
xhr.onerror = function() {
console.error('Request failed');
alert('An error occurred while switching groups. Please try again.');
};
const params = 'ajax=true&action=switchGroup'; // Build the query string
xhr.send(params);
});
} else {
console.warn('b2b-switch element not found.'); // Log a warning if the switch is not present
}
});
</script>
<style>
.b2b-group-switch {
display: flex;
align-items: center;
margin-bottom: 10px;
}
.b2b-group-switch p {
margin-right: 10px;
}
/* The switch - the box around the slider */
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
/* Hide default HTML checkbox */
.switch input {
opacity: 0;
width: 0;
height: 0;
}
/* The slider */
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input:checked + .slider {
background-color: #2196F3;
}
input:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}
input:checked + .slider:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(26px);
}
/* Rounded sliders */
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
</style>

View File

@@ -0,0 +1,74 @@
{if $show_switch}
<style>
.payments-selection {
justify-content: flex-end;
margin: 10px 0;
}
</style>
<div class="col-md-6 col-lg-3">
<div class="payments-selection">
{* The select dropdown *}
<select id="b2b-group-select" name="b2b_group_type" class="custom-select" onchange="switchB2Bpayment();">
{* Option for Prepaid *}
<option value="prepaid" {if !$current_group_is_postpaid}selected{/if}>
{l s='Prepaid' d='Modules.B2bpayments.ShopBreadcrumb'}
</option>
{* Option for Postpaid *}
<option value="postpaid" {if $current_group_is_postpaid}selected{/if}>
{l s='Postpaid' d='Modules.B2bpayments.ShopBreadcrumb'}
</option>
</select>
</div>
</div>
<script type="text/javascript">
function switchB2Bpayment() {
const xhr = new XMLHttpRequest();
xhr.open('POST', '{$switch_url|escape:'javascript'}');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); // Required for POST requests
xhr.onload = function() {
if (xhr.status >= 200 && xhr.status < 300) {
try {
const response = JSON.parse(xhr.responseText);
if (response.success) {
// Success: Reload the page to reflect the change
location.reload();
} else {
alert('{$error_message|escape:'javascript'}');
}
} catch (e) {
console.error('JSON Parsing Error:', e, xhr.responseText);
alert('An error occurred while processing the response. Please try again.');
}
} else {
// HTTP error: Show generic error
console.error('HTTP Error:', xhr.status, xhr.statusText);
alert('An error occurred while switching groups (HTTP ' + xhr.status + '). Please try again.');
}
};
xhr.onerror = function() {
// Network or other request error
console.error('Request failed');
alert(
'An error occurred while switching groups (Network Error). Please check your connection and try again.'
);
};
const params = 'ajax=true&action=switchGroup'; // Build the query string
xhr.send(params);
};
</script>
{/if}

View File

@@ -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 <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*}
<div id="b2bpayments-displayOrderConfirmation">
{if $b2b_order_confirmation_message}
<p>{$b2b_order_confirmation_message}</p>
{/if}
</section>

View File

@@ -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 <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*}
<section id="b2bpayments-postpaidAdditionalInformation">
<p>{l s='You pay after 5 days' d='Modules.B2bpayments.PaymentAdditionalInformation'}</p>
</section>

View File

@@ -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 <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*}
<section id="b2bpayments-prepaidAdditionalInformation">
<p>{l s='You pay in advance' d='Modules.B2bpayments.PaymentAdditionalInformation'}</p>
</section>