Skip to content

Instantly share code, notes, and snippets.

@d-sea
Last active October 19, 2023 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d-sea/cd2fbf48d48b032c74135c5a5fa43de1 to your computer and use it in GitHub Desktop.
Save d-sea/cd2fbf48d48b032c74135c5a5fa43de1 to your computer and use it in GitHub Desktop.
firebase functions iOS サブスク更新をチェックして最新レシートをFirestoreに保存する
exports.verifySubscriptionIos = functions.pubsub.schedule('0 1 * * 0').timeZone('Asia/Tokyo').onRun(async (context) => {
// 該当レシートを検索
const now = Date.now();
const termTime = 24 * 60 * 60 * 1000; // 24時間間隔でチェックするため
const minTime = now - termTime;
const receiptsSnapshot = await admin.firestore().collectionGroup("receipts")
.where("expires_date_ms", "<=", now)
.where("expires_date_ms", ">", minTime)
.orderBy("expires_date_ms")
.orderBy("transaction_id")
.get();
if (receiptsSnapshot.empty) {
console.log('result: DOCUMENT_NOT_FOUND');
return { result: DOCUMENT_NOT_FOUND };
}
console.log('Find target receipts.');
receiptsSnapshot.forEach(async doc => {
// receipt_data を使って検証する
const verificationData = doc.data().receipt_data;
const latestReceipt = await verifyReceiptIos(verificationData, null);
if (latestReceipt === null || latestReceipt === undefined) {
console.log('result: INVALID_RECEIPT');
return { result: INVALID_RECEIPT };
}
// 期限内であることを確認する
const expireDate = Number(latestReceipt["expires_date_ms"]);
if (now < expireDate) {
// Firestoreに保存する
const userRef = doc.ref.parent.parent;
const receiptRef = userRef.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(),
});
console.log('firestore set document, SUCCESS.');
return { result: SUCCESS };
} else {
console.log('Last receipt is expired, maybe stopped subscription.');
return { result: EXPIRED };
}
});
// DONE
console.log("DONE verifySubscriptionIos.");
return null;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment