跨境系统设计

PHP独立站集成PayPal 和信用卡支付

1. 项目结构规划
首先,我们需要规划项目的基本结构,以下是一个简单的示例:

project/
├── index.php           # 商品展示页面
├── checkout.php        # 结算页面
├── paypal_ipn.php      # PayPal 即时付款通知处理页面
├── credit_card_pay.php # 信用卡支付处理页面

2. 商品展示页面(index.php)
这个页面用于展示商品信息,用户可以选择商品并添加到购物车。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>跨境电商独立站 - 商品列表</title>
</head>
<body>
    <h1>商品列表</h1>
    <ul>
        <li>
            商品名称:T恤
            价格:$20
            <a href="checkout.php?product=T恤&price=20">结算</a>
        </li>
    </ul>
</body>
</html>

3. 结算页面(checkout.php)
该页面允许用户选择支付方式,如 PayPal 或信用卡支付。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>跨境电商独立站 - 结算</title>
</head>
<body>
    <h1>结算</h1>
    <?php
    $product = $_GET['product'];
    $price = $_GET['price'];
    echo "<p>商品名称:$product</p>";
    echo "<p>价格:$$price</p>";
    ?>
    <h2>选择支付方式</h2>
    <form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
        <input type="hidden" name="cmd" value="_xclick">
        <input type="hidden" name="business" value="your_paypal_email@example.com">
        <input type="hidden" name="item_name" value="<?php echo $product; ?>">
        <input type="hidden" name="amount" value="<?php echo $price; ?>">
        <input type="hidden" name="currency_code" value="USD">
        <input type="hidden" name="return" value="https://yourwebsite.com/success.php">
        <input type="hidden" name="cancel_return" value="https://yourwebsite.com/cancel.php">
        <input type="hidden" name="notify_url" value="https://yourwebsite.com/paypal_ipn.php">
        <input type="submit" value="使用 PayPal 支付">
    </form>
    <form action="credit_card_pay.php" method="post">
        <input type="hidden" name="product" value="<?php echo $product; ?>">
        <input type="hidden" name="price" value="<?php echo $price; ?>">
        <input type="submit" value="使用信用卡支付">
    </form>
</body>
</html>

注意事项
请将 your_paypal_email@example.com 替换为你的 PayPal 商家邮箱。
https://yourwebsite.com 需要替换为你的实际网站域名。
4. PayPal 即时付款通知处理页面(paypal_ipn.php)
这个页面用于处理 PayPal 的即时付款通知(IPN),验证付款是否成功。

<?php
// 接收 PayPal IPN 数据
$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
$myPost = array();
foreach ($raw_post_array as $keyval) {
    $keyval = explode ('=', $keyval);
    if (count($keyval) == 2) {
        $myPost[$keyval[0]] = urldecode($keyval[1]);
    }
}
// 构建验证请求
$req = 'cmd=_notify-validate';
if(function_exists('get_magic_quotes_gpc')) {
    $get_magic_quotes_exists = true;
}
foreach ($myPost as $key => $value) {
    if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) {
        $value = urlencode(stripslashes($value));
    } else {
        $value = urlencode($value);
    }
    $req .= "&$key=$value";
}
// 向 PayPal 发送验证请求
$ch = curl_init('https://ipnpb.sandbox.paypal.com/cgi-bin/webscr');
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
$res = curl_exec($ch);
curl_close($ch);
// 验证响应
if (strcmp ($res, "VERIFIED") == 0) {
    // 处理付款成功逻辑
    $item_name = $_POST['item_name'];
    $payment_status = $_POST['payment_status'];
    $payment_amount = $_POST['mc_gross'];
    $payment_currency = $_POST['mc_currency'];
    $txn_id = $_POST['txn_id'];
    // 这里可以将付款信息记录到数据库等操作
    file_put_contents('paypal_ipn.log', "Payment verified: $item_name, $payment_status, $payment_amount, $payment_currency, $txn_id\n", FILE_APPEND);
} elseif (strcmp ($res, "INVALID") == 0) {
    // 处理无效付款逻辑
    file_put_contents('paypal_ipn.log', "Payment invalid\n", FILE_APPEND);
}
?>

5. 信用卡支付处理页面(credit_card_pay.php)
这里我们使用 Stripe 作为示例来处理信用卡支付,因为它是一个流行的支付网关,支持多种信用卡。

<?php
require_once('vendor/autoload.php');
\Stripe\Stripe::setApiKey('your_stripe_secret_key');
$product = $_POST['product'];
$price = $_POST['price'];
try {
    $intent = \Stripe\PaymentIntent::create([
        'amount' => $price * 100, // 金额需要以美分计算
        'currency' => 'usd',
        'description' => $product,
    ]);
    echo json_encode([
        'clientSecret' => $intent->client_secret
    ]);
} catch (\Stripe\Exception\ApiErrorException $e) {
    http_response_code(500);
    echo json_encode(['error' => $e->getMessage()]);
}
?>

注意事项
你需要先安装 Stripe PHP 库,可以使用 Composer 进行安装:composer require stripe/stripe-php。
将 your_stripe_secret_key 替换为你的 Stripe 秘密密钥。
前端集成 Stripe
在结算页面(checkout.php)中添加以下 JavaScript 代码来处理 Stripe 支付:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>跨境电商独立站 - 结算</title>
    <script src="https://js.stripe.com/v3/"></script>
</head>
<body>
    <!-- 前面的结算页面内容 -->
    <form id="payment-form">
        <div id="card-element"></div>
        <button id="submit">使用信用卡支付</button>
    </form>
    <script>
        const stripe = Stripe('your_stripe_public_key');
        const elements = stripe.elements();
        const cardElement = elements.create('card');
        cardElement.mount('#card-element');
        const form = document.getElementById('payment-form');
        form.addEventListener('submit', async (event) => {
            event.preventDefault();
            const {paymentIntent, error} = await stripe.confirmCardPayment(
                '<?php echo $clientSecret; ?>',
                {
                    payment_method: {
                        card: cardElement
                    }
                }
            );
            if (error) {
                console.error(error);
            } else if (paymentIntent.status === 'succeeded') {
                console.log('Payment succeeded');
            }
        });
    </script>
</body>
</html>

注意事项
将 your_stripe_public_key 替换为你的 Stripe 公开密钥。
总结
通过以上步骤,你可以创建一个简单的跨境电商独立站,并集成 PayPal 和信用卡支付功能。需要注意的是,实际应用中还需要处理更多的错误情况、安全性问题和用户体验优化。