첫 번째 단계는 요청의 절대 URL을 가져오고 도메인 이름 부분을 제거하여 참여 서명된 URL을 얻는 것입니다. 요청에 쿼리 매개변수가 있는 경우 URL에 '? '와 해당 쿼리 문자열을 추가해야 합니다.
/v1/transaction/query
두 번째 단계는 요청이 시작될 때 시스템의 현재 타임스탬프(밀리초)를 가져오는 것입니다. 즉, 1970년 1월 1일 00:00:00 GMT부터 현재까지의 총 초를 요청 타임스탬프로 사용합니다. 플랫폼은 오래전에 이루어진 요청의 처리를 거부하므로 가맹점 자체 시스템의 시간을 정확하게 유지하십시오.
세 번째 단계는 32비트 무작위 문자열 (random string)을 생성하는 것입니다.
네 번째 단계는 요청(request body)에서 요청 메시지 본문을 가져오는 것입니다.
다섯 번째 단계는 다음과 같이 이전 규칙에 따라 요청 서명 문자열을 구성하는 것입니다.
암호화 (Encryption)
자바스크립트 암호화 과정을 예로 들어 보겠습니다:
암호화 후 예시:
연결 (Concatenate)
서명 정보의 연결 형식은 다음과 같습니다.
app_id=APP_ID, mch_id=가맹점 ID, nonce_str=3단계에서 생성된 무작위 문자열, timestamp=2단계에서 생성된 타임스탬프, signature=암호화된 서명 문자열.
HTTP 헤더 (HTTP header)
문서 API는 HTTP Authorization 헤더를 통해 서명을 전달합니다. Authorization은 인증 유형과 서명 정보의 두 부분으로 구성됩니다. 현재 인증 유형은 TTPAY-AES-256-ECB만 지원합니다.
Authorization 헤더는 다음과 같습니다. (참고: 조판상 줄바꿈이 포함될 수 있으므로 이는 예시이며 실제 데이터는 한 줄에 있어야 합니다.)
let cryptoJs = require("crypto-js");
let jsonObj = JSON.parse(pm.request.body.raw); //Convert the submitted body string into an object
let jsonStr = JSON.stringify(jsonObj)//Turn the object back into a string to compress, avoiding line breaks in the body that affect signature compression.
let appID = jsonObj.app_id;
let mchID = jsonObj.mch_id;
// Generate the current timestamp
let timestamp = Math.round(new Date() / 1000).toString()
//Define random string
var nonceStr = randomString(); // Random string
function randomString(e) {
e = e || 32;
var t = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678",
a = t.length,
n = "";
for (i = 0; i < e; i++) n += t.charAt(Math.floor(Math.random() * a));
return n
}
//Get the full path
var reqURL = "/" + pm.request.url.path.join("/");
//Splice signature plaintext
message = reqURL + "\n" + timestamp + "\n" + nonceStr + "\n" + jsonStr
let key = CryptoJS.enc.Utf8.parse("********************************") // AppSecret
//AES encryption
let ciphertext = cryptoJs.AES.encrypt(message, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7,
}).toString();
//Splice on the authorization of Header, TTPAY-{EncryMode)},currently supports only AES-256-ECB
let authorization = 'TTPAY-AES-256-ECB app_id=' + appID + ',mch_id=' + mchID + ',nonce_str=' + nonceStr + ',timestamp=' + timestamp + ',signature=' + ciphertext
pm.request.headers.upsert({ key: "authorization", value: authorization })