• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Android In-app Subscriptions: Facing issue to test the in-app subscription

  1. I have implemented in App subscription in my android application. But i'm unable to test in debug mode. We always need to generate an signed APK and check the next process - can anyone please suggest how to handle this scenario.

  2. After installed signed APK, When I open my application it's showing my subscription product and play dialog with 3 days trial and one active subscription button. On the next day when I open the app, it again shows the same message and trial count is still showing as 3 days.

  3. When I hit the subscription button it was processed to payment option. After full process it was cut the monthly subscription charge from my account and showing payment success method. - but I was still in trial mode then why it's cut the amount. It should be charged after 3 day's
Can anyone please suggest... find my code below



Code:

Code:
/*In-App BillingConfig*/
   private void initBillingConfig() {
       try {
           mBillingClient = BillingClient.newBuilder(context).setListener(new PurchasesUpdatedListener() {
               [USER=1021285]@override[/USER]
               public void onPurchasesUpdated(int responseCode, [USER=1996173]@nullable[/USER] List<Purchase> purchases) {
                   try {
                       if (responseCode == BillingClient.BillingResponse.OK && purchases != null) {
                           for (Purchase purchase : purchases) {
                               if (purchase.getSku().equalsIgnoreCase(MONTHLY_2USD)) {
                                   showToast("You have subscribed our product(" + MONTHLY_2USD + ") for next 1 month");
                                   appendLog("You have subscribed our product(" + MONTHLY_2USD + ") for next 1 month");
                               }
                           }
                       } else if (responseCode == BillingClient.BillingResponse.ITEM_ALREADY_OWNED) {
                           showToast("onPurchasesUpdated: Item already purchased");
                           appendLog("onPurchasesUpdated: Item already purchased");
                       } else if (responseCode == BillingClient.BillingResponse.USER_CANCELED) {
                           showToast("You have canceled our subscription mode.");
                           appendLog("You have canceled our subscription mode.");
                       } else if (responseCode == BillingClient.BillingResponse.DEVELOPER_ERROR)   {
                           showToast("BillingClient.BillingResponse.DEVELOPER_ERROR");
                           appendLog("BillingClient.BillingResponse.DEVELOPER_ERROR");
                           overridePendingTransition(0,0);
                           finish();
                       }
                   } catch (Exception e) {
                       e.printStackTrace();
                       showToast("onPurchasesUpdated: " + e.getMessage());
                       appendLog("onPurchasesUpdated: " + e.getMessage());
                   }
               }
           }).build();
           mBillingClient.startConnection(new BillingClientStateListener() {
               [USER=1021285]@override[/USER]
               public void onBillingSetupFinished(@BillingClient.BillingResponse int billingResponseCode) {
                   try {
                       if (billingResponseCode == BillingClient.BillingResponse.OK) {
                           // The billing client is ready. You can query purchases here.
                           checkBillingSKU();
                       } else if (billingResponseCode == BillingClient.BillingResponse.ITEM_ALREADY_OWNED) {
                           appendLog("onBillingSetupFinished: Item already purchased");
                       }
                   } catch (Exception e) {
                       e.printStackTrace();
                       showToast("onBillingSetupFinished: " + e.getMessage());
                       appendLog("onBillingSetupFinished: " + e.getMessage());
                   }
               }

               [USER=1021285]@override[/USER]
               public void onBillingServiceDisconnected() {
                   // Try to restart the connection on the next request to
                   // Google Play by calling the startConnection() method.
                   try {
                       mBillingClient.startConnection(this);
                   } catch (Exception e) {
                       e.printStackTrace();
                       showToast("onBillingServiceDisconnected: " + e.getMessage());
                       appendLog("onBillingServiceDisconnected: " + e.getMessage());
                   }
               }
           });
       } catch (Exception e) {
           e.printStackTrace();
           showToast("initBillingConfig: " + e.getMessage());
           appendLog("initBillingConfig: " + e.getMessage());
       }
   }

   private void showToast(String msg) {
       runOnUiThread(new Runnable() {
           [USER=1021285]@override[/USER]
           public void run() {
               Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
           }
       });
   }

   private void checkBillingSKU() {
       try {
           List skuList = new ArrayList<>();
           skuList.add(MONTHLY_2USD);
           SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
           params.setSkusList(skuList).setType(BillingClient.SkuType.SUBS);
           mBillingClient.querySkuDetailsAsync(params.build(),
                   new SkuDetailsResponseListener() {
                       [USER=1021285]@override[/USER]
                       public void onSkuDetailsResponse(int responseCode, List<SkuDetails> skuDetailsList) {
                           // Process the result.
                           try {
                               if (responseCode == BillingClient.BillingResponse.OK && skuDetailsList != null) {
                                   for (SkuDetails skuDetails : skuDetailsList) {
                                       String sku = skuDetails.getSku();
                                       String price = skuDetails.getPrice();
                                       showToast(skuDetails.toString());
                                       if (MONTHLY_2USD.equals(sku)) {
                                           //mPremiumUpgradePrice = price;
                                           purchaseProduct();
                                       }
                                   }
                               }
                           } catch (Exception e) {
                               e.printStackTrace();
                               showToast("onSkuDetailsResponse: " + e.getMessage());
                               appendLog("onSkuDetailsResponse: " + e.getMessage());
                           }
                       }
                   });
       } catch (Exception e) {
           e.printStackTrace();
           showToast("checkBillingSKU: " + e.getMessage());
           appendLog("checkBillingSKU: " + e.getMessage());
       }
   }

   private void purchaseProduct() {
       try {
           BillingFlowParams flowParams = BillingFlowParams.newBuilder()
                   .setSku(MONTHLY_2USD)
                   .setType(BillingClient.SkuType.SUBS) // SkuType.SUB for subscription
                   .build();
           int responseCode = mBillingClient.launchBillingFlow(DashBoard.this, flowParams);
           showToast("BillingFlowParams: " + responseCode);
           appendLog("BillingFlowParams: " + responseCode);
       } catch (Exception e) {
           e.printStackTrace();
           showToast("purchaseProduct: " + e.getMessage());
           appendLog("purchaseProduct: " + e.getMessage());
       }
   }
 
Last edited by a moderator:
Moved to Android Development forum, and added [code][/code] tags to make the code readable.
 
Back
Top Bottom