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

Django集成PayPal按钮:添加订单描述后加载失败求助

问题解决:PayPal按钮加载失败的原因及修复

你的问题核心出在createOrder函数里的description字段格式错误——PayPal REST API要求purchase_units下的description是字符串类型,但你写成了对象格式,同时order_id变量也没从Django模板正确注入到JS中。

修复步骤:

1. 修正description字段格式

先在JS开头从Django模板注入order_id变量,再把description改成字符串格式:

<script>
    var total = '{{ order.get_cart_total }}'
    var orderId = '{{ order.id }}'  <!-- 新增:从模板获取订单ID -->
    // 渲染PayPal按钮到指定容器
    paypal.Buttons({

        style: {
            color:  'blue',
            shape:  'rect',
        },

        // 创建订单
        createOrder: function(data, actions) {
            return actions.order.create({
                purchase_units: [{
                    amount: {
                        value: parseFloat(total).toFixed(2)
                    },
                    description: "订单ID: " + orderId,  <!-- 改为字符串格式 -->
                }]
            });
        },

        // 完成交易
        onApprove: function(data, actions) {
            return actions.order.capture().then(function(details) {
                console.log(JSON.stringify(details));
                submitFormData()
            });
        }

    }).render('#paypal-button-container');
</script>

2. 更优方案:用custom_id传递内部订单标识

如果只是想在PayPal商家后台关联内部订单ID,更推荐使用custom_id字段(该字段仅商家后台可见,不会展示给买家),description留作给买家看的订单说明:

purchase_units: [{
    amount: {
        value: parseFloat(total).toFixed(2)
    },
    description: "您的商品订单",  <!-- 买家可见的描述 -->
    custom_id: orderId  <!-- 商家后台可见的内部订单ID -->
}]

3. 关于settings.py的配置

你当前的Cookie配置是正确的,已经解决了HTTPS环境下的跨域Cookie问题,无需修改:

SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SAMESITE = 'None'
CSRF_COOKIE_SAMESITE = 'None'

内容的提问来源于stack exchange,提问作者David Henson

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 22:27:41