Created
October 24, 2023 14:31
-
-
Save paigeshin/425c568466360f5ce55cb87ad1876ab9 to your computer and use it in GitHub Desktop.
gc_function_get_server_notification_and_parse_data.js
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 functions = require("firebase-functions"); | |
const admin = require("firebase-admin"); | |
admin.initializeApp(); | |
exports.v2 = functions.https.onRequest(async (req, res) => { | |
// Ensure the request method is POST | |
if (req.method !== "POST") { | |
return res.status(400).send("Only POST requests are accepted"); | |
} | |
// Save data to Firestore | |
try { | |
// Extract data from POST request body | |
const data = req.body; | |
if (!data.signedPayload) { | |
res.status(500).send("Necessary Data is not provided"); | |
return; | |
} | |
await admin | |
.firestore() | |
.collection("ios-iap-server-notifications") | |
.add(data); | |
const transaction = generateTransaction(data.signedPayload); | |
if (!transaction) { | |
res.status(500).send("Failed to generate transaction"); | |
return; | |
} | |
await admin.firestore().collection("ios-iap-transactions").add(transaction); | |
res.status(200).send("Success!"); | |
} catch (error) { | |
console.error("Error saving data:", error); | |
res.status(500).send("Failed to save data"); | |
} | |
}); | |
function generateTransaction(signedPayload) { | |
const decodedNotification = decode(signedPayload).payload; | |
if (!decodedNotification) { | |
return null; | |
} | |
const decodedSignedTransactionInfo = decode( | |
decodedNotification.data.signedTransactionInfo | |
).payload; | |
if (!decodedSignedTransactionInfo) { | |
return null; | |
} | |
const decodedSignedRenewalInfo = decode( | |
decodedNotification.data.signedRenewalInfo | |
).payload; | |
if (!decodedSignedRenewalInfo) { | |
return null; | |
} | |
return { | |
notification: { | |
signedPayload: signedPayload, | |
data: decodedNotification.data, | |
}, | |
transactionInfo: { | |
signedTransactionInfo: decodedNotification.data.signedTransactionInfo, | |
data: decodedSignedTransactionInfo, | |
}, | |
renewalInfo: { | |
signedRenewalInfo: decodedNotification.data.signedRenewalInfo, | |
data: decodedSignedRenewalInfo, | |
}, | |
}; | |
} | |
function decode(signedPayload) { | |
// Split the JWS transaction into its components | |
const [encodedHeader, encodedPayload, signature] = signedPayload.split("."); | |
const header = base64UrlDecode(encodedHeader); | |
const payload = base64UrlDecode(encodedPayload); | |
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment