การยืนยันตัวตน

ในการยืนยันตัวตนสำหรับ API request ผู้ใช้ต้องสร้าง Signature โดยใช้ secret key ที่ได้รับ และแนบไว้ใน request header X-Signature

การคำนวณ Signature

กระบวนการสร้าง Signature ใช้การ hash request body ด้วยอัลกอริทึม HMAC-SHA256 และ secret key

ตัวอย่าง Merchant Secret Key

secret_key = 1234567890abcdef1234567890abcdef1234567890abcdef

ตัวอย่าง Payload:

{
  "merchant_id": "AA12345678",
  "token": "testtokentesttokentesttokentesttokentesttoken",
  "time": 1656272222
}

ตัวอย่างด้านล่างแสดงการคำนวณ Signature ใน PHP และ Node.js

<?php

$data = [
  "merchant_id" => "AA12345678",
  "token" => "testtokentesttokentesttokentesttokentesttoken",
  "time" => "1656272222"
];
$post_data = json_encode($data);
$secret_key = '1234567890abcdef1234567890abcdef1234567890abcdef';
$signature = hash_hmac("SHA256", $post_data, $secret_key);
echo $signature . "\n";

js:

const crypto = require('crypto');

const data = {
  merchant_id: "AA12345678",
  token: "testtokentesttokentesttokentesttokentesttoken",
  time: "1656272222"
};

const postData = JSON.stringify(data);
const secretKey = '1234567890abcdef1234567890abcdef1234567890abcdef';

const signature = crypto.createHmac('sha256', secretKey)
                    .update(postData)
                    .digest('hex');

console.log(signature);