Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create products Google play and in-app billing

I'm doing an app that have a RecyclerView with many items (some of them are fixed and are by default for everyone), but I'd like to make some of them "not free", means that you have to pay (if possible using Google Play) to unlock those items, can you guide to me how do I figure out this scenario? How do I know that the user has items that has paid for them, and when I know that he paid for them.

I do not want to use a Database because I only need two tables I guess, for example;

TABLE USER

Where I'll will store an id (I guess it will be the email if I finally create a Login GMAIL,FACEBOOK)

TABLE BOX

Where I'll put the BOX that are free and not free and as a Foreign Key I'll put the user.id to get references what BOX's he/she owns.

Right?

But, If I have to choose this way, because if the user pays for a box on my APP and then he want to use it in other device, it won't have the box unless he logs in on the new device, correct? So, If I had had to choose a Database and Hosting to do it, what will be your recommendation? (From start, I won't have too much data, so I don't need the best one, I only want to communicate with my Android app).

I hope it's clear now, otherwise, put a comment and I'll try to explain it better.

EDIT

Reading the answers I see that I can use Firebase, as @Haris Qureshi said, well what I'm confused is :

How do I store the product? I mean, I've read and watched videos on YouTube and I have to do it in Console of Google Play but do I need to send the .apk and then add the products?

If I do not need to create a Database, and I have to end up using Firebase, using the Storage stuff from Firebase will store my products? But I can assign it as a PURCHASED, PAID, AVAILABLE? Also I'll need to integrate the Authentication, right? Because he said that once you buy a product, it stores the gmail that has paid for it and you know who paid and not paid, so if he uses another device it will have the same products boughts (if he still with the same gmail).

The products can be dynamics? I mean, you can create for example an "event" and put that product only for x days, as I'll need an Image to show the product, I mean the product will have also an Image to show on the UI, but I see in the Console of Google Play I only can create an id for the product and if I'd have to create it dynamic I could do that, because I have to store the Images on /res/mipmap right?

EDIT2

What I need is what @Alex Mamo told to me :

You cannot store images in your Firebase database. You need to store them in Firebase Storage. In Firebase database you need to store only the urls of those images.

The flow is like this: upload image to Firebase Storage -> get the url of the corresponding image (when uploading) -> store the url in Firebase Database -> use the reference to display the image

I've created the products on google console developer, and I have to connect my app to in-app billing

like image 234
Skizo-ozᴉʞS Avatar asked Dec 14 '22 19:12

Skizo-ozᴉʞS


1 Answers

There are good answers here but because you have asked me, i'll try to give you some more details. The best practice for Implementing In-app Billing is the official documentation. Please se below my my answer for some of your questions.

As many other users have advised you, so do I, use Firebase as your database for your app. It's a NoSQL database and it's very easy to use.

How do you store the product?

Please see below a database structure that i recomand you to use for your app.

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

Using this database structure you can query your database for:

  1. All products (purchased, paid or available) that belong to a single user
  2. All users that have bought a particular product
  3. All purchased products that belong to a single user
  4. All paid products that belong to a single user
  5. All available products that belong to a single user

Every time a product is bought, you only need to move the product to the coresponding category. As AL. said, you could set your app to get the products by using the Firebase SDK. That would not be a problem.

This is called in Firebase denormalization is very normal with the Firebase Database. For this, i recomand see this video. If you want something more complex, i recomand you read this post, Structuring your Firebase Data correctly for a Complex App.

Using the Storage stuff from Firebase will store my products?

Yes, you can use Firebase Cloud Storage which is built for app developers who need to store and serve user-generated content, such as photos or videos.

Also I'll need to integrate the Authentication, right?

Yes, you need to use Firebase Authentication because you need to know the identity of a user. Knowing a user's identity allows an app to securely save user data in the cloud and provide the same personalized experience across all of the user's devices. Firebase Authentication provides backend services, easy-to-use SDKs, and ready-made UI libraries to authenticate users to your app. It supports authentication using passwords, phone numbers, popular federated identity providers like Google, Facebook and Twitter, and more.

The products can be dynamics? I mean, you can create for example an "event" and put that product only for x days?

Yes, you can do that using Cloud Functions For Firebase which lets you automatically run backend code in response to events triggered by Firebase features and HTTPS requests. Your code is stored in Google's cloud and runs in a managed environment. There's no need to manage and scale your own servers.

This is how you can query the database to display the product name and the price of all products.

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference yourRef = rootRef.child("products");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String productName = ds.child("productName").getValue(String.class);
            double price = ds.child("price").getValue(Double.class);
            Log.d("TAG", productName + price);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
yourRef.addListenerForSingleValueEvent(eventListener);

And this how you can query your database to see all purchasedProducts that belong to a specific user:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("purchasedProducts").child("uid1");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String productKey = ds.getKey();
            Log.d("TAG", productKey);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
uidRef.addListenerForSingleValueEvent(eventListener);
like image 135
Alex Mamo Avatar answered Dec 27 '22 13:12

Alex Mamo