In-App billing connect to Firebase and get Product

2019-01-09 13:47发布

问题:

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?

回答1:

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



回答2:

In-app Billing API tracks the purchases but managing the user inventory is the developer job. You can get what have been purchased by using getPurchases so you don't need to save them into the database but is not the case for free products cause you can't add them to in-app billing products list,so you need to save them into Firebase. .Finally about the adapter if what you are querying is simple then use the FirebaseRecycleradapter else use Recycleradapter.



回答3:

I think you should have in the Firebase database the complete list of your products (both free and paid). For paid ones, you should also save in the product subtree the product_id provided by Google Play Console, so you can retreive it easily in your app.

If the user obtains a free product: you just need to update the user subtree in the Firebase database, adding the productId to the user's product list.

If the user wants to obtain a paid product: you need to go through all the in app billing purchase process.

First, you get the product_id from the Firebase database and start the process using it.

Once the purchase is successful, you should check the validity of the purchase, using the token provided by the Billing API. For this operation you need to use a backend server and call the Google Billing APIs using the purchase token.

Just after these operation, you can safely add the product_id to the list of the products owned by the user in the Firebase database and show a completion dialog in the app.

For the implementation of the purchase flow, I followed the sample project provided by Google and obtained a working verification process. LINK