<?php
require 'db_conn.php';

$client_id = "AXtjsn6XKHPOOx5Voj_Ip_R8BIvv_NGXCQTtkB9nzfNWSHwYNay8V4u5urSURhXYhER4UuSACmz1bDfS";
$secret = "EKC6CJ7SflaTi7zBbKIY3fJB3rat6R-UfaUQDQHk9_y-f86zzt1sZMZ50y1WA5jDW0s5-vRcDMglkdGD";

// Iegūst piekļuves pilnvaru
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api-m.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$client_id:$secret");
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");

$headers = [
    "Accept: application/json",
    "Accept-Language: en_US"
];

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = json_decode(curl_exec($ch));
$access_token = $response->access_token;

// Izveido pasūtījumu
$total = $_POST['total'];

$data = [
    "intent" => "CAPTURE",
    "purchase_units" => [[
        "amount" => [
            "currency_code" => "EUR",
            "value" => $total
        ]
    ]]
];

$ch = curl_init("https://api-m.sandbox.paypal.com/v2/checkout/orders");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Content-Type: application/json",
    "Authorization: Bearer $access_token"
]);

$result = curl_exec($ch);
echo $result;