From 05f948294da4cb38d4f55334744593bb0928a213 Mon Sep 17 00:00:00 2001 From: kevinowino869 Date: Sun, 30 Mar 2025 13:44:44 +0200 Subject: [PATCH] Update modMpesapay.class.php Signed-off-by: kevinowino869 --- modMpesapay.class.php | 173 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 151 insertions(+), 22 deletions(-) diff --git a/modMpesapay.class.php b/modMpesapay.class.php index 2098708..4d1163d 100644 --- a/modMpesapay.class.php +++ b/modMpesapay.class.php @@ -1,43 +1,172 @@ db = $db; - $this->numero = 104900; - $this->rights_class = 'mpesapay'; - $this->family = 'payment'; - $this->name = "M-Pesa Payment Module"; - $this->description = "Accept M-Pesa payments in Dolibarr."; - $this->version = '1.0.0'; + // 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 + // Configuration constants $this->const = array( - array('MPESAPAY_PAYBILL', 'chaine', '', 'M-Pesa Paybill Number', 0, 'current', 1), - array('MPESAPAY_PASSKEY', 'chaine', '', 'M-Pesa Passkey', 0, 'current', 1) + 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 + ) ); - $this->module_parts = array('hooks' => array('invoicecard', 'payment')); + // 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 + ); - $this->config_page_url = "mpesapay_setup.php"; - $this->dirs = array('/mpesapay'); - $this->picto = 'payment'; + // 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 Payment URL - function generatePaymentUrl($invoiceId) + /** + * Generate a secure payment URL for an invoice + * + * @param int $invoiceId Invoice ID + * @return string Payment URL + */ + public function generatePaymentUrl($invoiceId) { - $token = base64_encode(hash('sha256', $invoiceId . time())); - return DOL_URL_ROOT . "/custom/mpesapay/payment.php?invoice_id={$invoiceId}&token={$token}"; + 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 } } -?> +?> \ No newline at end of file