Connect with us

WP Superstar

Connecting WooCommerce To Salesforce: OAuth2 Authorization

Connecting WooCommerce To Salesforce: OAuth2 Authorization

WooCommerce

Connecting WooCommerce To Salesforce: OAuth2 Authorization

Along with learning Typescript in the fast few months, I’ve also been putting a lot of time an energy into learning Salesforce and the Apex language. So far it has been an amazing journey and my end goal for learning Salesforce is so that I can write REST APIs that would synchronize WooCommerce and Salesforce seamlessly.

With that purpose in mind, I realized doing something like this is no easy task or something you can write in a day or two and never have to touch it again, specially since the whole ecosystem (WordPress, WooCommerce, Salesforce) gets updated very frequent and things change all the time. I’ve taken the approach of dividing this humongous task into smaller modules that would help me accomplish this in faster and more maintainable way. Being able to re-use the components and sort of add or delete them as needed is what I’m after and the first step would be to establish a connection with Salesforce so that I can begin sending my Requests.

Since this would be tied to WooCommerce on this side, I figured I would do it the WordPress/WooCommerce way so I wrote a little plugin that integrates with WooCommerce and handles the saving of all the data received from Salesforce after the initial oAuth2 authorization and saves it to the database for later use. Note that this part doesn’t include the option the revoke the token or to send the refresh token in order to obtain a new access token once the old one has expired.

Register Our Integration With WooCommerce

Let’s start coding and register our registration with WooCommerce so that it appears under the WooCommerce -> Settings -> Integrations tab. This file’s sole purpose is to help WooCommerce know this is an integration and to load other PHP Classes we are going to be using. The actual fields, settings and any other logic will be in another file.

Integration Fields Using WooCommerce’s Settings API

Everything is pretty standard, just a few fields to save our the values we get back from Salesforce.

  • SF App Consumer Key
  • SF App Consumer Secret
  • SF Login URI
  • SF oAuth2 Token
    Salesforce Token (Not actually used to communicate back and forth but it was used for obtaining the Access Token
  • SF Access Token
    Access token sent with every request for authenticating purposes.
  • SF Refresh Token
    Refresh token used whenever the Access Token needs to be renewed/refreshed
  • SF Instance URI
    All REST Request will be send to this base Uri.
  • Debug Log
    Log events such as API requests

Register Custom WordPress Endpoints

Next we register 2 endpoints which are used in the 2 step process. What follows takes care of registering two endpoints, oauth2 and oauth2-step2. It will also add some quick and dirty logic to overriding what each of these endpoints display. These templates are called authorization-step-1.php and authorization-step-2.php.

Endpoint Templates

Finally, these are the layouts for those 2 endpoints.

Download WooCommerce To Salesforce Integration: Initial Integration

WooCommerce To Salesforce Integration: Initial Integration

To me, WordPress is a system to which versatility and power come naturally. Over the last thirteen years of my life, I have seen WordPress evolve from a simple blogging platform into the leading CMS that powers more than 27% of the Internet. My experience is broad, ranging all the way from writing WordPress plugins to creating WordPress themes based on PSD files. I am a master of developing and deploying membership sites powered by WooCommerce, WooCommerce Memberships, and WooCommerce Subscriptions.

Comments

More in WooCommerce

To Top