<?php
include "../main_includes/init.php";

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

// $input = '{
//     "jsjsj":"ssss",
// 	"payingBank": "Access Bank Plc",
// 	"amount": "57000",
// 	"transactionReference": "RT32-YU23-RE435",
// 	"transactionDate": "09/09/2021",
// 	"narrations": "Money for contributions",
// 	"accountName":"Adebami",
// 	"accountNumber":"2504185694",
// 	"transactionType": "",
// 	"senderName":"",
// 	"recipientName":"",
// 	"instrumentNumber":"",
// 	"sessionId":""
// }';


$body = json_decode($input);

$amount = intval($body->amount) / 100;

$payment = new Payment;
$acc = Account::find_by_query("SELECT * FROM account WHERE accountNumber='$body->accountNumber' LIMIT 1");
$acc = array_shift($acc);

if ($acc) {

    $payment->user_id = $acc->user_id;
    $payment->amount = $amount;
    $payment->reference = $body->transactionReference;
    $payment->status = 'Varified';
    $payment->date_created = $date;
    $payment->date_updated = $date;
    $payment->method = "Virtual Acc.";

    if ($payment->save()) {
        $wallet = Wallet::find_by_query("SELECT * FROM wallet WHERE user_id=$payment->user_id LIMIT 1");
        $wallet = array_shift($wallet);
        $wallet->balance += $amount;
        $wallet->date_updated = $date;

        $wallet->save();
    }
}
print_r($body);

http_response_code(200);

exit();