moisoni97 / google-inapp-billing

A simple implementation of the Android In-App Billing API.
https://github.com/moisoni97/google-inapp-billing
Apache License 2.0
126 stars 39 forks source link

How to suscribe to a product->offer->Phase ? #64

Open Mckill3r opened 11 months ago

Mckill3r commented 11 months ago

Hi, i'm looking to make a purchase for a subscription using the function :

billingConnector.subscribe(this, "APP_PRODUCT", 0);

I have a product named "APP_PRODUCT" and in it i have 3 plans (weekly,montly,yearly) and in the weekly i have a phase that let the user get a free trial of 1week.

Now using the code above i can make the user suscribe to the APP_PRODUCT -> weekly.

But how about to use the phase with a free trial ?

Regards

akardas16 commented 11 months ago

if trial is available user will subscribe to trial otherwise user will be subscribe normal package

if ((productInfo.subscriptionOfferDetails?.size ?: 0) == 1){
    price = productInfo.getSubscriptionOfferPrice(0,0)
    "$price / Monthly".also { binding.price2.text = it }
    p2 = productInfo.subsJustPrice(0,0) / 4
}else if ((productInfo.subscriptionOfferDetails?.size ?: 0) == 2){
    price = productInfo.getSubscriptionOfferPrice(1,0)
    val s = productInfo.subscriptionOfferDetails?.get(0)?.pricingPhases?.pricingPhaseList?.get(0)?.billingPeriod?.filter { it.isDigit() }
    "$s days free, then $price / Monthly".also { binding.price2.text = it }
    p2 = productInfo.subsJustPrice(1,0) / 4
}
Mckill3r commented 11 months ago

I'm using a webview for my app so i don't know the information about the ID of the offer.

I have manage to do what i need to check if user is granted FreeTrial or not by saving IDs of the offer on a Map with the name of the offer :

Map<String, Integer> offreMapping = new HashMap<>();

for (ProductInfo productInfo : productDetails) {
                    product = productInfo.getProduct();
                    int num_offers = productInfo.getSubscriptionOfferDetails().size();

                    for (int i = 0; i < num_offers; i++) {
                        String plan_id = productInfo.getSubscriptionOfferDetails().get(i).getBasePlanId();

                        if (!offreMapping.containsKey(plan_id)) {
                            offreMapping.put(plan_id, i);
                        }
                    }

I should make a verification on the OfferPrice but since i only use FreeTrial in my case i dont need to.

This way i can call a javascript function with the offer name that will convert it with the correct ID.

public void make_payment(String offer_name){
        String Offer = mContext.getString(R.string.inapp_offer);
        Integer id = offreMapping.get(offer_name);
        if(id != null) {
            billingConnector.subscribe(this, Offer, id);
        }
    }

Not perfect but working fine if other need it :)