Update modMpesapay.class.php

Signed-off-by: kevinowino869 <kevinowino869@www.codelab.nestict.africa>
This commit is contained in:
kevinowino869 2025-03-30 13:50:07 +02:00
parent 05f948294d
commit 785648ebf9

View File

@ -1,172 +1,86 @@
<?php
/**
* Dolibarr Module Descriptor for Dolipesa - M-Pesa Payment Module
* Dolipesa - M-Pesa Payment Module Setup Page
* Author: NESTICT INFOTECH
* Version: 1.0.1
* License: GNU General Public License v3.0
*/
include_once DOL_DOCUMENT_ROOT . '/core/modules/DolibarrModules.class.php';
require '../../main.inc.php';
require_once DOL_DOCUMENT_ROOT . '/core/lib/admin.lib.php';
require_once DOL_DOCUMENT_ROOT . '/core/class/html.form.class.php';
/**
* Class modDolipesa
* Module descriptor for Dolipesa M-Pesa payment integration
*/
class modDolipesa extends DolibarrModules
{
/**
* Constructor
*
* @param DoliDB $db Database handler
*/
public function __construct($db)
{
global $langs, $conf;
$langs->load('admin');
$langs->load('dolipesa@dolipesa');
$this->db = $db;
if (!$user->admin) accessforbidden();
// Module metadata
$this->numero = 104900; // Unique module ID
$this->rights_class = 'dolipesa';
$this->family = 'financial';
$this->module_position = '50';
$this->name = preg_replace('/^mod/i', '', get_class($this)); // Extracts "Dolipesa" from class name
$this->description = 'Enables M-Pesa payment acceptance within Dolibarr ERP/CRM.';
$this->descriptionlong = 'Integrates Safaricom M-Pesa STK Push for seamless invoice and order payments.';
$this->editor_name = 'NESTICT INFOTECH';
$this->editor_url = 'https://www.nestict.com';
$this->version = '1.0.1';
$this->langs = 'dolipesa@dolipesa'; // Language file
$action = GETPOST('action', 'alpha');
// Configuration constants
$this->const = array(
0 => array(
'name' => 'MPESAPAY_CONSUMER_KEY',
'type' => 'chaine',
'default' => '',
'desc' => 'M-Pesa Consumer Key from Safaricom Daraja API',
'visible' => 0,
'entity' => 'current',
'enabled' => 1
),
1 => array(
'name' => 'MPESAPAY_CONSUMER_SECRET',
'type' => 'chaine',
'default' => '',
'desc' => 'M-Pesa Consumer Secret from Safaricom Daraja API (encrypted)',
'visible' => 0,
'entity' => 'current',
'enabled' => 1
),
2 => array(
'name' => 'MPESAPAY_SHORTCODE',
'type' => 'chaine',
'default' => '',
'desc' => 'M-Pesa Business Shortcode (Paybill or Till Number)',
'visible' => 0,
'entity' => 'current',
'enabled' => 1
),
3 => array(
'name' => 'MPESAPAY_PASSKEY',
'type' => 'chaine',
'default' => '',
'desc' => 'M-Pesa Passkey for STK Push (encrypted)',
'visible' => 0,
'entity' => 'current',
'enabled' => 1
),
4 => array(
'name' => 'MPESAPAY_CALLBACK_URL',
'type' => 'chaine',
'default' => DOL_MAIN_URL_ROOT . '/custom/dolipesa/callback.php',
'desc' => 'Callback URL for M-Pesa transaction responses',
'visible' => 0,
'entity' => 'current',
'enabled' => 1
)
);
if ($action === 'save') {
$consumer_key = GETPOST('MPESAPAY_CONSUMER_KEY', 'alpha');
$consumer_secret = GETPOST('MPESAPAY_CONSUMER_SECRET', 'alpha');
$shortcode = GETPOST('MPESAPAY_SHORTCODE', 'alpha');
$passkey = GETPOST('MPESAPAY_PASSKEY', 'alpha');
$callback_url = GETPOST('MPESAPAY_CALLBACK_URL', 'alpha');
// Module parts
$this->module_parts = array(
'hooks' => array('invoicecard', 'paymentcard', 'newpayment'), // Hooks for UI and payment processing
'triggers' => 0,
'login' => 0,
'substitutions' => 0,
'menus' => 0,
'theme' => 0,
'css' => array(),
'js' => array(),
'models' => 0
);
if (empty($shortcode) || empty($passkey) || empty($consumer_key) || empty($consumer_secret)) {
setEventMessages($langs->trans('ErrorFieldRequired'), null, 'errors');
} else {
dolibarr_set_const($db, 'MPESAPAY_CONSUMER_KEY', $consumer_key, 'chaine', 0, '', $conf->entity);
dolibarr_set_const($db, 'MPESAPAY_CONSUMER_SECRET', dol_encrypt($consumer_secret), 'chaine', 0, '', $conf->entity);
dolibarr_set_const($db, 'MPESAPAY_SHORTCODE', $shortcode, 'chaine', 0, '', $conf->entity);
dolibarr_set_const($db, 'MPESAPAY_PASSKEY', dol_encrypt($passkey), 'chaine', 0, '', $conf->entity);
dolibarr_set_const($db, 'MPESAPAY_CALLBACK_URL', $callback_url, 'chaine', 0, '', $conf->entity);
// Config page
$this->config_page_url = array('mpesapay_setup.php@dolipesa'); // Path relative to module dir
// Data directories
$this->dirs = array('/dolipesa/temp');
// Icon
$this->picto = 'payment@payment'; // Use Dolibarr payment icon
// Dependencies
$this->depends = array('modFacture'); // Requires invoice module
$this->requiredby = array();
$this->phpmin = array(7, 4); // Minimum PHP version
}
/**
* Generate a secure payment URL for an invoice
*
* @param int $invoiceId Invoice ID
* @return string Payment URL
*/
public function generatePaymentUrl($invoiceId)
{
global $conf;
// Generate a secure token using invoice ID and a salt
$token = hash_hmac('sha256', $invoiceId . time(), $conf->global->MAIN_SECURITY_SALT);
$url = DOL_MAIN_URL_ROOT . '/custom/dolipesa/payment.php';
$url .= '?invoice_id=' . urlencode($invoiceId) . '&token=' . urlencode($token);
return $url;
}
/**
* Initialize the module (called during activation)
*
* @return int 1 on success, < 0 on error
*/
public function init()
{
// Create SQL tables if needed
$sql = array(
"CREATE TABLE IF NOT EXISTS " . MAIN_DB_PREFIX . "dolipesa_transactions (
id int AUTO_INCREMENT PRIMARY KEY,
checkout_request_id varchar(50) NOT NULL,
fk_invoice int NOT NULL,
transaction_id varchar(50) DEFAULT NULL,
result_code varchar(10) DEFAULT NULL,
result_desc varchar(255) DEFAULT NULL,
datec datetime NOT NULL,
INDEX idx_fk_invoice (fk_invoice),
INDEX idx_checkout_request_id (checkout_request_id)
) ENGINE=InnoDB;"
);
return $this->_init($sql);
}
/**
* Remove the module (called during deactivation)
*
* @return int 1 on success, < 0 on error
*/
public function remove()
{
$sql = array();
return $this->_remove($sql); // No table drop by default; customize if needed
setEventMessages($langs->trans('SettingsSaved'), null, 'mesgs');
}
}
print load_fiche_titre($langs->trans('MpesaPaymentModuleConfig'));
print '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '">';
print '<input type="hidden" name="action" value="save">';
print '<table class="noborder centpercent">';
print '<tr class="liste_titre">';
print '<th>' . $langs->trans('Parameter') . '</th>';
print '<th>' . $langs->trans('Value') . '</th>';
print '</tr>';
print '<tr class="oddeven">';
print '<td><label for="consumer_key">' . $langs->trans('MpesaConsumerKey') . '</label></td>';
print '<td><input type="text" name="MPESAPAY_CONSUMER_KEY" id="consumer_key" value="' . dolibarr_get_const($db, 'MPESAPAY_CONSUMER_KEY') . '" size="40"></td>';
print '</tr>';
print '<tr class="oddeven">';
print '<td><label for="consumer_secret">' . $langs->trans('MpesaConsumerSecret') . '</label></td>';
print '<td><input type="text" name="MPESAPAY_CONSUMER_SECRET" id="consumer_secret" value="' . dol_decrypt(dolibarr_get_const($db, 'MPESAPAY_CONSUMER_SECRET')) . '" size="40"></td>';
print '</tr>';
print '<tr class="oddeven">';
print '<td><label for="shortcode">' . $langs->trans('MpesaShortcode') . '</label></td>';
print '<td><input type="text" name="MPESAPAY_SHORTCODE" id="shortcode" value="' . dolibarr_get_const($db, 'MPESAPAY_SHORTCODE') . '" size="20"></td>';
print '</tr>';
print '<tr class="oddeven">';
print '<td><label for="passkey">' . $langs->trans('MpesaPasskey') . '</label></td>';
print '<td><input type="text" name="MPESAPAY_PASSKEY" id="passkey" value="' . dol_decrypt(dolibarr_get_const($db, 'MPESAPAY_PASSKEY')) . '" size="40"></td>';
print '</tr>';
print '<tr class="oddeven">';
print '<td><label for="callback_url">' . $langs->trans('MpesaCallbackURL') . '</label></td>';
print '<td><input type="text" name="MPESAPAY_CALLBACK_URL" id="callback_url" value="' . dolibarr_get_const($db, 'MPESAPAY_CALLBACK_URL') . '" size="60"></td>';
print '</tr>';
print '</table>';
print '<div class="center" style="margin-top: 20px;">';
print '<input type="submit" class="button" value="' . $langs->trans('Save') . '">';
print '</div>';
print '</form>';
llxFooter();
$db->close();
?>