Skip to content

Webhook

Webhook request

By setting the Webhook URL in the application registration screen it will be possible to receive events that occur in BUYMA for orders and product updates.

Also, since the Personal Shopper API processes requests asynchronously, the results of processing will be notified to the Webhook URL.

For more information on the asynchronous processing of the API, please refer to here.

The following header will be added to the Webhook request.

header

value (e.g.)

X-Buyma-Event

"order/create"

X-Buyma-Hmac-Sha256

"XWmrwMey6OsLMeiZKwP4FppHH3cmAiiJJAweH5Jo4bM="

X-Buyma-Api-Version

"v1"

Where X-Buyma-Event is the name of the event that was triggered. X-Buyma-Hmac-Sha256 is used for validating webhooks. X-Buyma-Api-Version is the API version of the data that can be retrieved from the webhook.

Identify the seller account

The payload of the webhook contains a seller_id that identifies the seller account.


List of available events

Data related to events can be retrieved in JSON format.

Resource Event Description
Product product/create Create a product
View Payload
product/update Update product
View Payload
product/fail_to_create Product creation failed
View Payload
product/fail_to_update Product update failed
View Payload
product/bulk_variants_update_requested Registered for bulk inventory update completed
View Payload
Order order/create New Order
View Payload
order/update Update order/transaction status
View Payload
order/fail_to_update Failed to update transaction status
View Payload

Event Payload

product/create, product/update

The following payload will be sent when a product is created or updated.

{
  "reference_number": "ABC0001",
  "seller_id": 10000000,
  "status": "public",
  "created_at": "2020-01-22T15:42:22+09:00",
  "updated_at": "2020-01-22T15:42:26+09:00",
  "category_id": 3064,
  "brand_id": 203,
  "theme_id": 0,
  "season_id": 0,
  "comments": "Thank you for your interest in our products." ,
  "colorsize_comments": "",
  "brand_name": "GUCCI",
  "price": 22500,
  "list_price": 22500,
  "order_quantity": 3,
  "available_until": "2020-02-01",
  "reference_price": 25500,
  "regular_price": null,
  "shipping_area_id": "2001001000",
  "buying_area_id": "2001001000",
  "buying_shop_name": "Authorized Dealer",
  "size_unit": "cm",
  "duty": "included",
  "images": [
    { "path": "https://example.org/images/1.jpg", "position": 1 },
    { "path": "https://example.org/images/2.jpg", "position": 2 }
  ],
  "style_numbers": [
    { "memo": "White", "number": "CATALOG-001-WHITE" },
    { "memo": "Black", "number": "CATALOG-001-BLACK" }
  ],
  "shipping_methods": [
    { "shipping_method_id": 888 },
    { "shipping_method_id": 891 }
  ],
  "tags": [
    { "tag_id": 419 },
    { "tag_id": 710 }
  ],
  "variants": [
    {
      "stock_type": "stock_in_hand",
      "stocks": 1,
      "options": [
        { "type": "color", "value": "White" }
        { "type": "size", "value": "S" }
      ]
    },
    {
      "stock_type": "stock_in_hand",
      "stocks": 1,
      "options": [
        { "type": "color", "value": "White" },
        { "type": "size", "value": "M" }
      ]
    },
    {
      "stock_type": "out_of_stock",
      "options": [
        { "type": "color", "value": "Black" }
        { "type": "size", "value": "S" }
      ]
    },
    {
      "stock_type": "purchase_for_order",
      "options": [
        { "type": "color", "value": "Black" },
        { "type": "size", "value": "M" }
      ]
    }
  ],
  "shop_urls": [
    {
      "label": "label1",
      "url": "http://example.com/url/1",
      "description": "description1"
    },
    {
      "label": "label2",
      "url": "http://example.com/url/2",
      "description": "description2"
    }
  ],
  "id": 123456,
  "name": "quilted jacket",
  "options": [
    { "type": "color", "value": "White", "position": 1, "master_id": 1 }
    { "type": "color", "value": "Black", "position": 2, "master_id": 2 }
    {
      "type": "size", "value": "S", "position": 1, "master_id": 1,
      "details": [
        { "key": "length", "value": "67.0", "optional_value": "67.5" },
        { "key": "shoulder width", "value": "46.0" },
        { "key": "chest circumference", "value": "53.0" },
        { "key": "sleeve length", "value": "63.0", "optional_value": "63.5" }
      ]
    },
    {
      "type": "size", "value": "M", "position": 2, "master_id": 2,
      "details": [
        { "key": "length", "value": "68.0", "optional_value": "68.5" },
        { "key": "shoulder width", "value": "48.0" },
        { "key": "chest circumference", "value": "55.0" },
        { "key": "sleeve length", "value": "65.0", "optional_value": "65.5" }
      ]
    }
  ]
}

