From 785648ebf9928c884d3b192f72b376b7a5881a48 Mon Sep 17 00:00:00 2001 From: kevinowino869 Date: Sun, 30 Mar 2025 13:50:07 +0200 Subject: [PATCH] Update modMpesapay.class.php Signed-off-by: kevinowino869 --- modMpesapay.class.php | 226 +++++++++++++----------------------------- 1 file changed, 70 insertions(+), 156 deletions(-) diff --git a/modMpesapay.class.php b/modMpesapay.class.php index 4d1163d..f1c0b95 100644 --- a/modMpesapay.class.php +++ b/modMpesapay.class.php @@ -1,172 +1,86 @@ 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 '
'; +print ''; + +print ''; +print ''; +print ''; +print ''; +print ''; + +print ''; +print ''; +print ''; +print ''; + +print ''; +print ''; +print ''; +print ''; + +print ''; +print ''; +print ''; +print ''; + +print ''; +print ''; +print ''; +print ''; + +print ''; +print ''; +print ''; +print ''; + +print '
' . $langs->trans('Parameter') . '' . $langs->trans('Value') . '
'; + +print '
'; +print ''; +print '
'; + +print '
'; + +llxFooter(); +$db->close(); ?> \ No newline at end of file