File: //www/备份的/ly.ydrbh.com/3.php
<?php
// PayPal API凭证
// 获取 PayPal Access Token
function getPaypalAccessToken($clientId, $secret) {
$url = "https://api.sandbox.paypal.com/v1/oauth2/token";
// 初始化 cURL
$ch = curl_init();
// 设置请求头和请求数据
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, $clientId . ":" . $secret);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
// 执行请求
$response = curl_exec($ch);
curl_close($ch);
// 解析响应
$response = json_decode($response, true);
return $response['access_token'] ?? null;
}
// 创建支付
function createPaypalPayment($accessToken, $orderId, $cardDetails) {
$url = "https://api.sandbox.paypal.com/v1/payments/payment";
// 设置请求头
$headers = [
"Authorization: Bearer $accessToken",
"Content-Type: application/json"
];
// 创建支付请求数据
$data = [
"intent" => "sale",
"payer" => [
"payment_method" => "credit_card",
"funding_instruments" => [
[
"credit_card" => [
"type" => "visa",
"number" => "4111111111111111",
"expire_month" => "12",
"expire_year" => "2025",
"cvv2" => "123",
"first_name" => "John",
"last_name" => "Doe"
]
]
]
],
"transactions" => [
[
"amount" => [
"total" => $cardDetails['amount'], // 支付金额
"currency" => "USD" // 支付货币
],
"description" => "Order #$orderId"
]
]
];
// 初始化 cURL
$ch = curl_init();
// 设置请求
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
// 执行请求
$response = curl_exec($ch);
curl_close($ch);
// 解析响应
$response = json_decode($response, true);
return $response;
}
// 设置 PayPal 沙盒的客户端ID和Secret
$clientId = 'AV215uV-cE-AZ9nd4dNWf24vzrj6UyvA3rA8FDGVW09Y531j9H3GO7FtJmphimXNwisgYNHFvW-6wYMo';
$secret = 'EJb5xUSW8QtuPJeaHuOjAXl9WXp2kj4-zkym-7fXCBM-TssNhkK2FB1-iJOmxYsGcASO8zBTHaPRoJH_';
// 获取 PayPal 访问令牌
$accessToken = getPaypalAccessToken($clientId, $secret);
// 假设的订单信息和信用卡信息
$orderId = "123456789"; // 假设订单ID
$cardDetails = [
'type' => 'visa', // 信用卡类型 (visa, mastercard, etc.)
'number' => '4111111111111111', // 信用卡号
'expire_month' => '12', // 到期月份
'expire_year' => '2025', // 到期年份
'cvv' => '123', // CVV
'first_name' => 'John', // 名字
'last_name' => 'Doe', // 姓氏
'amount' => '10.00' // 支付金额 (USD)
];
// 调用 PayPal API 创建支付
$paymentResponse = createPaypalPayment($accessToken, $orderId, $cardDetails);
// 处理响应
if (isset($paymentResponse['state']) && $paymentResponse['state'] == 'approved') {
// 成功支付,输出交易ID
$transactionId = $paymentResponse['transactions'][0]['related_resources'][0]['sale']['id'];
echo "支付成功,交易ID:$transactionId";
} else {
// 失败,输出错误信息
echo "支付失败,原因:";
echo isset($paymentResponse['message']) ? $paymentResponse['message'] : "未知错误";
}
?>