product/fail_to_create, product/fail_to_update

The following payload will be sent if the API request fails due to validation or other reasons.

{
    "request_uid": "202001103173552322167443276",
    "errors": {
        "style_numbers": {
            "0": {
                "memo": [
                    "Please enter an identification memo with a maximum of 50 double-byte characters and 100 single-byte characters."
                ]
            }
        }
    },
    "failed_at": "2020-01-10T17:35:54.335287+09:00",
    "seller_id": 10000000
}

product/bulk_variants_update_requested

The request IDs (request_uid) registered for each product will be sent together after the bulk inventory request has been sent.

{
    "request_uid": "202101141130940540962167701",
    "request_received_at": "2021-01-14T13:09:40.540962+09:00",
    "products": [
        {
            "request_uid": "202101141130941706727270763",
            "request_received_at": "2021-01-14T13:09:41.706727+09:00",
            "reference_number": "448200997-0",
            "id": 1234567
        },
        {
            "request_uid": "202101141130941717827239297",
            "request_received_at": "2021-01-14T13:09:41.717827+09:00",
            "reference_number": "761351003-4",
            "id": 1234568
        }
    ]
}

order/create, order/update

The following payload will be sent when a new order or an order/transaction status update occurs.

{
    "status": "new",
    "id": 771829,
    "seller_id": 10000000,
    "customer_id": 123123,
    "product": {
        "id": 543821,
        "reference_number": "ABC0001",
        "name": "【Immediate】GUCCI Women's Bifold Wallet [Domestic]",
        "comment": "Thank you for your interest in our products."
    },
    "shipping_deadline": "2019-11-08 14:42:57 +0900",
    "recipient": {
        "first_name": "taro",
        "last_name": "tanaka",
        "first_name_kana": "taro",
        "last_name_kana": "tanaka",
        "first_name_kanji": "Taro",
        "last_name_kanji": "Tanaka",
        "country": "Japan",
        "country_kanji": "Japan",
        "zip_code": "124-0025",
        "address_1": "1-1-1 MNF building 6F",
        "address_2": "Minami Aoyama,",
        "address_3": "Minato-ku",
        "address_4": "Tokyo",
        "address_kanji_1": "Tokyo,",
        "address_kanji_2": "Minato-ku Minami-Aoyama",
        "address_kanji_3": "1-1-1 MNF Building 6F",
        "phone_number": "090-1111-1111"
    },
    "unit_price": 12000,
    "subtotal_price": 24000,
    "coupon": {
        "price": 2000,
        "id": 1090,
        "code": "GALERSBN"
    },
    "amount": 2,
    "request_shipment": {
        "service_name": "Japan Post - Yu-pack a",
        "is_domestic": true,
        "payment_type": "cash_on_delivery",
        "tracking": true,
        "delivery_period_from": 1,
        "delivery_period_to": 2,
        "cash_on_delivery_cost_from": 1000,
        "cash_on_delivery_cost_to": 2000,
        "description": "Japan Post, tracked, cash on delivery."
    },
    "pre_ordered_at": "2019-10-08 14:42:57 +0900",
    "payment_deadline": "2019-10-12 14:42:57 +0900",
    "ordered_at": "2019-10-10 14:42:57 +0900",
    "color_size_text": "white/10 cm",
    "order_message": "free msg",
    "order_shipment": {
        "service_name": "Japan Post - Yu-Pack",
        "tracking": true,
        "tracking_number_input_url": "https://example.com",
        "tracking_number_url": "https://example.com",
        "tracking_number": "12293920-1-20303333-21",
        "receipt_number": "12293920-1-20303333-21",
        "message": "Thank you very much for your purchase.",
        "shipped_at": "2019-10-11 14:42:57 +0900",
        "received_at": "2019-10-17 14:42:57 +0900"
    }
}

order/fail_to_update

The following payload will be sent if the shipping request fails due to validation or other reasons.

