DoliPesa/hooks/dolipesa_hook.php

131 lines
4.6 KiB
PHP
Raw Permalink Normal View History

<?php
/**
* Dolipesa - M-Pesa Payment Module Hook Handling
* Author: NESTICT INFOTECH
* Version: 1.0.1
* License: GNU General Public License v3.0
*/
class ActionsDolipesa
{
/**
* 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
*
* @param array $parameters Hook parameters
* @param CommonObject $object Current object (e.g., Facture)
* @param string $action Current action
* @param HookManager $hookmanager Hook manager instance
* @return int 0 on success, < 0 on error
*/
public function formObjectOptions($parameters, &$object, &$action, $hookmanager)
{
global $langs, $conf;
$this->hookmanager = $hookmanager;
// 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
}
// 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>';
}
return 0;
}
/**
* Add M-Pesa transaction input to payment card
*
* @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 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)
{
global $langs, $db;
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;
}
}
return 0;
}
}
?>