javascript - 更新 Stripe 数据量

标签 javascript jquery stripe-payments

我正在将 Stripe 实现到一个 Django 网站中,除一部分外,一切正常。在我的购物车中,用户可以更新更改总数的项目。除了在 Stripe Checkout 上设置数据量外,一切正常js脚本。

当页面加载时,一切正常,但是如果客户更改他们的购物车,数据量不会更新。我有另一个显示总数的框,并且该金额更新正常。

<!-- here is the script tag in HTML-->
<script
id="stripe-script"
src="https://checkout.stripe.com/checkout.js" 
class="stripe-button"
data-image="{% static 'img/marketplace.png' %}"
data-key="{{ STRIPE_PUBLIC_KEY }}"
data-name="Serendipity Artisan Blends"
data-description="Purchase Items"
data-amount="{{ cart_stripe_total }}">
</script>

然后我尝试更新的 javascript 是这样的:

function updateTotal(amount) {
    /* update the total in the cart in both the table cell and
        in the stripe button data-amount */
    var totalStr = shoppingTotalCell.text().replace('$', ''),
        originalTotal = parseFloat(totalStr),
        newTotal = originalTotal + amount,
        newTotalStripe = newTotal * 100,
        newTotalStr = newTotal.toFixed(2),
        script = $('#stripe-script');

    shoppingTotalCell.text('$' + newTotalStr);

    console.log(script.data("amount"));
    // this returns the correct original amount

    script.data("amount", newTotalStripe);

    console.log(script.data("amount"));
    /* this returns the updated amount, however the HTML data-amount 
        attribute does not update. */
  }

最佳答案

事实证明,要为 strip 支付提供动态数据量,您必须使用 Custom Checkout而不是简单结帐。这段代码成功了。

      <button class="btn btn-primary btn-lg" id="stripe-button">
        Checkout <span class="glyphicon glyphicon-shopping-cart"></span>
      </button>

      <script>
        $('#stripe-button').click(function(){
          var token = function(res){
            var $id = $('<input type=hidden name=stripeToken />').val(res.id);
            var $email = $('<input type=hidden name=stripeEmail />').val(res.email);
            $('form').append($id).append($email).submit();
          };

          var amount = $("#stripeAmount").val();
          StripeCheckout.open({
            key:         '{{ STRIPE_PUBLIC_KEY }}',
            amount:      amount,
            name:        'Serendipity Artisan Blends',
            image:       '{% static "img/marketplace.png" %}',
            description: 'Purchase Products',
            panelLabel:  'Checkout',
            token:       token
          });

          return false;
        });
      </script>

关于javascript - 更新 Stripe 数据量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28016103/

相关文章:

javascript - 一旦达到另一个元素的像素宽度,如何防止输入到文本区域?

javascript - Fancybox不显示-转到youtube

ruby-on-rails - 如果 API 调用失败,我该如何回滚事务,反之亦然?

javascript - 未设置对象属性,但控制台另有说明

ios - 使用 Stripe 的 Swift 应用程序 - 类型 'STPCustomer' 没有成员 'decodedObject'

javascript - 使用 jQuery 创建表单输入计算器

javascript - 一起使用 Cufon 和 @font-face

javascript - div中的随机图像背景

javascript - jquery 附加的两个输入未在同一行上对齐

asp.net - $('#<%= txtFirstName.ClientID%>')。 $和#在这段代码中做了什么?