Shopify integration with salesforce
Shopify Integration with Salesforce
Can my Shopify be in alignment with Salesforce for better ROIs?
Shopify, a widely popular e-commerce platform, caters to businesses of all sizes, offering excellent control over product features. Such as managing variants, storefront publishing, pricing, packaging, shipping, and payments. If you utilize Shopify for selling your products and have Salesforce to process comprehensive insights for each order, along with managing aspects like invoicing, inventory, and support. This blog will serve as a valuable resource to understand the seamless integration of both systems. Discover how the integration of Shopify and Salesforce can optimize your business operations and provide valuable insights for successful product selling.
What is the idea all about?
The concept revolves around leveraging the capabilities of both the Shopify and Salesforce platforms through custom REST architecture-based integrations. Shopify offers two interfaces: the Admin side for managing products and the Customer side where visitors access the storefront. With well-documented APIs and Webhooks support, integrating Shopify data with Salesforce becomes seamless.
The goal is to enhance business processes by creating tailored integrations between Salesforce and Shopify. Some sample scenarios include:
- Syncing paid Orders from Shopify to Salesforce to handle customer service activities like feedback, suggestions, and tracking cases within Salesforce.
- Ensuring that new products, such as Sugar Cookies with variations like Choco-chips and Kitkat, are readily available in Salesforce for further processing.
- Managing complex inventories for stocked products in Shopify within Salesforce.
- Facilitating the creation of custom orders directly from Salesforce for existing customers.
How can this be achieved?
Shopify to Salesforce
Shopify stores all its data in JSON Object format. If you are at Admin side and if you are trying to view for example a product on below URL:
https://yourcustomdomain.myshopify.com/admin/products/productId
Then you can view the JSON Object view of the same product on the below URL:
https://yourcustomdomain.myshopify.com/admin/products/productId.json
Shopify supports Webhooks, which will help you send the above JSON data to your created Apex based REST Webservice. You can follow below documentation on Shopify webhooks and how you can add one from your Shopify Admin:
https://help.shopify.com/en/manual/sell-online/notifications/webhooks
Now to process the same JSON in your Apex, you will need to create three things:
- Apex Wrapper for the JSON structure of what are your integrating, in the above example, this is Product Json, then it should be ProductJSONWrapper.
- Apex REST Webservice, which would give you the relative endpoint URL.
- A Force.com site such that provides a publicly exposed URL which along with above-generated endpoint can be used while creating Webhooks in Shopify. The force.com site’s public access settings should be appropriately configured.
NOTE: If a webhook URL repeatedly sends a non-200 response, the webhook is auto removed in Shopify.
You can use tools available online for free to convert JSON to Apex Wrapper class, which can be used right away or with little tweaks as required.
Below is a sample Apex REST webservice for processing Create Product webhook’s data request:
@RestResource (urlMapping = ‘/ProductSyncing/*’) global class ProductSyncingFromSH { @HttpPost global static void doPost () { //Requesting REST Service to fetch the data sent by Shopify to this web service class for Product Syncing RestRequest req = RestContext.request; RestResponse res = RestContext.response; Blob body = req.requestBody; String bodyString = body.toString(); if(bodyString!=null && bodyString!=”){ ShopifyProductWrapper productJSON = (ShopifyProductWrapper)JSON.deserialize(bodyString, ShopifyProductWrapper.class); //productJSON is Wrapperclass instance which can be further used here to insert/update products or to implement //other complex business logic } } global class ShopifyProductWrapper { global String id; global String title; global String body_html; global String vendor; global String product_type; global String handle; global String tags; global String published_scope; global String admin_graphql_api_id; global ImageWrapper image; global List<ImageWrapper> images; global List<VariantWrapper> variants; global List<OptionWrapper> options; } }
Salesforce to Shopify
Shopify provides their API documentation for each resource. For example, refer following Products API:
https://help.shopify.com/en/api/reference/products/product?api[version]=2019-07
Below is sample request to create new product in Shopify from Salesforce:
//API Callout to Shopify to Add Product Http h = new Http(); HttpRequest req = new HttpRequest(); req.setEndpoint(‘https://yourShopifyStoreURL/admin/products/’); //Fetching Shopify Admin Credentials from custom settings Shopify_Credentials__c ShopCred = Shopify_Credentials__c.getOrgDefaults(); String username = ShopCred.User_Name__c; String password = ShopCred.Password__c; Blob headerValue = Blob.valueOf(username + ‘:’ + password); String authorizationHeader = ‘Basic ‘ + EncodingUtil.base64Encode(headerValue); req.setHeader(‘Authorization’, authorizationHeader); req.setHeader(‘Content-type’, ‘application/json’); req.setHeader(‘Accept’, ‘application/json’); //Here payload needs to be required Shopify’s JSON structure based ApexWrapper class instance. //For ex. ProductWrapper class instance that follows Product.json structure of Shopify req.setBody(JSON.serialize(payload)); req.setMethod(‘POST’); HttpResponse res = h.send(req);
Conclusion:
You can create REST architecture based complex integrations between Shopify and Salesforce with help of Webhooks in Shopify and Apex REST callouts.