paypal - 我是否需要在选择更改时重新呈现 PayPal 的智能按钮?

标签 paypal paypal-rest-sdk

我的页面上有一个选择元素。此选择元素选择产品的不同变体。所有这些产品的价格都相同,但我想在发票上注明选择了哪种产品。我正在使用 PayPal Smart Button 在我的页面上集成结账功能。假设我有以下内容:

<select id="product-select">
    <option value="product-A" selected> Product A </option>
    <option value="product-B"> Product B </option>
    <option value="product-C"> Product C </option>
</select>

<div id="paypal-button-container"> </div>

<script>
  paypal.Buttons({
    createOrder: function(data, actions) {
      return actions.order.create({
        purchase_units: [{
          amount: {
            value: '50'
          },
          items: [{
            unit_amount: {
                value: '50'
            },
            quantity: '1',
            name: 'Product A',
          }],
        }],
      });
    }
  }).render('#paypal-button-container');
</script>

当用户将所选选项更改为产品 B 或产品 C 时,我是否需要在选择更改时重新呈现 PayPal 按钮?如果是这样,我该如何处理旧的?

最佳答案

仅当 currencyintent 或加载 SDK 的类似查询字符串参数发生变化时,才需要重新渲染。这就是为什么它们是 SDK 系列的一部分,因为这些东西需要在渲染时知道。

其他的东西,比如订单的金额和内容,可以在点击按钮时计算出来。那就是调用 createOrder 函数的时候。因此,您可以使用 document.getElementById().value 或类似方法来填充您在创建订单时要使用的数据。

……

如果您要重新渲染(此处不需要),您可以这样做:

不是立即调用 .render(),而是先保存对对象的引用...

var myButton = paypal.Buttons({...});
myButton.render('#paypal-button-container');

// things happen, user makes changes that require re-rendering the buttons
myBytton.close();

// render the buttons again here
// if query string parameters to the SDK change, the SDK needs to be reloaded

关于paypal - 我是否需要在选择更改时重新呈现 PayPal 的智能按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59386457/

相关文章:

php - 通过 PHP-SDK 托管的唯一解决方案

php - Paypal返回值问题

testing - 使用沙盒 PayPal 测试多产品购买

php - Paypal 与 DIY 购物车的整合

paypal通过单笔付款将钱汇到paypal中未注册的电子邮件

paypal - 将 Paypal PHP SDK 与 Cakephp 3.x 集成

php - Paypal IPN curl 问题

c# - 如何在 asp.net 中从 paypal(Sandbox) 获取返回 url 中的参数

PayPal Rest API 错误总和不相加但确实如此

php - PayPal REST API 订单摘要自定义说明