Created
January 30, 2024 21:42
-
-
Save atakde/8937e1e39169a5c1767c4bc4d7e0485f 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 { onRequest } from 'firebase-functions/v2/https'; | |
import { google } from 'googleapis'; | |
import admin from 'firebase-admin'; | |
import { setGlobalOptions } from 'firebase-functions/v2'; | |
setGlobalOptions({ | |
maxInstances: 1 | |
}); | |
admin.initializeApp(); | |
const auth = new google.auth.GoogleAuth({ | |
keyFile: 'YOUR_KEY.json', | |
scopes: ['https://www.googleapis.com/auth/androidpublisher'], | |
}); | |
const androidpublisher = google.androidpublisher({ | |
version: 'v3', | |
auth, | |
}); | |
const handleValidation = onRequest(async (request, response) => { | |
const returnContent = { | |
isValid: false, | |
error: null, | |
}; | |
try { | |
const { packageName, subscriptionId, token } = request.body; | |
const subscriptionRes = await checkSubscription(packageName, subscriptionId, token); | |
if (subscriptionRes) { | |
const { startTimeMillis, expiryTimeMillis } = subscriptionRes; | |
const now = Date.now(); | |
const startTime = parseInt(startTimeMillis, 10); | |
const expiryTime = parseInt(expiryTimeMillis, 10); | |
const isValid = now >= startTime && now < expiryTime; | |
if (isValid) { | |
returnContent.isValid = true; | |
} | |
} | |
} catch (error) { | |
returnContent.error = error; | |
} | |
response.status(200).send(returnContent); | |
}); | |
const checkSubscription = async (packageName, subscriptionId, token) => { | |
try { | |
const res = await androidpublisher.purchases.subscriptions.get({ | |
packageName, | |
subscriptionId, | |
token, | |
}); | |
return res?.data; | |
} catch (error) { | |
console.log(error); | |
} | |
return null; | |
}; | |
export default handleValidation; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment