Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In-App billing connect to Firebase and get Products

I'm starting to create an APP where I can buy items from it. Could be like this: image] 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?

like image 231
Skizo-ozᴉʞS Avatar asked Nov 22 '17 15:11

Skizo-ozᴉʞS


1 Answers

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

like image 69
Hasan Bou Taam Avatar answered Oct 01 '22 19:10

Hasan Bou Taam