File: /www/备份的/ly.ydrbh.com/1.php
<?php
// API endpoint
// 获取 Access Token
$clientId = 'AV215uV-cE-AZ9nd4dNWf24vzrj6UyvA3rA8FDGVW09Y531j9H3GO7FtJmphimXNwisgYNHFvW-6wYMo';
$clientSecret = 'EJb5xUSW8QtuPJeaHuOjAXl9WXp2kj4-zkym-7fXCBM-TssNhkK2FB1-iJOmxYsGcASO8zBTHaPRoJH_';
// 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" => [
"number" => "4111111111111111", // 使用沙盒测试卡号
"expiry" => "2028-12",
"cvv" => "123",
"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: ' . $clientId
]);
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);
?>
<!DOCTYPE html>
<html>
<head>
<title>支付结果</title>
<style>
.container { max-width: 800px; margin: 50px auto; padding: 20px; }
.success { color: #155724; background: #d4edda; padding: 15px; border-radius: 4px; }
.error { color: #721c24; background: #f8d7da; padding: 15px; border-radius: 4px; }
</style>
</head>
<body>
<div class="container">
<?php if(isset($error)): ?>
<div class="error">
<h3>交易失败</h3>
<p>错误信息:<?= htmlspecialchars($error) ?></p>
<?php if(isset($responseData)): ?>
<pre><?= json_encode($responseData, JSON_PRETTY_PRINT) ?></pre>
<?php endif; ?>
</div>
<?php elseif($httpCode >= 200 && $httpCode < 300): ?>
<div class="success">
<h3>支付成功!</h3>
<?php if(isset($responseData['id'])): ?>
<p>订单ID:<?= $responseData['id'] ?></p>
<?php endif; ?>
<?php
// 提取交易ID(Capture ID)
$captureId = $responseData['purchase_units'][0]['payments']['captures'][0]['id'] ?? null;
if($captureId): ?>
<p>交易ID:<?= $captureId ?></p>
<?php endif; ?>
<pre><?= json_encode($responseData, JSON_PRETTY_PRINT) ?></pre>
</div>
<?php else: ?>
<div class="error">
<h3>支付失败(HTTP <?= $httpCode ?>)</h3>
<?php if(isset($responseData['message'])): ?>
<p>错误信息:<?= $responseData['message'] ?></p>
<?php endif; ?>
<?php if(isset($responseData['details'])): ?>
<ul>
<?php foreach($responseData['details'] as $detail): ?>
<li><?= $detail['field'] ?? '' ?>: <?= $detail['description'] ?? '' ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<pre><?= json_encode($responseData, JSON_PRETTY_PRINT) ?></pre>
</div>
<?php endif; ?>
</div>
</body>
</html>