You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将PayPal按钮返回URL配置到WordPress并展示支付信息

配置PayPal按钮Return URL到WordPress并展示支付信息

我来一步步帮你搞定这个需求,分两个核心部分:修改PayPal按钮添加返回地址,以及在WordPress里处理返回页面展示支付详情。

1. 给你的PayPal按钮添加Return URL

首先,你需要在现有的PayPal表单里添加一个隐藏字段,指定支付完成后跳转回的WordPress页面地址。修改后的按钮代码大概是这样:

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
    <!-- 商家账户 -->
    <input type="hidden" name="business" value="myPayPalAccount@gmail.com">
    <!-- 按钮类型 -->
    <input type="hidden" name="cmd" value="_xclick">
    <!-- 商品信息 -->
    <input type="hidden" name="item_name" value="item-to-buy">
    <!-- 添加Return URL:替换成你WordPress里新建的返回页面的永久链接 -->
    <input type="hidden" name="return" value="https://your-wordpress-site.com/payment-complete/">
    <!-- 可选:添加cancel_return指定取消支付后的跳转地址 -->
    <input type="hidden" name="cancel_return" value="https://your-wordpress-site.com/payment-canceled/">
    <!-- 显示PayPal按钮 -->
    <input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynow_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
    <img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>

注意:先在WordPress后台新建一个页面(比如命名为“支付完成”),复制它的永久链接(permalink)替换上面的return字段值。

另外,必须在PayPal商家后台开启自动返回:

  • 登录你的PayPal商家账户,进入「设置」->「网站支付」->「自动返回」
  • 开启自动返回功能,把默认返回地址设为你刚才的WordPress页面地址(可选,但建议设置)
  • 保存设置

2. 在WordPress返回页面展示支付信息

当用户支付完成后,PayPal会把支付相关的参数通过GET请求传递到你的返回页面,你可以在页面里编写PHP代码获取这些参数并展示。

方法1:使用页面编辑器的自定义PHP代码块(推荐,如果你有支持PHP的编辑器插件)

在你新建的“支付完成”页面里,添加一个自定义PHP代码块,写入以下代码:

<?php
// 检查是否有PayPal返回的参数
if (isset($_GET['item_name']) && isset($_GET['payment_status'])) {
    $itemName = sanitize_text_field($_GET['item_name']);
    $paymentStatus = sanitize_text_field($_GET['payment_status']);
    $transactionId = isset($_GET['tx']) ? sanitize_text_field($_GET['tx']) : '未获取到';
    $amount = isset($_GET['amt']) ? sanitize_text_field($_GET['amt']) : '未获取到';
?>
    <div style="padding: 20px; background: #f8f9fa; border-radius: 8px;">
        <h3>支付成功!</h3>
        <p><strong>商品名称:</strong><?php echo $itemName; ?></p>
        <p><strong>支付状态:</strong><?php echo ucfirst($paymentStatus); ?></p>
        <p><strong>交易ID:</strong><?php echo $transactionId; ?></p>
        <p><strong>支付金额:</strong><?php echo $amount; ?> USD</p>
        <p>感谢你的购买!</p>
    </div>
<?php
} else {
    // 如果用户直接访问这个页面,显示提示
?>
    <div style="padding: 20px; background: #f8f9fa; border-radius: 8px;">
        <h3>无效的访问</h3>
        <p>请通过正常的支付流程访问此页面。</p>
    </div>
<?php
}
?>

方法2:使用自定义页面模板(适合懂一点WordPress主题开发的用户)

  1. 在你的WordPress主题文件夹里新建一个文件,命名为page-payment-complete.php
  2. 写入以下代码(替换主题的header和footer部分为你主题的对应模板标签):
<?php
/*
Template Name: 支付完成页面
*/
get_header(); ?>

<div id="primary" class="content-area">
    <main id="main" class="site-main">

        <?php
        // 检查PayPal返回参数
        if (isset($_GET['item_name']) && isset($_GET['payment_status'])) {
            $itemName = sanitize_text_field($_GET['item_name']);
            $paymentStatus = sanitize_text_field($_GET['payment_status']);
            $transactionId = isset($_GET['tx']) ? sanitize_text_field($_GET['tx']) : '未获取到';
            $amount = isset($_GET['amt']) ? sanitize_text_field($_GET['amt']) : '未获取到';
        ?>
            <article class="page type-page status-publish hentry">
                <div class="entry-content">
                    <div style="padding: 20px; background: #f8f9fa; border-radius: 8px;">
                        <h3>支付成功!</h3>
                        <p><strong>商品名称:</strong><?php echo $itemName; ?></p>
                        <p><strong>支付状态:</strong><?php echo ucfirst($paymentStatus); ?></p>
                        <p><strong>交易ID:</strong><?php echo $transactionId; ?></p>
                        <p><strong>支付金额:</strong><?php echo $amount; ?> USD</p>
                        <p>感谢你的购买!</p>
                    </div>
                </div>
            </article>
        <?php
        } else {
        ?>
            <article class="page type-page status-publish hentry">
                <div class="entry-content">
                    <div style="padding: 20px; background: #f8f9fa; border-radius: 8px;">
                        <h3>无效的访问</h3>
                        <p>请通过正常的支付流程访问此页面。</p>
                    </div>
                </div>
            </article>
        <?php
        }
        ?>

    </main><!-- #main -->
</div><!-- #primary -->

<?php get_footer(); ?>

然后在WordPress后台编辑“支付完成”页面,在右侧「页面属性」里选择“支付完成页面”模板,更新页面即可。

重要提示

  • 安全验证:上面的代码只是基础展示,生产环境建议使用PayPal的IPN(即时付款通知)来验证支付的真实性,避免恶意伪造参数。你可以在PayPal后台开启IPN,并编写对应的PHP脚本处理验证。
  • 参数过滤:代码里用了sanitize_text_field()来过滤用户输入(PayPal传递的参数),避免XSS攻击,这一步很重要。

内容的提问来源于stack exchange,提问作者Escudo Bravo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:36:16