In-App billing 连接到 Firebase 并获取产品

In-App billing connect to Firebase and get Products

我开始创建一个可以从中购买物品的应用程序。 可能是这样的:,您可以在其中看到 ImageName of productPrice。好吧,我已经有一个用 Authentication Firebase 制作的 Sign-in with Google 并将其存储在 Firebase database 上,我想创建以下 @Alex Mamo 向我推荐的结构:

Firebase-root
    |
    --- users
    |     |
    |     --- uid1
    |          |
    |          --- //user details (name, age, address, email and so on)
    |          |
    |          --- products
    |                |
    |                --- productId1 : true
    |                |
    |                --- productId2 : true
    |
    --- products
    |     |
    |     --- productId1
    |     |     |
    |     |     --- productName: "Apples"
    |     |     |
    |     |     --- price: 11
    |           |
    |           |
    |           --- users
    |                |
    |                --- uid1: true
    |                |
    |                --- uid2: true
    |
    --- purchasedProducts
         |      |
         |      --- uid1
         |           |
         |           --- productId1: true
         |           |
         |           --- productId2: true
         |
         --- paidProducts
         |      |
         |      --- uid2
         |           |
         |           --- productId3: true
         |
         --- availableProducts
         |      |
         |      --- uid3
         |           |
         |           --- productId4: true

制作像我这样简单的应用程序看起来不错。 因为场景很简单:

从一开始我就将 uid 存储在 users 中。 我不知道的是;

  1. 我必须在 Google Play ConsoleFirebase database 上创建相同的产品吗?我已经创建了 1 个产品来在 Google Play Console 上进行测试,我还必须在我的应用程序上创建产品(我的意思是在 Firebase database 上)?

为了实现 in-app billing 我正在关注这个 tutorial but I've found this android-inapp-billing-v3 库,不过看起来不错。

如果你们中有人曾使用 in-app-purchased 并创建了项目并知道如何从 Google Play Console 获取它们,请随时分享如何操作的演示。

编辑

我已经知道如何购买我最终使用这个 Library 的物品,问题是在我的 Login 页面上我有 firebase 的数据库创建我的意思是我把 id用户和电子邮件...我是否也必须在那里添加所有产品?

应用内结算API 跟踪购买,但管理用户库存是开发人员的工作。您可以使用 getPurchases 获取已购买的商品,因此您无需将它们保存到数据库中,但免费产品并非如此,因为您无法将它们添加到应用内结算产品列表中,所以您需要将它们保存到 Firebase。 .最后关于适配器,如果您查询的内容很简单,则使用 FirebaseRecycleradapter,否则使用 Recycleradapter

我认为您应该在 Firebase 数据库中拥有 完整 产品列表(免费和付费)。对于付费的,您还应该在产品子树中保存 Google Play 控制台提供的 product_id,这样您就可以在您的应用中轻松检索它。

如果用户获得免费产品:您只需更新Firebase数据库中的用户子树,将productId添加到用户的产品列表中。

如果用户想要获得付费产品:您需要完成所有应用内支付购买流程。

首先,您从 Firebase 数据库中获取 product_id 并使用它启动进程。

购买成功后,您应该使用账单API提供的令牌检查购买的有效性。对于此操作,您需要使用后端服务器并使用购买令牌调用 Google Billing APIs

完成这些操作后,您可以安全地将 product_id 添加到 Firebase 数据库中用户拥有的产品列表中,并在应用程序中显示完成对话框。

为了实现购买流程,我遵循了Google提供的示例项目,并获得了一个工作验证流程。 LINK

我将对您计划的数据库结构进行一些更改,希望能稍微简化一些。

我们将有两种类型的保存数据:

第一种

是您将添加并手动准备的节点数据,我们将其称为"Products"。在此产品节点中,您将添加从 google 游戏控制台获得的产品 ID,然后为其提供一些类似的详细信息:

    Products
    |
    |--------Wood_id
    |         | 
    |         |--name : "wood"
    |          --price: "10"
    |          -- ........ 
    |
    |---------Iron_id
              | 
              |--name : "iron"
               --price: "20"
               -- ........

好的,现在让我们定义上图:

Wood_id 或 Iron_id :代表您在 google 游戏控制台中创建产品时使用的确切 ID。 ((您在数据库中手动输入它及其详细信息))

名称:代表要在回收站视图中显示的项目的名称((也可以手动输入所有这些详细信息))。

价格:积分或您决定的任何东西。

所以这些是您在 firebase 控制台的数据库树中手动输入的内容。

在回收站视图中填充时,它看起来像这样

        |---------------|
        |     wood      |
        ------------------
        |     iron      |
        |               |
        -----------------

