File: //www/wwwroot/jf.cwoyt.com/1.php
<?php
// API endpoint
// 获取 Access Token
$clientId = 'AV215uV-cE-AZ9nd4dNWf24vzrj6UyvA3rA8FDGVW09Y531j9H3GO7FtJmphimXNwisgYNHFvW-6wYMo';
$clientSecret = 'EJb5xUSW8QtuPJeaHuOjAXl9WXp2kj4-zkym-7fXCBM-TssNhkK2FB1-iJOmxYsGcASO8zBTHaPRoJH_';
$paypalRequestId = $clientId;
// PayPal API 认证 URL (沙盒环境)
$authUrl = "https://api.sandbox.paypal.com/v1/oauth2/token";
// 创建认证信息
$auth = base64_encode($clientId . ":" . $clientSecret);
// 初始化 cURL
$ch = curl_init();
// 设置 cURL 请求选项
curl_setopt($ch, CURLOPT_URL, $authUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Basic $auth",
"Content-Type: application/x-www-form-urlencoded"
]);
// 设置请求体
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
// 执行 cURL 请求
$response = curl_exec($ch);
curl_close($ch);
// 解析响应并获取访问令牌
$data = json_decode($response, true);
$accessToken = $data['access_token'];
// Access Token and other headers
$url = "https://api.sandbox.paypal.com/v2/checkout/orders/";
// Create the request body as JSON
$data = [
"intent" => "CAPTURE",
"payment_source" => [
"card" => [
'type' => 'visa', // 卡片类型,支持:visa, masterCard, amex 等
"number" => "4111111111111111", // 使用沙盒测试卡号
'expire_month' => 12, // 卡片过期月份
'expire_year' => 2025, // 卡片过期年份
'cvv' => '123', // 卡片的 CVV2 安全码
"name" => "Firstname Lastname",
"billing_address" => [
"address_line_1" => "123 Main St.",
"admin_area_1" => "CA",
"admin_area_2" => "Anytown",
"postal_code" => "12345",
"country_code" => "US"
]
]
],
"purchase_units" => [
[
"reference_id" => "REFID-000-1001",
"description" => "Description of PU1",
"custom_id" => "CUSTOMID-1001",
"soft_descriptor" => "SOFT-1001",
"amount" => [
"currency_code" => "USD",
"value" => "100.00",
"breakdown" => [
"item_total" => [
"currency_code" => "USD",
"value" => "100.00"
],
"shipping" => [
"currency_code" => "USD",
"value" => "0"
],
"handling" => [
"currency_code" => "USD",
"value" => "0"
],
"tax_total" => [
"currency_code" => "USD",
"value" => "0"
],
"shipping_discount" => [
"currency_code" => "USD",
"value" => "0"
]
]
],
"items" => [
[
"name" => "VRLens2",
"description" => "VRLens2",
"sku" => "259483234816",
"unit_amount" => [
"currency_code" => "USD",
"value" => "50.00"
],
"tax" => [
"currency_code" => "USD",
"value" => "0"
],
"quantity" => "1",
"category" => "PHYSICAL_GOODS"
],
[
"name" => "VRLens1",
"description" => "VRLens1",
"sku" => "259483234817",
"unit_amount" => [
"currency_code" => "USD",
"value" => "50.00"
],
"tax" => [
"currency_code" => "USD",
"value" => "0"
],
"quantity" => "1",
"category" => "PHYSICAL_GOODS"
]
],
"shipping" => [
"name" => [
"full_name" => "Firstname Lastname"
],
"address" => [
"address_line_1" => "123 Main St.",
"admin_area_2" => "Anytown",
"admin_area_1" => "CA",
"postal_code" => "12345",
"country_code" => "US"
]
]
]
]
];
// Convert the data to JSON
$jsonData = json_encode($data);
// Initialize cURL session
$ch = curl_init($url);
// Set cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $accessToken,
'PayPal-Request-Id: ' . $paypalRequestId
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
// Execute the cURL request and get the response
$response = curl_exec($ch);
// Check for cURL errors
if(curl_errno($ch)) {
echo 'Curl error: ' . curl_error($ch);
} else {
// Handle the response
echo $response;
}
// Close cURL session
curl_close($ch);
?>