File: /www/wwwroot//jf.cwoyt.com/2.php
<?php
$clientId = 'AV215uV-cE-AZ9nd4dNWf24vzrj6UyvA3rA8FDGVW09Y531j9H3GO7FtJmphimXNwisgYNHFvW-6wYMo';
$clientSecret = 'EJb5xUSW8QtuPJeaHuOjAXl9WXp2kj4-zkym-7fXCBM-TssNhkK2FB1-iJOmxYsGcASO8zBTHaPRoJH_';
// PayPal OAuth2 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, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Basic ' . $auth,
'Content-Type: application/x-www-form-urlencoded'
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
// 执行请求并获取响应
$response = curl_exec($ch);
// 关闭 cURL 连接
curl_close($ch);
// 解析 JSON 响应
$responseData = json_decode($response, true);
// 检查是否成功获取访问令牌
if (isset($responseData['access_token'])) {
$accessToken = $responseData['access_token'];
} else {
echo "Error: " . $responseData['error_description'];
exit;
}
$paymentData = [
'intent' => 'sale', // 交易类型
'payer' => [
'payment_method' => 'credit_card', // 使用信用卡
'funding_instruments' => [
[
'credit_card' => [
'type' => 'visa', // 卡片类型,支持 visa, mastercard 等
'number' => '4111111111111111', // 沙盒环境信用卡号
'expire_month' => 12, // 到期月份
'expire_year' => 2025, // 到期年份
'cvv2' => '123', // 卡片 CVV2
'first_name' => 'John',
'last_name' => 'Doe',
'billing_address' => [
'line1' => '123 Main St',
'city' => 'San Jose',
'state' => 'CA',
'postal_code' => '95131',
'country_code' => 'US'
]
]
]
]
],
'transactions' => [
[
'amount' => [
'total' => '100.00', // 支付金额
'currency' => 'USD'
],
'description' => 'Payment for product/service'
]
]
];
// PayPal 沙盒支付创建接口
$paymentUrl = "https://api.sandbox.paypal.com/v1/payments/payment";
// 初始化 cURL
$ch = curl_init();
// 设置 cURL 请求的选项
curl_setopt($ch, CURLOPT_URL, $paymentUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $accessToken,
'Content-Type: application/json'
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($paymentData));
// 执行请求并获取响应
$response = curl_exec($ch);
curl_close($ch);
// 解析响应数据
$responseData = json_decode($response, true);
// 处理支付结果
if (isset($responseData['state']) && $responseData['state'] == 'approved') {
echo "Payment Successful! Payment ID: " . $responseData['id'];
} else {
echo "Payment failed. Error: " . $responseData['message'];
}