更新条带数据量 [英] Update Stripe data-amount
问题描述
我正在将Stripe实施到django网站中,除了一部分之外,其他所有功能都在工作.在我的购物车中,用户可以更新更改总数的项目.除了在 Stripe Checkout js脚本上设置数据量以外,其他所有操作均正常运行.
I'm implementing Stripe into a django website and everything is working except for one part. In my cart, users can update the items which changes the total. Everything is working correctly except for setting the data-amount on the Stripe Checkout js script.
页面加载时,一切正常,但是,如果客户更换购物车,则数据量不会更新.我还有另一个框显示总数,该数量可以更新.
When the page loads, everything works great, however if the customer changes their cart, the data-amount does not update. I have another box which shows the total, and that amount updates fine.
<!-- 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是这样的:
And then my javascript that attempts to update is this:
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. */
}
推荐答案
Turns out that to have a dynamic data-amount for the stripe payment, you have to use Custom Checkout instead of Simple Checkout. This code did the trick.
<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>
这篇关于更新条带数据量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!