<?php

////////////////////////////////////////////////////////
// GET AN ACCESS TOKEN FROM PAYPAL 

//$ch = curl_init();
//
//// PRODUCTION
//$clientId = "AZsMPHqx0TlbCyV_8AY5LlNzzmilpaP8lbfQQQ4iPRpk_WC8hOWFWdvnGMgln4RJAAO2oWd7g1PhhMtu";
//$secret = "EFZpihetGeQCYOA_zWas7iTQ1Le32fSmBvgZd5tZ01TvonU8hwxEQnbjD6Xjmqzasu4ZtiDHmO4sBaRa";
//
//// SANDBOX
////$clientId = "AfbGagMA8OwwgX0fxBjzZrHn_aCZQEXUptCPW6e8ybFG7nepqq9sNDg-CdnHGFzEH_t9b5BalIwwiRrZ";
////$secret = "EISNV0-LkFw6R8302kwFVRhnXI8iHOt4Clr_F0UJmH9oeaeG8sh_UsLMReh-hMZfdgNQInVEjcpTzj1u";
//
//curl_setopt($ch, CURLOPT_URL, "https://api.paypal.com/v1/oauth2/token");
////curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
//curl_setopt($ch, CURLOPT_HEADER, false);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_SSLVERSION , 6); //NEW ADDITION
//curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$secret);
//curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
//
//$result = curl_exec($ch);
//
//if (empty($result))
//	die("Error: No response.");
//else {
//	$json = json_decode($result);
//	print_r($json);
//	//print_r($json->access_token);
//}
//
//curl_close($ch); //THIS CODE IS NOW WORKING!
//
//exit;
////////////////////////////////////////////////////////
// https://voicetrivia.com/3rdparty/payments/create.php

//A21AANYPHc5vWJ7hFkmRo-5BglccMgZLIKwhMgGL2wdkkOYAsxiyugiX7H5M8kgYuiEOaHbzMl3e5Ph2VXcHG7xUdRvURQ43Q 
//[token_type] => Bearer 
//[app_id] => APP-80B18685FE3645629 
//[expires_in] => 32400 
//[nonce] => 2024-08-12T23:52:42Zgj4eJFoliALztgqMr4xZoXW3Fr0kDYbdxaZpTbFgCBE )

/**

string(1005) "{"status":"APPROVAL_PENDING","status_update_time":"2024-04-18T05:06:35Z","id":"I-YGTYH9PVSB4H","plan_id":"P-8DB42077DL151831FMYPLZSI","start_time":"2024-04-18T05:06:35Z","quantity":"1","shipping_amount":{"currency_code":"USD","value":"0.0"},"subscriber":{"email_address":"customer@example.com","name":{"given_name":"John","surname":"Doe"},"shipping_address":{"name":{"full_name":"John Doe"},"address":{"address_line_1":"2211 N First Street","address_line_2":"Building 17","admin_area_2":"San Jose","admin_area_1":"CA","postal_code":"95131","country_code":"US"}}},"create_time":"2024-04-18T05:06:35Z","custom_id":"4","plan_overridden":false,"links":[{"href":"https://www.sandbox.paypal.com/webapps/billing/subscriptions?ba_token=BA-8T6099765E462764Y","rel":"approve","method":"GET"},{"href":"https://api.sandbox.paypal.com/v1/billing/subscriptions/I-YGTYH9PVSB4H","rel":"edit","method":"PATCH"},{"href":"https://api.sandbox.paypal.com/v1/billing/subscriptions/I-YGTYH9PVSB4H","rel":"self","method":"GET"}]}"
**/

$postData = '{
    "intent": "CAPTURE",
    "purchase_units": [
        {
            "items": [
                {
                    "name": "T-Shirt",
                    "description": "Green XL",
                    "quantity": "1",
                    "unit_amount": {
                        "currency_code": "USD",
                        "value": "1.00"
                    }
                }
            ],
            "amount": {
                "currency_code": "USD",
                "value": "1.00",
                "breakdown": {
                    "item_total": {
                        "currency_code": "USD",
                        "value": "1.00"
                    }
                }
            }
        }
    ],
    "application_context": {
        "return_url": "https://example.com/return",
        "cancel_url": "https://example.com/cancel"
    }
}';

// https://voicetrivia.com/pay/reference2/createOrder.php
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api-m.paypal.com/v2/checkout/orders');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$postData);
$headers = array();
$headers[] = 'Authorization: Bearer A21AANYPHc5vWJ7hFkmRo-5BglccMgZLIKwhMgGL2wdkkOYAsxiyugiX7H5M8kgYuiEOaHbzMl3e5Ph2VXcHG7xUdRvURQ43Q';
$headers[] = 'Content-Type: application/json';
$headers[] = 'Accept: application/json';
$headers[] = 'Paypal-Request-Id: TESTORDER-21092019-001';
$headers[] = 'Prefer: return=representation';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
//var_dump($result);

return $result;

//$data_formatted = json_decode($result, TRUE);
//echo "<pre>";
//print_r($data_formatted);

//print_r($data_formatted['links'][0]['href']);

//$links = $data_formatted['links'];
//
//foreach ($links as $link) {
//	if ($link['rel']=="approve") {
//		//echo $link['href'];
//		$paypal_link = $link['href'];
//		echo json_encode(array("user_id"=> $_POST['user_id'], "link"=> $paypal_link));
//		exit;
//	}
//}

if (curl_errno($ch)) {
	echo 'Error:' . curl_error($ch);
}
curl_close($ch);

//curl -v -X POST https://api-m.sandbox.paypal.com/v1/billing/subscriptions \
//-H 'Authorization: Bearer A21AAGHr9qtiRRXH4oYcQokQgV99rGqEIfgrr8xHCclP0OzmD9KVgg5ppIIg1jzJgQkV4wd02svIvBJyg6cLFJjFow_SjBhxQ' \
//-H 'Content-Type: application/json' \
//-H 'Accept: application/json' \
//-H 'PayPal-Request-Id: SUBSCRIPTION-21092019-001' \
//-H 'Prefer: return=representation' \
//-d '{
//  "plan_id": "P-5ML4271244454362WXNWU5NQ",
//  "start_time": "2018-11-01T00:00:00Z",
//  "quantity": "20",
//  "shipping_amount": {
//    "currency_code": "USD",
//    "value": "10.00"
//  },
//  "subscriber": {
//    "name": {
//      "given_name": "John",
//      "surname": "Doe"
//    },
//    "email_address": "customer@example.com",
//    "shipping_address": {
//      "name": {
//        "full_name": "John Doe"
//      },
//      "address": {
//        "address_line_1": "2211 N First Street",
//        "address_line_2": "Building 17",
//        "admin_area_2": "San Jose",
//        "admin_area_1": "CA",
//        "postal_code": "95131",
//        "country_code": "US"
//      }
//    }
//  },
//  "application_context": {
//    "brand_name": "walmart",
//    "locale": "en-US",
//    "shipping_preference": "SET_PROVIDED_ADDRESS",
//    "user_action": "SUBSCRIBE_NOW",
//    "payment_method": {
//      "payer_selected": "PAYPAL",
//      "payee_preferred": "IMMEDIATE_PAYMENT_REQUIRED"
//    },
//    "return_url": "https://example.com/returnUrl",
//    "cancel_url": "https://example.com/cancelUrl"
//  }
//}' 

?>