Stripe API

Generate Card via Stripe.js

Stripe.js makes it easy to collect credit card (and other similarly sensitive) details without having the information touch your server. (Read More)

First, load the Stripe JS API.

<script type="text/javascript" src="https://js.stripe.com/v2/"></script>

Second, on submit of the form, the following code is executed and will run the stripeResponseHandler() method.

Note: $form.submit() instead of $form.get(0).submit() will trigger an infinite loop.

jQuery(function($) {
    $('form').submit(function(event) {
        var $form = $(this);

        $('input[type=submit]').prop('disabled', true); //  Disable the submit button from being clicked multiple times

        Stripe.card.createToken({
            number          : $('input#Users_card_number').val(),
            cvc             : $('input#Users_cvc').val(),
            exp_month       : $('input#Users_expire_month').val(),
            exp_year        : $('input#Users_expire_year').val(),
            name            : $('input#Users_billing_name_on_card').val(), // optional
            address_line1   : $('input#Users_billing_address').val(),      // optional
            address_city    : $('input#Users_billing_city').val(),         // optional
            address_country : $('input#Users_billing_country').val(),      // optional
            address_state   : $('input#Users_billing_state').val(),        // optional
            address_zip     : $('input#Users_billing_zip').val()           // optional
        }, stripeResponseHandler);

        event.preventDefault();
        return false;
    });
});

var stripeResponseHandler = function(status, response) {
    var $form = $('form');

    if (response.error) {
        $('input[type=submit]').prop('disabled', false); //  Turn the submit button back on.
    }
    else {
        var token = response['id'];
        $('input#stripe_token').val(token);  //  Ensure you have a hidden field for the stripe token to be submitted to your post request.
        $form.get(0).submit();               //  Submit the form.
    }

    return false;
}

Create the Customer

Create the customer with the post data from the JS form submission. Read up More on the Stripe API for other fields that you can use. (Read More)

Note: The $stripe_token that you created in the JS portion above is the relationship to the card created via the Stripe API.

try {
$customer = Stripe_Customer::create(array(
    'description'     => 'This is the description of the purchase', //  String (optional)
    'card'            => $stripe_token,                             //  String (optional)
    'email'           => '[email protected]',                       //  String
    'plan'            => 'plan',                                    //  String (optional)
    'trial_end'       => 'timestamp',                               //  Timestamp (optional)
    'prorate'         => true,                                      //  Boolean (optional)
    'metadata'        => array(                                     //  Key/Value Dictionary of meta data (below are examples)
        'billing_name_on_card' => $post['billing_name_on_card'],
        'billing_address'      => $post['billing_address'],
        'billing_city'         => $post['billing_city'],
        'billing_country'      => $post['billing_country'],
        'billing_state'        => $post['billing_state'],
        'billing_zip'          => $post['billing_zip'],
    ),
));
}
catch (Stripe_CardError $e) {
//  Card was declined.
}
catch (Stripe_ApiConnectionError $e) {
//  Network problem, perhaps try again.
}
catch (Stripe_InvalidRequestError $e) {
//  You screwed up in your programming. Shouldn't happen!
}
catch (Stripe_ApiError $e) {
//  Stripe's servers are down!
}
catch (Stripe_CardError $e) {
//  Something else that's not the customer's fault.
}
catch (Exception $ex) {
//  Some other Exception occurred.
}

Note: You should return errors on each exception.

Update Customer

First, let's get the customer. The Stripe ID($stripe_id) is something you should have saved to your database. Once we have a customer then we can update the customer information. Check out the Stripe API for more arguments you can update. (Read More)

$customer = Stripe_Customer::retrieve($stripe_id);
try {
    $customer->description = "This is the updated description of the purchase";
    $customer->card = $stripe_token; // obtained with Stripe.js or from the database (See above)
    $customer->metadata = array(
        'billing_name_on_card'  => $post['billing_name_on_card'],
        'billing_address'       => $post['billing_address'],
        'billing_city'          => $post['billing_city'],
        'billing_country'       => $post['billing_country'],
        'billing_state'         => $post['billing_state'],
        'billing_zip'           => $post['billing_zip'],
    );
    $customer->save();
}
catch (Stripe_CardError $e) {
//  Card was declined.
}
catch (Stripe_ApiConnectionError $e) {
//  Network problem, perhaps try again.
}
catch (Stripe_InvalidRequestError $e) {
//  You screwed up in your programming. Shouldn't happen!
}
catch (Stripe_ApiError $e) {
//  Stripe's servers are down!
}
catch (Stripe_CardError $e) {
//  Something else that's not the customer's fault.
}
catch (Exception $ex) {
//  Some other Exception occurred.
}

Change Plan

First, let's get the customer. The Stripe ID($stripe_id) is something you should have saved to your database. Once we have a customer then we can update the subscription information. Check out the Stripe API for more information. (Read More)

try {
    $customer = Stripe_Customer::retrieve($stripe_id);
    $subscription = $customer->subscriptions->retrieve($customer->subscriptions->data[0]->id);
    $subscription->plan = 'new_plan';
    $subscription->save();
}
catch (Stripe_CardError $e) {
//  Card was declined.
}
catch (Stripe_ApiConnectionError $e) {
//  Network problem, perhaps try again.
}
catch (Stripe_InvalidRequestError $e) {
//  You screwed up in your programming. Shouldn't happen!
}
catch (Stripe_ApiError $e) {
//  Stripe's servers are down!
}
catch (Stripe_CardError $e) {
//  Something else that's not the customer's fault.
}
catch (Exception $ex) {
//  Some other Exception occurred.
}