Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Firebase云功能和Firestore在android上验证购买_Android_Firebase_Firebase Realtime Database_Google Cloud Firestore_Google Cloud Functions - Fatal编程技术网

使用Firebase云功能和Firestore在android上验证购买

使用Firebase云功能和Firestore在android上验证购买,android,firebase,firebase-realtime-database,google-cloud-firestore,google-cloud-functions,Android,Firebase,Firebase Realtime Database,Google Cloud Firestore,Google Cloud Functions,我一直在开发一个功能来验证android设备上的购买情况。 从这里和那里,我得到了使用Firebase实时数据库的代码,我不想使用: const functions = require('firebase-functions'); const admin = require('firebase-admin'); const {google} = require("googleapis"); const publisher = google.androidpublisher('v2'); cons

我一直在开发一个功能来验证android设备上的购买情况。 从这里和那里,我得到了使用Firebase实时数据库的代码,我不想使用:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const {google} = require("googleapis");
const publisher = google.androidpublisher('v2');
const authClient = new google.auth.JWT({
    email: 'Service Account Email',
    key: '-----BEGIN PRIVATE KEY-----\n**********************************************************************************==\n-----END PRIVATE KEY-----\n',
    scopes: ['https://www.googleapis.com/auth/androidpublisher']
});
admin.initializeApp();

exports.validatePurchases = functions.database
    .ref('/purchases/{uId}/{orderId}')
    .onCreate((event, context) => {
        const purchase = event.val();
        if (purchase.is_processed === true) {
            console.log('Purchase already processed!, exiting');
            return null;
        }
        const orderId = context.params.orderId;
        const dbRoot = event.ref.root;
        const package_name = purchase.package_name;
        const sku = purchase.sku;
        const my_token = purchase.token;

        authClient.authorize((err, result) => {
            if (err) {
                console.log(err);
            }
            publisher.purchases.products.get({
                auth: authClient,
                packageName: package_name,
                productId: sku,
                token: my_token
            }, (err, response) => {
                if (err) {
                    console.log(err);
                }
                // Result Status must be equals to 200 so that the purchase is valid
                if (response.status === 200) {
                    return event.ref.child('is_validated').set(true);
                } else {
                    return event.ref.child('is_validated').set(false);
                }
            });
        });
        return null;
    });

如何将his与功能一起转换为Firebase firestore,以及如何从android设备调用它?

有几种可能的方法:

1/从您的应用程序中,调用与Firestore交互的云功能

如中所述,您可以从应用程序调用云函数:

Firebase客户端SDK的云函数允许您调用函数 直接从Firebase应用程序。在此应用程序中从应用程序调用函数 在云函数中编写并部署HTTPS可调用函数, 然后添加客户端逻辑以从应用程序调用函数

在这个HTTPS可调用函数中,您可以按照您希望的方式(读/写/修改/删除)与Firestore数据库交互,并将一些数据返回到您的应用程序

for Firestore和Cloud函数显示了如何在云函数中写入和读取Firestore。还有一个官方示例演示了如何通过HTTP云函数与Firestore交互(不完全是HTTPS可调用函数,但非常类似)

您可以按如下方式调用HTTPS可调用函数(摘自上面引用的内容):

addMessage(inputMessage)
.addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
如果(!task.issusccessful()){
异常e=task.getException();
if(FirebaseFunctionsException的实例){
FirebaseFunctionsException ffe=(FirebaseFunctionsException)e;
FirebaseFunctionsException.Code=ffe.getCode();
对象详细信息=ffe.getDetails();
}
// ...
}
// ...
}
});
2/根据Firestore中的某些事件触发云函数,并侦听此云函数执行的更改

如Firestore和云功能的中所述:

您可以部署Node.js代码来处理由中的更改触发的事件 您的云Firestore数据库

Firestore支持创建、更新、, 删除,然后写入事件

然后,您将使用Android应用程序中的侦听器来检测Firestore中的更改,如前所述

在您的情况下,根据问题中包含的代码,您将在订单文档中写入一个值,这将触发对
authClient.authorize()
(类似于问题中的实时数据库代码)的调用,并在成功后更新/创建另一个您正在应用程序中侦听的文档


因此,使用这种方法,您不会直接调用函数

啊..谢谢..您为我提供了这么多有用的链接:)我是一个新手,您能否提供触发的云函数(更新firestore)来验证购买?我建议您尝试编写一些代码,如果遇到特定问题,请寻求帮助。毫无疑问,社区会很乐意帮助您更正代码。我会试试的。谢谢你,你刚刚复制粘贴了我的答案
addMessage(inputMessage)
        .addOnCompleteListener(new OnCompleteListener<String>() {
            @Override
            public void onComplete(@NonNull Task<String> task) {
                if (!task.isSuccessful()) {
                    Exception e = task.getException();
                    if (e instanceof FirebaseFunctionsException) {
                        FirebaseFunctionsException ffe = (FirebaseFunctionsException) e;
                        FirebaseFunctionsException.Code code = ffe.getCode();
                        Object details = ffe.getDetails();
                    }

                    // ...
                }

                // ...
            }
        });