<?php

error_reporting(E_ALL);

function api_call()
{
	// Basic data for the call
	$pp_accessToken = "YOUR_ACCESS_TOKEN";
	$pp_auth_assertion = "YOUR_AUTH_ASSERTION_HEADER"; // Only needed if you do this in 3rd party as a platform
	$custom_id = uniqid();
	$invoice_id = uniqid();
	$request_id = uniqid();
	$metadata_id = uniqid();
	$pp_description = "Gaming Laptop from VR Shopping Mall";
	$pp_currency = "EUR";
	$pp_vaultID = "THE_VAULT_TOKEN_ID";
	$pp_amount = $_GET["amount"];
	$pp_tax = 2.0;
	$pp_total = $pp_amount + $pp_tax;

	$success = false;

	$ch = curl_init();

	curl_setopt($ch, CURLOPT_URL, "https://api-m.sandbox.paypal.com/v2/checkout/orders");
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_HTTPHEADER, array (
			"Content-Type: application/json",
			"Authorization: Bearer ".$pp_accessToken,
			"PayPal-Auth-Assertion: ".$pp_auth_assertion, // Remove this line if doing 1st party payment
			"PayPal-Request-Id: ".$request_id,
			"PayPal-Metadata-Id: ".$metadata_id,
			"Prefer: return=representation"
	));

	curl_setopt($ch, CURLOPT_POSTFIELDS, '{
    "intent": "CAPTURE",
    "purchase_units":
    [
        {
            "reference_id": "pp_order_item_1",
            "description": "'.$pp_description.'",
            "custom_id": "'.$custom_id.'",
            "invoice_id": "ELUSIVE '.$invoice_id.'",
            "soft_descriptor": "ELUSIVE CORP",
            "amount": {
            "currency_code": "'.$pp_currency.'",
            "value": "'.$pp_total.'",
            "breakdown": {
                "item_total": {
                    "currency_code": "'.$pp_currency.'",
                    "value": "'.$_GET["amount"].'"
                },
                "tax_total": {
                    "currency_code": "EUR",
                    "value": "'.$pp_tax.'"
                }
            }
        },
        "shipping":
            {
                "type": "SHIPPING",
                "name": {
                    "full_name": "Elusive Man"
                },
            "address": {
                "address_line_1": "Kamener Str. 75",
                "address_line_2": "",
                "admin_area_2": "Unna",
                "admin_area_1": "",
                "postal_code": "59425",
                "country_code": "DE"
            }
            },
        "items": [
            {
                "name": "VR Gaming Shopping",
                "quantity": 1,
                "description": "Nvidia Gaming Laptop",
                "category": "PHYSICAL_GOODS",
                "unit_amount": {
                "currency_code": "'.$pp_currency.'",
                "value": "'.$_GET["amount"].'"
            },
            "tax":
                {
                "currency_code": "EUR",
                "value": "'.$pp_tax.'"
                }
            }
        ]
        }
    ],
    "payment_source": {
        "paypal": {
            "vault_id": "'.$pp_vaultID.'"
        }
    }
}'
	);
	
	$result = curl_exec($ch);
	curl_close($ch);
	$res = json_decode($result, true);

	if ($res["status"] == "COMPLETED") { $success = true; }	
	return $success;
}

$success = api_call();
if ($success == true) { echo "SUCCESS"; } else { echo "DENIED"; }

?>
