<?php

// PAYVESSEL API WEBHOOK NOTIFICATION

// Auto Load Classes
require_once("..ajax/vendor/autoloader.php");
require_once("../../core/helpers/vendor/autoload.php");
header('Content-Type: application/json');
date_default_timezone_set('Africa/Lagos');

// Define trusted IP addresses
$trusted_ips = ['3.255.23.38', '162.246.254.36']; // Replace with actual IP addresses

$headers = getallheaders();
$response = array();
$controller = new ApiAccess;

$input = @file_get_contents("php://input");
$res = json_decode($input);

$payvessel_signature = $_SERVER['HTTP_PAYVESSEL_HTTP_SIGNATURE'];
$ip_address = (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']; 

// Verify if the IP address is trusted
if (!in_array($ip_address, $trusted_ips)) {
    echo "UnAuthorized"; 
        http_response_code(401); 
            exit();
            }

            $email = $res->customer->email;
            $amount = $res->order->amount;
            $transactionReference = $res->transaction->reference;

            // Verify The Transaction
            $check = $controller->verifyPayvesselRef($email, $payvessel_signature, $input);

            if ($check->status == "success") {
                $userid = $check->userid;
                    $userbalance = $check->balance;
                        $email = $check->useremail;
                            $charges = (float) $check->charges;
                                $chargestype = $check->chargestype;
                                    $amount = (float) $amount;

                                        if ($chargestype == "flat") { 
                                                $amounttosave = $amount - $charges;
                                                        $chargesText = "N".$charges;
                                                            } else { 
                                                                    $amounttosave = $amount - ($amount * ($charges/100)); 
                                                                            $chargesText = $charges."%";
                                                                                }

                                                                                    $servicename = "Wallet Topup";
                                                                                        $servicedesc = "Wallet funding of N{$amount} via Payvessel transfer with a service charge of $chargesText.";
                                                                                            $servicedesc .= " Your wallet has been credited with N{$amounttosave}.";
                                                                                                $transactionReference = "PAYVESSEL_".$transactionReference;
                                                                                                    $result = $controller->recordPayvesselTransaction($userid, $servicename, $servicedesc, $amounttosave, $userbalance, $transactionReference, "0");
                                                                                                        $message = $servicedesc . " Your transaction reference is $transactionReference.";
                                                                                                            
                                                                                                                // Send Email Notification
                                                                                                                    $controller->sendEmailNotification($servicename, $message, $email);

                                                                                                                        echo "Success";
                                                                                                                            http_response_code(200);
                                                                                                                                exit();

                                                                                                                                } else {
                                                                                                                                    echo "UnAuthorized"; 
                                                                                                                                        http_response_code(401); 
                                                                                                                                            exit();
                                                                                                                                            }

                                                                                                                                            ?>