<?php

require 'config.php';

$amount = $_POST['cashValue'] * 100;

session_start();

security::InitSecurity();

member::checkLoggedInUser();

security::Bearer(Bearer::LOGIN);

if(!isset($_POST['csrf'])){
    member::logout();
    header("Location: index.php?code=logout");
    exit();    
}

if($_POST['csrf'] != $_SESSION['csrf']){
    member::logout();
    header("Location: index.php?code=logout");
    exit();
}

$package = package::getPackId($_POST['cashValue']);

if(empty($package)) {
    header("Location: donate.php?code=error");
    exit();
}

$customer_email = $_POST['character'].'_'.$package['id'].'_'.$_SESSION['account_id'].'@'. STRIPE_EMAIL . '.com';

\Stripe\Stripe::setApiKey(STRIPE_API_KEY);

$checkout_session = \Stripe\Checkout\Session::create([
    'submit_type' => 'donate',
    "mode" => "payment",
    "success_url" => "http://".DOMAIN_SITE."/donate.php?sid={CHECKOUT_SESSION_ID}&cid=".$_POST['character'],
    "cancel_url" => "http://".DOMAIN_SITE."/donate.php?code=cancel",
    "locale" => "th",
    'allow_promotion_codes' => STRIPE_PROMO_CODE,
    'customer_email' => $customer_email,
    'payment_method_types' => ['promptpay'],
    "line_items" => [
        [
            "quantity" => 1,
            "price_data" => [
                "currency" => "thb",
                "unit_amount" => $amount,
                "product_data" => [
                    "name" => $package['description']
                ]
            ]
        ],
    ]
]);

http_response_code(303);
header("Location: " . $checkout_session->url);

?>
