2025-03-30 12:55:37 +02:00
|
|
|
<?php
|
2025-03-30 13:09:02 +02:00
|
|
|
/**
|
2025-03-30 13:33:43 +02:00
|
|
|
* Dolipesa - M-Pesa Payment Module Hook Handling
|
2025-03-30 13:09:02 +02:00
|
|
|
* Author: NESTICT INFOTECH
|
2025-03-30 13:33:43 +02:00
|
|
|
* Version: 1.0.1
|
|
|
|
* License: GNU General Public License v3.0
|
2025-03-30 13:09:02 +02:00
|
|
|
*/
|
|
|
|
|
2025-03-30 13:33:43 +02:00
|
|
|
class ActionsDolipesa
|
2025-03-30 13:09:02 +02:00
|
|
|
{
|
|
|
|
/**
|
2025-03-30 13:33:43 +02:00
|
|
|
* Hook manager instance
|
|
|
|
*
|
|
|
|
* @var DoliHookManager
|
|
|
|
*/
|
|
|
|
protected $hookmanager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param DoliDB $db Database handler
|
|
|
|
*/
|
|
|
|
public function __construct($db)
|
|
|
|
{
|
|
|
|
$this->db = $db;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add M-Pesa payment option to invoice card
|
2025-03-30 13:09:02 +02:00
|
|
|
*
|
|
|
|
* @param array $parameters Hook parameters
|
2025-03-30 13:33:43 +02:00
|
|
|
* @param CommonObject $object Current object (e.g., Facture)
|
2025-03-30 13:09:02 +02:00
|
|
|
* @param string $action Current action
|
2025-03-30 13:33:43 +02:00
|
|
|
* @param HookManager $hookmanager Hook manager instance
|
|
|
|
* @return int 0 on success, < 0 on error
|
2025-03-30 13:09:02 +02:00
|
|
|
*/
|
2025-03-30 13:33:43 +02:00
|
|
|
public function formObjectOptions($parameters, &$object, &$action, $hookmanager)
|
2025-03-30 13:09:02 +02:00
|
|
|
{
|
2025-03-30 13:33:43 +02:00
|
|
|
global $langs, $conf;
|
|
|
|
|
|
|
|
$this->hookmanager = $hookmanager;
|
2025-03-30 13:09:02 +02:00
|
|
|
|
2025-03-30 13:33:43 +02:00
|
|
|
// Check if we're on the invoice card and the invoice is validated (statut = 1)
|
|
|
|
if (in_array('invoicecard', explode(':', $parameters['currentcontext'])) && $object->statut == 1) {
|
|
|
|
// Load module configuration
|
|
|
|
$shortcode = $conf->global->MPESAPAY_SHORTCODE;
|
|
|
|
if (empty($shortcode)) {
|
|
|
|
return 0; // Do nothing if module not configured
|
|
|
|
}
|
2025-03-30 13:09:02 +02:00
|
|
|
|
2025-03-30 13:33:43 +02:00
|
|
|
// Generate payment URL for the public payment page
|
|
|
|
$paymentUrl = DOL_MAIN_URL_ROOT . '/public/payment/newpayment.php?source=invoice&ref=' . urlencode($object->ref);
|
|
|
|
|
|
|
|
// Output M-Pesa payment button
|
|
|
|
print '<tr class="oddeven"><td colspan="2">';
|
|
|
|
print '<div class="dolipesa-payment" style="margin-top: 10px;">';
|
|
|
|
print '<a href="' . $paymentUrl . '" class="butAction" target="_blank">' . $langs->trans('PayWithMpesa') . '</a>';
|
|
|
|
print '</div>';
|
|
|
|
print '</td></tr>';
|
2025-03-30 13:09:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2025-03-30 13:33:43 +02:00
|
|
|
* Add M-Pesa transaction input to payment card
|
2025-03-30 13:09:02 +02:00
|
|
|
*
|
|
|
|
* @param array $parameters Hook parameters
|
2025-03-30 13:33:43 +02:00
|
|
|
* @param CommonObject $object Current object (e.g., Payment)
|
2025-03-30 13:09:02 +02:00
|
|
|
* @param string $action Current action
|
2025-03-30 13:33:43 +02:00
|
|
|
* @param HookManager $hookmanager Hook manager instance
|
|
|
|
* @return int 0 on success, < 0 on error
|
2025-03-30 13:09:02 +02:00
|
|
|
*/
|
2025-03-30 13:33:43 +02:00
|
|
|
public function formAddObjectLine($parameters, &$object, &$action, $hookmanager)
|
|
|
|
{
|
|
|
|
global $langs, $conf;
|
|
|
|
|
|
|
|
$this->hookmanager = $hookmanager;
|
|
|
|
|
|
|
|
// Check if we're on the payment card and payment is draft (statut = 0)
|
|
|
|
if (in_array('paymentcard', explode(':', $parameters['currentcontext'])) && $object->statut == 0) {
|
|
|
|
// Load module configuration
|
|
|
|
$shortcode = $conf->global->MPESAPAY_SHORTCODE;
|
|
|
|
if (empty($shortcode)) {
|
|
|
|
return 0; // Do nothing if module not configured
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output M-Pesa transaction ID input field
|
|
|
|
print '<tr class="oddeven"><td colspan="2">';
|
|
|
|
print '<div class="dolipesa-payment" style="margin-top: 10px;">';
|
|
|
|
print '<label for="mpesapay_transaction_id">' . $langs->trans('MpesaTransactionID') . ':</label> ';
|
|
|
|
print '<input type="text" name="mpesapay_transaction_id" id="mpesapay_transaction_id" placeholder="' . $langs->trans('EnterTransactionID') . '" size="30">';
|
|
|
|
print '</div>';
|
|
|
|
print '</td></tr>';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle actions after payment creation (e.g., process M-Pesa transaction ID)
|
|
|
|
*
|
|
|
|
* @param array $parameters Hook parameters
|
|
|
|
* @param CommonObject $object Current object (e.g., Payment)
|
|
|
|
* @param string $action Current action
|
|
|
|
* @param HookManager $hookmanager Hook manager instance
|
|
|
|
* @return int 0 on success, < 0 on error
|
|
|
|
*/
|
|
|
|
public function createPayment($parameters, &$object, &$action, $hookmanager)
|
2025-03-30 13:09:02 +02:00
|
|
|
{
|
|
|
|
global $langs, $db;
|
|
|
|
|
2025-03-30 13:33:43 +02:00
|
|
|
if ($action === 'addpayment' && !empty(GETPOST('mpesapay_transaction_id', 'alpha'))) {
|
|
|
|
$transactionId = GETPOST('mpesapay_transaction_id', 'alpha');
|
|
|
|
|
|
|
|
// Here you could add logic to verify the transaction ID with M-Pesa API
|
|
|
|
// For now, we'll just log it (assuming a transaction log table exists)
|
|
|
|
$sql = "INSERT INTO " . MAIN_DB_PREFIX . "dolipesa_transactions (transaction_id, fk_payment, datec) ";
|
|
|
|
$sql .= "VALUES ('" . $db->escape($transactionId) . "', " . $object->id . ", NOW())";
|
|
|
|
$resql = $db->query($sql);
|
|
|
|
|
|
|
|
if (!$resql) {
|
|
|
|
setEventMessages($langs->trans('ErrorSavingTransaction'), null, 'errors');
|
|
|
|
return -1;
|
|
|
|
}
|
2025-03-30 13:09:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2025-03-30 12:55:37 +02:00
|
|
|
}
|
2025-03-30 13:33:43 +02:00
|
|
|
?>
|