Last active
July 31, 2020 12:26
-
-
Save terrierscript/85b270e69aec4ae2fb1e5648fde29ce1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import CryptoJS from 'crypto-js'; | |
import {v4 as uuidv4 } from "uuid" | |
// https://github.com/paypay/paypayopa-sdk-node/blob/646a420f6e61f65cfd4855f355d21de95af5fef8/src/lib/paypay-rest-sdk.ts#L31 | |
// ↑ここから引っ張ってきてとりあえず動くっぽい感じにしたやつ | |
export const createAuthHeader = (method: string, resourceUrl: string, payLoad: any, auth: any) => { | |
let AUTH_TYPE = "hmac OPA-Auth"; | |
const epoch = Math.ceil(Date.now() / 1000); // ミリ秒を削る必要アリ | |
let contentType = 'application/json'; | |
const nonce = uuidv4().slice(0,8); | |
let bodyHash = ""; | |
if (method == "GET" || !payLoad) { | |
payLoad = ""; | |
bodyHash = "empty"; | |
contentType = "empty"; | |
console.log(`coming inside GET`); | |
} else { | |
let md5 = CryptoJS.algo.MD5.create(); | |
md5.update(contentType); | |
md5.update(JSON.stringify(payLoad)); // JSON stringifyする必要アリ | |
bodyHash = md5 | |
.finalize() | |
.toString(CryptoJS.enc.Base64) | |
.toString("utf-8"); | |
} | |
let pathfinder = resourceUrl.split("?"); | |
resourceUrl = pathfinder[0]; | |
let signatureRawData = [resourceUrl, method, nonce, epoch, contentType, bodyHash].join("\n"); | |
let hash = CryptoJS.HmacSHA256(signatureRawData, auth[1]); | |
let hashInBase64 = encodeURI(CryptoJS.enc.Base64.stringify(hash)); | |
let header = [auth[0], hashInBase64, nonce, epoch, bodyHash].join(":"); | |
return AUTH_TYPE + ":" + header; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const API_KEY = process.env.API_KEY | |
const API_SECRET = process.env.API_SECRET | |
const payload = { | |
merchantPaymentId: 'my_payment_id', | |
amount: { | |
amount: 1, | |
currency: 'JPY', | |
}, | |
codeType: 'ORDER_QR', | |
orderDescription: "Mune's Favourite Cake", | |
isAuthorization: false, | |
redirectUrl: 'https://paypay.ne.jp/', | |
redirectType: 'WEB_LINK', | |
userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.75 Mobile/14E5239e Safari/602.1' | |
} | |
const apiBase = "https://stg-api.paypay.ne.jp" | |
const createQrCode = "/v2/codes" | |
const auth = [ | |
API_KEY, | |
API_SECRET, | |
] | |
const header = createAuthHeader("POST", createQrCode, payload,auth) | |
axios.post(`${apiBase}${createQrCode}`,payload, { | |
headers: { | |
"Authorization":`${header}`, | |
'Content-Type': 'application/json' // content type指定 | |
} | |
}).catch(e => { | |
console.log(e.response.data) | |
}).then(r => { | |
console.log(r) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment