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

请求协助检查PayPal Smart Button代码(含NO_SHIPPING配置)

PayPal智能按钮代码检查与修正建议

问题描述

我添加了NO_SHIPPING配置,但不确定各类括号((){}[])的使用是否正确,以及应使用双引号还是单引号。我熟悉HTML,但对这类脚本代码不太理解,恳请帮忙检查。
——Steve

原代码

<div id="smart-button-container">
  <div style="text-align: center;">
    <div id="paypal-button-container"></div>
  </div>
</div>
<script src="https://www.paypal.com/sdk/js?client-id=sb&enablefunding=venmo&currency=GBP" data-sdk-integration-source="button-factory"></script>
<script>
  function initPayPalButton() {
    paypal.Buttons({
      style: {
        shape: 'pill',
        color: 'gold',
        layout: 'vertical',
        label: 'buynow',
        
    },

      createOrder: function(data, actions) {
        return actions.order.create({
            purchase_units: [{"description":"item for sale\nacceptM/accept43_BB1frT6.htm","amount":{"currency_code":"GBP","value":20}}],
        
      application_context: {
         shipping_preference: 'NO_SHIPPING'
      }

      });
      },

      onApprove: function(data, actions) {
        return actions.order.capture().then(function(orderData) {
          
          // Full available details
          console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));

          // Show a success message within this page, e.g.
          const element = document.getElementById('paypal-button-container');
          element.innerHTML = '';
          element.innerHTML = '<h3>Thank you for your payment!</h3>';

         //actions.redirect('https://www.website.com/');
          
        });
      },

      onError: function(err) {
        console.log(err);
      }
    }).render('#paypal-button-container');
  }
  initPayPalButton();
</script>

代码修正说明

  • 括号与对象结构修正:原代码中createOrder里的application_context是actions.order.create的参数对象属性,需和purchase_units保持同级,原格式的换行缩进易导致结构混淆,已调整层级确保清晰。
  • 引号统一规范:JavaScript中单、双引号功能等价,建议保持风格统一。这里将代码中混合的单、双引号统一为单引号,避免风格混乱。
  • 多余逗号清理:style对象最后一个属性后的尾逗号可能在旧版JS环境引发错误,已删除。

修正后完整代码

<div id="smart-button-container">
  <div style="text-align: center;">
    <div id="paypal-button-container"></div>
  </div>
</div>
<script src="https://www.paypal.com/sdk/js?client-id=sb&enablefunding=venmo&currency=GBP" data-sdk-integration-source="button-factory"></script>
<script>
  function initPayPalButton() {
    paypal.Buttons({
      style: {
        shape: 'pill',
        color: 'gold',
        layout: 'vertical',
        label: 'buynow'
      },

      createOrder: function(data, actions) {
        return actions.order.create({
          purchase_units: [{
            description: 'item for sale\nacceptM/accept43_BB1frT6.htm',
            amount: {
              currency_code: 'GBP',
              value: 20
            }
          }],
          application_context: {
            shipping_preference: 'NO_SHIPPING'
          }
        });
      },

      onApprove: function(data, actions) {
        return actions.order.capture().then(function(orderData) {
          // Full available details
          console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));

          // Show a success message within this page, e.g.
          const element = document.getElementById('paypal-button-container');
          element.innerHTML = '';
          element.innerHTML = '<h3>Thank you for your payment!</h3>';

          //actions.redirect('https://www.website.com/');
        });
      },

      onError: function(err) {
        console.log(err);
      }
    }).render('#paypal-button-container');
  }
  initPayPalButton();
</script>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 00:45:35