Last active
October 13, 2023 05:20
-
-
Save d-sea/d0b412c76977b161715adc568111e4f9 to your computer and use it in GitHub Desktop.
firebase functions Android レシートデータを検証してlatest_receiptを返す
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 PACKAGE_NAME_ANDROID = "(PUT YOUR APP PACKAGE NAME. ex. com.example.your.app)"; | |
const RECEIPT_VERIFICATION_ENDPOINT_FRO_ANDROID = "https://androidpublisher.googleapis.com/androidpublisher/v3/applications/"; | |
const SERVICE_CLIENT_EMAIL_FOR_ANDROID = "(PUT YOUR CLIENT EMAIL)"; | |
const SERVICE_PRIVATE_KEY_FOR_ANDROID = "(PUT YOUR PRIVATE KEY)" | |
// Android検証用のAuthClient | |
const authClient = new google.auth.JWT({ | |
email: SERVICE_CLIENT_EMAIL_FOR_ANDROID, | |
key: SERVICE_PRIVATE_KEY_FOR_ANDROID, | |
scopes: ["https://www.googleapis.com/auth/androidpublisher"], | |
}); | |
async function verifyReceiptAndroid(verificationData, auth) { | |
try { | |
const decodedReceipt = JSON.parse(verificationData); | |
// サブスク商品であることを確認する | |
if (!decodedReceipt["autoRenewing"]) { | |
return null; | |
} | |
const credential = await authClient.authorize(); | |
const url = | |
RECEIPT_VERIFICATION_ENDPOINT_FRO_ANDROID + | |
PACKAGE_NAME_ANDROID + | |
"/purchases/subscriptions/" + | |
decodedReceipt["productId"] + | |
"/tokens/" + | |
decodedReceipt["purchaseToken"] + | |
"?access_token=" + | |
credential.access_token; | |
const response = await axios.get(url); | |
const verifiedData = response.data; | |
if (!verifiedData) { | |
return null; | |
} | |
return verifiedData; | |
} catch (err) { | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment