Files
unifiedpayment/app/Http/Controllers/AirtelMoneyMalawiController.php
2025-11-17 18:49:02 +00:00

279 lines
8.9 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Models;
use App\Library\AirtelMoneyMw;
use Carbon\Carbon;
use Config;
class AirtelMoneyMalawiController extends Controller
{
public function collectTest(Request $request){
return response()->json(['code' => 1, 'data' => json_decode($request->all())]);
}
public function Collect(Requests\CollectPaymentsRequest $request){
$current_date = date('Y-m-d');
// $incoming=file_get_contents("php://input");
// $data = json_decode($incoming, true);
$data = $request->only(['msisdn', 'country', 'currency']);
//country, currency, msisdn,
// $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";
//no comments
//CONTINENTAL CAPITAL
$clientID = "9ff18a6d-331e-4ec5-9ecc-4e512e13747c";
$clientSecret = "40f44254-10e7-4eb8-b161-38125117f4ba";
$result = $this->authenticate($authURL, $clientID, $clientSecret);
if($result['success']){
$bearerToken = $result['token'];
//send a ussd push
$retval = $this->sendUSSDPush($bearerToken, $data);
$result_data = json_decode($retval, true);
// dump($result_data);
// Check if the response has a status and success flag
if (isset($result_data['status']['success']) && $result_data['status']['success'] === true) {
// Success case
$transactionId = $result_data['data']['transaction']['id'];
$transactionStatus = $result_data['data']['transaction']['status'];
$message = $result_data['status']['message'];
$msg = "✅ Transaction Successful!\n";
$msg .= "Transaction ID: $transactionId\n";
$msg .= "Status: $transactionStatus\n";
$msg .= "Message: $message\n";
return response()->json(['code' => 1, 'msg' => $msg]);
} else {
// Failure case
$errorCode = $result_data['status']['result_code'] ?? 'N/A';
$errorMessage = $result_data['status']['message'] ?? 'Unknown error';
$msg = "❌ Transaction Failed!\n";
$msg .= "Error Code: $errorCode\n";
$msg .= "Message: $errorMessage\n";
return response()->json(['code' => 3, 'msg' => $msg, 'responseRaw' => $result_data ]);
}
}
else{
$msg = $result;
return response()->json(['code' => 5, 'msg' => $msg]);
exit();
}
}
public function getProductIDs($product_name){
$product = Models\Product::where('name', $product_name)->first();
return $product;
}
public function getProductIDsFirst($session_uuid){
$params = [
"request_reference" => 31000,
"session_uuid" => $session_uuid
];
$kazang = Config('kazang');
$kaz_host = $kazang['test_base_url'];
$url = "$kaz_host/apimanager/api_rest/v1/productList";
$retval = $this->globalCurlPost($url, $params);
return $retval;
}
public 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;
}
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, $clientID, $clientSecret){
// JSON payload
$postData = json_encode([
'client_id' => $clientID,
'client_secret' => $clientSecret,
'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'] ?? []
];
}
}
}