Last updated January 20, 2014. Created on January 14, 2014.
Edited by jenlampton, cafuego. Log in to edit this page.

GENERAL USAGE

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.

<?php
/**
 * 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' => '<div id="stripe-wrapper">',
   
'#suffix' => '</div>',
   
'#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.

<?php
/**
 * 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 Stripe.com.
 
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;
      return;
    }
  }
}
?>

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.

<?php
/**
 * 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.
 
unset($element['name']['#description']);
 
$element['expiry_month']['#title'] = t('Expiration');
  unset(
$element['expiry_year']['#title']);
 
$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.

TECHNICAL NOTES

LIBRARIES API INTEGRATION

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:

<?php
libraries_load
('stripe');
?>

API KEYS

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 argument of the various Stripe statics methods.

See https://drupal.org/node/1342238 for more information.

STRIPE FORM ELEMENT

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 used to created a new token which will be send 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 support 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, in must contains 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, in must contains the identifier (ie. like the #parents property) of an existing form container with the input elements to use for the 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.

ADDRESS DATA

The address field set uses a structure similar the one provided by the Address Field module (https://drupal.org/project/addressfield):

  • '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

CONFIGURATION VARIABLES

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.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

Upchuk’s picture

Eric Brown wrote a nice article on Stripe with Drupal.

D

You can follow me on Twitter: @drupalexp or on my blog.