refactoring, airtelmoney test
This commit is contained in:
11
app/Library/AirtelMoneyMw/AirtelPush.php
Normal file
11
app/Library/AirtelMoneyMw/AirtelPush.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Library\AirtelMoneyMw;
|
||||
|
||||
class AirtelPush {
|
||||
/**
|
||||
* @param Collection $collection
|
||||
*/
|
||||
|
||||
}
|
||||
?>
|
||||
50
app/Library/AirtelMoneyMw/callback.php
Normal file
50
app/Library/AirtelMoneyMw/callback.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
/*
|
||||
Developer: David Kumwenda
|
||||
Contact: 0881161942 or 0996139030
|
||||
App: Mpamba 4 MSE
|
||||
Date: 30 August 2025
|
||||
Duration: 1 day dev work*/
|
||||
|
||||
|
||||
// Read raw POST data
|
||||
$rawData = file_get_contents("php://input");
|
||||
|
||||
// Decode JSON
|
||||
$data = json_decode($rawData, true);
|
||||
|
||||
// Basic logging (optional, useful for debugging)
|
||||
file_put_contents("logs/callback_log_".date('Ymd').'.txt', date("Y-m-d H:i:s") . " - " . $rawData . PHP_EOL, FILE_APPEND); exit();
|
||||
|
||||
// Validate required fields
|
||||
if (isset($data["receipt_number"], $data["result_code"], $data["transaction_id"])) {
|
||||
|
||||
$receiptNumber = $data["receipt_number"];
|
||||
$resultCode = $data["result_code"];
|
||||
$resultDescription = $data["result_description"] ?? "";
|
||||
$resultTime = $data["result_time"] ?? date("Y-m-d H:i:s");
|
||||
$transactionId = $data["transaction_id"];
|
||||
$success = $data["success"] ?? false;
|
||||
|
||||
// Example: Save to database
|
||||
// (Replace this with your actual DB insert/update code)
|
||||
/*
|
||||
$conn = mysqli_connect("localhost", "user", "password", "dbname");
|
||||
$stmt = mysqli_prepare($conn, "INSERT INTO payments (transaction_id, receipt_number, result_code, result_description, result_time, success) VALUES (?, ?, ?, ?, ?, ?)");
|
||||
mysqli_stmt_bind_param($stmt, "ssissi", $transactionId, $receiptNumber, $resultCode, $resultDescription, $resultTime, $success);
|
||||
mysqli_stmt_execute($stmt);
|
||||
mysqli_stmt_close($stmt);
|
||||
mysqli_close($conn);
|
||||
*/
|
||||
|
||||
// Respond with 200 OK to acknowledge receipt
|
||||
http_response_code(200);
|
||||
echo json_encode(["status" => "callback received"]);
|
||||
|
||||
} else {
|
||||
// Missing required fields
|
||||
http_response_code(400);
|
||||
echo json_encode(["error" => "Invalid callback payload"]);
|
||||
}
|
||||
|
||||
?>
|
||||
170
app/Library/AirtelMoneyMw/check_balance.php
Normal file
170
app/Library/AirtelMoneyMw/check_balance.php
Normal file
@@ -0,0 +1,170 @@
|
||||
<?php
|
||||
|
||||
$clientID="94351d4d-4909-4056-ad9d-8052a332d6b9";
|
||||
$clientSecret="bf665590-2519-49af-8d1f-7cd0dce1dc7a";
|
||||
|
||||
//CONTINENTAL CAPITAL
|
||||
$clientID="9ff18a6d-331e-4ec5-9ecc-4e512e13747c";
|
||||
$clientSecret="40f44254-10e7-4eb8-b161-38125117f4ba";
|
||||
|
||||
$authURL="https://openapiuat.airtel.africa/auth/oauth2/token";
|
||||
$res=authenticate($authURL, $clientID, $clientSecret);
|
||||
|
||||
if($res['success']){
|
||||
$bearerToken=$res['token'];
|
||||
$country = "MW";
|
||||
$currency = "MWK";
|
||||
//enquire trans status
|
||||
$res=getAirtelBalance($country, $currency, $bearerToken);
|
||||
|
||||
|
||||
if ($res["status"] === "SUCCESS") {
|
||||
echo "Balance: {$res['balance']} {$res['currency']}\n";
|
||||
echo "Account Status: {$res['account_status']}";
|
||||
} else {
|
||||
echo "Error: " . $res["message"];
|
||||
}
|
||||
|
||||
|
||||
}else{
|
||||
echo(print_r($res,true));
|
||||
exit();
|
||||
}
|
||||
|
||||
|
||||
|
||||
function getAirtelBalance($country, $currency, $token) {
|
||||
$url = "https://openapiuat.airtel.africa/standard/v1/users/balance";
|
||||
|
||||
// Initialize cURL
|
||||
$curl = curl_init();
|
||||
|
||||
curl_setopt_array($curl, [
|
||||
CURLOPT_URL => $url,
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_ENCODING => "",
|
||||
CURLOPT_MAXREDIRS => 10,
|
||||
CURLOPT_TIMEOUT => 30,
|
||||
CURLOPT_FOLLOWLOCATION => true,
|
||||
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
|
||||
CURLOPT_CUSTOMREQUEST => "GET",
|
||||
CURLOPT_HTTPHEADER => [
|
||||
"Accept: application/json",
|
||||
"X-Country: $country",
|
||||
"X-Currency: $currency",
|
||||
"Authorization: Bearer $token"
|
||||
],
|
||||
]);
|
||||
|
||||
$response = curl_exec($curl);
|
||||
|
||||
if (curl_errno($curl)) {
|
||||
$error = curl_error($curl);
|
||||
curl_close($curl);
|
||||
return [
|
||||
"status" => "ERROR",
|
||||
"message" => "cURL Error: $error"
|
||||
];
|
||||
}
|
||||
|
||||
curl_close($curl);
|
||||
|
||||
// Decode response
|
||||
$result = json_decode($response, true);
|
||||
|
||||
// Handle invalid JSON
|
||||
if (!$result) {
|
||||
return [
|
||||
"status" => "ERROR",
|
||||
"message" => "Invalid JSON response from Airtel API"
|
||||
];
|
||||
}
|
||||
|
||||
// Check for API structure
|
||||
if (!isset($result["status"])) {
|
||||
return [
|
||||
"status" => "ERROR",
|
||||
"message" => "Unexpected API response format".print_r($result,true)
|
||||
];
|
||||
}
|
||||
|
||||
$statusCode = $result["status"]["code"] ?? null;
|
||||
$message = $result["status"]["message"] ?? "Unknown error";
|
||||
|
||||
// ✅ SUCCESS case
|
||||
if ($statusCode == "200" && isset($result["data"])) {
|
||||
return [
|
||||
"status" => "SUCCESS",
|
||||
"balance" => $result["data"]["balance"] ?? "0",
|
||||
"currency" => $result["data"]["currency"] ?? $currency,
|
||||
"account_status" => $result["data"]["account_status"] ?? "Unknown"
|
||||
];
|
||||
}
|
||||
|
||||
// ❌ ERROR case (e.g. "User not found", "Invalid token", etc.)
|
||||
return [
|
||||
"status" => "ERROR",
|
||||
"message" => $message,
|
||||
"code" => $statusCode,
|
||||
"responseCode" => $result["status"]["response_code"] ?? null,
|
||||
"resultCode" => $result["status"]["result_code"] ?? null
|
||||
];
|
||||
}
|
||||
|
||||
function authenticate($baseURL, $wallet, $password)
|
||||
{
|
||||
// JSON payload
|
||||
$postData = json_encode([
|
||||
'client_id' => $wallet,
|
||||
'client_secret' => $password,
|
||||
'grant_type' => "client_credentials"
|
||||
]);
|
||||
|
||||
// Initialize cURL
|
||||
$ch = curl_init($baseURL);
|
||||
|
||||
// Set cURL options
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the response
|
||||
curl_setopt($ch, CURLOPT_POST, true); // Use POST method
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);// Set the request body
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||
'Content-Type: application/json',
|
||||
'Content-Length: ' . strlen($postData)
|
||||
]);
|
||||
|
||||
// Execute the request
|
||||
$response = curl_exec($ch);
|
||||
|
||||
// Check for cURL errors
|
||||
if (curl_errno($ch)) {
|
||||
curl_close($ch);
|
||||
return [
|
||||
'success' => false,
|
||||
'error' => 'Curl error: ' . curl_error($ch)
|
||||
];
|
||||
}
|
||||
|
||||
// Get HTTP status code
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
// Decode JSON response
|
||||
$result = json_decode($response, true);
|
||||
|
||||
// Check if token is present
|
||||
if ($httpCode === 200 && isset($result['access_token'])) {
|
||||
return [
|
||||
'success' => true,
|
||||
'token' => $result['access_token']
|
||||
];
|
||||
} else {
|
||||
return [
|
||||
'success' => false,
|
||||
'error' => $result['error_description'] ?? 'Unknown error',
|
||||
'details' => $result['error'] ?? []
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
?>
|
||||
246
app/Library/AirtelMoneyMw/testing.php
Normal file
246
app/Library/AirtelMoneyMw/testing.php
Normal file
@@ -0,0 +1,246 @@
|
||||
<?php
|
||||
$incoming=file_get_contents("php://input");
|
||||
$data=json_decode($incoming,true);
|
||||
|
||||
|
||||
$subscriber_country = $data['subscriber']['country'];
|
||||
$subscriber_currency = $data['subscriber']['currency'];
|
||||
$subscriber_msisdn = $data['subscriber']['msisdn'];
|
||||
|
||||
$transaction_amount = $data['transaction']['amount'];
|
||||
$transaction_country = $data['transaction']['country'];
|
||||
$transaction_currency = $data['transaction']['currency'];
|
||||
$transaction_id = $data['transaction']['id'];
|
||||
|
||||
|
||||
$authURL="https://openapiuat.airtel.africa/auth/oauth2/token";
|
||||
/*CEDAR CAPITAL
|
||||
$clientID="94351d4d-4909-4056-ad9d-8052a332d6b9";
|
||||
$clientSecret="bf665590-2519-49af-8d1f-7cd0dce1dc7a";*/
|
||||
|
||||
|
||||
//CONTINENTAL CAPITAL
|
||||
$clientID="9ff18a6d-331e-4ec5-9ecc-4e512e13747c";
|
||||
$clientSecret="40f44254-10e7-4eb8-b161-38125117f4ba";
|
||||
|
||||
|
||||
$res=authenticate($authURL, $clientID, $clientSecret);
|
||||
|
||||
if($res['success']){
|
||||
$bearerToken=$res['token'];
|
||||
|
||||
//send a ussd push
|
||||
$res=sendUSSDPush($bearerToken, $data);
|
||||
$data = json_decode($res, true);
|
||||
|
||||
// Check if the response has a status and success flag
|
||||
if (isset($data['status']['success']) && $data['status']['success'] === true) {
|
||||
// Success case
|
||||
$transactionId = $data['data']['transaction']['id'];
|
||||
$transactionStatus = $data['data']['transaction']['status'];
|
||||
$message = $data['status']['message'];
|
||||
|
||||
echo "✅ Transaction Successful!\n";
|
||||
echo "Transaction ID: $transactionId\n";
|
||||
echo "Status: $transactionStatus\n";
|
||||
echo "Message: $message\n";
|
||||
} else {
|
||||
// Failure case
|
||||
$errorCode = $data['status']['result_code'] ?? 'N/A';
|
||||
$errorMessage = $data['status']['message'] ?? 'Unknown error';
|
||||
|
||||
echo "❌ Transaction Failed!\n";
|
||||
echo "Error Code: $errorCode\n";
|
||||
echo "Message: $errorMessage\n";
|
||||
}
|
||||
|
||||
|
||||
}else{
|
||||
echo(print_r($res,true));
|
||||
exit();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
function sendUSSDPush($token, $data) {
|
||||
// Endpoint
|
||||
$url = "https://openapiuat.airtel.africa/merchant/v1/payments/";
|
||||
|
||||
// Initialize cURL
|
||||
$ch = curl_init($url);
|
||||
|
||||
// Set cURL options
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||
"Authorization: Bearer " . $token,
|
||||
"Content-Type: application/json",
|
||||
"X-Country: MW",
|
||||
"X-Currency: MWK"
|
||||
]);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
||||
|
||||
// Execute request
|
||||
$response = curl_exec($ch);
|
||||
|
||||
// Check for cURL errors
|
||||
if (curl_errno($ch)) {
|
||||
echo "cURL Error: " . curl_error($ch);
|
||||
curl_close($ch);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Close connection
|
||||
curl_close($ch);
|
||||
|
||||
// Decode and return response
|
||||
return $response;
|
||||
}
|
||||
|
||||
function changePassword($baseURL, $token,$newPassword, $newPasswordConfirmation) {
|
||||
// Endpoint URL
|
||||
$url = rtrim($baseURL, "/") . "/password";
|
||||
|
||||
// Prepare data
|
||||
$data = [
|
||||
"new_password" => $newPassword,
|
||||
"new_password_confirmation" => $newPasswordConfirmation
|
||||
];
|
||||
|
||||
// Initialize cURL
|
||||
$ch = curl_init($url);
|
||||
|
||||
// Set cURL options
|
||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH"); // PATCH request
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||
"Authorization: Bearer " . $token,
|
||||
"Content-Type: application/json"
|
||||
]);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
||||
|
||||
// Execute and capture response
|
||||
$response = curl_exec($ch);
|
||||
|
||||
// Check for errors
|
||||
if (curl_errno($ch)) {
|
||||
echo "cURL Error: " . curl_error($ch);
|
||||
curl_close($ch);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Close connection
|
||||
curl_close($ch);
|
||||
|
||||
// Decode JSON response
|
||||
return json_decode($response, true);
|
||||
}
|
||||
|
||||
function validate_msisdn($baseURL, $msisdn, $bearerToken)
|
||||
{
|
||||
// Ensure proper endpoint format
|
||||
$url = rtrim($baseURL, '/') . '/payments/validate/' . urlencode($msisdn);
|
||||
|
||||
// Initialize cURL
|
||||
$ch = curl_init($url);
|
||||
|
||||
// Set cURL options
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPGET, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||
'Authorization: Bearer ' . $bearerToken,
|
||||
'Accept: application/json'
|
||||
]);
|
||||
|
||||
// Execute the request
|
||||
$response = curl_exec($ch);
|
||||
|
||||
// Handle cURL error
|
||||
if (curl_errno($ch)) {
|
||||
curl_close($ch);
|
||||
return [
|
||||
'success' => false,
|
||||
'error' => 'Curl error: ' . curl_error($ch)
|
||||
];
|
||||
}
|
||||
|
||||
// Get HTTP status code
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
// Decode JSON response
|
||||
$result = json_decode($response, true);
|
||||
|
||||
if ($httpCode === 200 && isset($result['data']['full_name'])) {
|
||||
return [
|
||||
'success' => true,
|
||||
'full_name' => $result['data']['full_name']
|
||||
];
|
||||
} else {
|
||||
return [
|
||||
'success' => false,
|
||||
'error' => $result['message'] ?? 'Unknown error',
|
||||
'details' => $result['errors'] ?? []
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
function authenticate($baseURL, $wallet, $password)
|
||||
{
|
||||
// JSON payload
|
||||
$postData = json_encode([
|
||||
'client_id' => $wallet,
|
||||
'client_secret' => $password,
|
||||
'grant_type' => "client_credentials"
|
||||
]);
|
||||
|
||||
// Initialize cURL
|
||||
$ch = curl_init($baseURL);
|
||||
|
||||
// Set cURL options
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the response
|
||||
curl_setopt($ch, CURLOPT_POST, true); // Use POST method
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);// Set the request body
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||
'Content-Type: application/json',
|
||||
'Content-Length: ' . strlen($postData)
|
||||
]);
|
||||
|
||||
// Execute the request
|
||||
$response = curl_exec($ch);
|
||||
|
||||
// Check for cURL errors
|
||||
if (curl_errno($ch)) {
|
||||
curl_close($ch);
|
||||
return [
|
||||
'success' => false,
|
||||
'error' => 'Curl error: ' . curl_error($ch)
|
||||
];
|
||||
}
|
||||
|
||||
// Get HTTP status code
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
// Decode JSON response
|
||||
$result = json_decode($response, true);
|
||||
|
||||
// Check if token is present
|
||||
if ($httpCode === 200 && isset($result['access_token'])) {
|
||||
return [
|
||||
'success' => true,
|
||||
'token' => $result['access_token']
|
||||
];
|
||||
} else {
|
||||
return [
|
||||
'success' => false,
|
||||
'error' => $result['error_description'] ?? 'Unknown error',
|
||||
'details' => $result['error'] ?? []
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
142
app/Library/AirtelMoneyMw/trans_enquiry.php
Normal file
142
app/Library/AirtelMoneyMw/trans_enquiry.php
Normal file
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
//$requestUri = $_SERVER['REQUEST_URI'];
|
||||
//$parts = explode('/', trim($requestUri, '/'));
|
||||
$id = $_GET['id'];//end($parts);
|
||||
//echo $id;
|
||||
|
||||
|
||||
$enqURL="https://openapiuat.airtel.africa/standard/v1/payments/".$id;
|
||||
|
||||
$authURL="https://openapiuat.airtel.africa/auth/oauth2/token";
|
||||
|
||||
$clientID="94351d4d-4909-4056-ad9d-8052a332d6b9";
|
||||
$clientSecret="bf665590-2519-49af-8d1f-7cd0dce1dc7a";
|
||||
|
||||
//CONTINENTAL CAPITAL
|
||||
$clientID="9ff18a6d-331e-4ec5-9ecc-4e512e13747c";
|
||||
$clientSecret="40f44254-10e7-4eb8-b161-38125117f4ba";
|
||||
|
||||
$res=authenticate($authURL, $clientID, $clientSecret);
|
||||
|
||||
if($res['success']){
|
||||
$bearerToken=$res['token'];
|
||||
|
||||
//enquire trans status
|
||||
$res=trans_enquiry($enqURL,$bearerToken);
|
||||
|
||||
echo $res;
|
||||
exit();
|
||||
$data = json_decode($res, true);
|
||||
|
||||
// Check if the response has a status and success flag
|
||||
if (isset($data['status']['success']) && $data['status']['success'] === true) {
|
||||
// Success case
|
||||
$transactionId = $data['data']['transaction']['id'];
|
||||
$transactionStatus = $data['data']['transaction']['status'];
|
||||
$message = $data['status']['message'];
|
||||
|
||||
echo "✅ Transaction Successful!\n";
|
||||
echo "Transaction ID: $transactionId\n";
|
||||
echo "Status: $transactionStatus\n";
|
||||
echo "Message: $message\n";
|
||||
} else {
|
||||
// Failure case
|
||||
$errorCode = $data['status']['result_code'] ?? 'N/A';
|
||||
$errorMessage = $data['status']['message'] ?? 'Unknown error';
|
||||
|
||||
echo "❌ Transaction Failed!\n";
|
||||
echo "Error Code: $errorCode\n";
|
||||
echo "Message: $errorMessage\n";
|
||||
}
|
||||
|
||||
|
||||
}else{
|
||||
echo(print_r($res,true));
|
||||
exit();
|
||||
}
|
||||
|
||||
|
||||
|
||||
function trans_enquiry($enqURL,$token){
|
||||
$curl = curl_init();
|
||||
|
||||
curl_setopt_array($curl, array(
|
||||
CURLOPT_URL => $enqURL,
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_ENCODING => '',
|
||||
CURLOPT_MAXREDIRS => 10,
|
||||
CURLOPT_TIMEOUT => 0,
|
||||
CURLOPT_FOLLOWLOCATION => true,
|
||||
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
|
||||
CURLOPT_CUSTOMREQUEST => 'GET',
|
||||
CURLOPT_HTTPHEADER => array(
|
||||
'Authorization:'.$token,
|
||||
'Accept: */* ',
|
||||
'X-Country: MW',
|
||||
'X-Currency: MWK'
|
||||
),
|
||||
));
|
||||
|
||||
$response = curl_exec($curl);
|
||||
|
||||
curl_close($curl);
|
||||
return $response;
|
||||
}
|
||||
|
||||
function authenticate($baseURL, $wallet, $password)
|
||||
{
|
||||
// JSON payload
|
||||
$postData = json_encode([
|
||||
'client_id' => $wallet,
|
||||
'client_secret' => $password,
|
||||
'grant_type' => "client_credentials"
|
||||
]);
|
||||
|
||||
// Initialize cURL
|
||||
$ch = curl_init($baseURL);
|
||||
|
||||
// Set cURL options
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the response
|
||||
curl_setopt($ch, CURLOPT_POST, true); // Use POST method
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);// Set the request body
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||
'Content-Type: application/json',
|
||||
'Content-Length: ' . strlen($postData)
|
||||
]);
|
||||
|
||||
// Execute the request
|
||||
$response = curl_exec($ch);
|
||||
|
||||
// Check for cURL errors
|
||||
if (curl_errno($ch)) {
|
||||
curl_close($ch);
|
||||
return [
|
||||
'success' => false,
|
||||
'error' => 'Curl error: ' . curl_error($ch)
|
||||
];
|
||||
}
|
||||
|
||||
// Get HTTP status code
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
// Decode JSON response
|
||||
$result = json_decode($response, true);
|
||||
|
||||
// Check if token is present
|
||||
if ($httpCode === 200 && isset($result['access_token'])) {
|
||||
return [
|
||||
'success' => true,
|
||||
'token' => $result['access_token']
|
||||
];
|
||||
} else {
|
||||
return [
|
||||
'success' => false,
|
||||
'error' => $result['error_description'] ?? 'Unknown error',
|
||||
'details' => $result['error'] ?? []
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
?>
|
||||
63
app/Library/Kazang.php
Normal file
63
app/Library/Kazang.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Library;
|
||||
|
||||
use Config;
|
||||
use Log;
|
||||
use App\Models;
|
||||
|
||||
class Kazang
|
||||
{
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
public function HttpPost($url, $postfields){
|
||||
$curl = curl_init();
|
||||
curl_setopt_array($curl, array(
|
||||
CURLOPT_URL => $url,
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_ENCODING => '',
|
||||
CURLOPT_MAXREDIRS => 10,
|
||||
CURLOPT_TIMEOUT => 0,
|
||||
CURLOPT_FOLLOWLOCATION => true,
|
||||
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
|
||||
CURLOPT_CUSTOMREQUEST => 'POST',
|
||||
CURLOPT_POSTFIELDS => json_encode($postfields),
|
||||
CURLOPT_HTTPHEADER => array(
|
||||
'Content-Type: application/json'
|
||||
),
|
||||
));
|
||||
$response = curl_exec($curl);
|
||||
curl_close($curl);
|
||||
return $response;
|
||||
}
|
||||
public function Login(){
|
||||
$kaz_config = config('kazang');
|
||||
// $url = 'https://testapi.kazang.net/apimanager/api_rest/v1/authClient';
|
||||
$url = $kaz_config['test_base_url'] . '/apimanager/api_rest/v1/authClient';
|
||||
$postfields = [
|
||||
"username" => env('KAZ_USERNAME'),
|
||||
"password" => env('KAZ_PASS'),
|
||||
"channel" => env('KAZ_CHANNEL'),
|
||||
];
|
||||
$session_data = $this->HttpPost($url, $postfields);
|
||||
$decoded = json_decode($session_data, true);
|
||||
return $decoded['session_uuid'];
|
||||
}
|
||||
public function get_products($kaz_session_uuid){
|
||||
// $url = 'https://testapi.kazang.net/apimanager/api_rest/v1/productList';
|
||||
$url = $url = $kaz_config['test_base_url'] . '/apimanager/api_rest/v1/productList';
|
||||
$kaz_request_reference = uniqid("_route");
|
||||
$postfields = [
|
||||
"request_reference" => $kaz_request_reference,
|
||||
"session_uuid" => $kaz_session_uuid
|
||||
];
|
||||
$products = $this->HttpPost($url, $postfields);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
193
app/Library/MpambaTnm/MpambaPush.php
Normal file
193
app/Library/MpambaTnm/MpambaPush.php
Normal file
@@ -0,0 +1,193 @@
|
||||
<?php
|
||||
|
||||
namespace App\Library\MpambaTnm;
|
||||
|
||||
class MpambaPush {
|
||||
|
||||
public function sendUSSDPush($baseURL, $token, $invoiceNumber, $amount, $msisdn, $description) {
|
||||
// Endpoint
|
||||
$url = rtrim($baseURL, "/") . "/invoices";
|
||||
|
||||
// Prepare payload
|
||||
$data = [
|
||||
"invoice_number" => $invoiceNumber,
|
||||
"amount" => (int)$amount,
|
||||
"msisdn" => $msisdn,
|
||||
"description" => $description
|
||||
];
|
||||
|
||||
// Initialize cURL
|
||||
$ch = curl_init($url);
|
||||
|
||||
// Set cURL options
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||
"Authorization: Bearer " . $token,
|
||||
"Content-Type: application/json"
|
||||
]);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
||||
|
||||
// Execute request
|
||||
$response = curl_exec($ch);
|
||||
|
||||
// Check for cURL errors
|
||||
if (curl_errno($ch)) {
|
||||
echo "cURL Error: " . curl_error($ch);
|
||||
curl_close($ch);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Close connection
|
||||
curl_close($ch);
|
||||
|
||||
// Decode and return response
|
||||
return json_decode($response, true);
|
||||
}
|
||||
|
||||
public function changePassword($baseURL, $token,$newPassword, $newPasswordConfirmation) {
|
||||
// Endpoint URL
|
||||
$url = rtrim($baseURL, "/") . "/password";
|
||||
|
||||
// Prepare data
|
||||
$data = [
|
||||
"new_password" => $newPassword,
|
||||
"new_password_confirmation" => $newPasswordConfirmation
|
||||
];
|
||||
|
||||
// Initialize cURL
|
||||
$ch = curl_init($url);
|
||||
|
||||
// Set cURL options
|
||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH"); // PATCH request
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||
"Authorization: Bearer " . $token,
|
||||
"Content-Type: application/json"
|
||||
]);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
||||
|
||||
// Execute and capture response
|
||||
$response = curl_exec($ch);
|
||||
|
||||
// Check for errors
|
||||
if (curl_errno($ch)) {
|
||||
echo "cURL Error: " . curl_error($ch);
|
||||
curl_close($ch);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Close connection
|
||||
curl_close($ch);
|
||||
|
||||
// Decode JSON response
|
||||
return json_decode($response, true);
|
||||
}
|
||||
|
||||
public function validate_msisdn($baseURL, $msisdn, $bearerToken){
|
||||
// Ensure proper endpoint format
|
||||
$url = rtrim($baseURL, '/') . '/payments/validate/' . urlencode($msisdn);
|
||||
|
||||
// Initialize cURL
|
||||
$ch = curl_init($url);
|
||||
|
||||
// Set cURL options
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPGET, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||
'Authorization: Bearer ' . $bearerToken,
|
||||
'Accept: application/json'
|
||||
]);
|
||||
|
||||
// Execute the request
|
||||
$response = curl_exec($ch);
|
||||
|
||||
// Handle cURL error
|
||||
if (curl_errno($ch)) {
|
||||
curl_close($ch);
|
||||
return [
|
||||
'success' => false,
|
||||
'error' => 'Curl error: ' . curl_error($ch)
|
||||
];
|
||||
}
|
||||
|
||||
// Get HTTP status code
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
// Decode JSON response
|
||||
$result = json_decode($response, true);
|
||||
|
||||
if ($httpCode === 200 && isset($result['data']['full_name'])) {
|
||||
return [
|
||||
'success' => true,
|
||||
'full_name' => $result['data']['full_name']
|
||||
];
|
||||
} else {
|
||||
return [
|
||||
'success' => false,
|
||||
'error' => $result['message'] ?? 'Unknown error',
|
||||
'details' => $result['errors'] ?? []
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
public function authenticate($baseURL, $wallet, $password){
|
||||
// Endpoint URL
|
||||
$url = rtrim($baseURL, '/') . '/authenticate';
|
||||
|
||||
// JSON payload
|
||||
$postData = json_encode([
|
||||
'wallet' => $wallet,
|
||||
'password' => $password
|
||||
]);
|
||||
|
||||
// Initialize cURL
|
||||
$ch = curl_init($url);
|
||||
|
||||
// Set cURL options
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the response
|
||||
curl_setopt($ch, CURLOPT_POST, true); // Use POST method
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);// Set the request body
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||
'Content-Type: application/json',
|
||||
'Content-Length: ' . strlen($postData)
|
||||
]);
|
||||
|
||||
// Execute the request
|
||||
$response = curl_exec($ch);
|
||||
|
||||
// Check for cURL errors
|
||||
if (curl_errno($ch)) {
|
||||
curl_close($ch);
|
||||
return [
|
||||
'success' => false,
|
||||
'error' => 'Curl error: ' . curl_error($ch)
|
||||
];
|
||||
}
|
||||
|
||||
// Get HTTP status code
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
// Decode JSON response
|
||||
$result = json_decode($response, true);
|
||||
|
||||
// Check if token is present
|
||||
if ($httpCode === 200 && isset($result['data']['token'])) {
|
||||
return [
|
||||
'success' => true,
|
||||
'token' => $result['data']['token'],
|
||||
'expires_at' => $result['data']['expires_at']
|
||||
];
|
||||
} else {
|
||||
return [
|
||||
'success' => false,
|
||||
'error' => $result['message'] ?? 'Unknown error',
|
||||
'details' => $result['errors'] ?? []
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
||||
50
app/Library/MpambaTnm/callback.php
Normal file
50
app/Library/MpambaTnm/callback.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
/*
|
||||
Developer: David Kumwenda
|
||||
Contact: 0881161942 or 0996139030
|
||||
App: Mpamba 4 MSE
|
||||
Date: 30 August 2025
|
||||
Duration: 1 day dev work*/
|
||||
|
||||
|
||||
// Read raw POST data
|
||||
$rawData = file_get_contents("php://input");
|
||||
|
||||
// Decode JSON
|
||||
$data = json_decode($rawData, true);
|
||||
|
||||
// Basic logging (optional, useful for debugging)
|
||||
file_put_contents("logs/callback_log_".date('Ymd').'.txt', date("Y-m-d H:i:s") . " - " . $rawData . PHP_EOL, FILE_APPEND);
|
||||
|
||||
// Validate required fields
|
||||
if (isset($data["receipt_number"], $data["result_code"], $data["transaction_id"])) {
|
||||
|
||||
$receiptNumber = $data["receipt_number"];
|
||||
$resultCode = $data["result_code"];
|
||||
$resultDescription = $data["result_description"] ?? "";
|
||||
$resultTime = $data["result_time"] ?? date("Y-m-d H:i:s");
|
||||
$transactionId = $data["transaction_id"];
|
||||
$success = $data["success"] ?? false;
|
||||
|
||||
// Example: Save to database
|
||||
// (Replace this with your actual DB insert/update code)
|
||||
/*
|
||||
$conn = mysqli_connect("localhost", "user", "password", "dbname");
|
||||
$stmt = mysqli_prepare($conn, "INSERT INTO payments (transaction_id, receipt_number, result_code, result_description, result_time, success) VALUES (?, ?, ?, ?, ?, ?)");
|
||||
mysqli_stmt_bind_param($stmt, "ssissi", $transactionId, $receiptNumber, $resultCode, $resultDescription, $resultTime, $success);
|
||||
mysqli_stmt_execute($stmt);
|
||||
mysqli_stmt_close($stmt);
|
||||
mysqli_close($conn);
|
||||
*/
|
||||
|
||||
// Respond with 200 OK to acknowledge receipt
|
||||
http_response_code(200);
|
||||
echo json_encode(["status" => "callback received"]);
|
||||
|
||||
} else {
|
||||
// Missing required fields
|
||||
http_response_code(400);
|
||||
echo json_encode(["error" => "Invalid callback payload"]);
|
||||
}
|
||||
|
||||
?>
|
||||
5
app/Library/MpambaTnm/index.php
Normal file
5
app/Library/MpambaTnm/index.php
Normal file
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
$data=file_get_contents('php://input');
|
||||
echo 'This is the correct path.';
|
||||
|
||||
?>
|
||||
214
app/Library/MpambaTnm/testing.php
Normal file
214
app/Library/MpambaTnm/testing.php
Normal file
@@ -0,0 +1,214 @@
|
||||
<?php
|
||||
$incoming=file_get_contents("php://input");
|
||||
$details=json_decode($incoming,true);
|
||||
|
||||
$invoiceNumber=$details['invoiceNumber'];
|
||||
$amount=$details['amount'];
|
||||
$msisdn=$details['msisdn'];
|
||||
$description=$details['description'];
|
||||
|
||||
|
||||
$baseURL="https://devpayouts.tnmmpamba.co.mw/api";
|
||||
$wallet="505072";
|
||||
$password="N8O7L0vpl5mflfwHzf4DSle9bToV*";//CEDAR PASSWORD
|
||||
|
||||
//$res=authenticate($baseURL, $wallet, $password);
|
||||
$bearerToken="102164|newoEd6QOFaTptUiGAp232jULtUmgMtaX1x2CRww4Ka2270dc49";
|
||||
|
||||
|
||||
|
||||
//$res=validate_msisdn($baseURL, '265881161942', $bearerToken);
|
||||
//$res=changePassword($baseURL, $bearerToken, "N8O7L0vpl5mflfwHzf4DSle9bToV*", "N8O7L0vpl5mflfwHzf4DSle9bToV*");
|
||||
$res=sendUSSDPush($baseURL, $bearerToken, $invoiceNumber, $amount, $msisdn, $description);
|
||||
echo (print_r($res,true));
|
||||
|
||||
function sendUSSDPush($baseURL, $token, $invoiceNumber, $amount, $msisdn, $description) {
|
||||
// Endpoint
|
||||
$url = rtrim($baseURL, "/") . "/invoices";
|
||||
|
||||
// Prepare payload
|
||||
$data = [
|
||||
"invoice_number" => $invoiceNumber,
|
||||
"amount" => (int)$amount,
|
||||
"msisdn" => $msisdn,
|
||||
"description" => $description
|
||||
];
|
||||
|
||||
// Initialize cURL
|
||||
$ch = curl_init($url);
|
||||
|
||||
// Set cURL options
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||
"Authorization: Bearer " . $token,
|
||||
"Content-Type: application/json"
|
||||
]);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
||||
|
||||
// Execute request
|
||||
$response = curl_exec($ch);
|
||||
|
||||
// Check for cURL errors
|
||||
if (curl_errno($ch)) {
|
||||
echo "cURL Error: " . curl_error($ch);
|
||||
curl_close($ch);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Close connection
|
||||
curl_close($ch);
|
||||
|
||||
// Decode and return response
|
||||
return json_decode($response, true);
|
||||
}
|
||||
|
||||
function changePassword($baseURL, $token,$newPassword, $newPasswordConfirmation) {
|
||||
// Endpoint URL
|
||||
$url = rtrim($baseURL, "/") . "/password";
|
||||
|
||||
// Prepare data
|
||||
$data = [
|
||||
"new_password" => $newPassword,
|
||||
"new_password_confirmation" => $newPasswordConfirmation
|
||||
];
|
||||
|
||||
// Initialize cURL
|
||||
$ch = curl_init($url);
|
||||
|
||||
// Set cURL options
|
||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH"); // PATCH request
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||
"Authorization: Bearer " . $token,
|
||||
"Content-Type: application/json"
|
||||
]);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
||||
|
||||
// Execute and capture response
|
||||
$response = curl_exec($ch);
|
||||
|
||||
// Check for errors
|
||||
if (curl_errno($ch)) {
|
||||
echo "cURL Error: " . curl_error($ch);
|
||||
curl_close($ch);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Close connection
|
||||
curl_close($ch);
|
||||
|
||||
// Decode JSON response
|
||||
return json_decode($response, true);
|
||||
}
|
||||
|
||||
function validate_msisdn($baseURL, $msisdn, $bearerToken)
|
||||
{
|
||||
// Ensure proper endpoint format
|
||||
$url = rtrim($baseURL, '/') . '/payments/validate/' . urlencode($msisdn);
|
||||
|
||||
// Initialize cURL
|
||||
$ch = curl_init($url);
|
||||
|
||||
// Set cURL options
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPGET, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||
'Authorization: Bearer ' . $bearerToken,
|
||||
'Accept: application/json'
|
||||
]);
|
||||
|
||||
// Execute the request
|
||||
$response = curl_exec($ch);
|
||||
|
||||
// Handle cURL error
|
||||
if (curl_errno($ch)) {
|
||||
curl_close($ch);
|
||||
return [
|
||||
'success' => false,
|
||||
'error' => 'Curl error: ' . curl_error($ch)
|
||||
];
|
||||
}
|
||||
|
||||
// Get HTTP status code
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
// Decode JSON response
|
||||
$result = json_decode($response, true);
|
||||
|
||||
if ($httpCode === 200 && isset($result['data']['full_name'])) {
|
||||
return [
|
||||
'success' => true,
|
||||
'full_name' => $result['data']['full_name']
|
||||
];
|
||||
} else {
|
||||
return [
|
||||
'success' => false,
|
||||
'error' => $result['message'] ?? 'Unknown error',
|
||||
'details' => $result['errors'] ?? []
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
function authenticate($baseURL, $wallet, $password)
|
||||
{
|
||||
// Endpoint URL
|
||||
$url = rtrim($baseURL, '/') . '/authenticate';
|
||||
|
||||
// JSON payload
|
||||
$postData = json_encode([
|
||||
'wallet' => $wallet,
|
||||
'password' => $password
|
||||
]);
|
||||
|
||||
// Initialize cURL
|
||||
$ch = curl_init($url);
|
||||
|
||||
// Set cURL options
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the response
|
||||
curl_setopt($ch, CURLOPT_POST, true); // Use POST method
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);// Set the request body
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||
'Content-Type: application/json',
|
||||
'Content-Length: ' . strlen($postData)
|
||||
]);
|
||||
|
||||
// Execute the request
|
||||
$response = curl_exec($ch);
|
||||
|
||||
// Check for cURL errors
|
||||
if (curl_errno($ch)) {
|
||||
curl_close($ch);
|
||||
return [
|
||||
'success' => false,
|
||||
'error' => 'Curl error: ' . curl_error($ch)
|
||||
];
|
||||
}
|
||||
|
||||
// Get HTTP status code
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
// Decode JSON response
|
||||
$result = json_decode($response, true);
|
||||
|
||||
// Check if token is present
|
||||
if ($httpCode === 200 && isset($result['data']['token'])) {
|
||||
return [
|
||||
'success' => true,
|
||||
'token' => $result['data']['token'],
|
||||
'expires_at' => $result['data']['expires_at']
|
||||
];
|
||||
} else {
|
||||
return [
|
||||
'success' => false,
|
||||
'error' => $result['message'] ?? 'Unknown error',
|
||||
'details' => $result['errors'] ?? []
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
58
app/Library/MpambaTnm/transaction_check.php
Normal file
58
app/Library/MpambaTnm/transaction_check.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
$incoming=file_get_contents("php://input");
|
||||
$details=json_decode($incoming,true);
|
||||
|
||||
$invoiceNumber=$details['invoiceNumber'];
|
||||
$token=$details['token'];
|
||||
|
||||
|
||||
$baseURL="https://devpayouts.tnmmpamba.co.mw/api";
|
||||
|
||||
//$res=authenticate($baseURL, $wallet, $password);
|
||||
$bearerToken="102164|newoEd6QOFaTptUiGAp232jULtUmgMtaX1x2CRww4Ka2270dc49";
|
||||
|
||||
|
||||
|
||||
|
||||
$res=checkInvoiceStatus($invoiceNumber, $bearerToken, $baseURL);
|
||||
echo (print_r($res,true));
|
||||
|
||||
function checkInvoiceStatus($invoiceNumber, $bearerToken, $baseURL) {
|
||||
// Build request URL
|
||||
$url = rtrim($baseURL, '/') . "/invoices/" . urlencode($invoiceNumber);
|
||||
|
||||
// Initialize cURL
|
||||
$ch = curl_init();
|
||||
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_URL => $url,
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_HTTPHEADER => [
|
||||
"Authorization: Bearer " . $bearerToken,
|
||||
"Accept: application/json"
|
||||
]
|
||||
]);
|
||||
|
||||
// Execute the request
|
||||
$response = curl_exec($ch);
|
||||
|
||||
// Handle errors
|
||||
if (curl_errno($ch)) {
|
||||
echo "cURL Error: " . curl_error($ch);
|
||||
curl_close($ch);
|
||||
return null;
|
||||
}
|
||||
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
// Check for successful response
|
||||
if ($httpCode === 200 && $response) {
|
||||
return json_decode($response, true);
|
||||
} else {
|
||||
echo "Request failed. HTTP Code: " . $httpCode . "\nResponse: " . $response;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user