[Stripe] Checkout w/ user-specified amount

Example of Checkout custom integration where the amount is input by the user.

by ywain

HTML

<script src="https://checkout.stripe.com/checkout.js"></script>
<form id="myForm" action="//httpbin.org/post" method="POST">
  <input type="text" id="amountInDollars" />
  <input type="hidden" id="stripeToken" name="stripeToken" />
  <input type="hidden" id="stripeEmail" name="stripeEmail" />
  <input type="hidden" id="amountInCents" name="amountInCents" />
</form>

<input type="button" id="customButton" value="Pay">

JavaScript

var handler = StripeCheckout.configure({
  key: 'pk_test_6pRNASCoBOKtIshFeQd4XMUh',
  image: 'https://stripe.com/img/documentation/checkout/marketplace.png',
  token: function(token) {
    $("#stripeToken").val(token.id);
    $("#stripeEmail").val(token.email);
    $("#amountInCents").val(Math.floor($("#amountInDollars").val() * 100));
    $("#myForm").submit();
  }
});

$('#customButton').on('click', function(e) {
  var amountInCents = Math.floor($("#amountInDollars").val() * 100);
  var displayAmount = parseFloat(Math.floor($("#amountInDollars").val() * 100) / 100).toFixed(2);
  // Open Checkout with further options
  handler.open({
    name: 'Demo Site',
    description: 'Custom amount ($' + displayAmount + ')',
    amount: amountInCents,
  });
  e.preventDefault();
});

// Close Checkout on page navigation
$(window).on('popstate', function() {
  handler.close();
});