Django-paypal集成问题:PayPalPaymentsForm不显示“使用信用卡或借记卡支付”选项
Django-paypal集成问题:PayPalPaymentsForm不显示“使用信用卡或借记卡支付”选项
我之前也碰到过类似的问题,给你几个可以尝试的解决方向,一步步排查:
1. 确认PayPal沙箱商家账户的设置是否完全生效
你已经开启了「Website Payment Preferences -> PayPal Account Optional」,但还要检查几个细节:
- 确保同一设置页面里的「Allow guests to checkout without a PayPal account」也设为开启状态(部分地区这个选项和前者关联,但最好手动确认);
- 你的沙箱商家账户(就是
business参数里填写的邮箱)需要完成沙箱内的商家认证流程,比如绑定虚拟的银行账户或信用卡,否则部分支付选项可能会被限制; - 沙箱设置有时候会有延迟,修改后建议等待5-10分钟,或者清空浏览器缓存再测试。
2. 给paypal_dict添加关键参数,强制显示账单页面
在你的paypal配置字典里加入landing_page: 'billing'参数,这个参数会让PayPal跳转页面直接展示信用卡/借记卡支付选项,而不是默认的PayPal账户登录页面。修改后的代码如下:
def process_payment(request): price = request.GET.get("price") or 0 paypal_dict = { 'business': settings.PAYPAL_RECEIVER_EMAIL, 'amount': str(price), 'item_name': 'refill amount', 'item_number': str(request.user.id), 'invoice': get_timestamp_uid(request.user.id), 'currency_code': 'USD', 'notify_url': f'{BASE_URL}{reverse("paypal-ipn")}', 'return_url': f'{BASE_URL}{reverse("paypal_payment:success_payment")}', 'cancel_return': f'{BASE_URL}{reverse("accounts:user-profile")}', 'landing_page': 'billing', # 新增这个参数 } form = PayPalPaymentsForm(initial=paypal_dict) context = { 'form': form } return render(request, 'paypal_payment/process_payment.html', context)
3. 考虑切换到PayPal官方推荐的Smart Payment Buttons(JS SDK)
django-paypal的PayPalPaymentsForm基于旧版的PayPal标准支付,而现在PayPal更推荐使用JS SDK的智能按钮,原生支持信用卡支付选项,兼容性也更好。你可以尝试替换原来的表单渲染方式:
在你的模板process_payment.html里,替换掉{{ form.render }},改成以下代码(记得替换成你的沙箱Client ID):
<script src="https://www.paypal.com/sdk/js?client-id=你的沙箱Client ID¤cy=USD"></script> <div id="paypal-button-container"></div> <script> paypal.Buttons({ // 创建订单 createOrder: function(data, actions) { return actions.order.create({ purchase_units: [{ amount: { value: "{{ price }}" } }] }); }, // 订单确认后处理 onApprove: function(data, actions) { return actions.order.capture().then(function(details) { // 这里可以跳转到支付成功页面,或者发送请求到后端验证订单 window.location.href = "{% url 'paypal_payment:success_payment' %}"; }); }, // 取消支付时的处理 onCancel: function(data) { window.location.href = "{% url 'accounts:user-profile' %}"; } }).render('#paypal-button-container'); </script>
这种方式不需要依赖django-paypal的表单,直接调用PayPal的官方SDK,能确保显示信用卡支付选项。
备注:内容来源于stack exchange,提问作者Ivan Lysenko
相关产品推荐
相关产品推荐

