Skip to content

Commit d817df4

Browse files
reidbakerTecHaxter
authored andcommitted
[in_app_purchase] Add countryCode implementation to android and storekit (flutter#6556)
Part of flutter/flutter/issues/141627 reviewed pr flutter#6540
1 parent d56cfcd commit d817df4

10 files changed

+73
-5
lines changed

packages/in_app_purchase/in_app_purchase_android/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.3.4
2+
3+
* Adds `countryCode` API.
4+
15
## 0.3.3+1
26

37
* Moves alternative billing listener creation to BillingClientFactoryImpl.

packages/in_app_purchase/in_app_purchase_android/lib/src/in_app_purchase_android_platform.dart

+11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:in_app_purchase_platform_interface/in_app_purchase_platform_inte
1111

1212
import '../billing_client_wrappers.dart';
1313
import '../in_app_purchase_android.dart';
14+
import 'billing_client_wrappers/billing_config_wrapper.dart';
1415

1516
/// [IAPError.code] code for failed purchases.
1617
const String kPurchaseErrorCode = 'purchase_error';
@@ -311,4 +312,14 @@ class InAppPurchaseAndroidPlatform extends InAppPurchasePlatform {
311312
];
312313
}
313314
}
315+
316+
/// Returns Play billing country code based on ISO-3166-1 alpha2 format.
317+
///
318+
/// See: https://developer.android.com/reference/com/android/billingclient/api/BillingConfig
319+
/// See: https://unicode.org/cldr/charts/latest/supplemental/territory_containment_un_m_49.html
320+
Future<String> getCountryCode() async {
321+
final BillingConfigWrapper billingConfig = await billingClientManager
322+
.runWithClient((BillingClient client) => client.getBillingConfig());
323+
return billingConfig.countryCode;
324+
}
314325
}

packages/in_app_purchase/in_app_purchase_android/lib/src/in_app_purchase_android_platform_addition.dart

+1
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ class InAppPurchaseAndroidPlatformAddition
167167
///
168168
/// See: https://developer.android.com/reference/com/android/billingclient/api/BillingConfig
169169
/// See: https://unicode.org/cldr/charts/latest/supplemental/territory_containment_un_m_49.html
170+
@Deprecated('Use InAppPurchasePlatfrom.getCountryCode')
170171
Future<String> getCountryCode() async {
171172
final BillingConfigWrapper billingConfig = await _billingClientManager
172173
.runWithClient((BillingClient client) => client.getBillingConfig());

packages/in_app_purchase/in_app_purchase_android/pubspec.yaml

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ name: in_app_purchase_android
22
description: An implementation for the Android platform of the Flutter `in_app_purchase` plugin. This uses the Android BillingClient APIs.
33
repository: https://github.com/flutter/packages/tree/main/packages/in_app_purchase/in_app_purchase_android
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+in_app_purchase%22
5-
version: 0.3.3+1
5+
6+
version: 0.3.4
67

78
environment:
89
sdk: ^3.1.0
@@ -20,7 +21,7 @@ dependencies:
2021
collection: ^1.15.0
2122
flutter:
2223
sdk: flutter
23-
in_app_purchase_platform_interface: ^1.3.0
24+
in_app_purchase_platform_interface: ^1.4.0
2425
json_annotation: ^4.8.0
2526

2627
dev_dependencies:

packages/in_app_purchase/in_app_purchase_android/test/in_app_purchase_android_platform_test.dart

+18
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import 'package:flutter/widgets.dart' as widgets;
99
import 'package:flutter_test/flutter_test.dart';
1010
import 'package:in_app_purchase_android/billing_client_wrappers.dart';
1111
import 'package:in_app_purchase_android/in_app_purchase_android.dart';
12+
import 'package:in_app_purchase_android/src/billing_client_wrappers/billing_config_wrapper.dart';
1213
import 'package:in_app_purchase_android/src/messages.g.dart';
1314
import 'package:in_app_purchase_platform_interface/in_app_purchase_platform_interface.dart';
1415
import 'package:mockito/mockito.dart';
1516

17+
import 'billing_client_wrappers/billing_client_wrapper_test.dart';
1618
import 'billing_client_wrappers/billing_client_wrapper_test.mocks.dart';
1719
import 'billing_client_wrappers/product_details_wrapper_test.dart';
1820
import 'billing_client_wrappers/purchase_wrapper_test.dart';
@@ -747,4 +749,20 @@ void main() {
747749
expect(await completer.future, equals(expectedBillingResult));
748750
});
749751
});
752+
753+
group('billingConfig', () {
754+
test('getCountryCode success', () async {
755+
const String expectedCountryCode = 'US';
756+
const BillingConfigWrapper expected = BillingConfigWrapper(
757+
countryCode: expectedCountryCode,
758+
responseCode: BillingResponse.ok,
759+
debugMessage: 'dummy message');
760+
761+
when(mockApi.getBillingConfigAsync())
762+
.thenAnswer((_) async => platformBillingConfigFromWrapper(expected));
763+
final String countryCode = await iapAndroidPlatform.getCountryCode();
764+
765+
expect(countryCode, equals(expectedCountryCode));
766+
});
767+
});
750768
}