您可以使用 Firebase UI 实现此目的((这将使您更容易检索节点并在 RecyclerView 中查看它们))。

好的,到现在为止,我们可以向用户展示产品并在产品列表中查看它们((每个项目都带有它和与 google 游戏机上的 ID 相同的 ID))

第二种

是我们将从应用程序添加的数据类型,即(用户)节点和(购买的项目)节点。

用户节点是这样的

     Users
     |
     |--------user1
     |         | 
     |         |--name : "..."
     |          --e-mail: "..."
     |          -- ........ 
     |
     |---------user2
               | 
               |--name : "..."
                --e-mail: "..."
                -- ........

好的,这是一个由您来添加的节点,通常在创建用户((注册)之后出现),但您似乎已经使用 google 登录完成了该操作。

所以这个节点不是很重要,但是如果你想的话可以添加。

现在看看我们将要做什么。我们将让用户点击一个项目,然后我们将检查该项目是否存在于他的列表中,如果不存在,我们将向他显示付款意向。 he/she 购买商品后,我们将像这样将其添加到(已购买商品)节点中:

             Purchased items
             |
             |--------user1
             |         | 
             |         |--Wood_id : "any_value"
             |          --Iron_id:  "any_value"
             |         
             |
             |---------user2
                       | 
                       |--Iron_id : "any_value"

上面的树表明用户 1 购买了(木头和铁),而用户 2 只购买了(铁)。

当用户 2 例如点击木材时,我们将获取与木材关联的产品 ID,即 (Wood_id),我们将 运行 使用 firebase 的值事件侦听器,我们将检查是否user2 是否有 (Wood_id) ,在我们的例子中 user2 没有木头。因此,如果您想要您提到的计费库并通过 (Wood_id) 从 google 播放控制台购买,您可以在这里使用,并在您提到的这个库的侦听器中检查付款是否成功(在这里你为 user2 添加 Wood_id in "Purchased items" 节点,这样下次 user2 单击 firebase 时将 运行 一个监听器并且不会再次显示付款)。

这就是您使用想要使用的库的方式。

更新

如何在回收站视图中检索数据库(产品)。

首先添加最新的firebase数据库和firebaseui

   compile 'com.google.firebase: firebase-database: 11.4.2'
   compile 'com.firebaseui: firebase-ui-database: 3.1.0'

之后你需要创建一个 java class 在里面调用它 (ProductsClass) 添加这些:

    public class ProductsClass{

     //these should be the same name as keys in your database
     public String name;
     public String product_id;
     public String state;

      //empty constructor
     public  ProductsClass(){
     }

     public  ProductsClass(String name, String product_id, String state){
       this.name=name; 
       this.product_id=product_id;
       this.state=state;


     }

      public String getName(){
         return name;

         }

        public void setName(){
         this.name=name;

         }

         public String getProduct_id(){
         return product_id;

         }

        public void setProduct_id(){
         this.product_id=product_id;

         }



         public String getState(){
         return state;

         }

         public void setState(){
         this.sate=state;

         }



    } 

现在在您的(产品 activity)中

         private Recyclerview products_recycler;
         private FirebaseAuth mauth;
         private DatabaseReference products_ref;
         private String currentuser;

         //in on create

            products_recycler=find it by id......
            mauth=FirebaseAuth.getInstance();
            currentuser=mauth.getCurrentUser().getUid();

            products_ref=FirebaseDatabase.getInstance().getReference().child("products");


        //manger
         products_recycler.setHasFixedSize(true);
         products_recycler.setLayoutManager(new LinearLayoutManager(this));


           //in onstart

          FirebaseRecyclerOptions< ProductsClass > options =
            new FirebaseRecyclerOptions.Builder< ProductsClass >()
                    .setQuery(products_ref, ProductsClass.class)
                    .build();

           FirebaseRecyclerAdapter adapter = new   FirebaseRecyclerAdapter< ProductsClass, ProductsHolder>(options) {
@Override
public ProductsHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    //inflate the single recycler view layout(item)

    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.productslayout, parent, false);

    return new ProductsHolder(view);
}

    @Override
    protected void onBindViewHolder(ProductsHolder holder, int position, ProductsClass model) {

      //here you set stuff in items , get data, ....

      //model is your class model (you get data from it) 





     }
   };


    // now  set adapter
       adapter.startListening();
       products_recycler.setAdapter(adapter);




      //make a static class called ProductsHolder in the same activity

      public static class ProductsHolder extends RecyclerView.ViewHolder{


        View view;

         public ProductsHolder(View itemView){

             view= itemView;
          }

          //what ever methods you need to add

       }

注意:R.layout.productslayout 是回收站视图中每个项目的自定义布局。

通过这种方式,您可以在任何 activity 中检索任何您想要的节点。

参考 link : this link