Update pages/payment.php
Signed-off-by: kevinowino869 <kevinowino869@www.codelab.nestict.africa>
This commit is contained in:
parent
c3e6e0a5f9
commit
4ea8f875c3
@ -1,4 +1,56 @@
|
|||||||
<?php
|
<?php
|
||||||
// Payment Processing Page
|
/**
|
||||||
echo "Initiate M-Pesa Payment";
|
* M-Pesa Payment Module - Callback Handler
|
||||||
?>
|
* Author: NESTICT INFOTECH
|
||||||
|
* Version: 1.0.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
require '../../main.inc.php';
|
||||||
|
require_once DOL_DOCUMENT_ROOT . "/core/lib/admin.lib.php";
|
||||||
|
|
||||||
|
header('Content-Type: application/json');
|
||||||
|
|
||||||
|
// Retrieve M-Pesa callback data
|
||||||
|
$callbackData = json_decode(file_get_contents('php://input'), true);
|
||||||
|
|
||||||
|
if (!$callbackData) {
|
||||||
|
http_response_code(400);
|
||||||
|
echo json_encode(["error" => "Invalid callback data"]);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract data from callback
|
||||||
|
$invoiceId = $callbackData['AccountReference'] ?? null;
|
||||||
|
$transactionId = $callbackData['MpesaReceiptNumber'] ?? null;
|
||||||
|
$resultCode = $callbackData['ResultCode'] ?? 1;
|
||||||
|
$resultDesc = $callbackData['ResultDesc'] ?? "Unknown error";
|
||||||
|
|
||||||
|
if (!$invoiceId || !$transactionId) {
|
||||||
|
http_response_code(400);
|
||||||
|
echo json_encode(["error" => "Missing invoice or transaction ID"]);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle successful transaction
|
||||||
|
if ($resultCode == 0) {
|
||||||
|
$sql = "UPDATE ".MAIN_DB_PREFIX."facture SET transaction_number = '$transactionId', paye = 1 WHERE rowid = $invoiceId";
|
||||||
|
$db->query($sql);
|
||||||
|
|
||||||
|
// Log payment in Dolibarr
|
||||||
|
$sqlPayment = "INSERT INTO ".MAIN_DB_PREFIX."paiement (facid, amount, datep, num_payment) VALUES ($invoiceId, (SELECT total FROM ".MAIN_DB_PREFIX."facture WHERE rowid = $invoiceId), NOW(), '$transactionId')";
|
||||||
|
$db->query($sqlPayment);
|
||||||
|
|
||||||
|
// Update invoice status
|
||||||
|
$sqlUpdateInvoice = "UPDATE ".MAIN_DB_PREFIX."facture SET statut = 2 WHERE rowid = $invoiceId"; // Paid status
|
||||||
|
$db->query($sqlUpdateInvoice);
|
||||||
|
|
||||||
|
http_response_code(200);
|
||||||
|
echo json_encode(["success" => "Payment successful", "transaction_id" => $transactionId]);
|
||||||
|
} else {
|
||||||
|
// Handle failed transaction
|
||||||
|
http_response_code(400);
|
||||||
|
echo json_encode(["error" => $resultDesc]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$db->close();
|
||||||
|
?>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user