Created
July 13, 2018 11:02
-
-
Save Zennaxxtech/4db45ad800c0110fb05a4cac1cffe3cc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HomeActivity extends BaseActivity implements BillingProcessor.IBillingHandler { | |
//This is for in-app subscription | |
BillingProcessor billingProcessor; | |
boolean IsInAppBillSetUpOk = false; | |
boolean isItemSubscribed = false; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_home); | |
activity = HomeActivity.this; | |
ButterKnife.bind(this); | |
if (Utils.isNetworkAvailable(activity)) { | |
initializeInAppBillingProcess(); | |
} else { | |
Utils.internetAlertDialog(activity); | |
} | |
} | |
private void initializeInAppBillingProcess() { | |
try { | |
billingProcessor = new BillingProcessor(activity, AppConstant.InApp.BASE64PUBLIC_KEY, this); | |
boolean isAvailable = billingProcessor.isIabServiceAvailable(activity); | |
if (isAvailable) { | |
IsInAppBillSetUpOk = true; | |
} else { | |
IsInAppBillSetUpOk = false; | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
//When i make request for subscription at that time this methot is called | |
private void startInAppSubscriptionProcess() { | |
if (IsInAppBillSetUpOk) { | |
billingProcessor.subscribe(activity, AppConstant.InApp.SKU_LOCATION_ID); | |
} else { | |
Log.e(TAG, "in-app not supported"); | |
} | |
} | |
@Override | |
public void onProductPurchased(@NonNull String productId, @Nullable TransactionDetails details) { | |
inAppSuccessful(productId, details); | |
} | |
@Override | |
public void onPurchaseHistoryRestored() { | |
} | |
@Override | |
public void onBillingError(int errorCode, @Nullable Throwable error) { | |
Utils.inAppFail(activity, errorCode); | |
} | |
@Override | |
public void onBillingInitialized() { | |
} | |
private void updatePurchase() { | |
if (billingProcessor != null) { | |
try { | |
billingProcessor.loadOwnedPurchasesFromGoogle(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
try { | |
if (billingProcessor.isSubscribed(AppConstant.InApp.SKU_LOCATION_ID)) { | |
getSKUDetail(); | |
} else { | |
Log.e(TAG, "item not isSubscribed or something went's wrong, please try agin"); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} else { | |
Log.e(TAG, "billingProcessor is null"); | |
} | |
} | |
private void getSKUDetail() { | |
try { | |
if (billingProcessor != null) { | |
try { | |
SkuDetails skuDetails = billingProcessor.getSubscriptionListingDetails(AppConstant.InApp.SKU_LOCATION_ID); | |
if (skuDetails != null) { | |
Log.e(TAG, "skuDetails is not null"); | |
Log.e(TAG, "transactionDetails is below"); | |
String productId = skuDetails.productId; | |
String title = skuDetails.title; | |
String description = skuDetails.description; | |
String currency = skuDetails.currency; | |
String priceText = skuDetails.priceText; | |
String introductoryPriceText = skuDetails.introductoryPriceText; | |
String introductoryPricePeriod = skuDetails.introductoryPricePeriod; | |
String subscriptionPeriod = skuDetails.subscriptionPeriod; | |
String subscriptionFreeTrialPeriod = skuDetails.subscriptionFreeTrialPeriod; | |
Log.i(TAG, "productId = " + productId); | |
Log.i(TAG, "title = " + title); | |
Log.i(TAG, "description = " + description); | |
Log.i(TAG, "currency = " + currency); | |
Log.i(TAG, "priceText = " + priceText); | |
Log.i(TAG, "introductoryPriceText = " + introductoryPriceText); | |
Log.i(TAG, "introductoryPricePeriod = " + introductoryPricePeriod); | |
Log.i(TAG, "subscriptionPeriod = " + subscriptionPeriod); | |
Log.i(TAG, "subscriptionFreeTrialPeriod = " + subscriptionFreeTrialPeriod); | |
} else { | |
Log.e(TAG, "skuDetails is null"); | |
} | |
}catch (Exception e){ | |
e.printStackTrace(); | |
} | |
}else{ | |
Log.e(TAG, "billingProcessor is null"); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment