312 lines
8.1 KiB
PHP
312 lines
8.1 KiB
PHP
<?php
|
|
$incoming=file_get_contents("php://input");
|
|
$details=json_decode($incoming,true);
|
|
|
|
$transactionId=$details['transactionId'];
|
|
$amount=$details['amount'];
|
|
$msisdn=$details['msisdn'];
|
|
$description=$details['description'];
|
|
|
|
|
|
$baseURL="https://devpayouts.tnmmpamba.co.mw/api";
|
|
$wallet="505073";
|
|
$password="N8O7L0vpl5mflfwHzf4DSle9bToV*";//CEDAR PASSWORD & All other wallets
|
|
|
|
$res=authenticate($baseURL, $wallet, $password);
|
|
|
|
//echo print_r($res,true);
|
|
$bearerToken=$res['token'];//"102164|newoEd6QOFaTptUiGAp232jULtUmgMtaX1x2CRww4Ka2270dc49";
|
|
|
|
//echo $bearerToken;
|
|
|
|
//$res=validate_msisdn($baseURL, '265881161942', $bearerToken);
|
|
//$res=changePassword($baseURL, $bearerToken, "N8O7L0vpl5mflfwHzf4DSle9bToV*", "N8O7L0vpl5mflfwHzf4DSle9bToV*");
|
|
//$res=sendUSSDPush($baseURL, $bearerToken, $invoiceNumber, $amount, $msisdn, $description);
|
|
$result=makePayment($baseURL,$bearerToken,$msisdn, $amount, $transactionId, $description);
|
|
echo (print_r($result,true));
|
|
|
|
if (!isset($result['http_code'])) {
|
|
// Network or cURL failure
|
|
echo "SYSTEM ERROR: " . $result['message'];
|
|
exit;
|
|
}
|
|
|
|
switch ($result['http_code']) {
|
|
|
|
case 200:
|
|
// Completed successfully
|
|
$transID = $result['response']['data']['transaction_id'] ?? '';
|
|
$receipt = $result['response']['data']['receipt_number'] ?? '';
|
|
echo "SUCCESS: Payment completed. Transaction ID: $transID Receipt: " . $receipt;
|
|
|
|
//mark transaction as SUCCESSFUL in db later
|
|
break;
|
|
|
|
case 202:
|
|
// Accepted but pending
|
|
echo "PENDING: Transaction accepted and processing.";
|
|
|
|
//mark transaction as PENDING / SUSPENSE in the db
|
|
break;
|
|
|
|
case 400:
|
|
// Invalid request
|
|
echo "FAILED: Bad request. Check parameters.";
|
|
|
|
//mark transaction as FAILED in the db
|
|
break;
|
|
|
|
case 503:
|
|
// Service unavailable
|
|
echo "FAILED: Service unavailable. Try again later.";
|
|
|
|
//mark transaction as FAILED or RETRY later after setting a retry cron
|
|
break;
|
|
|
|
default:
|
|
echo "UNKNOWN RESPONSE: " . $result['http_code'];
|
|
//unkown response, do nothing
|
|
}
|
|
|
|
|
|
function makePayment($baseURL,$bearerToken,$msisdn, $amount, $transactionId, $description)
|
|
{
|
|
|
|
|
|
|
|
$url = $baseURL . "/payments";
|
|
|
|
// Request payload
|
|
$payload = array(
|
|
"msisdn" => $msisdn,
|
|
"amount" => (int)$amount,
|
|
"transaction_id" => $transactionId,
|
|
"narration" => $description
|
|
);
|
|
|
|
$ch = curl_init($url);
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
|
|
"Content-Type: application/json",
|
|
"Authorization: Bearer " . $bearerToken
|
|
));
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
|
|
|
|
$response = curl_exec($ch);
|
|
|
|
if ($response === false) {
|
|
$error = curl_error($ch);
|
|
curl_close($ch);
|
|
|
|
return array(
|
|
"status" => "ERROR",
|
|
"message" => "cURL error: " . $error
|
|
);
|
|
}
|
|
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
|
|
$responseData = json_decode($response, true);
|
|
|
|
return array(
|
|
"http_code" => $httpCode,
|
|
"response" => $responseData
|
|
);
|
|
}
|
|
|
|
|
|
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 $response;//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'] ?? []
|
|
];
|
|
}
|
|
}
|
|
|
|
?>
|