new_window arrow_01 arrow_02 arrow_03 arrow_04 arrow_05 arrow_06 arrow_07 arrow_08 arrow_09 menu close metaphase facebook instagram twitter tumblr logo_metaphase

[Shopify]Webhookを処理する

Other

2022.03.01

メタフェイズではShopifyを利用したストア構築を行っています。
今回はWebhookを処理する方法を紹介いたします。

 

1 Webhookを作成しURLを設定する



 

2 設定したURLへ下記を記述したindex.phpを配置する

<?php
define('SHOPIFY_APP_SECRET', 'xxx');
function verify_webhook($data, $hmac_header)
{
    $calculated_hmac = base64_encode(hash_hmac('sha256', $data, SHOPIFY_APP_SECRET, true));
    return hash_equals($hmac_header, $calculated_hmac);
}

$hmac_header = $_SERVER['HTTP_X_SHOPIFY_HMAC_SHA256'];
$data = file_get_contents('php://input');
$verified = verify_webhook($data, $hmac_header);
$headers = getallheaders();
$txt = [
"----------------verified---------------",
$verified ? "true":"false",
"----------------header---------------"
];
foreach ($headers as $key => $value){
    $txt[] = "{$key} : {$value}";
}
$txt[] = "----------------body---------------";
$txt[] = $data;
if( $file_handle = fopen( "./webhook".date("YmdHis").".txt", "w") ) {
    fwrite( $file_handle, implode("\n",$txt));
    fclose( $file_handle);
}
http_response_code(200);

 
define(‘SHOPIFY_APP_SECRET’, ‘xxx’);の「xxx」の部分は下図の赤枠内の文字列で置き換えて下さい。


 

3 確認する

shopify上で買い物を完了すると、設定したURLの配下にWebhookで受け取った情報がテキストファイルで保存されています。

 
サンプル

----------------verified---------------
true
----------------header---------------
Host : xxxx
X-Forwarded-Host : xxxx
X-Forwarded-For : xxx.xxx.xxx.xxx
X-Forwarded-Proto : https
X-Backend : xxx
Content-Length : 7380
User-Agent : Shopify-Captain-Hook
Accept : */*
Accept-Encoding : gzip;q=1.0,deflate;q=0.6,identity;q=0.3
Content-Type : application/json
X-Shopify-Api-Version : 2021-10
X-Shopify-Hmac-Sha256 : xxxxx
X-Shopify-Order-Id : 4252613804209
X-Shopify-Shop-Domain : xxx.myshopify.com
X-Shopify-Test : true
X-Shopify-Topic : orders/paid
X-Shopify-Webhook-Id : f55cba9a-573e-484d-894c-e42abd859b0e
----------------body---------------
{"id":4252613804209,"admin_graphql_api_id":"gid:\/\/shopify\/Order\/4252613804209","app_id":580111,"browser_ip":"150.249.255.158","buyer_accepts_marketing":false,"cancel_reason":null,"cancelled_at":null,"cart_token":"8e9c70b42d1cc16dbef690f905c39987","checkout_id":23751721124017,"checkout_token":"54325bab17269c73c64a88cae128b3f8","client_details":{"accept_language":"ja,en;q=0.9,en-US;q=0.8,da;q=0.7,la;q=0.6,et;q=0.5,it;q=0.4,fr;q=0.3,pt;q=0.2,mt;q=0.1,zh-CN;q=0.1,zh;q=0.1,cs;q=0.1,zh-TW;q=0.1,ny;q=0.1,nb;q=0.1","browser_height":889,"browser_ip":"150.249.255.158","browser_width":1744,"session_hash":null,"user_agent":"Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/96.0.4664.45 Safari\/537.36"},"closed_at":null,"confirmed":true,"contact_email":"hisa+user@metaphase.co.jp","created_at":"2021-11-18T16:53:29+09:00","currency":"JPY","current_subtotal_price":"1200","current_subtotal_price_set":{"shop_money":{"amount":"1200","currency_code":"JPY"},"presentment_money":{"amount":"1200","currency_code":"JPY"}},"current_total_discounts":"0","current_total_discounts_set":{"shop_money":{"am

 

 
Shopifyを利用したストアの構築・運用のご相談はお問い合わせフォームより受け付けております。

 

 

最新のブログ記事