How to integrate with the Stripe module

Last updated on
25 January 2018


This module does very little on its own. In order to use this module to integrate Stripe with Drupal, you will need to either A) use another integration module or B) write your own custom module and alter an existing form to include Stripe.

A) Integration modules with other Drupal projects

B) Altering an existing form

On my site, I am using the basic_cart module to create a very simple shopping cart and checkout process. I would like to add to this basic cart, the ability to accept payments through Stripe. (Stay tuned for a Simple Cart Stripe module where you can see it in action!)

* In the following code example, the name of my custom module is basic_cart_stripe.

First, I will need to alter the existing form (provided by basic_cart module) to add the Stripe payment details.

 * Implements hook_form_FORM_ID_alter().
function basic_cart_stripe_form_basic_cart_checkout_form_alter(&$form, $form_state) {

  // Add the stripe form element.
  $form['stripe'] = array(
    '#type' => 'stripe_payment',
    '#address' => TRUE,
    '#prefix' => '

', '#suffix' => '

', '#after_build' => array('basic_cart_stripe_form_basic_cart_checkout_after_build'), ); // Add the amount. $price = basic_cart_get_total_price(); $amount = $price->total * 100; // Amount is in cents. $form['amount'] = array( '#type' => 'value', '#value' => $amount, ); // Adjust form element weights so submit and message come last. $form['basic_cart_checkout_message']['#weight'] = 9; $form['basic_cart_checkout_submit']['#weight'] = 10; // Add custom submit handler, and make sure it comes first. $form['#submit'] = array( '0' => 'basic_cart_stripe_form_basic_cart_checkout_form_submit', '1' => 'basic_cart_checkout_form_submit', ); }

Next, I need to add a submit handler to the form that submits the Stripe payment. The first step of adding a submit handler is altering $form['#submit'] to make sure it is called (see above). The second step is writing the submit handler function, as follows.

 * Submit handler for simple cart. This is where we process via Stripe.
function basic_cart_stripe_form_basic_cart_checkout_form_submit(&$form, &$form_state) {
  // Process the transaction with
  if (($library = libraries_load('stripe')) && !empty($library['loaded'])) {
    try {
      $charge = Stripe_Charge::create(array(
        "amount" => $form_state['values']['amount'], // Amount in cents, again
        "currency" => "usd",
        "card" => $form_state['values']['stripe']['stripe_token'],
        "description" => "Test Charge from " . variable_get('site_name', 'My Drupal Site'))
    catch (Exception $e) {
      form_set_error('', $e->getMessage());
      $form_state['rebuild'] = TRUE;

As an added bonus, I also wanted to clean up the Stripe form element to match the needs of my site. In order to do this, I need to add an #after_build function for that Stripe element. It's in this after_build function that I'll be able to alter all the inner elements that weren't available to me in the original form_alter.

 * After build function to clean up formatting on Stripe form element.
function basic_cart_stripe_form_basic_cart_checkout_after_build($element, &$form_state) {
  // Minor text cleanup of element titles and descriptions.
  $element['expiry_month']['#title'] = t('Expiration');
  $element['address']['postal_code']['#title'] = t('Zip');

  // Add classes to each sub element for easier styling.
  foreach ($element as $key => $sub_element) {
    if (substr($key, 0, 1) != '#') {
      $element[$key]['#attributes']['class'][] = 'stripe-' . str_replace('_', '-', $key);

  // Set the country to US only.
  $element['address']['country'] = array(
    '#type' => 'value',
    '#value' => 'US',

  return $element;

With this small amount of code, your integration should be complete.



The Stripe module uses Libraries API to integrate Stripe's PHP API into Drupal.

To load the Stripe PHP API from inside your module, simply do:



When the library is loaded, the configured (secret) site-wide API key is
automatically set using Stripe::setApiKey(). If needed, the API key can be
changed by calling Stripe::setApiKey() with another value or passing the
alternative key as an argument of the various Stripe statics methods.

See for more information.


The module defines a new Form API element type to input a Stripe token. This
form element will be rendered as a fieldset containing the elements required to
collect the information needed to create a token. On submit, the information
will be used to create a new token which will be sent to the server. AJAX form
submission is supported too. A failed token creation will prevent a normal form
submission but not an AJAX submission.

This input form element supports the following properties:

  • #cardholder_name: Can either be a boolean or an array. If TRUE, the field set will include an element to input the card holder's name. If an array is used, it must contain the identifier (ie. like the #parents property) of an existing form element to be used to input the card holder's name. In all other cases, the field set will not include a card holder's input element.
  • #address: Can either be a boolean or an array. If TRUE, the field set will include elements to input the card holder's address. If an array is used, it must contain the identifier (ie. like the #parents property) of an existing form container with the input elements to use for the card holder's address. In all other cases, the field set will not include card holder's address input element.
  • #cvc: If TRUE, the field set will include a CVC input element.
  • #publishable_key: The publishable key to use to create the token. If empty, the currently active site-wide publishable key will be used.


The address field set uses a structure similar the one provided by the Address Field module (

  • 'thoroughfare' as 'address_line1'
  • 'premise' as 'address_line2'
  • 'locality' as 'address_city'
  • 'administrative_area' as 'address_state'
  • 'postal_code' as 'address_zip'
  • 'country' as 'address_country


The Stripe module store site-wide API keys in 4 configuration variables:
'stripe_test_secret', 'stripe_test_publishable', 'stripe_live_secret' and
'stripe_publishable_secret'. A fifth variable is used to configure the active
environment: 'stripe_key_status'. Its value should be one of 'test' or 'live'.

The stripe_get_key() function is the recommended way to retrieve the value of
these variables.