top of page

Salesforce and Paypal Integration: OAuth Authentication

Jun 9, 2019
2 min read

Hi! 😀

I was recently working on something which involved integration between Salesforce and Paypal, and found myself stuck during the implementation of the same. And to my surprise, when I was searching the web I couldn’t find any posts or solution for my problem. But eventually I figured it out, and thought of posting the solution as a post here.

So in this article we will touch up on how we can set up the OAuth 2.0 authentication between Salesforce and Paypal.

Things we need before start hitting the keys:

  • A paypal account; a sandbox account is recommended

  • Client ID and Secret for your sandbox app.

  • A salesforce dev/sandbox org(obviously, duh!)

So first step to sign up for a developer account on PayPal. Refer the following link: https://developer.paypal.com/docs/classic/lifecycle/sb_create-accounts/#

Next step is to create an App(a regular drill if you have worked with intergrations) in your PayPal developer account, and get the credentials, which will be used when making the OAuth call for access token. Refer the following link: https://developer.paypal.com/docs/api/overview/#get-credentials

Now it’s time to code! 😀

Function(or method) to request the access token:

string clientId='AQL......67'; //Use your Client ID credential herestring clientSecret='EB......qq'; //Use your Client Secret key credential herestring endpoint = 'https://api.paypal.com/v1/oauth2/token'; // URL for sandbox will be https://api.sandbox.paypal.comstring body='grant_type=client_credentials';/* Here comes the tricky part, i.e. setting up the Authorization Header for the OAuth call.*/Blob headerValue = Blob.valueOf(clientId +':' +clientSecret);String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);//system.debug('Encoder Value' + EncodingUtil.base64Encode(headerValue)); http http = new http();httpRequest req = new httpRequest();req.setMethod('POST');req.setEndpoint(endpoint);req.setHeader('Content-Type', 'application/x-www-form-urlencoded');req.setHeader('Authorization', authorizationHeader);req.setBody(body);httpResponse Res = http.send(req);system.debug(res.getStatus());system.debug(res.getBody());//Further create a wrapper class and deserialize the JSON response to store the access token in a variable

And that concludes this post!

Thank you for being an awesome reader! Subscribe to this blog for receiving all the latest updates straight to your inbox. 🙂

 
 
 

Comments


bottom of page