I've created a function to check the in app payments of the google play and execute some actions if everything is ok.
this i the functions:
exports.validatePurchases = functions.firestore
.document('users/{userID}/purchasesRemoveAds/{documentID}')
.onCreate(async (snap, context) => {
const userID = context.params.userID;
const purchase = snap.data();
if (!purchase) {
return null
} else {
if (purchase.is_processed === true) {
console.log('Purchase already processed!, exiting');
return null;
}
// const orderId = context.params.orderId;
// const dbRoot = event.ref.root;
const packageName = purchase.packageName;
const productId = purchase.productId;
const purchaseToken = purchase.purchaseToken;
authClient.authorize()
// authClient.authorize() returns a credentials Object
.then(credentials => {
console.log(credentials, productId, purchaseToken);
return playDeveloperApiClient.purchases.products.get({
auth: authClient,
packageName: packageName,
productId: productId,
token: purchaseToken
});
})
// publisher.purchases.products.get() Returns a axiosResponse object with Purchase data within and the status that should be enough for the validation
.then((axiosResponse) => {
console.log(`Status Code: ${axiosResponse.status} Purchase state: ${ axiosResponse.data.purchaseState} ${typeof axiosResponse.status} ${typeof axiosResponse.data.purchaseState}`);
if (axiosResponse.status === 200 && axiosResponse.data.purchaseState === 0) {
console.log('ok here');
// Your purchase is valid, do your thing
return changeShowAdsFalse(userID);
} else {
console.log(typeof axiosResponse.status);
}
return null;
})
.catch(reason => {
console.log(`Rejection Code: ${reason.code}`);
console.log(`Rejection Message: ${reason.message}`);
return null;
});
return null;
}
});
It's pretty simple, it listen on the firestore database, whene there is a new document in the directory it trigger the function.
then it gets the data from the document and it get the purchases with playDeveloperApiClient.purchases.products.get, then it check the new data with axiosResponse and get the purchase status.
After that, it should check for the status == 200 and the purchaseState == 0, thing that happen (and I see it because I log those value) but from there nothing more happen.
The if is not executed neither the else.
Another weird thing is that before the first log and the second log pass almost a minute or sometimes more.
I have a blaze plan on firebase and I don't have any credit card linked.
I don't know what is happening
the log of the cloud function is :
10:14:22.856 PM
validatePurchases
Billing account not configured. External network is not accessible and quotas are severely limited. Configure billing account to remove these restrictions
10:14:23.561 PM
validatePurchases
Function execution took 706 ms, finished with status: 'ok'
10:14:24.457 PM
validatePurchases
{ access_token: 'token.AO-token-token',
token_type: 'Bearer',
expiry_date: 1571433263000,
id_token: undefined,
refresh_token: 'jwt-placeholder' } 'remove_ads' 'token.AO-token-token'
10:14:27.457 PM
validatePurchases
Status Code: 200 Purchase state: 0 number number
truetofalsein the firestore db. It really does just that, but it never being called - alessandro buffoli