Video Course

Build a Monthly Subscription App with Laravel Cashier: Lesson 23 of 36

Create a Token

Up Next

Stripe Validation

Autoplaying in 7 seconds!

Cancel

To create a token from a user's credit card information, we'll use the Stripe.js library.

Here is the code to process our form:

// target the form
  // on form submission, create a token
  $('#subscribe-form').submit(function(e) {
    var form = $(this);

    // disable the form button
    form.find('button').prop('disabled', true);

    // use the stripe library. create a single use token
    Stripe.card.createToken(form, function(status, response) {

      // if there are errors, show them
      if (response.error) {
        form.find('.stripe-errors').text(response.error.message).addClass('alert alert-danger');
        form.find('button').prop('disabled', false);
      } else {

        // if there are no errors...
        console.log(response);

        // append the token to the form
        form.append($('<input type="hidden" name="cc_token">').val(response.id));

        // submit the form
        form.get(0).submit();
      }
    });

    e.preventDefault();
  });

Chris Sevilleja

161 posts

Co-founder of Scotch.io. Slapping the keyboard until something good happens.