<?php
include("../../includes/init.php");

require_once "../../vendor/autoload.php";

use Matscode\Paystack\Transaction;
use Matscode\Paystack\Utility\Debug;
use Matscode\Paystack\Utility\Http;

// print_r($_POST);

// return false;

// if (isset($_POST['amount']) && $_POST['amount'] != 0) {
if (isset($_POST['amount'], $_SESSION['user_id']) && $_POST['amount'] != 0 && $_SESSION['user_id'] != "") {

	extract($_POST);

	if (intval($amount) < 500) {
		redirect("../../?page=fund_wallet");
		return false;
	}

	$user = User::find_by_id($_SESSION['user_id']);
	if (!$user) {
		header("Location: ../../?page=fund_wallet");
	}
	// creating the transaction object
	$Transaction = new Transaction(SECRET_KEY);

	// Set data to post using this method
	/*
		$response = $Transaction->initialize( [
			'email'  => 'customer@email.com',
			'amount' => 500000
		] );
	*/

	// OR
	// print_r(get_class_methods($Transaction));

	$response =
		$Transaction
		->setEmail($user->email)
		->setAmount($amount)
		->setCallbackUrl("https://dashboard.9jatrust.com.ng/payment/test/callback.php?sasd=" . $_SESSION['user_id'])
		->initialize();

	// print response
	// Debug::print_r($response);
	$my_transaction = new MyTransaction;
	$my_transaction->user_id = $_SESSION['user_id'];
	$my_transaction->amount = $amount;
	$my_transaction->reference = $response->reference;
	$my_transaction->status = "Not Varified";
	$my_transaction->date_created = $date;
	$my_transaction->date_updated = $date;

	$my_transaction->save();

	Http::redirect($response->authorizationUrl);

	// save reference somewhere and do a redirect
} else {
	header("Location: ../../?page=fund_wallet");
}