Update hooks/mpesapay_hook.php
Signed-off-by: kevinowino869 <kevinowino869@www.codelab.nestict.africa>
This commit is contained in:
parent
325d011128
commit
0901fc1015
@ -1,58 +1,131 @@
|
||||
<?php
|
||||
/**
|
||||
* M-Pesa Payment Module - Hook Handling
|
||||
* Dolipesa - M-Pesa Payment Module Hook Handling
|
||||
* Author: NESTICT INFOTECH
|
||||
* Version: 1.0.0
|
||||
* Version: 1.0.1
|
||||
* License: GNU General Public License v3.0
|
||||
*/
|
||||
|
||||
class ActionsMpesapay
|
||||
class ActionsDolipesa
|
||||
{
|
||||
/**
|
||||
* Execute hooks for payment and invoice card
|
||||
* 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 object $object Current object (invoice or payment)
|
||||
* @param CommonObject $object Current object (e.g., Facture)
|
||||
* @param string $action Current action
|
||||
* @param object $hookmanager Hook manager
|
||||
* @return int
|
||||
* @param HookManager $hookmanager Hook manager instance
|
||||
* @return int 0 on success, < 0 on error
|
||||
*/
|
||||
function formObjectOptions($parameters, $object, $action, $hookmanager)
|
||||
public function formObjectOptions($parameters, &$object, &$action, $hookmanager)
|
||||
{
|
||||
global $langs, $db;
|
||||
global $langs, $conf;
|
||||
|
||||
if ($parameters['currentcontext'] === 'invoicecard' && $object->statut == 1) {
|
||||
$module = new modMpesapay($db);
|
||||
$paymentUrl = $module->generatePaymentUrl($object->id);
|
||||
$this->hookmanager = $hookmanager;
|
||||
|
||||
echo '<div class="mpesapay-payment">';
|
||||
echo '<a href="' . $paymentUrl . '" class="button">Pay with M-Pesa</a>';
|
||||
echo '</div>';
|
||||
// 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 a payment button on payment card
|
||||
* Add M-Pesa transaction input to payment card
|
||||
*
|
||||
* @param array $parameters Hook parameters
|
||||
* @param object $object Current object (invoice or payment)
|
||||
* @param CommonObject $object Current object (e.g., Payment)
|
||||
* @param string $action Current action
|
||||
* @param object $hookmanager Hook manager
|
||||
* @return int
|
||||
* @param HookManager $hookmanager Hook manager instance
|
||||
* @return int 0 on success, < 0 on error
|
||||
*/
|
||||
function formAddObjectLine($parameters, $object, $action, $hookmanager)
|
||||
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 ($parameters['currentcontext'] === 'payment' && $object->statut == 0) {
|
||||
echo '<div class="mpesapay-payment">';
|
||||
echo '<label>M-Pesa Payment:</label>';
|
||||
echo '<input type="text" name="mpesapay_transaction_id" placeholder="Transaction ID">';
|
||||
echo '</div>';
|
||||
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;
|
||||
}
|
||||
}
|
||||
?>
|
||||
?>
|
Loading…
x
Reference in New Issue
Block a user