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 w
OR you can just use the following code before clicking the stripe button ;-)
StripeCheckout.__app.configurations.button0.amount = 1234;
$('#stripe-button').click();
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>
As @awwester said, you can use Stripe's Custom Checkouts. Although, I found an easier way to do this through jQuery simply by remounting the script with the changed variable each time they increase the quantity:
$("#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>'
);