1. Are you ready for the Galaxy S20? Here is everything we know so far!

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

Discussion in 'Android Development' started by Dibakar Mistry, Sep 12, 2018.

  1. Dibakar Mistry

    Thread Starter

    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 (Text):
    1.  
    2. /*In-App BillingConfig*/
    3.    private void initBillingConfig() {
    4.        try {
    5.            mBillingClient = BillingClient.newBuilder(context).setListener(new PurchasesUpdatedListener() {
    6.                [USER=1021285]@override[/USER]
    7.                public void onPurchasesUpdated(int responseCode, [USER=1996173]@nullable[/USER] List<Purchase> purchases) {
    8.                    try {
    9.                        if (responseCode == BillingClient.BillingResponse.OK && purchases != null) {
    10.                            for (Purchase purchase : purchases) {
    11.                                if (purchase.getSku().equalsIgnoreCase(MONTHLY_2USD)) {
    12.                                    showToast("You have subscribed our product(" + MONTHLY_2USD + ") for next 1 month");
    13.                                    appendLog("You have subscribed our product(" + MONTHLY_2USD + ") for next 1 month");
    14.                                }
    15.                            }
    16.                        } else if (responseCode == BillingClient.BillingResponse.ITEM_ALREADY_OWNED) {
    17.                            showToast("onPurchasesUpdated: Item already purchased");
    18.                            appendLog("onPurchasesUpdated: Item already purchased");
    19.                        } else if (responseCode == BillingClient.BillingResponse.USER_CANCELED) {
    20.                            showToast("You have canceled our subscription mode.");
    21.                            appendLog("You have canceled our subscription mode.");
    22.                        } else if (responseCode == BillingClient.BillingResponse.DEVELOPER_ERROR)   {
    23.                            showToast("BillingClient.BillingResponse.DEVELOPER_ERROR");
    24.                            appendLog("BillingClient.BillingResponse.DEVELOPER_ERROR");
    25.                            overridePendingTransition(0,0);
    26.                            finish();
    27.                        }
    28.                    } catch (Exception e) {
    29.                        e.printStackTrace();
    30.                        showToast("onPurchasesUpdated: " + e.getMessage());
    31.                        appendLog("onPurchasesUpdated: " + e.getMessage());
    32.                    }
    33.                }
    34.            }).build();
    35.            mBillingClient.startConnection(new BillingClientStateListener() {
    36.                [USER=1021285]@override[/USER]
    37.                public void onBillingSetupFinished(@BillingClient.BillingResponse int billingResponseCode) {
    38.                    try {
    39.                        if (billingResponseCode == BillingClient.BillingResponse.OK) {
    40.                            // The billing client is ready. You can query purchases here.
    41.                            checkBillingSKU();
    42.                        } else if (billingResponseCode == BillingClient.BillingResponse.ITEM_ALREADY_OWNED) {
    43.                            appendLog("onBillingSetupFinished: Item already purchased");
    44.                        }
    45.                    } catch (Exception e) {
    46.                        e.printStackTrace();
    47.                        showToast("onBillingSetupFinished: " + e.getMessage());
    48.                        appendLog("onBillingSetupFinished: " + e.getMessage());
    49.                    }
    50.                }
    51.  
    52.                [USER=1021285]@override[/USER]
    53.                public void onBillingServiceDisconnected() {
    54.                    // Try to restart the connection on the next request to
    55.                    // Google Play by calling the startConnection() method.
    56.                    try {
    57.                        mBillingClient.startConnection(this);
    58.                    } catch (Exception e) {
    59.                        e.printStackTrace();
    60.                        showToast("onBillingServiceDisconnected: " + e.getMessage());
    61.                        appendLog("onBillingServiceDisconnected: " + e.getMessage());
    62.                    }
    63.                }
    64.            });
    65.        } catch (Exception e) {
    66.            e.printStackTrace();
    67.            showToast("initBillingConfig: " + e.getMessage());
    68.            appendLog("initBillingConfig: " + e.getMessage());
    69.        }
    70.    }
    71.  
    72.    private void showToast(String msg) {
    73.        runOnUiThread(new Runnable() {
    74.            [USER=1021285]@override[/USER]
    75.            public void run() {
    76.                Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    77.            }
    78.        });
    79.    }
    80.  
    81.    private void checkBillingSKU() {
    82.        try {
    83.            List skuList = new ArrayList<>();
    84.            skuList.add(MONTHLY_2USD);
    85.            SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
    86.            params.setSkusList(skuList).setType(BillingClient.SkuType.SUBS);
    87.            mBillingClient.querySkuDetailsAsync(params.build(),
    88.                    new SkuDetailsResponseListener() {
    89.                        [USER=1021285]@override[/USER]
    90.                        public void onSkuDetailsResponse(int responseCode, List<SkuDetails> skuDetailsList) {
    91.                            // Process the result.
    92.                            try {
    93.                                if (responseCode == BillingClient.BillingResponse.OK && skuDetailsList != null) {
    94.                                    for (SkuDetails skuDetails : skuDetailsList) {
    95.                                        String sku = skuDetails.getSku();
    96.                                        String price = skuDetails.getPrice();
    97.                                        showToast(skuDetails.toString());
    98.                                        if (MONTHLY_2USD.equals(sku)) {
    99.                                            //mPremiumUpgradePrice = price;
    100.                                            purchaseProduct();
    101.                                        }
    102.                                    }
    103.                                }
    104.                            } catch (Exception e) {
    105.                                e.printStackTrace();
    106.                                showToast("onSkuDetailsResponse: " + e.getMessage());
    107.                                appendLog("onSkuDetailsResponse: " + e.getMessage());
    108.                            }
    109.                        }
    110.                    });
    111.        } catch (Exception e) {
    112.            e.printStackTrace();
    113.            showToast("checkBillingSKU: " + e.getMessage());
    114.            appendLog("checkBillingSKU: " + e.getMessage());
    115.        }
    116.    }
    117.  
    118.    private void purchaseProduct() {
    119.        try {
    120.            BillingFlowParams flowParams = BillingFlowParams.newBuilder()
    121.                    .setSku(MONTHLY_2USD)
    122.                    .setType(BillingClient.SkuType.SUBS) // SkuType.SUB for subscription
    123.                    .build();
    124.            int responseCode = mBillingClient.launchBillingFlow(DashBoard.this, flowParams);
    125.            showToast("BillingFlowParams: " + responseCode);
    126.            appendLog("BillingFlowParams: " + responseCode);
    127.        } catch (Exception e) {
    128.            e.printStackTrace();
    129.            showToast("purchaseProduct: " + e.getMessage());
    130.            appendLog("purchaseProduct: " + e.getMessage());
    131.        }
    132.    }
    133.  
     


    #1 Dibakar Mistry, Sep 12, 2018
    Last edited by a moderator: Sep 12, 2018
  2. Deleted User

    Deleted User Guest

    Moved to Android Development forum, and added [code][/code] tags to make the code readable.
     
Loading...
Similar Threads - Android app Subscriptions
  1. PearMobile Ltd
    Replies:
    3
    Views:
    124
  2. Brainsoft
    Replies:
    2
    Views:
    273
  3. dan55
    Replies:
    8
    Views:
    290
  4. montecarlo1987
    Replies:
    12
    Views:
    398
  5. Janisroze
    Replies:
    0
    Views:
    353
  6. Watsonhank
    Replies:
    0
    Views:
    292
  7. MoodyBlues
    Replies:
    17
    Views:
    1,105
  8. elzbone
    Replies:
    9
    Views:
    560
  9. slightedge411
    Replies:
    2
    Views:
    389

Share This Page

Loading...