Skip to content

Commit

Permalink
Merge fdff1eb into 4268721
Browse files Browse the repository at this point in the history
  • Loading branch information
mgonzalezc committed Nov 2, 2020
2 parents 4268721 + fdff1eb commit 64b9ba4
Show file tree
Hide file tree
Showing 3 changed files with 220 additions and 55 deletions.
@@ -1,5 +1,7 @@
package com.dooboolab.flutterinapppurchase;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.amazon.device.iap.PurchasingListener;
Expand Down Expand Up @@ -27,31 +29,38 @@
import java.util.Map;
import java.util.Set;

import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;

/** AmazonInappPurchasePlugin */
public class AmazonInappPurchasePlugin implements MethodCallHandler {
public static Registrar reg;

private final String TAG = "InappPurchasePlugin";
private Result result = null;
private static MethodChannel channel;
private MethodChannel channel;
private Context context;
private Activity activity;

public void setContext(Context context) {
this.context = context;
}

public void setActivity(Activity activity) {
this.activity = activity;
}

/** Plugin registration. */
public static void registerWith(Registrar registrar) {
channel = new MethodChannel(registrar.messenger(), "flutter_inapp");
channel.setMethodCallHandler(new FlutterInappPurchasePlugin());
reg = registrar;
public void setChannel(MethodChannel channel) {
this.channel = channel;
}

@Override
public void onMethodCall(final MethodCall call, final Result result) {
this.result=result;
try {
PurchasingService.registerListener(reg.context(), purchasesUpdatedListener);
PurchasingService.registerListener(context, purchasesUpdatedListener);

} catch (Exception e) {
result.error(call.method, "Call endConnection method if you want to start over.", e.getMessage());
Expand Down Expand Up @@ -87,7 +96,7 @@ public void onMethodCall(final MethodCall call, final Result result) {
Log.d(TAG, "gaibt="+type);
// NOTE: getPurchaseUpdates doesnt return Consumables which are FULFILLED
if(type.equals("inapp")) {
PurchasingService.getPurchaseUpdates(true);
PurchasingService.getPurchaseUpdates(true);
} else if(type.equals("subs")) {
// Subscriptions are retrieved during inapp, so we just return empty list
result.success("[]");
Expand Down Expand Up @@ -204,9 +213,9 @@ public void onPurchaseResponse(PurchaseResponse response) {
Long transactionDate=date.getTime();
try {
JSONObject item = getPurchaseData(receipt.getSku(),
receipt.getReceiptId(),
receipt.getReceiptId(),
transactionDate.doubleValue());
receipt.getReceiptId(),
receipt.getReceiptId(),
transactionDate.doubleValue());
Log.d(TAG, "opr Putting "+item.toString());
result.success(item.toString());
channel.invokeMethod("purchase-updated", item.toString());
Expand Down
@@ -1,6 +1,11 @@
package com.dooboolab.flutterinapppurchase;

import androidx.annotation.Nullable;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.android.billingclient.api.AcknowledgePurchaseParams;
Expand All @@ -26,29 +31,80 @@
import java.util.ArrayList;
import java.util.List;

import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.FlutterException;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;

/** AndroidInappPurchasePlugin */
public class AndroidInappPurchasePlugin implements MethodCallHandler {
public static Registrar reg;
public class AndroidInappPurchasePlugin implements MethodCallHandler, Application.ActivityLifecycleCallbacks {
static private ArrayList<SkuDetails> skus;
private final String TAG = "InappPurchasePlugin";
private BillingClient billingClient;
private static MethodChannel channel;
private Context context;
private Activity activity;
private MethodChannel channel;

/** Plugin registration. */
public static void registerWith(Registrar registrar) {
channel = new MethodChannel(registrar.messenger(), "flutter_inapp");
channel.setMethodCallHandler(new FlutterInappPurchasePlugin());
reg = registrar;
AndroidInappPurchasePlugin() {
skus = new ArrayList<>();
}

public void setContext(Context context) {
this.context = context;
}

public void setActivity(Activity activity) {
this.activity = activity;
}

public void setChannel(MethodChannel channel) {
this.channel = channel;
}

public void onDetachedFromActivity() {
endBillingClientConnection();
}

@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

}

@Override
public void onActivityStarted(Activity activity) {

}

@Override
public void onActivityResumed(Activity activity) {

}

@Override
public void onActivityPaused(Activity activity) {

}

@Override
public void onActivityDestroyed(Activity activity) {
if (this.activity == activity && this.context != null) {
((Application) this.context).unregisterActivityLifecycleCallbacks(this);
endBillingClientConnection();
}
}

@Override
public void onActivityStopped(Activity activity) {

}

@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

}

@Override
public void onMethodCall(final MethodCall call, final Result result) {
if (call.method.equals("getPlatformVersion")) {
Expand All @@ -68,9 +124,9 @@ else if (call.method.equals("initConnection")) {
return;
}

billingClient = BillingClient.newBuilder(reg.context()).setListener(purchasesUpdatedListener)
.enablePendingPurchases()
.build();
billingClient = BillingClient.newBuilder(context).setListener(purchasesUpdatedListener)
.enablePendingPurchases()
.build();
billingClient.startConnection(new BillingClientStateListener() {
private boolean alreadyFinished = false;

Expand Down Expand Up @@ -389,7 +445,9 @@ else if (call.method.equals("buyItemByType")) {

builder.setSkuDetails(selectedSku);
BillingFlowParams flowParams = builder.build();
billingClient.launchBillingFlow(reg.activity(), flowParams);
if (activity != null) {
billingClient.launchBillingFlow(activity, flowParams);
}
}

/*
Expand All @@ -411,11 +469,11 @@ else if (call.method.equals("acknowledgePurchase")) {
billingClient.acknowledgePurchase(acknowledgePurchaseParams, new AcknowledgePurchaseResponseListener() {
@Override
public void onAcknowledgePurchaseResponse(BillingResult billingResult) {
if (billingResult.getResponseCode() != BillingClient.BillingResponseCode.OK) {
String[] errorData = DoobooUtils.getInstance().getBillingResponseData(billingResult.getResponseCode());
result.error(call.method, errorData[0], errorData[1]);
return;
}
if (billingResult.getResponseCode() != BillingClient.BillingResponseCode.OK) {
String[] errorData = DoobooUtils.getInstance().getBillingResponseData(billingResult.getResponseCode());
result.error(call.method, errorData[0], errorData[1]);
return;
}
try {
JSONObject item = new JSONObject();
item.put("responseCode", billingResult.getResponseCode());
Expand Down Expand Up @@ -528,4 +586,13 @@ public void onPurchasesUpdated(BillingResult billingResult, @Nullable List<Purch
}
}
};

private void endBillingClientConnection() {
if (billingClient != null) {
try {
billingClient.endConnection();
billingClient = null;
} catch (Exception ignored) {}
}
}
}

0 comments on commit 64b9ba4

Please sign in to comment.