コンテンツにスキップ

Webhook

Webhook リクエスト

アプリケーションの登録画面で Webhook URL を設定しておくことで BUYMA で受注や商品の更新で発生するイベントを受信することが可能になります。

また、パーソナルショッパー API は依頼内容を非同期で処理するため処理結果は Webhook URL に通知します。

API の非同期処理の詳細についてはこちらを参照してください。

Webhook リクエストには以下のヘッダーが付与されます。

ヘッダー

値 (例)

X-Buyma-Event

"order/create"

X-Buyma-Hmac-Sha256

"XWmrwMey6OsLMeiZKwP4FppHH3cmAiiJJAweH5Jo4bM="

X-Buyma-Api-Version

"v1"

X-Buyma-Event にはトリガーされたイベント名を表します。 X-Buyma-Hmac-Sha256 は Webhook の妥当性を検証する ために利用されます。 X-Buyma-Api-Version は Webhook から取得できるデータのAPIバージョンを表します。

出品者アカウントの特定

Webhook の payload には出品者アカウントを特定できる seller_id が含まれます。


利用可能なイベント一覧

イベントに関連するデータは JSON 形式で取得することができます。Webhook では下記のイベントが利用可能です。

リソース イベント 説明
Product product/create 商品の作成
Payload を見る
product/update 商品の更新
Payload を見る
product/fail_to_create 商品の作成に失敗
Payload を見る
product/fail_to_update 商品の更新に失敗
Payload を見る
product/bulk_variants_update_requested 在庫の一括更新の登録が完了
Payload を見る
Order order/create 新規受注
Payload を見る
order/update 受注・取引ステータスの更新
Payload を見る
order/fail_to_update 取引ステータスの更新に失敗
Payload を見る

イベント Payload

product/create, product/update

商品の作成、更新が発生した場合に以下のペイロードが送信されます。

{
    "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": "この度は、商品をご覧いただきありがとうございます。",
    "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": "正規取扱店",
    "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": "ラベル1",
            "url": "http://example.com/url/1",
            "description": "説明1"
        },
        {
            "label": "ラベル2",
            "url": "http://example.com/url/2",
            "description": "説明2"
        }
    ],
    "id": 123456,
    "name": "キルティングジャケット",
    "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": "着丈", "value": "67.0", "optional_value": "67.5" },
                { "key": "肩幅", "value": "46.0" },
                { "key": "胸囲", "value": "53.0" },
                { "key": "袖丈", "value": "63.0", "optional_value": "63.5" }
            ]
        },
        {
            "type": "size",
            "value": "M",
            "position": 2,
            "master_id": 2,
            "details": [
                { "key": "着丈", "value": "68.0", "optional_value": "68.5" },
                { "key": "肩幅", "value": "48.0" },
                { "key": "胸囲", "value": "55.0" },
                { "key": "袖丈", "value": "65.0", "optional_value": "65.5" }
            ]
        }
    ]
}

product/fail_to_create, product/fail_to_update

API リクエストがバリデーション等の理由で失敗した場合に以下のようなペイロードが送信されます。

{
    "request_uid": "202001103173552322167443276",
    "errors": {
        "style_numbers": {
            "0": {
                "memo": [
                    "識別メモは全角50文字・半角100文字以内で入力してください。"
                ]
            }
        }
    },
    "failed_at": "2020-01-10T17:35:54.335287+09:00",
    "seller_id": 10000000
}

product/bulk_variants_update_requested

在庫の一括リクエストの送信完了後に商品ごとに個別に登録された依頼 ID (request_uid) がまとめて送信されます。

{
    "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

新規受注、受注・取引ステータスの更新が発生した場合に以下のペイロードが送信されます。

{
    "status": "new",
    "id": 771829,
    "seller_id": 10000000,
    "customer_id": 123123,
    "product": {
        "id": 543821,
        "reference_number": "ABC0001",
        "name": "【即発】GUCCI レディース2つ折り財布【国内発】",
        "comment": "この度は、商品をご覧いただきありがとうございます。"
    },
    "shipping_deadline": "2019-11-08 14:42:57 +0900",
    "recipient": {
        "first_name": "taro",
        "last_name": "tanaka",
        "first_name_kana": "タロウ",
        "last_name_kana": "タナカ",
        "first_name_kanji": "太郎",
        "last_name_kanji": "田中",
        "country": "Japan",
        "country_kanji": "日本",
        "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": "東京都",
        "address_kanji_2": "港区南青山",
        "address_kanji_3": "1-1-1 MNFビル 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": "日本郵便 - ゆうパック 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": "日本郵便・追跡あり・着払いでの配送となります。"
    },
    "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": "白/10 cm",
    "order_message": "free msg",
    "order_shipment": {
        "service_name": "日本郵便 - ゆうパック",
        "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": "この度はお買い上げいただき誠にありがとうございました。",
        "shipped_at": "2019-10-11 14:42:57 +0900",
        "received_at": "2019-10-17 14:42:57 +0900"
    }
}

order/fail_to_update

発送リクエストがバリデーションなどの理由で失敗した場合に以下のペイロードが送信されます。

{
    "request_uid": "201909051121040114775093219",
    "id": 771829,
    "errors": {
        "service_id": ["配送サービスID は入力必須です。"]
    },
    "failed_at": "2020-01-10T17:35:54.335287+09:00",
    "seller_id": 10000000
}

Webhook の妥当性を検証する

BUYMA サービスからのリクエストか検証する方法として、 リクエストヘッダーに X-Buyma-Hmac-Sha256 というキーで署名の検証に利用するためのシグネチャーを付与しています。

検証方法

  1. クライアントアプリケーションのシークレットを秘密鍵として、 HMAC-SHA256 アルゴリズムを使用してリクエストボディのダイジェスト値を取得します。
  2. ダイジェスト値を Base64 エンコードした値とリクエストヘッダーにある署名が一致することを確認します。

サンプル

Sinatra web framework を使った ruby のサンプルコードです。 直接 openssl コマンドを実行した検証方法の実装(25行目)も含めました。

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

レスポンス

パーソナルショッパー API プラットフォームから送信されるHTTP POSTリクエストを Webhook サーバーで受信したときは、ステータスコード200を返してください。

Webhook の再試行

200でレスポンスが返されなかった場合や5秒以内にレスポンスが返されなかった場合はウェブフックリクエストは失敗し、 以下の仕様で接続を再試行します。

リソース

間隔

Product

約1時間の間に5回程度

Order

約24時間の間に10回程度

タイムアウトやエラーを回避するために Webhook リクエストへの応答 が正常に送信されるまでアプリの処理を延期するなど検討ください。