Shopify and Business Central WebHooks continuous synchronisation: a smart replication with your e-commerce.

This is how I designed a solution to replicate customers and orders and have these entities always up-to-date in both the ERP and the E-Commerce.

Scenario

  • With this solution I implemented data replication between two databases in a modern, clean way allowing to have entities automatically and immediately updated on both sides.
  • In the example I’ll show a simple case for having Customers automatically updated between your ERP and your e-commerce platform.

LIVE DEMO: 5 SECONDS TO GET THE CUSTOMER IN SHOPIFY

WEBHOOKS

In the pre-web-hooks era, a long time ago, synchronisation among ERP and E-Commerce was usually developed as a scheduled task, often run by night, and was basically implemented as TXT or CSV files being transferred usually via FTP and then parsed and imported. Parsers are fragile, sometimes an unwanted comma or a separator not in the right place is enough to abort the whole procedure. Moreover to work with different character sets (UTF, latin, Chinese, Russian, Japanese etc etc) is a nightmare, sometimes not all of the softwares and components are so smart to accept different encodings and so you have to give up and implement just a single culture.

This “old way” of data exchange is now deprecated in favour of modern techniques that takes care of everything in the correct way.

Web-hooks allow to implement a clean, easy, modern solution for replicating data between different databases.

All of the modern e-commerce platforms support nowadays this technology: PrestaShop via a Plugin, Magento thought Event Observer, NopCommerce via Seven Spikes Api Plugin, OpenCart.

THE RECIPE

  1. A WebHook on customer table subscribed in your Business Central;
  2. A WebHook on customer table subscribed in your Shopify e-commerce;
  3. An Azure Function working as an orchestrator between the two platforms.
  4. In this sample I am not using Azure Vault to store passwords and I just use settings.json, you should when in production environment 🙂

SHOPIFY WEBHOOK CONFIGURATION

In your Shopify shop, configure subscribe to Customer events: update, create and delete.

DOWNLOAD AND DEPLOYS AZURE FUNCTION

A simple Azure Function works as subscriber for Business Central WebHook.

Here are sources in GitHub: https://github.com/avalonit/AzBcCustomerShopifyWebHook

The azure function consists basically in five .cs files.

CustomerWebHook.cs contains the azure function subscribed as Business Central WebHook. In my sample the WebHook is trigger every time a Customer modification occurs. The function is in charge to manage the high level logic: it Gets business central customer, converts to Shopify customer and Posts in Shopify.

Pay attention to these couple of lines of code, they are very important and this is the response the azure function must reply when you will subscribe your web hook in Business Central. Basically they response back the validation token to BC.

BusinessCentralConnector.cs contains class that manages calls to API. It connects to API and get the requested customer.

Customers.cs is the class representing Business Central Customer table. It is used for deserialising API data. I find useful https://app.quicktype.io/ to automatically convert Json returned from BC API to C# class. Just pay attention to DateTimeOffset field type that are not supported by JSON.

ShopifyCustomers.cs is the class representing Shopify Customer table. It is used for serialising API data.

CustomerConverter.cs is the class that converts a Business Central customer entity to a Shopify customer entity. It’s possibile to customise this converter.

ShopifyConnector.cs is the class that post the new customer in Shopify.

To have your solution working just download sources, configure your Business Central endpoint and deploy it to your azure subscription.

SUBSCRIBE A WEB HOOK IN BUSINESS CENTRAL

There is brief but well written article about web hooks here:

https://docs.microsoft.com/en-us/dynamics-nav/api-reference/v1.0/dynamics_subscriptions

I use Insomia to register a new web hook to subscribe to Customer table modifications. Basically you POST to your Business Central URL

https://api.businesscentral.dynamics.com/v1.0/{your_business_central_instance}/sandbox/api/v1.0/subscriptions

a JSON content like this:

{
“notificationUrl”: “https:/{your_azurefunction_address}.azurewebsites.net/api/SalesOrderWebHook”,
“resource”: “/api/v1.0/companies({your_company_id})/customers”,
“clientState”: “optionalValueOf250”
}

If the azure function is working you get a confirmation reply:

{
“@odata.context”: “https://api.businesscentral.dynamics.com/v1.0/{your_bc_instance}/sandbox/api/v1.0/$metadata#subscriptions/$entity”,
“@odata.etag”: “W/\”JzQ0O1djTE9JamVYVk0yMDYxc0dwRElCT1lPQlAwOHVISSsyeG9Tem81RlJhUXc9MTswMDsn\””,
“subscriptionId”: “39db02dc8ccf4fd59755d92a9080197c”,
“notificationUrl”: “https://{your_azurefunction_url.azurewebsites.net}/api/CustomerWebHook”,
“resource”: “api/v1.0/companies({your_company})/customers”,
“userId”: “dc77101d-e4d4-4e42-84ae-68c54851c232”,
“lastModifiedDateTime”: “2020-04-12T08:04:53Z”,
“clientState”: “optionalValueOf250”,
expirationDateTime“: “2020-04-15T08:04:53Z”
}

You can check anytime your active web hooks subscribed with a simple API query:

https://api.businesscentral.dynamics.com/v1.0/your_bc_instance/Sandbox/api/v1.0/subscriptions

ENABLE API ACCESS ON SHOPIFY

To enable access to ShopifyApi you have to create a Private App in Admin. Just fill

https://yourshopid.myshopify.com/admin/apps/private/new

You will get your credentials for accessing Shopify API.

SHOPIFY API REFERENCE

https://shopify.dev/docs/admin-api/rest/reference/customers/customer

https://shopify.dev/docs/admin-api/rest/reference

LAST UPDATED

17th of April, 2020

One thought on “Shopify and Business Central WebHooks continuous synchronisation: a smart replication with your e-commerce.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s