Skip to content

Instantly share code, notes, and snippets.

@BlacKCaT27
Created June 11, 2016 02:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BlacKCaT27/88e5a8ba2e6139fe7338862cdcff1636 to your computer and use it in GitHub Desktop.
Save BlacKCaT27/88e5a8ba2e6139fe7338862cdcff1636 to your computer and use it in GitHub Desktop.
public class PurchaseHelper{
// Billing response codes
public static final int BILLING_RESPONSE_USER_CLOSED_PURCHASE_WINDOW = -1;
public static final int BILLING_RESPONSE_RESULT_OK = 0;
public static final int BILLING_RESPONSE_RESULT_USER_CANCELED = 1;
public static final int BILLING_RESPONSE_RESULT_BILLING_UNAVAILABLE = 3;
public static final int BILLING_RESPONSE_RESULT_ITEM_UNAVAILABLE = 4;
public static final int BILLING_RESPONSE_RESULT_DEVELOPER_ERROR = 5;
public static final int BILLING_RESPONSE_RESULT_ERROR = 6;
public static final int BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED = 7;
public static final int BILLING_RESPONSE_RESULT_ITEM_NOT_OWNED = 8;
public static String PREMIUM_SKU; // BE SURE TO SET THIS!
static{
if(BuildConfig.DEBUG){
//Debug build detected. Overriding premium SKU with test purchase
PREMIUM_SKU = "android.test.purchased";
}
}
public interface QueryIsFreeResultCallback{
void onQueryResult(boolean result);
}
public interface GetSkuDetailsCallback{
void onSkuDetailsResult(List<SkuDetails> result);
}
public interface PurchasePremiumVersionFlowResultCallback {
void onPurchaseFlowStartResult(int resultCode);
}
public interface PurchasePremiumVersionResultCallback{
void onPurchaseResult(int resultCode);
}
public interface GooglePlayServicesSupportedCallback{
void isPlaySupported(boolean result);
}
private static Subscription purchaseFlowSubscription;
public static void queryIsFree(Context context, QueryIsFreeResultCallback callback) {
ReactiveBilling.getInstance(context)
.getPurchases(PurchaseType.PRODUCT, null)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
(response) ->{
boolean isFree = false;
if(response.isSuccess()){
List<GetPurchasesResponse.PurchaseResponse> purchases = response.getList();
for (GetPurchasesResponse.PurchaseResponse p : purchases) {
if(p.getProductId().equals(PREMIUM_SKU)){
isFree = false;
break;
}
}
}
callback.onQueryResult(isFree);
},
(throwable) ->{
callback.onQueryResult(false);
}
);
}
public static void getSkuDetails(Context context, GetSkuDetailsCallback callback){
ReactiveBilling.getInstance(context)
.getSkuDetails(PurchaseType.PRODUCT, PREMIUM_SKU)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
(response) -> {
DebugLog.d("Sku response code: " + response.getResponseCode());
if(response.isSuccess()){
callback.onSkuDetailsResult(response.getList());
}
},
(throwable) -> {
callback.onSkuDetailsResult(new ArrayList<>());
});
}
/**
* Starts the purchase flow for premium version of the app. Result
* is whether or not purchase flow STARTED, NOT succeeded.
*/
public static void startPremiumVersionPurchaseFlow(Context context, PurchasePremiumVersionFlowResultCallback callback) {
ReactiveBilling.getInstance(context)
.startPurchase(PREMIUM_SKU, PurchaseType.PRODUCT, null, null)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
(response) -> {
callback.onPurchaseFlowStartResult(response.getResponseCode());
},
(throwable) -> {
callback.onPurchaseFlowStartResult(BILLING_RESPONSE_RESULT_ERROR);
}
);
}
public static Subscription getPurchaseResults(Context context, PurchasePremiumVersionResultCallback callback){
//Purchase window will fail to show if a subscription to the observable already exists, so ensure it's cleaned up
if(purchaseFlowSubscription != null){
try {
purchaseFlowSubscription.unsubscribe();
}
catch(Exception e){}
}
purchaseFlowSubscription = ReactiveBilling.getInstance(context)
.purchaseFlow()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
(response) -> {
callback.onPurchaseResult(response.getResponseCode());
},
(throwable -> {
callback.onPurchaseResult(BILLING_RESPONSE_RESULT_DEVELOPER_ERROR);
})
);
return purchaseFlowSubscription;
}
public static void isGooglePlayServicesSupported(Context context, GooglePlayServicesSupportedCallback callback) {
ReactiveBilling.getInstance(context)
.isBillingSupported(PurchaseType.PRODUCT)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
(response) -> {
callback.isPlaySupported(response.isSuccess());
},
(throwable) -> {
callback.isPlaySupported(false);
}
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment