HEX
Server: nginx/1.28.1
System: Linux 10-41-63-61 6.8.0-31-generic #31-Ubuntu SMP PREEMPT_DYNAMIC Sat Apr 20 00:40:06 UTC 2024 x86_64
User: www (1001)
PHP: 7.4.33
Disabled: passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv
Upload Files
File: /www/wwwroot/www.ydrbh.com/2.php
<?php
// 设置沙盒环境 PayPal 的 API URL 和凭证
$client_id = 'AZ5q35RRvAI9oU9ss-E4yRYVELSIaIcLpv_mmd4oPgv7WmvwJupJmzQaQXN2RTsEZgPNQhXmwzGx-D0z';   // 替换为你的Client ID
$secret = 'EOsd5viKcp8D9nd6JewX7tuqFLc6m-0wl4tmHNeHFu8Giwbw1Hf06rxLuDVRH1QJyJTtmg2VXH9-VEcO'; // 替换为你的Client Secret
$paypal_api_url = 'https://api.sandbox.paypal.com'; // 沙盒环境 URL

// 1. 获取 Access Token
function getAccessToken($client_id, $secret) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "$GLOBALS[paypal_api_url]/v1/oauth2/token");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERPWD, "$client_id:$secret");  // Basic auth
    curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");

    $response = curl_exec($ch);
    curl_close($ch);

    $response_data = json_decode($response, true);
    return isset($response_data['access_token']) ? $response_data['access_token'] : null;
}

// 2. 创建支付请求
function createPayment($access_token) {
    $payment_url = "$GLOBALS[paypal_api_url]/v1/payments/payment";

    $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',
                        'billing_address' => [
                            'line1' => '1 Main St',
                            'city' => 'San Jose',
                            'state' => 'CA',
                            'postal_code' => '95131',
                            'country_code' => 'US'
                        ]
                    ]
                ]
            ]
        ],
        'transactions' => [
            [
                'amount' => [
                    'total' => '100.00',  // 支付金额
                    'currency' => 'USD'
                ],
                'description' => 'Payment for goods and services'
            ]
        ],
        'redirect_urls' => [
            'return_url' => 'https://www.yoursite.com/success',  // 支付成功时的返回 URL
            'cancel_url' => 'https://www.yoursite.com/cancel'   // 支付取消时的返回 URL
        ]
    ];

    $json_data = json_encode($data);

    // 发起支付请求
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $payment_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        "Content-Type: application/json",
        "Authorization: Bearer $access_token"
    ]);

    $response = curl_exec($ch);
    curl_close($ch);

    $response_data = json_decode($response, true);
    return $response_data;
}

// 3. 处理支付成功后的回调
function getPaymentDetails($access_token, $payment_id) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "$GLOBALS[paypal_api_url]/v1/payments/payment/$payment_id");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        "Content-Type: application/json",
        "Authorization: Bearer $access_token"
    ]);

    $response = curl_exec($ch);
    curl_close($ch);

    $response_data = json_decode($response, true);
    return $response_data;
}

// 如果是支付成功回调页面
if (isset($_GET['paymentId']) && isset($_GET['PayerID'])) {
    $payment_id = $_GET['paymentId'];
    $payer_id = $_GET['PayerID'];

    // 获取 PayPal API 访问令牌
    $access_token = getAccessToken($client_id, $secret);

    // 获取支付详情
    $payment_details = getPaymentDetails($access_token, $payment_id);
    echo '<pre>';
    print_r($payment_details);
    echo '</pre>';
} elseif ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // 获取 PayPal API 访问令牌
    $access_token = getAccessToken($client_id, $secret);

    if ($access_token) {
        // 创建支付
        $payment_response = createPayment($access_token);

        if (isset($payment_response['links'])) {
            // 查找 PayPal 返回的重定向链接
            foreach ($payment_response['links'] as $link) {
                if ($link['rel'] == 'approval_url') {
                    $approval_url = $link['href'];
                    // 重定向到 PayPal 支付页面
                    header("Location: $approval_url");
                    exit;
                }
            }
        } else {
            echo '支付创建失败';
            echo '<pre>';
            print_r($payment_response);
            echo '</pre>';
        }
    } else {
        echo '无法获取 Access Token';
    }
} else {
    // 显示支付按钮
    echo '<form method="POST">';
    echo '<button type="submit">Pay with Credit Card</button>';
    echo '</form>';
}
?>