first commit
This commit is contained in:
360
b2bpayments.php
Normal file
360
b2bpayments.php
Normal 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');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user