云函数firebase无法正常工作

云函数firebase无法正常工作,firebase,google-cloud-firestore,google-cloud-functions,Firebase,Google Cloud Firestore,Google Cloud Functions,我已经创建了一个功能来检查GooglePlay的应用程序内支付,如果一切正常,则执行一些操作 这是我的职责: exports.validatePurchases = functions.firestore .document('users/{userID}/purchasesRemoveAds/{documentID}') .onCreate(async (snap, context) => { const userID = context.params.

我已经创建了一个功能来检查GooglePlay的应用程序内支付,如果一切正常,则执行一些操作

这是我的职责:

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;
        }
    });
这很简单,它监听firestore数据库,当目录中有新文档时,它触发函数。 然后它从文档中获取数据,并使用
playdeveloperrapiclient.purchases.products.get
获取购买信息,然后使用
axiosResponse
检查新数据并获取购买状态。 在那之后,它应该检查状态
==200
和purchaseState
==0
,这是发生的事情(我看到它是因为我记录了这些值),但从那以后就不会再发生任何事情了。 if和else都不执行。 另一件奇怪的事情是,在第一个日志和第二个日志经过几乎一分钟或更长时间之前

我在firebase有一个燃烧计划,我没有任何信用卡链接

我不知道发生了什么事


云功能的日志为:

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

在您的云功能中,有几点需要调整

首先,您需要配置您的账单帐户,如错误日志中所述。显然,你正在调用一个不被认为是谷歌所有的服务,因此你需要有一个“主动”火焰或火焰计划。请参阅(将鼠标悬停在“云功能”标题后面的问号上)


第二,你通过这样做来连锁不同的承诺

        authClient.authorize()
            .then(credentials => {
                //....
                return playDeveloperApiClient.purchases.products.get();
            })
            .then((axiosResponse) => {..})
            .catch(reason => {
                //....
                return null;
            });
但是您并没有返回链中的第一个承诺:您应该
返回authClient.authorize()


第三,平行于允诺链(或在其中间),你做<代码>返回null;<代码>

        authClient.authorize()
            .then(credentials => {...})
            .then((axiosResponse) => {..})
            .catch(reason => {...});

        return null;   //  <--- !!

什么是changeShowAdsFalse()?@RenaudTarnec是一个函数,它将firestore db中的值从
true
更改为
false
。它真的就是这样,但它从来没有被称为DOK,谢谢。我们可以看看它的代码吗:因为它是一个异步函数,包含在承诺链中,所以查看详细信息很重要。谢谢你的回复,你的函数工作得很好。这个问题与账单无关。
            .then((axiosResponse) => {
                if (axiosResponse.status === 200 && axiosResponse.data.purchaseState === 0) {
                    return changeShowAdsFalse(userID);
                } else {
                    console.log(typeof axiosResponse.status);
                    //Here you should do something, i.e. throw an error
                }
                return null;   //  <--- !!
            });
exports.validatePurchases = functions.firestore
    .document('users/{userID}/purchasesRemoveAds/{documentID}')
    .onCreate((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 packageName = purchase.packageName;
            const productId = purchase.productId;
            const purchaseToken = purchase.purchaseToken;

            return authClient.authorize()
                .then(credentials => {
                    return playDeveloperApiClient.purchases.products.get({
                        auth: authClient,
                        packageName: packageName,
                        productId: productId,
                        token: purchaseToken
                    });
                })
                .then((axiosResponse) => {
                    if (axiosResponse.status === 200 && axiosResponse.data.purchaseState === 0) {
                        return changeShowAdsFalse(userID);
                    } else {
                        console.log(typeof axiosResponse.status);
                        throw new Error("Wrong type: " + typeof axiosResponse.status);
                    }
                })
                .catch(reason => {
                    console.log(`Rejection Code: ${reason.code}`);
                    console.log(`Rejection Message: ${reason.message}`);
                    return null;
                });

        }

    });