Salesforce Shopify Integration

A Little Background About This Post:

In my recent project with one of my client , they came up with salesforce to shopify integration as their challenge as  there was lot of manual work involved from shofipy to salesforce . I worked over this piece of integration and thought of sharing this with community .




What they use shopify for ? Well here is brief introduction for shopify.

Shopify is a complete ecommerce solution that allows you to set up an online store to sell your goods. It lets you organize your products, customize your storefront, accept credit card payments, track and respond to orders — all with a few clicks of the mouse.

Happy To See:

I had look into Shofipy API and was happy to see that.
1.       It provide beautiful Rest API.
2.       It uses OAuth 2.0 for authorization and authentication.

Tool Needed For Demonstration :

1.       Salesforce Developer Org .
2.       Shofipy Developer Account
3.       Shofipy Trial Account

Enough Talk’s Let’s Start:

Let’s break this into discrete tasks with their outcomes and we will complete each task one by one.

Task 1 : Register a Shofipy developer account and create one app .
Outcome 1 : Receive Client Id and Client Secret.

Task 2 :  Authorize Salesforce user on behalf of Shofipy app .
Outcome 2 : Receive Authorization Code.

Task 3 : Request Access token using Above Authorization Code .
Outcome 3: Receive Access token

Taks 4 : Make API calls to Shofipy application using  above access token.
Outcome 4 : Well ! both systems are integrated now .



Taks 1 : Login into your salesforce developer account and create a visualforce page ,as this will     be used while creating shofipy app .
Login into your shofipy developer account and create app as below .

      1.       Create a visualforce page  named ‘shofipy’ in your salesforce org..
      2.       Go To https://developers.shopify.com/
      3.       Register with your details.
      4.       Click ‘Apps’ in left menu bar . Then click ‘create a new app ‘ in top right corner.
       5.      Set callback URL to your ‘shofipy’  visualforce page URL.

         





Once we create app in shofipy ,it will look like below .



Outcome 1 : We have client id and client secret now .

Task 2: Authorize Salesforce user on behalf of shofipy credentials .

Paste below code in your visualforce page. Click Authorize link, We will authorize salesforce user with shofipy app on behalf of shofipy app.

   <apex:pageBlockSection >
               <apex:outputlink value="http://salesforce-integration-demo-store.myshopify.com/admin/oauth/authorize?client_id=YOUR_CLIENT_ID&scope=read_orders&redirect_uri=https://ashishdevlightning-dev-ed--ashishdev.ap1.visual.force.com/apex/shofipy&state=2343445">
                   Authorize Shofipy
               </apex:outputlink><br/><br/><br/>
          </apex:pageBlockSection>


Outcome 2: Receive Authorization Code.
It will redirect to the callback url which is your visualforce page only with authorization code as below.
Check your Browser URL will look like below URL carrying authorization code.

https://ashishdevlightning-dev-ed--ashishdev.ap1.visual.force.com/apex/shofipy?code=56aa904e6


Task 3: Request Access token using Above Authorization Code .
In this step we will use Previous step Authorization Code to get access token,
We will make one apex http callout like below.

           Http h = new Http();
           // Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
           HttpRequest req = new HttpRequest();
           
           req.setEndpoint('https://salesforce-integration-demo-store.myshopify.com/admin/oauth/access_token');
           req.setMethod('POST');
           
           req.setHeader('Content-Type','application/json');
           AccessTokenBody body = new AccessTokenBody();
           body.code =authCode;
           body.client_secret =ClientSecret;
           body.client_id =ClientId;
           system.debug(JSON.serialize(body));
           
           req.SetBody(JSON.serialize(body));
          
           // Send the request, and return a response
           HttpResponse res = h.send(req);
           String body1= res.getBody();
           system.debug(body1);


Outcome 3: Debug your response json ,we have access token now.

Taks 4: Make API calls to shofipy application using  above access token.

We now have access token ,we can make shofipy api calls using above access token.
Here is sample callout to get all your orders .

           Http h1 = new Http();
           // Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
           HttpRequest req1 = new HttpRequest();
           
           req1.setEndpoint('https://salesforce-integration-demo-store.myshopify.com/admin/orders.json');
           req1.setMethod('GET');
           req1.setHeader('X-Shopify-Access-Token',acessTokenCode );
           // Send the request, and return a response
           HttpResponse res1 = h1.send(req1);
           String body2= res1.getBody();



Outcome 4:Debug your response json .Well ! both systems are integrated now .

Full Working Code : Here are apex class and vf page for same .

Apex Class:


