refactored Mpamba Controller, added wallet tables

This commit is contained in:
Kwesi Banson Jnr
2025-11-26 21:45:45 +00:00
parent 094efd008e
commit 1e18492dc2
19 changed files with 466 additions and 38 deletions

View File

@@ -0,0 +1,253 @@
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Http\Request;
use App\Models;
use App\Library\MpambaTnm;
use Carbon\Carbon;
use Config;
use Storage;
class MpambaTnmController extends Controller
{
//
public function collect(Requests\MpambaTnmRequest $request){
/*
//$incoming = file_get_contents("php://input");
//$details = json_decode($incoming, true);
$invoiceNumber = $details['invoiceNumber'];
$amount = $details['amount'];
$msisdn = $details['msisdn'];
$description = $details['description'];
{"message":"Request accepted and processing","errors":[],"trace":[],"data":[]}
*/
$filename = "requests_" . date("Y-m-d") . ".txt";
$logdata = date("Y-m-d H:i:s") . " - " . json_encode($request->all());
Storage::disk('mpambatnm')->append($filename, $logdata);
//todo
// grab broker wallet detals
$wallet = Models\MpambaTnmWallet::where('wallet_id', $request->wallet_id)->first();
if ($wallet == false) {
// code...
return response()->json(['code' => 3, 'msg' => 'Wallet Not found']);
}
$invoiceNumber = . $wallet->id . "_" . time();
$description = "Stock purchase Payment from " . $wallet->name;
// generate invoiceNumber
// return response()->json(['code' => 1, 'data' => $wallet]);
$baseURL = Config('mpambatnm.base_url'); // "https://devpayouts.tnmmpamba.co.mw/api";
#$wallet = "505072";
#$password = "N8O7L0vpl5mflfwHzf4DSle9bToV*";//CEDAR PASSWORD
//$res=authenticate($baseURL, $wallet, $password);
#$bearerToken = "102164|newoEd6QOFaTptUiGAp232jULtUmgMtaX1x2CRww4Ka2270dc49";
$token_arr = $this->authenticate($baseURL, $wallet->wallet_id, $wallet->password);
if ($token_arr['success'] == true) {
$result = $this->sendUSSDPush($baseURL, $token_arr['token'], $invoiceNumber, $request->amount, $request->msisdn, $description);
//create transaction here
$filename = "ussd_push_responses_" . date("Y-m-d") . ".txt";
$logdata = date("Y-m-d H:i:s") . " - " . json_encode($result);
Storage::disk('mpambatnm')->append($filename, $logdata);
$transaction_params = [
'msisdn' => $request->msisdn,
'amount' => $request->amount,
'invoice_number' => $invoiceNumber,
'wallet_id' => $wallet->wallet_id,
'reference_id' => $request->refID,
];
$transaction = Models\TnmTransaction::create($transaction_params);
return response()->json(['code' => 1, 'data' => $result]);
}
}
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);
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'] ?? []
];
}
}
}