Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/in_app_purchase/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.2.1+1

* Android: Require Activity only to use launchBillingFlow method.
Comment thread
juliocbcotta marked this conversation as resolved.
Outdated

## 0.2.1

* iOS: Add currencyCode to priceLocale on productDetails.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
public class InAppPurchasePlugin implements MethodCallHandler {
private static final String TAG = "InAppPurchasePlugin";
private @Nullable BillingClient billingClient;
private final Activity activity;
private final Context context;
private final Registrar registrar;
private final Context applicationContext;
private final MethodChannel channel;

@VisibleForTesting
Expand Down Expand Up @@ -69,13 +69,12 @@ static final class MethodNames {
public static void registerWith(Registrar registrar) {
final MethodChannel channel =
new MethodChannel(registrar.messenger(), "plugins.flutter.io/in_app_purchase");
channel.setMethodCallHandler(
new InAppPurchasePlugin(registrar.context(), registrar.activity(), channel));
channel.setMethodCallHandler(new InAppPurchasePlugin(registrar, channel));
}

public InAppPurchasePlugin(Context context, Activity activity, MethodChannel channel) {
this.context = context;
this.activity = activity;
public InAppPurchasePlugin(Registrar registrar, MethodChannel channel) {
this.applicationContext = registrar.context();
this.registrar = registrar;
this.channel = channel;
}

Expand Down Expand Up @@ -114,16 +113,17 @@ public void onMethodCall(MethodCall call, Result result) {
}

@VisibleForTesting
/*package*/ InAppPurchasePlugin(@Nullable BillingClient billingClient, MethodChannel channel) {
/*package*/ InAppPurchasePlugin(
Registrar registrar, @Nullable BillingClient billingClient, MethodChannel channel) {
this.billingClient = billingClient;
this.channel = channel;
this.context = null;
this.activity = null;
this.applicationContext = registrar.context();
this.registrar = registrar;
}

private void startConnection(final int handle, final Result result) {
if (billingClient == null) {
billingClient = buildBillingClient(context, channel);
billingClient = buildBillingClient(applicationContext, channel);
}

billingClient.startConnection(
Expand Down Expand Up @@ -201,6 +201,17 @@ private void launchBillingFlow(String sku, @Nullable String accountId, Result re
null);
return;
}
final Activity activity = registrar.activity();

if (activity == null) {
result.error(
"ACTIVITY_UNAVAILABLE",
"Details for sku "
+ sku
+ " are not available. This method requires to be run with the app in foreground.",
Comment thread
juliocbcotta marked this conversation as resolved.
Outdated
null);
return;
}

BillingFlowParams.Builder paramsBuilder =
BillingFlowParams.newBuilder().setSkuDetails(skuDetails);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.app.Activity;
import android.content.Context;
import androidx.annotation.Nullable;
import com.android.billingclient.api.BillingClient;
import com.android.billingclient.api.BillingClient.BillingResponse;
Expand All @@ -41,9 +43,11 @@
import com.android.billingclient.api.SkuDetails;
import com.android.billingclient.api.SkuDetailsParams;
import com.android.billingclient.api.SkuDetailsResponseListener;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugins.inapppurchase.InAppPurchasePlugin.PluginPurchaseListener;
import java.util.HashMap;
import java.util.List;
Expand All @@ -60,11 +64,16 @@ public class InAppPurchasePluginTest {
@Mock BillingClient mockBillingClient;
@Mock MethodChannel mockMethodChannel;
@Spy Result result;
@Mock PluginRegistry.Registrar registrar;
@Mock Activity activity;
@Mock BinaryMessenger messenger;
@Mock Context context;

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
plugin = new InAppPurchasePlugin(mockBillingClient, mockMethodChannel);
when(registrar.context()).thenReturn(context);
plugin = new InAppPurchasePlugin(registrar, mockBillingClient, mockMethodChannel);
}

@Test
Expand Down Expand Up @@ -219,6 +228,7 @@ public void querySkuDetailsAsync_clientDisconnected() {

@Test
public void launchBillingFlow_ok_nullAccountId() {
when(registrar.activity()).thenReturn(activity);
// Fetch the sku details first and then prepare the launch billing flow call
String skuId = "foo";
queryForSkus(singletonList(skuId));
Expand All @@ -245,8 +255,27 @@ public void launchBillingFlow_ok_nullAccountId() {
verify(result, times(1)).success(responseCode);
}

@Test
public void launchBillingFlow_ok_null_Activity() {
// Fetch the sku details first and then prepare the launch billing flow call
String skuId = "foo";
String accountId = "account";
queryForSkus(singletonList(skuId));
HashMap<String, Object> arguments = new HashMap<>();
arguments.put("sku", skuId);
arguments.put("accountId", accountId);
MethodCall launchCall = new MethodCall(LAUNCH_BILLING_FLOW, arguments);

plugin.onMethodCall(launchCall, result);

// Verify we pass the response code to result
verify(result).error(contains("ACTIVITY_UNAVAILABLE"), contains("foreground"), any());
verify(result, never()).success(any());
}

@Test
public void launchBillingFlow_ok_AccountId() {
when(registrar.activity()).thenReturn(activity);
// Fetch the sku details first and query the method call
String skuId = "foo";
String accountId = "account";
Expand Down
2 changes: 1 addition & 1 deletion packages/in_app_purchase/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: in_app_purchase
description: A Flutter plugin for in-app purchases. Exposes APIs for making in-app purchases through the App Store and Google Play.
author: Flutter Team <flutter-dev@googlegroups.com>
homepage: https://github.com/flutter/plugins/tree/master/packages/in_app_purchase
version: 0.2.1
version: 0.2.1+1


dependencies:
Expand Down