更新 Stripe 数据量

Update Stripe data-amount

我正在将 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. */
  }

事实证明,要为条带支付提供动态数据量,您必须使用 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>

正如@awwester 所说,您可以使用 Stripe 的自定义结帐。虽然,我通过 jQuery 找到了一种更简单的方法,只需在每次增加数量时使用更改后的变量重新安装脚本即可:

$("#stripe-form").html(
  '<input type="hidden" name="amount" value="' +
    totalCost.replace(".", "") +
    '" /><input type="hidden" name="currency" value="usd" /><script src="https://checkout.stripe.com/checkout.js" class="stripe-button" data-key="pk_test_123123123123" data-amount="' +
    totalCost.replace(".", "") +
    '" data-zip-code="true" data-currency="usd" data-billing-address="true" data-shipping-address="true" data-name="Company Name" data-description="Product Name" data-image="https://image" data-locale="auto"></script>'
);

或者您可以在单击条纹按钮之前使用以下代码 ;-)

StripeCheckout.__app.configurations.button0.amount = 1234;

$('#stripe-button').click();