File: /var/www/vaspayment.com/app/Http/Controllers/Billers/MonnifyWebhookController.php
<?php
namespace App\Http\Controllers\Billers;
use App\Http\Controllers\Controller;
use App\Models\Gateway;
use App\Models\Transaction;
use App\Models\User;
use App\Models\WalletHistory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
class MonnifyWebhookController extends Controller
{
public function index(Request $request)
{
$payload = $request->all();
// Log the incoming payload
Log::info('Incoming Monnify Webhook Payload: ' . json_encode($payload));
$eventData = $payload['eventData'] ?? null;
if (!$eventData) {
// If eventData is missing, log and return a response
Log::error('Invalid Monnify Webhook Payload: eventData is missing');
return response()->json(['success' => false, 'error' => 'Invalid payload'], 400);
}
$customer = $eventData['customer'] ?? null;
if (!$customer || !isset($customer['email'])) {
// If customer data or email is missing, log and return a response
Log::error('Invalid Monnify Webhook Payload: customer data or email is missing');
return response()->json(['success' => false, 'error' => 'Invalid payload'], 400);
}
$transactionReference = $eventData['transactionReference'] ?? null;
$amountPaid = $eventData['amountPaid'] ?? null;
$customerName = $customer['name'] ?? null;
$customerEmail = $customer['email'];
// Check if the transaction with the provided reference already exists
if ($transactionReference && Transaction::where('reference', $transactionReference)->exists()) {
Log::info('Transaction with reference ' . $transactionReference . ' already exists. Skipping processing.');
return response()->json(['success' => true, 'message' => 'Transaction already processed'], 200);
}
// Process the payload based on the event type
if ($transactionReference && $amountPaid && $customerName) {
Log::info($customerName . ' just made a payment of ' . $amountPaid . ' with reference no ' . $transactionReference);
$user = User::where('email', $customerEmail)->first();
if ($user) {
// Calculate commission fee
$commissionRate = Gateway::pluck('rate')->first() ?? 3.0;
$percentFee = $commissionRate / 100;
$fee = $amountPaid * $percentFee;
// Calculate the remaining amount after deducting the fee
$remainingAmount = $amountPaid - $fee;
// Update user's wallet balance with the remaining amount
$user->wallet->balance += $remainingAmount;
$user->wallet->save();
// Log transaction
$this->logTransaction($user, $transactionReference, $remainingAmount);
// Log wallet history
$this->logWalletHistory($transactionReference, $user, $remainingAmount);
}
} else {
Log::error('Invalid Monnify Webhook Payload: Missing required data');
return response()->json(['success' => false, 'error' => 'Invalid payload'], 400);
}
// Respond with a success status
return response()->json(['success' => true]);
}
private function logTransaction($user, $transactionReference, $remainingAmount)
{
Transaction::create([
'user_id' => $user->id,
'reference' => $transactionReference,
'amount' => $remainingAmount,
'commission' => 0,
'status' => 200,
'type' => 'FUNDING',
'network' => 'WALLET',
'destination' => 'Dear customer, Your wallet funding of N' . $remainingAmount . ' successful',
]);
}
private function logWalletHistory($transactionReference, $user, $remainingAmount)
{
$previousBalance = $user->wallet->balance - $remainingAmount;
WalletHistory::create([
'user_id' => $user->id,
'previous_balance' => $previousBalance,
'current_balance' => $user->wallet->balance,
'amount' => $remainingAmount,
'transaction_type' => 'VIRTUAL WALLET FUNDING',
'transaction_id' => $transactionReference,
]);
}
}