File: /www/备份的/ly.ydrbh.com/2.php
<?php
// $clientId = 'AZ5q35RRvAI9oU9ss-E4yRYVELSIaIcLpv_mmd4oPgv7WmvwJupJmzQaQXN2RTsEZgPNQhXmwzGx-D0z';
// $clientSecret = 'EOsd5viKcp8D9nd6JewX7tuqFLc6m-0wl4tmHNeHFu8Giwbw1Hf06rxLuDVRH1QJyJTtmg2VXH9-VEcO';
$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'];
// echo $accessToken;
// Step 2: 发起查询请求
$access_token = $accessToken;
// PayPal交易ID或交易码
$transaction_id = '2FP84762A6562791V'; // 替换成你的交易ID或交易码
// PayPal REST API endpoint
$api_url = 'https://api.sandbox.paypal.com/v1/payments/sale/' . $transaction_id;
// 构建请求
$options = array(
'http' => array(
'header' => "Authorization: Bearer " . $access_token . "\r\n" .
"Content-Type: application/json\r\n",
'method' => 'GET'
)
);
$context = stream_context_create($options);
$response = file_get_contents($api_url, false, $context);
if ($response === false) {
die('Failed to fetch data from PayPal API');
}
// 解析响应
$data = json_decode($response, true);
echo $response;
if ($data === null) {
die('Error parsing JSON');
}
// 提取支付价格
if (isset($data['amount']['total']) && isset($data['amount']['currency'])) {
$total_amount = $data['amount']['total'];
$currency = $data['amount']['currency'];
echo "支付金额: $total_amount $currency";
} else {
echo '未找到支付信息';
}