Simplify Recurring Billing with the Stripe SDK for Salesforce

Build Stripe payment processing into your Salesforce org with the new open source Stripe SDK for Salesforce. This article talks through the use and implementation of the SDK.

Ryan Huff is the co-founder and CEO of Cirruspath, the makers of Cirrus Insight.

Most apps on the Salesforce AppExchange are managed packages and the ISVs that develop them can leverage all the tools that AppExchange partners typically get, like the License Management App (LMA), Patch Orgs and Push Patch Updates, and integrated recurring billing. We took a different approach to building our app, Cirrus Insight, when we wanted to develop a solution for integrating Google Apps and Salesforce CRM. Cirrus Insight, by contrast, is a browser extension; and we don’t  get to take advantage of these cool toys — so we had to build them ourselves.

Before we publicly released Cirrus Insight, we developed our own LMA in our Salesforce org and worked out a solution for live updates. Managing recurring billing was the final piece, and not one I was looking forward to because I had had challenges with it in the past. I’d worked with PayPal, Authorize.Net, and a handful of other systems in the previously and it was always difficult to make them work smoothly with our business processes. I set out to find a better solution, and I was looking for a few key things:

  1. Easily manage recurring billing
  2. Simplify PCI compliance: I didn’t want to ever see a customer’s credit card
  3. A simple and logical API that would make it easy to integrate with Salesforce

Long story short, we found Stripe, who easily met all of these requirements. Within a week we had our custom built LMA and website hooked up and ready to take payments and manage recurring billing.

Open Sourcing the Stripe SDK for Salesforce

Over the last 2+ years, we’ve streamlined and improved the library we built to integrate with Stripe. When salesforce.com recently announced their partnership with Stripe to provide the recurring billing platform for Salesforce AppExchange partners, we felt it was a good opportunity to open up the library to help out other partners and developers in the Force.com community.

To get started, head to Github and fork the stripeforce repo. Now you have your own copy of the code to do with as you wish. But we encourage your contributions to the library if you have something to share.

Website Integration

We didn’t want to have the security exposure of touching customers’ credit card information directly, and Stripe has an easy way to help you called Stripe Checkout. I’ll leave it to the docs to explain how Checkout works, but you have the choice of using a simple Javascript API or a nice form to put on your website, or even in a Visualforce page that ensures that you never see a credit card. We’re using it on our Cirrus Insight sign-up page and embedded in a Visualforce page for Cirrus Files (coming soon!). Checkout handles secure credit card input and gives you back a “token” that you can send to your back-end services to find that card when you need to add that customer to a subscription.

Where the SDK Fits In

The SDK handles communication to Stripe. Your specific business logic, and how it fits into your Salesforce org, is up to you. For instance, if you want to create a customer with a token from Checkout and add the customer to a subscription:

@RestResource(urlMapping='/my/rest/service/createCustomer')
global class CustomRestService {

  @HttpPost
  global static ResponseData createCustomer() {
    String json = RestContext.request.requestBody.toString();
    RequestPayload payload = (RequestPayload) System.JSON.deserialize(json, RequestPayload.class);

    StripeCustomer customer = StripeCustomer.create(payload.token, payload.description);
    StripeCustomer.updateSubscription(customer.id, 'ANNUAL_SUBSCRIPTION');

    // Update the existing account to include the Stripe Customer ID
    Account a = new Account(
      Id = payload.accountId,
      Stripe_Customer_ID__c = customer.id
    );
    update a;

    ResponseData data = new ResponseData();
    data.status = 'success';

    return data;
  }

  global class RequestPayload {
    global String token;
    global String description;
    global String accountId;
  }

  global class ResponseData {
    global String status;
    global String errorMessage;
  }
}

Webhooks

Stripe handles our recurring billing and manages when subscriptions are ready to be charged. We needed a way to get that information back into Salesforce and our accounting system. Stripe supports Webhooks, which means that Stripe will POST to any URL you provide with the data about a transaction that just took place.

The SDK includes a simple interface for handling Stripe webhooks. For each type of webhook, the interface will pass the Stripe data object received in the webhook to a method for that webhook event type (the readme in the Github project explains the details). You’re in control of what happens next since you write the logic in that method. This diagram may help describe a real-world scenario:

Contributing to the SDK

We’ve been using the SDK at Cirruspath for a couple years now. But we’re not using everything that Stripe can do. When you find use cases that haven’t been built out yet with the SDK, send a pull request so that those contributions can be included in the library and others can benefit.

If you have questions about using or contributing to the SDK, post them to the Github project and the repo contributors will be right on it.

Leave your comments...

Simplify Recurring Billing with the Stripe SDK for Salesforce