packages/in_app_purchase/in_app_purchase_storekit/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.3.14
2+
3+
* Adds `countryCode` API.
4+
15
## 0.3.13+1
26

37
* Handle translation of errors nested in dictionaries.

packages/in_app_purchase/in_app_purchase_storekit/lib/src/in_app_purchase_storekit_platform.dart

+8
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,14 @@ class InAppPurchaseStoreKitPlatform extends InAppPurchasePlatform {
153153
);
154154
return productDetailsResponse;
155155
}
156+
157+
/// Returns the country code from SKStoreFrontWrapper.
158+
///
159+
/// Uses the ISO 3166-1 Alpha-3 country code representation.
160+
/// See: https://developer.apple.com/documentation/storekit/skstorefront?language=objc
161+
Future<String?> getCountryCode() async {
162+
return (await _skPaymentQueueWrapper.storefront())?.countryCode;
163+
}
156164
}
157165

158166
enum _TransactionRestoreState {

packages/in_app_purchase/in_app_purchase_storekit/pubspec.yaml

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

77
environment:
88
sdk: ^3.2.3
@@ -23,7 +23,7 @@ dependencies:
2323
collection: ^1.15.0
2424
flutter:
2525
sdk: flutter
26-
in_app_purchase_platform_interface: ^1.3.0
26+
in_app_purchase_platform_interface: ^1.4.0
2727
json_annotation: ^4.3.0
2828

2929
dev_dependencies:

packages/in_app_purchase/in_app_purchase_storekit/test/fakes/fake_storekit_platform.dart

+12-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class FakeStoreKitPlatform implements TestInAppPurchaseApi {
2727
bool queueIsActive = false;
2828
Map<String, dynamic> discountReceived = <String, dynamic>{};
2929
bool isPaymentQueueDelegateRegistered = false;
30+
String _countryCode = 'USA';
31+
String _countryIdentifier = 'LL';
3032

3133
void reset() {
3234
transactionList = <SKPaymentTransactionWrapper>[];
@@ -53,6 +55,8 @@ class FakeStoreKitPlatform implements TestInAppPurchaseApi {
5355
queueIsActive = false;
5456
discountReceived = <String, dynamic>{};
5557
isPaymentQueueDelegateRegistered = false;
58+
_countryCode = 'USA';
59+
_countryIdentifier = 'LL';
5660
}
5761

5862
SKPaymentTransactionWrapper createPendingTransaction(String id,
@@ -163,9 +167,16 @@ class FakeStoreKitPlatform implements TestInAppPurchaseApi {
163167
}
164168
}
165169

170+
void setStoreFrontInfo(
171+
{required String countryCode, required String identifier}) {
172+
_countryCode = countryCode;
173+
_countryIdentifier = identifier;
174+
}
175+
166176
@override
167177
SKStorefrontMessage storefront() {
168-
throw UnimplementedError();
178+
return SKStorefrontMessage(
179+
countryCode: _countryCode, identifier: _countryIdentifier);
169180
}
170181

171182
@override

packages/in_app_purchase/in_app_purchase_storekit/test/in_app_purchase_storekit_platform_test.dart

+10
Original file line numberDiff line numberDiff line change
@@ -570,4 +570,14 @@ void main() {
570570
expect(fakeStoreKitPlatform.queueIsActive, false);
571571
});
572572
});
573+
574+
group('billing configuration', () {
575+
test('country_code', () async {
576+
const String expectedCountryCode = 'CA';
577+
fakeStoreKitPlatform.setStoreFrontInfo(
578+
countryCode: expectedCountryCode, identifier: 'ABC');
579+
final String? countryCode = await iapStoreKitPlatform.getCountryCode();
580+
expect(countryCode, expectedCountryCode);
581+
});
582+
});
573583
}

0 commit comments

Comments
 (0)