DoliPesa/modMpesapay.class.php

172 lines
5.6 KiB
PHP
Raw Normal View History

<?php
/**
* Dolibarr Module Descriptor for Dolipesa - M-Pesa Payment Module
* Author: NESTICT INFOTECH
* Version: 1.0.1
* License: GNU General Public License v3.0
*/
include_once DOL_DOCUMENT_ROOT . '/core/modules/DolibarrModules.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;
$this->db = $db;
// 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(
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
)
);
// 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
);
// 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
}
}
?>