Skip to content

Commit

Permalink
[in_app_purchase] Add countryCode implementation to android and store…
Browse files Browse the repository at this point in the history
…kit (#6556)

Part of flutter/flutter/issues/141627
reviewed pr #6540
  • Loading branch information
reidbaker authored Apr 17, 2024
1 parent 663b475 commit 87ae14f
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 5 deletions.
4 changes: 4 additions & 0 deletions packages/in_app_purchase/in_app_purchase_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.4

* Adds `countryCode` API.

## 0.3.3+1

* Moves alternative billing listener creation to BillingClientFactoryImpl.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:in_app_purchase_platform_interface/in_app_purchase_platform_inte

import '../billing_client_wrappers.dart';
import '../in_app_purchase_android.dart';
import 'billing_client_wrappers/billing_config_wrapper.dart';

/// [IAPError.code] code for failed purchases.
const String kPurchaseErrorCode = 'purchase_error';
Expand Down Expand Up @@ -311,4 +312,14 @@ class InAppPurchaseAndroidPlatform extends InAppPurchasePlatform {
];
}
}

/// Returns Play billing country code based on ISO-3166-1 alpha2 format.
///
/// See: https://developer.android.com/reference/com/android/billingclient/api/BillingConfig
/// See: https://unicode.org/cldr/charts/latest/supplemental/territory_containment_un_m_49.html
Future<String> getCountryCode() async {
final BillingConfigWrapper billingConfig = await billingClientManager
.runWithClient((BillingClient client) => client.getBillingConfig());
return billingConfig.countryCode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ class InAppPurchaseAndroidPlatformAddition
///
/// See: https://developer.android.com/reference/com/android/billingclient/api/BillingConfig
/// See: https://unicode.org/cldr/charts/latest/supplemental/territory_containment_un_m_49.html
@Deprecated('Use InAppPurchasePlatfrom.getCountryCode')
Future<String> getCountryCode() async {
final BillingConfigWrapper billingConfig = await _billingClientManager
.runWithClient((BillingClient client) => client.getBillingConfig());
Expand Down
5 changes: 3 additions & 2 deletions packages/in_app_purchase/in_app_purchase_android/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ name: in_app_purchase_android
description: An implementation for the Android platform of the Flutter `in_app_purchase` plugin. This uses the Android BillingClient APIs.
repository: https://github.com/flutter/packages/tree/main/packages/in_app_purchase/in_app_purchase_android
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+in_app_purchase%22
version: 0.3.3+1

version: 0.3.4

environment:
sdk: ^3.1.0
Expand All @@ -20,7 +21,7 @@ dependencies:
collection: ^1.15.0
flutter:
sdk: flutter
in_app_purchase_platform_interface: ^1.3.0
in_app_purchase_platform_interface: ^1.4.0
json_annotation: ^4.8.0

dev_dependencies:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import 'package:flutter/widgets.dart' as widgets;
import 'package:flutter_test/flutter_test.dart';
import 'package:in_app_purchase_android/billing_client_wrappers.dart';
import 'package:in_app_purchase_android/in_app_purchase_android.dart';
import 'package:in_app_purchase_android/src/billing_client_wrappers/billing_config_wrapper.dart';
import 'package:in_app_purchase_android/src/messages.g.dart';
import 'package:in_app_purchase_platform_interface/in_app_purchase_platform_interface.dart';
import 'package:mockito/mockito.dart';

import 'billing_client_wrappers/billing_client_wrapper_test.dart';
import 'billing_client_wrappers/billing_client_wrapper_test.mocks.dart';
import 'billing_client_wrappers/product_details_wrapper_test.dart';
import 'billing_client_wrappers/purchase_wrapper_test.dart';
Expand Down Expand Up @@ -747,4 +749,20 @@ void main() {
expect(await completer.future, equals(expectedBillingResult));
});
});

group('billingConfig', () {
test('getCountryCode success', () async {
const String expectedCountryCode = 'US';
const BillingConfigWrapper expected = BillingConfigWrapper(
countryCode: expectedCountryCode,
responseCode: BillingResponse.ok,
debugMessage: 'dummy message');

when(mockApi.getBillingConfigAsync())
.thenAnswer((_) async => platformBillingConfigFromWrapper(expected));
final String countryCode = await iapAndroidPlatform.getCountryCode();

expect(countryCode, equals(expectedCountryCode));
});
});
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.14

* Adds `countryCode` API.

## 0.3.13+1

* Handle translation of errors nested in dictionaries.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ class InAppPurchaseStoreKitPlatform extends InAppPurchasePlatform {
);
return productDetailsResponse;
}

/// Returns the country code from SKStoreFrontWrapper.
///
/// Uses the ISO 3166-1 Alpha-3 country code representation.
/// See: https://developer.apple.com/documentation/storekit/skstorefront?language=objc
Future<String?> getCountryCode() async {
return (await _skPaymentQueueWrapper.storefront())?.countryCode;
}
}

enum _TransactionRestoreState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: in_app_purchase_storekit
description: An implementation for the iOS and macOS platforms of the Flutter `in_app_purchase` plugin. This uses the StoreKit Framework.
repository: https://github.com/flutter/packages/tree/main/packages/in_app_purchase/in_app_purchase_storekit
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+in_app_purchase%22
version: 0.3.13+1
version: 0.3.14

environment:
sdk: ^3.2.3
Expand All @@ -23,7 +23,7 @@ dependencies:
collection: ^1.15.0
flutter:
sdk: flutter
in_app_purchase_platform_interface: ^1.3.0
in_app_purchase_platform_interface: ^1.4.0
json_annotation: ^4.3.0

dev_dependencies:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class FakeStoreKitPlatform implements TestInAppPurchaseApi {
bool queueIsActive = false;
Map<String, dynamic> discountReceived = <String, dynamic>{};
bool isPaymentQueueDelegateRegistered = false;
String _countryCode = 'USA';
String _countryIdentifier = 'LL';

void reset() {
transactionList = <SKPaymentTransactionWrapper>[];
Expand All @@ -53,6 +55,8 @@ class FakeStoreKitPlatform implements TestInAppPurchaseApi {
queueIsActive = false;
discountReceived = <String, dynamic>{};
isPaymentQueueDelegateRegistered = false;
_countryCode = 'USA';
_countryIdentifier = 'LL';
}

SKPaymentTransactionWrapper createPendingTransaction(String id,
Expand Down Expand Up @@ -163,9 +167,16 @@ class FakeStoreKitPlatform implements TestInAppPurchaseApi {
}
}

void setStoreFrontInfo(
{required String countryCode, required String identifier}) {
_countryCode = countryCode;
_countryIdentifier = identifier;
}

@override
SKStorefrontMessage storefront() {
throw UnimplementedError();
return SKStorefrontMessage(
countryCode: _countryCode, identifier: _countryIdentifier);
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -570,4 +570,14 @@ void main() {
expect(fakeStoreKitPlatform.queueIsActive, false);
});
});

group('billing configuration', () {
test('country_code', () async {
const String expectedCountryCode = 'CA';
fakeStoreKitPlatform.setStoreFrontInfo(
countryCode: expectedCountryCode, identifier: 'ABC');
final String? countryCode = await iapStoreKitPlatform.getCountryCode();
expect(countryCode, expectedCountryCode);
});
});
}

0 comments on commit 87ae14f

Please sign in to comment.