{
    "request_uid": "2001909051121040114775093219",
    "id": 771829,
    "errors": {
        "service_id": ["Shipping service ID is required."]
    },
    "failed_at": "2020-01-10T17:35:54.335287+09:00",
    "seller_id": 10000000
}

Validate the validity of the webhook

There are two ways to validate that the request is from the BUYMA service As a way to verify that the request is from the Buyma service, we have added a signature to the request header with the key X-Buyma-Hmac-Sha256 to be used for signature verification.

Verification Methods.

Obtain the digest value of the request body using the HMAC-SHA256 algorithm with the secret of the client application as the private key. 2. Verify that the Base64-encoded digest value matches the signature in the request header.

Sample

This is a sample code for ruby using Sinatra web framework. I've also included an implementation of the verification method (line 25) that directly executes the openssl command.

app.rb

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
require 'rubygems'
require 'sinatra'
require 'sinatra/reloader' if development?
require 'sinatra/custom_logger'
require 'logger'
require 'openssl'
require 'base64'

$stdout.sync = true

set :logger, Logger.new(STDOUT)
set :bind, '0.0.0.0'

APPLICATION_SECRET = 'YOUR_APPLICATION_SECRET'.freeze

helpers do
  def verified?(data, hmac_header)
    calculated_hmac =
      Base64.strict_encode64(OpenSSL::HMAC.digest('sha256', APPLICATION_SECRET, data)).strip
    logger.info "hmac(verified?): #{calculated_hmac}"
    Rack::Utils.secure_compare(calculated_hmac, hmac_header)
  end

  def verified_by_system?(data, hmac_header)
    calculated_hmac = `echo -n '#{data}' | openssl dgst -hmac #{APPLICATION_SECRET} -binary -sha256 | base64`.strip
    logger.info "hmac(verified_by_system?): #{calculated_hmac}"
    Rack::Utils.secure_compare(calculated_hmac, hmac_header)
  end
end

post '/' do
  hmac_header = env['HTTP_X_BUYMA_HMAC_SHA256']
  logger.info "hmac_header: #{hmac_header}"

  request.body.rewind
  data = request.body.read

  puts "verified: #{verified?(data, hmac_header)}"
  puts "verified_by_system: #{verified_by_system?(data, hmac_header)}"
end

Gemfile

1
2
3
4
source 'https://rubygems.org'

gem 'sinatra'
gem 'sinatra-contrib'

Gemfile.lock

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
GEM
  remote: https://rubygems.org/
  specs:
    multi_json (1.15.0)
    mustermann (1.1.1)
      ruby2_keywords (~> 0.0.1)
    rack (2.2.3)
    rack-protection (2.1.0)
      rack
    ruby2_keywords (0.0.2)
    sinatra (2.1.0)
      mustermann (~> 1.0)
      rack (~> 2.2)
      rack-protection (= 2.1.0)
      tilt (~> 2.0)
    sinatra-contrib (2.1.0)
      multi_json
      mustermann (~> 1.0)
      rack-protection (= 2.1.0)
      sinatra (= 2.1.0)
      tilt (~> 2.0)
    tilt (2.0.10)

PLATFORMS
  ruby

DEPENDENCIES
  sinatra
  sinatra-contrib

BUNDLED WITH
   2.1.4

Dockerfile

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
FROM ruby:2.6.6-alpine

ENV WORKSPACE /app

WORKDIR $WORKSPACE
ADD ./Gemfile* $WORKSPACE/
RUN apk upgrade --update-cache --available && \
    apk add openssl=1.1.1g-r0 && \
    rm -rf /var/cache/apk/*
RUN gem install bundler --version 2.1.4
RUN bundle install -j4
ADD . $WORKSPACE

EXPOSE 4567

docker-compose.yml

1
2
3
4
5
6
7
8
9
version: "3.8"
services:
  web:
    build: .
    command: bundle exec ruby app.rb
    ports:
      - 3005:4567
    volumes:
      - .:/app

Response

When the Webhook server receives an HTTP POST request sent from the Personal Shopper API platform, it should return a status code of 200.

Retry the webhook.

If no response is returned with 200 or within 5 seconds, the webhook request will fail and the Retry the connection with the following specifications.

resource

interval

Product

about 5 times in an hour

Order

about 10 times in about 24 hours

In order to avoid timeouts and errors, please consider postponing the application process until the response to the webhook request has been successfully sent.