Last active
August 16, 2023 19:27
-
-
Save paigeshin/25626a11651df9f242d7f364d315ad04 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
const jwt = require("jsonwebtoken"); | |
const fs = require("fs"); | |
const axios = require("axios"); | |
const privateKey = fs.readFileSync("./AuthKey.p8"); | |
// Production URL : https://api.storekit.itunes.apple.com/inApps/v1/transactions/{transactionId} | |
// Sandbox URL : https://api.storekit-sandbox.itunes.apple.com/inApps/v1/transactions/{transactionId} | |
async function getTransaction(token, ID) { | |
try { | |
const response = await axios.get( | |
`https://api.storekit-sandbox.itunes.apple.com/inApps/v1/transactions/${ID}`, | |
{ | |
headers: { | |
Authorization: `Bearer ${token}`, | |
}, | |
} | |
); | |
const signedTransaction = response.data; | |
return decode(signedTransaction.signedTransactionInfo); | |
} catch (error) { | |
console.error(error); | |
} | |
} | |
function decode(signedTransactionInfo) { | |
// Split the JWS transaction into its components | |
const [encodedHeader, encodedPayload, signature] = | |
signedTransactionInfo.split("."); | |
const header = base64UrlDecode(encodedHeader); | |
const payload = base64UrlDecode(encodedPayload); | |
console.log("Decoded Header: ", header); | |
console.log("Decoded Payload: ", payload); | |
return { header, payload }; | |
} | |
// Base64 URL decode the header and payload | |
function base64UrlDecode(str) { | |
const base64 = str.replace(/-/g, "+").replace(/_/g, "/"); | |
const json = Buffer.from(base64, "base64").toString("utf-8"); | |
return JSON.parse(json); | |
} | |
function generateToken(privateKey, keyID, issuerID, bundleID) { | |
const header = { | |
alg: "ES256", | |
kid: keyID, // Key ID | |
typ: "JWT", | |
}; | |
const currentTime = Math.floor(Date.now() / 1000); // Current UNIX time in seconds | |
const payload = { | |
iss: issuerID, // Issuer ID | |
iat: currentTime, | |
exp: currentTime + 3600, // Expires in 1 hour | |
aud: "appstoreconnect-v1", | |
bid: bundleID, // Bundle ID | |
}; | |
const token = jwt.sign(payload, privateKey, { | |
algorithm: "ES256", | |
header: header, | |
}); | |
console.log(token); | |
return token; | |
} | |
async function main() { | |
const token = generateToken( | |
privateKey, | |
"keyID", | |
"issuerID", | |
"BundleID" | |
); | |
const result = await getTransaction(token, 2000000222075857); | |
console.log({ result }); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment