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>
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. */
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…