Create a gist now

Instantly share code, notes, and snippets.

Stripe Tutorial Payment Form
<!DOCTYPE html>
<html lang="en">
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>Stripe Getting Started Form</title>
<script type="text/javascript" src=""></script>
<!-- jQuery is used only for this example; it isn't required to use Stripe -->
<script type="text/javascript" src=""></script>
<script type="text/javascript">
//this identifies your website in the createToken call below
function stripeResponseHandler(status, response) {
if (response.error) {
// re-enable the submit button
//show the errors on the form
} else {
var form$ = $("#payment-form");
// token contains id, last4, and card type
var token = response['id'];
// insert the token into the form so it gets submitted to the server
form$.append("<input type='hidden' name='stripeToken' value='" + token + "' />");
// and submit
$(document).ready(function() {
$("#payment-form").submit(function(event) {
// disable the submit button to prevent repeated clicks
$('.submit-button').attr("disabled", "disabled");
var chargeAmount = 1000; //amount you want to charge, in cents. 1000 = $10.00, 2000 = $20.00 ...
//createToken returns immediately - the supplied callback submits the form if there are no errors
number: $('.card-number').val(),
cvc: $('.card-cvc').val(),
exp_month: $('.card-expiry-month').val(),
exp_year: $('.card-expiry-year').val()
}, chargeAmount, stripeResponseHandler);
return false; //submit from callback
<h1>Charge $10 with Stripe</h1>
<!-- to display errors returned by createToken -->
<span class="payment-errors"></span>
<form action="" method="POST" id="payment-form">
<div class="form-row">
<label>Card Number</label>
<input type="text" size="20" autocomplete="off" class="card-number" />
<div class="form-row">
<input type="text" size="4" autocomplete="off" class="card-cvc" />
<div class="form-row">
<label>Expiration (MM/YYYY)</label>
<input type="text" size="2" class="card-expiry-month"/>
<span> / </span>
<input type="text" size="4" class="card-expiry-year"/>
<button type="submit" class="submit-button">Submit Payment</button>

This work with one caveat - The credit card details are still posted to your servers, you usually want to avoid this. Ideally you just want to post the stripeToken. Isn't that the whole point of Stripe.js ? - that you don't deal with PCI compliance since your systems never get credit card details.

Edit here: . Maybe this one should be linked from

boucher commented Jun 16, 2012

In fact, the details are not sent, because the elements have no name property.


Ah I see. Cheers!


You could ensure the form never gets sent even if there are implementation errors by making the button not have a type and hooking into the click event to trigger the form submit event:

<button class="submit-button">Submit Payment</button>
$(document).ready(function () {

    $('.submit-button').click(function () {

    $('#payment-form').submit(function (event) {

As there is no fallback for those not running script there is no need to have HTML written in that way. Remember that forms auto-submit on Enter if there's a submit button somewhere in the form (thanks Microsoft for introducing this behaviour). Removing any submit typing of buttons and only accessing events gets around this.

This also has the advantage that you can put ids back on the form inputs should you choose, instead of classes.

boucher commented Jul 13, 2012

Note that the submit-on-return is part of the HTTP 1.0 spec. It may have started with MS (I wouldn't want to blame them for this, it could have just as well been from lynx), but it's now standard.


wait.. why can't you have ids on form fields when their is a submit button? are the id's used as names?

TopView commented Aug 19, 2012

Perhaps someone knows. Can you put an id rather than class identifier in the input tags like for example,

And then change the js to: $('#card-number').val()

TopView commented Aug 19, 2012

.. sorry it wiped out my html. here is what I meant: change class="card-number" to id

tvaught commented Aug 24, 2012

This actually does not re-enable the "Process" button on IE (version 8 in my testing). What has worked for me is to replace:



$('.submit-button').prop("disabled", false);

There's a note about this behavior on this SO issue:

rajiv20 commented Aug 29, 2012

How would you pass a rails controller name in the form such that, in addition to passing the form values to Stripe server, the rails controller is called to update the customer information for my rails app. Currently, the rails controller that presents the form in the rails view is called. any code snippets would be much appreciated.

domeri commented Oct 11, 2012

How can you also add card holder's name and adress to this form. I tried it, bur doesn't work properly.


I was just about to create fields for name and address too! (peple don't like to give away credit card info if they are not sure they have logged-in or at least given their personal info! "How do they know where to deliver or whom to credit the payment to?", they would think.
Did you fix it?
Why is that failing?


Cardholders name etc.. other fields. You could use jQuery to get your own fields from the pseudo form by ID,name or class ( Not sure why a form is even necessary really). Then use an Ajax call to send that other data to your server for later login and for general information gathering and allow Stripe to do its thing.

The customers would never be the wiser.

Or build a first step form that gathers user information,..This form would submit to the server, then redirect to the second form. Then a second form to finalize with Stripe.


How do I make the amount Field Editable ??


So what's the current suggested method for accepting variable payment amounts? Users must be able to specify how much they're going to be paying, as seen here:

However, it's not possible with stripe-php?

Any chance someone could update this gist to accept payment amounts? Would love to see an example. Now I'm gonna dig through the 32 forks and see if anyone else has added amounts. :)

debcal2 commented Oct 31, 2016

I am using custom form-data and want to auto-submit the form, how can I do that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment