Skip to content

[in_app_purchase] Add countryCode implementation to android and storekit #6556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
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
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);
});
});
}