Shopify Integration with Salesforce
Can my Shopify be in alignment with Salesforce for better ROIs?
Shopify is a popular e-commerce platform for selling your products, whether it be a small business or a large business. You have better control on various product features like managing product variants, publishing the products to storefront, pricing, packaging, shipping, and payments, to name a few. If this platform is used for selling up products and if there is Salesforce in place for processing detailed insights against each Order and Other big picture behind the product selling – Invoicing, Inventory, and Support, then this blog would be helpful to understand how both systems can be integrated.
What is the idea all about?
- Shopify platform provides two interfaces; the Admin side where you manage the products, and the other one is where your Customers would visit daily to the storefront. Shopify provides proper documentation on their APIs and supports Webhooks, which makes it easier to integrate your Shopify data with Salesforce. We are talking here about creating customized REST architecture-based Integrations between Salesforce & Shopify to enhance up your business processes like some of the sample scenarios:
- Add up your paid Orders from Shopify to Salesforce for Processing customer service at Salesforce end like Feedbacks, Suggestions, Tracking Cases, etc.
- New Products like Sugar Cookies with Variations like Choco-chips and Kitkat added to Shopify, to be readily available in Salesforce for other processes.
- Add up your paid Orders from Shopify to Salesforce for Processing customer service at Salesforce end like Feedbacks, Suggestions, Tracking Cases, etc.
- If it’s a stocked product in Shopify and there exist complex inventories against same, manage those inventories in Salesforce.
- Create Custom Orders for your existing Customers directly from Salesforce.
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.
For payment what can i do in shopify
Hi Usman,
You can refer the Order, OrderTransaction or Checkout Webhook whichever is required as per your business use case, to have the payments information pushed from Shopify to Salesforce.
https://help.shopify.com/en/api/reference/events/webhook
You can also mark your orders as paid from Salesforce to Shopify using APIs if you have custom/manual payment processing. In Shopify, you will have to create a payment transaction on the Order which you want to mark as paid from Salesforce.
Order Transaction API details: https://help.shopify.com/en/api/reference/orders/transaction
As an example for the same following Shopify community discussion would be helpful:
https://community.shopify.com/c/Shopify-APIs-SDKs/How-to-mark-order-as-paid-through-REST-Admin-API/td-p/439227
Thanks,
Cloudalyze