public with sharing class ShofipyController {
   public fromJSON f{get;set;}
   String authCode;
   String ClientId=YOUR_CLIENT_ID;
   String ClientSecret=YOUR_CLIENT_SECRET;
   public ShofipyController(){
       if(ApexPages.currentPage().getParameters().keySet().contains('code')){
           authCode= ApexPages.currentPage().getParameters().get('code');   
           system.debug(authCode);
       }
       
   }
   
   
   public PageReference callShofipy() {
   
           Http h = new Http();
           // Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
           HttpRequest req = new HttpRequest();
           
           req.setEndpoint('https://salesforce-integration-demo-store.myshopify.com/admin/oauth/access_token');
           req.setMethod('POST');
           
           req.setHeader('Content-Type','application/json');
           AccessTokenBody body = new AccessTokenBody();
           body.code =authCode;
           body.client_secret =ClientSecret;
           body.client_id =ClientId;
           system.debug(JSON.serialize(body));
           
           req.SetBody(JSON.serialize(body));
          
           // Send the request, and return a response
           HttpResponse res = h.send(req);
           String body1= res.getBody();
           system.debug(body1);
           
           JSONWrapper jsonWrapObj = (JSONWrapper)JSON.deserialize(body1, JSONWrapper.class);
           String acessTokenCode =jsonWrapObj.access_token;
           String scope =jsonWrapObj.scope;
           
           system.debug(acessTokenCode );
           
           
           Http h1 = new Http();
           // Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
           HttpRequest req1 = new HttpRequest();
           
            req1.setEndpoint('https://salesforce-integration-demo-store.myshopify.com/admin/orders.json');

           
           req1.setMethod('GET');
           
           req1.setHeader('X-Shopify-Access-Token',acessTokenCode );
          
           // Send the request, and return a response
           HttpResponse res1 = h1.send(req1);
           String body2= res1.getBody();
           system.debug(body2);
           f =(fromJSON)System.JSON.deserialize(body2, fromJSON.class);
           system.debug(f);
           return null;
   }
   
   
   public class AccessTokenBody{
       String code;
       String client_id;
       String client_secret;
           
   }
   public class JSONWrapper{
      public String access_token;
      public string scope;
  }

}



Visualforce Page:

<apex:page controller="ShofipyController">
   <apex:form >
       <apex:pageBlock >
           <apex:pageBlockSection >
               <apex:outputlink value="http://salesforce-integration-demo-store.myshopify.com/admin/oauth/authorize?client_id=dba1177289e176fd92ceb00360e72e79&scope=read_orders&redirect_uri=https://ashishdevlightning-dev-ed--ashishdev.ap1.visual.force.com/apex/shofipy&state=2343445">
                   Authorize Shofipy
               </apex:outputlink><br/><br/><br/>
           </apex:pageBlockSection>
           <apex:pageBlockSection >
               <apex:commandButton value="Get Access Token" action="{!callShofipy}"/>
               <apex:repeat value="{!f.orders}" var="r">
                   {!r.email}
                   {!r.id}
                   {!r.created_at}
                   {!r.order_number}
                   {!r.total_price}
               </apex:repeat>
       </apex:pageBlockSection>
       </apex:pageBlock>
   </apex:form>
</apex:page>

Comments

  1. hi..ashish
    can you make it video
    if u had video plz share fazil.sk.sfdc19@gmail.com

    ReplyDelete
  2. The best E-Commerce Platform that helps you to Sell Online, on Facebook, or in person. Customize the Ecommerce site and Software as per your business needs with ease.
    Hire shopify developer for Custom Ecommerce Software for Your Needs

    ReplyDelete
  3. Its really informative ....
    Bluetech is the one of the best Company in india to design online shopping websites through Shopify. One should hire Shopify developer to enhance the eCommerce business traits

    ReplyDelete
  4. You have figured it fast!
    Trendy Shopify Development Company for online store.Hire Shopify Developer today!

    ReplyDelete
  5. Be ready to make your business in an online world!
    It is the best time to showcase your business product digitally. Because everyone is shopping online at this time. Choose the best Shopify Development Services provider for online store creation. Reach out to us for more details.
    Shopify Development Services

    ReplyDelete
  6. Thanks for sharing this informative article on shopify to salesforce integration with useful screenshot and in detail with examples. If you have any requirement to Hire Shopify Developers for your project. You can contact us for more information.

    ReplyDelete
  7. Thanks for sharing such a nice blog with us. Keep sharing this kind of blog with us.

    Shopify Third-party API Integration

    ReplyDelete
  8. Great Post!! Thanks for sharing a very nice and useful information. It's really helpful for us. Shopify Development Services

    ReplyDelete

Post a Comment

Popular posts from this blog

A Quick Salesforce OAuth 2.0 Usage Demonstration

Salesforce To Authorize.Net Integration