应用内结算连接到 Firebase 并获取产品 [英] In-App billing connect to Firebase and get Products

查看:25
本文介绍了应用内结算连接到 Firebase 并获取产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开始创建一个 APP,我可以在其中购买商品.可能是这样的: 在那里你可以看到一个 图片产品名称价格.好吧,我已经有一个使用 Authentication Firebase 制作的 Sign-in with Google 并且我将它存储在 Firebase 数据库 上,我想创建以下是@Alex Mamo 向我推荐的结构:

Firebase-root|--- 用户|||--- uid1|||---//用户详细信息(姓名、年龄、地址、电子邮件等)|||- - 产品|||--- productId1 : 真|||--- productId2 : 真|- - 产品|||--- 产品 ID1|||||--- 产品名称:苹果"|||||--- 价格:11|||||--- 用户|||--- uid1: 真|||--- uid2: 真|--- 购买的产品|||--- uid1|||--- productId1: 真|||--- productId2: 真|--- 付费产品|||--- uid2|||--- productId3: 真|--- 可用产品|||--- uid3|||--- productId4: 真

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

  • 用户在APP中的日志
  • 用户看到免费产品,可以购买一些产品
  • 用户可以看到已经付款的产品<​​/li>

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

  1. 我是否必须在 Google Play Console 上创建与 Firebase 数据库 相同的产品?我已经创建了 1 个产品来在 Google Play Console 上对其进行测试,我还必须在我的应用上创建该产品(我的意思是在 Firebase 数据库 上)?

要实施应用内计费,我正在关注此教程 但我发现了这个 android-inapp-billing-v3 图书馆,不过看起来不错.

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

编辑

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

解决方案

我会稍微改变你对数据库结构的计划,希望能简化一点.

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

第一类

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

 产品||--------Wood_id||||--name : "木头"|--价格:10"|——…………||---------Iron_id||--name : "铁"--价格:20"——…………

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

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

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

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

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

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

 |------------------------------||木|------------------|铁|||-----------------

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

好的,到目前为止,我们能够显示用户产品并在产品列表中查看它们((并且每个项目都带有它,并且 ID 等于 google play 控制台上的 id))

第二种类型

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

users 节点是这样的

 用户||--------用户1||||--name : "..."|- 电子邮件: "..."|——…………||---------user2||--name : "..."- 电子邮件: "..."——…………

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

所以这个节点不是很重要,但你可以根据需要添加它.

现在看看我们要做什么.我们会让用户点击一个项目,然后我们将检查该项目是否存在于他的列表中,如果不存在,我们将向他显示付款意图.在他/她购买了商品后,我们会将其添加到 (Purchased items) 节点中,如下所示:

 购买的物品||--------用户1||||--Wood_id : "any_value"|--Iron_id: "any_value"|||---------user2||--Iron_id : "any_value"

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

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

这是您使用要使用的库的方式.

更新

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

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

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

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

 公共类 ProductsClass{//这些应该与数据库中的键同名公共字符串名称;public String product_id;公共字符串状态;//空构造函数公共产品类(){}公共产品类(字符串名称,字符串 product_id,字符串状态){this.name=name;this.product_id=product_id;this.state=state;}公共字符串getName(){返回名称;}公共无效集名称(){this.name=name;}公共字符串 getProduct_id(){返回product_id;}公共无效 setProduct_id(){this.product_id=product_id;}公共字符串 getState(){返回状态;}公共无效 setState(){this.sate=state;}}

现在在您的(产品活动)中

 private Recyclerview products_recycler;私有 FirebaseAuth 权限;私人数据库参考 products_ref;私人字符串当前用户;//在创建products_recycler=通过id查找......mauth=FirebaseAuth.getInstance();当前用户=mauth.getCurrentUser().getUid();products_ref=FirebaseDatabase.getInstance().getReference().child("products");//马槽products_recycler.setHasFixedSize(true);products_recycler.setLayoutManager(new LinearLayoutManager(this));//在启动FirebaseRecyclerOptions<产品类别 >选项 =新的 FirebaseRecyclerOptions.Builder<产品类别 >().setQuery(products_ref, ProductsClass.class).建造();FirebaseRecyclerAdapter 适配器 = 新 FirebaseRecyclerAdapter(选项){@覆盖public ProductsHolder onCreateViewHolder(ViewGroup parent, int viewType) {//膨胀单个回收器视图布局(项目)视图视图 = LayoutInflater.from(parent.getContext()).inflate(R.layout.productslayout, parent, false);返回新的 ProductsHolder(view);}@覆盖protected void onBindViewHolder(ProductsHolder holder, int position, ProductsClass 模型) {//在这里你在 items 中设置东西,获取数据,....//model 是你的类模型(你从中获取数据)}};//现在设置适配器适配器.startListening();products_recycler.setAdapter(适配器);//在同一个活动中创建一个名为ProductsHolder的静态类公共静态类 ProductsHolder 扩展 RecyclerView.ViewHolder{查看视图;公共产品持有人(查看项目视图){视图= itemView;}//你需要添加什么方法}

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

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

参考链接:此链接

I'm starting to create an APP where I can buy items from it. Could be like this: where you can see an Image, Name of product, and Price. Well, I allready have a Sign-in with Google made with Authentication Firebase and I store it on Firebase database, I'd like to create the following this structure that @Alex Mamo recommend to me :

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

Looks fine to make an easy app like mine. Because the scenario is as easy as :

  • User loggs in the APP
  • User see the free products and can buy some products
  • User can see that products that has allready paid for them

From start I store the uid inside the users. What I do not know is;

  1. Do I have to create the same products on Google Play Console to Firebase database? I've already created 1 product to test it on Google Play Console, I have to create the product as well on my app (I mean on Firebase database)?

To implement in-app billing I'm following this tutorial but I've found this android-inapp-billing-v3 library and looks good though.

If there is any of you that has worked with in-app-purchased and has created items and know how to get them from Google Play Console please feel free to share a demo of how to.

EDIT

I allready got how to purchase items I ended up using this Library, the thing is that on my Login page I have the db create of firebase I mean there I put the id of user and email... Do I have to add all the products there aswell?

解决方案

I will change a bit in what you have in plan for the structure of the database, to hopefully simplify a little.

We will have 2 types of saved data:

First Type

Is a node data that you will add it and prepare it manually and we will call it "Products". In this products node you will add the product ids that you got from google play console, and then give it some details something like that :

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

okay now lets define the above diagram:

Wood_id or Iron_id : stands for the exact id that you created your products with in google play console. ((you type it manually in the database along with its details))

name: stands for the name of the item to show in the recycler view ((also you type all these details manually)).

price: points or whatever you decide that.

So these are stuff you type manually in your database tree in firebase console.

when populating in recycler view it will look like that

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

You can achieve this using Firebase UI ((which will make it a bit easy on you to retrieve nodes and view them in RecyclerView)).

okay until now we are able to show the user products and view them in a products list ((and each item carries with it and Id that is equal to the id on google play console))

Second type

Is the type of data that we will add from the app which are (Users) node and (Purchased items) node.

users node is like this

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

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"

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'

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;

         }



    } 

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

       }

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

这篇关于应用内结算连接到 Firebase 并获取产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