应用内结算连接到Firebase并获取产品

Ski*_*ᴉʞS 6 android in-app-purchase in-app-billing firebase firebase-realtime-database

我开始创建一个可以从中购买商品的APP。可能是这样的:图片]在这里你可以看到ImageName of productPrice。嗯,我有媒体链接一个Sign-in with Google用做Authentication Firebase和我存储它Firebase database,我想创建下面这个结构@Alex马莫向我建议:

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
Run Code Online (Sandbox Code Playgroud)

做一个像我这样的简单应用看起来不错。因为场景很简单:

  • APP中的用户登录
  • 用户看到免费产品,可以购买一些产品
  • 用户可以看到已经付款的产品

从一开始我就把uid里面的东西存放了users。我不知道的是

  1. 我一定要上创建相同的产品Google Play ConsoleFirebase database?我已经创建了1个产品对其进行测试Google Play Console,也必须在我的应用程序中也创建该产品(我的意思是Firebase database)?

为了实现in-app billing教程,我遵循了本教程,但是我找到了这个android-inapp-billing-v3库,尽管看起来不错。

如果有人与您合作in-app-purchased并创建了商品,并且知道如何获取它们,Google Play Console请随时分享如何演示。

编辑

我已经准备好如何购买最终使用此Library的商品,问题是在我的Login页面上,我有数据库创建的firebase,我的意思是我放置了用户ID和电子邮件...我是否必须添加所有产品还有吗?

svi*_*ata 5

我将对数据库结构计划中的内容进行一些更改,以期简化一下。

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

第一类

是节点数据,您将添加它并手动进行准备,我们将其称为“产品”。在这个产品节点中,您将添加从Google Play控制台获取的产品ID,然后提供一些类似的详细信息:

    Products
    |
    |--------Wood_id
    |         | 
    |         |--name : "wood"
    |          --price: "10"
    |          -- ........ 
    |
    |---------Iron_id
              | 
              |--name : "iron"
               --price: "20"
               -- ........
Run Code Online (Sandbox Code Playgroud)

现在让我们定义上面的图:

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

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

价格:点数或您决定的价格。

因此,这些是您在Firebase控制台的数据库树中手动键入的内容。

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

        |---------------|
        |     wood      |
        ------------------
        |     iron      |
        |               |
        -----------------
Run Code Online (Sandbox Code Playgroud)

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

好的,到现在为止,我们可以显示用户产品并在产品列表中查看它们((每个产品都带有它,并且ID等于Google Play控制台上的ID))

第二类

是我们将从应用程序中添加的数据类型,它们是(用户)节点和(已购买的物品)节点。

用户节点是这样的

     Users
     |
     |--------user1
     |         | 
     |         |--name : "..."
     |          --e-mail: "..."
     |          -- ........ 
     |
     |---------user2
               | 
               |--name : "..."
                --e-mail: "..."
                -- ........
Run Code Online (Sandbox Code Playgroud)

okay this is a node that is up to you to add and usually comes after a user is created ((Registration thing)), but you seem to have accomplished that using google sign-in.

So this node is not very important, but you can add it if you want.

And now look what we will do. We will let a user click on an item and then we will check if this item exists in his list if not we will show him a payment intent. After he/she purchased the item we will add it in the (Purchased items) node like that:

             Purchased items
             |
             |--------user1
             |         | 
             |         |--Wood_id : "any_value"
             |          --Iron_id:  "any_value"
             |         
             |
             |---------user2
                       | 
                       |--Iron_id : "any_value"
Run Code Online (Sandbox Code Playgroud)

The above tree indicates that user1 purchased (wood and iron) while user 2 purchased only (iron).

when user2 for example clicks on wood, we will grab the product id associated with wood which is (Wood_id) and we will run a value event listener using firebase and we will check if user2 has the (Wood_id) or not, in our case user2 doesn't have wood. So here you can use if you want the billing library that you mentioned and pass the (Wood_id) to buy from google play console, and in the listeners of this library that you mentioned you check for payment success (and here you add for user2 the Wood_id in "Purchased items" node so that next time user2 clicks firebase will run a listener and won't show a payment again).

This is how you would go about it using the library that you want to use.

UPDATE

How to retrieve in recycler view the database (Products).

first add the latest firebase database and firebase ui

   compile 'com.google.firebase: firebase-database: 11.4.2'
   compile 'com.firebaseui: firebase-ui-database: 3.1.0'
Run Code Online (Sandbox Code Playgroud)

after that you need a to create a java class call it (ProductsClass) inside it add these:

    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;

         }



    } 
Run Code Online (Sandbox Code Playgroud)

now inside your (Products 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

       }
Run Code Online (Sandbox Code Playgroud)

note: R.layout.productslayout is custom layout for each item in recycler view .

this way you retrieve in any activity any node you want.

reference link : this link