驗證方式
將傳送的內容進行簽章動作
API連結: https://tms-api.ktnet.com.tw/
範例: https://tms-api.ktnet.com.tw/Order/Add
範例: https://tms-api.ktnet.com.tw/Invoice/InvoiceAdd
可參考各功能頁的抬頭敘述
範例: https://tms-api.ktnet.com.tw/Invoice/InvoiceAdd
可參考各功能頁的抬頭敘述
Web Request Header
參數 | 名稱 | 說明 |
api-pno | 廠商代號 | 平台給予的廠商代號 |
api-Timestamp | 時間戳 | 取得當地時間的Timestamp |
api-api-ClientSign | 簽章 | 範例如下 |
C#範例
//步驟一. 簽章原始字串 = 廠商代號 + 時間戳 + 請求的body內容
string pno = "TMSA000XXX";//廠商代號
string timestamp = ((int)(DateTime.Now.ToLocalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds).ToString();//時間戳
string bodyData = "{ \"ProNum\":\"TMSA000XXX\",\"MessageType\":\"C0401\",\"Main\":{.....} }";//請求的body內容
string signSource = string.Format("{0}{1}{2}", pno, timestamp, bodyData);
string apiSignKey = "278A0AA4-1E95-45E3-8FE7-6E6E5834757A";//加密金鑰 請根據平台給予的填入
//步驟二.使用平台給予的金鑰進行加密(使用HMACSHA512),將加密結果轉大寫
string signEncrypt = HMACSHA512_String(signSource, apiSignKey).ToUpper();
//加密方法
public static string HMACSHA512_String(string input, string secretKey)
{
byte[] bKeys = Encoding.ASCII.GetBytes(secretKey);
HMACSHA512 msha1 = new HMACSHA512(bKeys);
var hash = msha1.ComputeHash(Encoding.UTF8.GetBytes(input));
return string.Join("", hash.Select(b => b.ToString("x2")).ToArray());
}
將WebRequest的請求Header加入內容
string apiUrl = "http://tms-api.ktnet.com.tw/Order/Add";//範例網址
HttpWebRequest hwrq = WebRequest.Create(apiUrl) as HttpWebRequest;;
hwrq.Method = "POST";
hwrq.ContentType = "application/json";
hwrq.Headers.Add("api-pno", pno);//廠商代號
hwrq.Headers.Add("api-Timestamp", timestamp);//時間戳
hwrq.Headers.Add("api-ClientSign", signEncrypt);//簽章加密字串
//設置請求bodyData的內容
byte[] bodyBytes = Encoding.UTF8.GetBytes(bodyData);
hwrq.ContentLength = bodyBytes.Length;
Stream poststream = hwrq.GetRequestStream();
poststream.Write(bodyBytes, 0, bodyBytes.Length);
poststream.Close();
using (hwrp = hwrq.GetResponse() as HttpWebResponse)
{
Stream stream = hwrp.GetResponseStream();
StreamReader streamreader = new StreamReader(stream, Encoding.UTF8);
string ResponseString = streamreader.ReadToEnd();//接收回傳結果
streamreader.Close();
stream.Close();
hwrp.Close();
}
hwrq.Abort();
PHP加密範例
//可以到 http://phptester.net/ 測試
<?php
echo hash_hmac('sha512', 'TMSDEMOWEB1509379717{"ProNum":"TMSDEMOWEB","Main":{"ODATE":"2017-10-27","OCOD4":"oc146"
,"OCCOD":"0","OSCOD":"","OBAK1":"請中午前送貨","OBAK2":"","OBAK3":""
,"OADD1":"Taiwan965932統一超商飛鳳門市 新竹縣芎林鄉富林路二段556號"
,"OADD2":"","OWAY":"oc146","OPAY":"oc綠界整合金流","OTONO":"99"
,"OTax":9,"ReceiveName":"Peiyao","ReceiveTEL":""
,"ReceiveMobile":"0927504321","ReceiveZIPCode":""
,"ReceiveAddr":"Taiwan965932統一超商飛鳳門市 新竹縣芎林鄉富林路二段556號"
,"ReceiveCTCOD":"","ReceiveComName":"","UseInvoice":"0"}
,"Details":[{"ICODE":"509001","INAME":"NIOU AMAZE 珍白淨肌潔顏慕絲"
,"Unit":"","Qty":"2","Price":"980.0000","TaxBA":"A"
,"Stock":"A001","Remark":""}
,{"ICODE":"508013","INAME":"JUST MOMENT 奇肌秒殺經典必敗組"
,"Unit":"","Qty":"2","Price":"5280.0000","TaxBA":"A"
,"Stock":"A001","Remark":""}
]}'
, '66E10461-249A-4A26-B846-180B2F4E3839');
結果
b77b9769757bb26d0031aac5b2b31957ef2fcced87108a853ea4fde89ae3364262ea7f5e80989b66f8e2cc5b6df5d18825f840d2ea906fd86cd9956e4d886a8b