Created
October 11, 2023 07:40
-
-
Save d-sea/54e2f30b7d901b58aa1392a234201b21 to your computer and use it in GitHub Desktop.
firebase functions verifyIos iOS 購入を検証してFirestoreに保存する
This file contains hidden or 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
exports.verifyIos = functions.https.onCall(async (data, context) => { | |
if (context.auth === null) { | |
return { result: NO_AUTH }; | |
} | |
const uid = context.auth.uid; | |
const verificationData = data["data"]; | |
const latestReceipt = await verifyReceiptIos( | |
verificationData, | |
context.auth | |
); | |
if (latestReceipt === null || latestReceipt === undefined) { | |
return { result: INVALID_RECEIPT }; | |
} | |
// 同じTransactionIdが存在するか確認し、購入情報が既に登録済みでないか確認する | |
const queryId = await admin.firestore().collectionGroup("receipts") | |
.where("transaction_id", "==", latestReceipt["transaction_id"]) | |
.get(); | |
if (!queryId.empty) { | |
return { result: ALREADY_EXIST }; | |
} | |
// Firestoreに保存する | |
const receiptRef = admin.firestore() | |
.collection('users').doc(uid).collection('receipts').doc(); | |
const result = await receiptRef.set({ | |
receipt_data : verificationData, | |
product_id : latestReceipt["product_id"], | |
transaction_id : latestReceipt["transaction_id"], | |
purchase_date_ms : parseInt(latestReceipt["purchase_date_ms"]), | |
purchase_date : latestReceipt["purchase_date"], | |
expires_date_ms : parseInt(latestReceipt["expires_date_ms"]), | |
expires_date : latestReceipt["expires_date"], | |
created_at: Firestore.FieldValue.serverTimestamp(), | |
}); | |
// 期限内であることを確認する | |
const now = Date.now(); | |
const expireDate = Number(latestReceipt["expires_date_ms"]); | |
if (now < expireDate) { | |
return { result: SUCCESS }; | |
} else { | |
return { result: EXPIRED }; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment