Call us: +1 914 933 7633

Stripe – Integration with Salesforce

Stripe – Integration with Salesforce
March 3, 2020 Rahul Ramakrishnan

Stripe Integration with Salesforce

What is the Stripe and why it is used?

Stripe is one of the fastest growing payment gateways in the world. It is popular because it is one of the most powerful, fastest, and flexible tools for the online business world. They are offering a lot of services like Online Payment Solutions, E-Commerce Payment Solutions & Credit Card processing options. Stripe is one of the simplest ways of collecting online payments globally.

In this blog, I am focusing on, how to integrate Stripe on your Salesforce Developer Org.

API’s and webhooks are the two important factors related to the Stripe integration with Salesforce. Stripe uses webhooks to notify your application when an event is triggered in your account or connected accounts. Webhooks are basically used to perform an event asynchronously, like when a customer’s bank confirms a payment, a customer disputes a charge, or a recurring payment succeeds.

Example: If the payment succeeds, Stripe gets a webhooks response which says “Payment Succeeded”.

For the authentication of requests, stripe API keys are used. We can manage these from the Stripe Dashboard. It is a resource-oriented URL API and it returns JSON encoded response. A basic Stripe API URL looks like: https://api.stripe.com

What’s the difference between API and Webhooks?

In the case of API’s, they place the call without knowing whether they will get any data as response or not. But in the case of webhooks, an HTTP post is used to retrieve calls and, it will get a call from the external system only when some data is updated.

Integrating Stripe with Salesforce would be really useful for businesses. Let’s go into the step by step instructions of how to get this done. 

Prerequisites

1) Registered Stripe Account

2) Salesforce Developer Org

PART 1:

  • Send the webbooks responds to salesforce

Step 1

If you have a stripe account just login, you will see a dashboard. (https://dashboard.stripe.com/dashboard). If you do not have an account, create a trial account by clicking on the Sign-Up button.

Step 2

Login to Salesforce Developer Org through (login.developer.com).

Step 3

Create an Apex class in your developer org. Remember to note the class name, as we should use this later when we create a public site in Salesforce.

This is how an example class would look like:


@RestResource(urlMapping=’/stripe/webhooks/v1/*’)

global class WebhookListener{

    @HttpPost

    global static String doPost(){

        RestResponse response = RestContext.response;

        response.addHeader(‘Content-type’,’application/json’);

        String reqJson= RestContext.request.requestBody.toString(); //Get response from the web hook

/*

The Json can be parsed and needed actions can be performed

*/

      return ‘Success’;  

}

Step 4

Create a public site in Salesforce with the class mentioned above.

  1. Click Setup -> Sites

Create a public site with a unique URL.

  1. Set ‘I have read and accepted the Salesforce’ as TRUE and click on ‘Register My Salesforce Site Domain’.
  2. Create a site with the required fields.
  1. Save the site and click on ‘Public Access Settings’.
  1. Click on ‘Enabled Apex Class Access’.
  2. Add the class which we created (in step 3) in the Enabled Apex Classes section.

Now our class and public site is ready. For testing the webhook, we need to create a sample webhook in Stripe and trigger the Stripe events.

Step 5

  1. Create WEBHOOK in Stripe.

Go to the Stripe Dashboard. Under the Developers section, select Webhooks.

2. Register webhook endpoint and select the event you want to test.

Note: Endpoint URL should be

Public site URL + URL Mapping (in apex class you created above)

3. Test the WEBHOOK by clicking on ‘Send Test Webhook ‘button

***If the configuration is correct, you will get a JSON response string inside the doPost() method you created.

 

PART 2: Retrieving data from Stripe using API

Step 1

Find API secret key from the Stripe dashboard.

Stripe Dashboard -> API Keys -> Copy secret key from under the Publishable key.

Step 2

Register Stripe API URL inside Salesforce as a Remote Site Setting.

Setup -> Security Controls -> Remote Site Settings

Step 3

Write a GET method in the class which you had already created.

Code Sample:


@HttpGet

public static void customerRetrieve(){

String customerId = ‘cus_GojOo9NTFGN2iJ’;

Http h = new Http();

HttpRequest http = new HttpRequest();

http.setEndpoint(‘https://api.stripe.com/v1/customers/’+customerId+");

http.setMethod(‘GET’);

Blob headerValue = Blob.valueOf(‘sk_test_aVN0ONwqZEfwdZJ3TKDrlqeW00TzNdNe8v’ + ‘:’);//Copied secret key from step1

String authorizationHeader = ‘BASIC ‘ +

EncodingUtil.base64Encode(headerValue);

http.setHeader(‘Authorization’, authorizationHeader);

HttpResponse response = h.send(http);

if(response.getStatusCode() == 200) {

/*

The Json can be parsed and needed actions can be performed from response.getBody()

*/

}

** We can call the above method, inside the webhook code or something

While calling this method, you will get customer details as a JSON response inside the response body, because in this example Since we are calling customer API in the above example, you can call any API like this.

Want more information on this? Check out the Stripe Official Documentation.

I have integrated Salesforce with Stripe using the above steps several times. These step by step instructions will help you too. Good Luck!

0 Comments

Leave a reply

Your email address will not be published. Required fields are marked *

*