292 lines
9.2 KiB
PHP
292 lines
9.2 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;
|
|
use Storage;
|
|
|
|
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']);
|
|
|
|
|
|
$request_data = [
|
|
"reference" => "sample",
|
|
"subscriber" => [
|
|
"country" => "MW",
|
|
"currency"=> "MWK",
|
|
"msisdn" => $request->msisdn // 996139030
|
|
],
|
|
"transaction" => [
|
|
"amount" => $request->amount, //10,
|
|
"country" => "MW",
|
|
"currency" => "MWK",
|
|
"id" => $request->refID //time() . uniqid()
|
|
]
|
|
];
|
|
$filename = "requests_" . date("Y-m-d") . ".txt";
|
|
$logdata = date("Y-m-d H:i:s") . " - " . json_encode($request->all());
|
|
Storage::disk('airtelmoney')->append($filename, $logdata);
|
|
|
|
$wallet = Models\AirtelMoneyWallet::where('name', $request->wallet_id)->first();
|
|
if ($wallet == false) {
|
|
// code...
|
|
return response()->json(['code' => 3, 'msg' => 'Wallet Not found']);
|
|
}
|
|
#$authURL = "https://openapiuat.airtel.africa/auth/oauth2/token";
|
|
$authURL = Config('airtelmoney.authURL');
|
|
|
|
$result = $this->authenticate($authURL, $wallet->clientID, $wallet->clientSecret);
|
|
if($result['success']){
|
|
|
|
$bearerToken = $result['token'];
|
|
|
|
//send a ussd push
|
|
$retval = $this->sendUSSDPush($bearerToken, $request_data);
|
|
|
|
$filename = "ussd_push_responses_" . date("Y-m-d") . ".txt";
|
|
$logdata = date("Y-m-d H:i:s") . " - " . json_encode($retval);
|
|
Storage::disk('airtelmoney')->append($filename, $logdata);
|
|
|
|
$result_data = json_decode($retval, true);
|
|
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)
|
|
];
|
|
}
|
|
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
|
|
$result = json_decode($response, true);
|
|
|
|
if ($httpCode === 200 && isset($result['access_token'])) {
|
|
return [
|
|
'success' => true,
|
|
'token' => $result['access_token']
|
|
];
|
|
}
|
|
else {
|
|
return [
|
|
'success' => false,
|
|
'error' => $result, // $result['error_description'] ?? 'Unknown error',
|
|
'details' => $result['error'] ?? []
|
|
];
|
|
}
|
|
}
|
|
}
|