How to use the customer webhook?

Please contact us to activate the customer webhook for your shop. After we enabled it, our system will call your predefined url every time a customer account gets updated using a POST to your endpoint where you have to set the euid of your system calling our API.

You can find a code example on how to use the webhook on Github: https://github.com/onefriendaday/storyblok-commerce-hook

The procedure to update your customer record looks like following:

class HookController < ApplicationController
  def create
    # 1. Optain the customer ID from the JSON post
    data_json = JSON.parse request.body.read
    token = 'YOUR_TOKEN' # Your API token

    # 2. Call the api to get the customer record
    customer = RestClient::Request.execute(method: :get,
                                           url: "https://capi.storyblok.com/v1/customers/#{data_json['id']}",
                                           headers: {
                                             authentication: "Token token=#{token}"
                                           })

    # 3. Update or create the customer in your database.
    if Customer.find_by_id(customer['id'])
      Customer.update!(customer)
    else
      Customer.create!(customer)
    end

    # 4 Enrich the customer data with the EUID which is the ID of your system
    RestClient::Request.execute(method: :post,
                                url: "https://capi.storyblok.com/v1/customers/#{data_json['id']}",
                                headers: {
                                  authentication: "Token token=#{token}"
                                },
                                payload: {euid: customer_in_your_db.id})
  end

  # Endpoint for us receive the customer record from your system
  def show
    @customer = Customer.find(params[:id])
    render json: @customer
  end
end

Following the api documentation to receive the customer record

Getting a Customer

Endpoint

GET /v1/customers/:id

Request

Query Parameters

page=0
curl "https://capi.storyblok.com/v1/customers/7229?page=0" -X GET \
	-H "Accept: application/json" \
	-H "Content-Type: application/json"
	-H "Authorization: Token token=KbNHeuE5FDH4Lf6QEsGfpwtt" \

Response

{
  "id": 7229,
  "email": "roseann.hane@kerluke.biz",
  "company": "",
  "firstname": "",
  "lastname": "",
  "phone": "",
  "mobile": "",
  "sex": true,
  "email_subscribe": null,
  "birthday": null,
  "active": true,
  "group_id": 41870,
  "confirmed": false,
  "company_alt": "",
  "company_registration": "",
  "company_registration_alt": "",
  "subscribed": false,
  "job": "",
  "company_description": "",
  "origin_id": null,
  "euid": null,
  "default_billing_address": null,
  "default_shipping_address": null,
  "meta_data": null
}