Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit e78b483

Browse files
authored
[in_app_purchase] Added priceCurrencySymbol to SkuDetailsWrapper (#4114)
1 parent 06fc7d1 commit e78b483

File tree

9 files changed

+63
-2
lines changed

9 files changed

+63
-2
lines changed

packages/in_app_purchase/in_app_purchase_android/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.1.4+2
2+
3+
* Added price currency symbol to SkuDetailsWrapper.
4+
15
## 0.1.4+1
26

37
* Fixed typos.

packages/in_app_purchase/in_app_purchase_android/android/src/main/java/io/flutter/plugins/inapppurchase/Translator.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
import com.android.billingclient.api.SkuDetails;
1414
import java.util.ArrayList;
1515
import java.util.Collections;
16+
import java.util.Currency;
1617
import java.util.HashMap;
1718
import java.util.List;
19+
import java.util.Locale;
1820

1921
/** Handles serialization of {@link com.android.billingclient.api.BillingClient} related objects. */
2022
/*package*/ class Translator {
@@ -30,6 +32,7 @@ static HashMap<String, Object> fromSkuDetail(SkuDetails detail) {
3032
info.put("price", detail.getPrice());
3133
info.put("priceAmountMicros", detail.getPriceAmountMicros());
3234
info.put("priceCurrencyCode", detail.getPriceCurrencyCode());
35+
info.put("priceCurrencySymbol", currencySymbolFromCode(detail.getPriceCurrencyCode()));
3336
info.put("sku", detail.getSku());
3437
info.put("type", detail.getType());
3538
info.put("subscriptionPeriod", detail.getSubscriptionPeriod());
@@ -123,4 +126,21 @@ static HashMap<String, Object> fromBillingResult(BillingResult billingResult) {
123126
info.put("debugMessage", billingResult.getDebugMessage());
124127
return info;
125128
}
129+
130+
/**
131+
* Gets the symbol of for the given currency code for the default {@link Locale.Category#DISPLAY
132+
* DISPLAY} locale. For example, for the US Dollar, the symbol is "$" if the default locale is the
133+
* US, while for other locales it may be "US$". If no symbol can be determined, the ISO 4217
134+
* currency code is returned.
135+
*
136+
* @param currencyCode the ISO 4217 code of the currency
137+
* @return the symbol of this currency code for the default {@link Locale.Category#DISPLAY
138+
* DISPLAY} locale
139+
* @exception NullPointerException if <code>currencyCode</code> is null
140+
* @exception IllegalArgumentException if <code>currencyCode</code> is not a supported ISO 4217
141+
* code.
142+
*/
143+
static String currencySymbolFromCode(String currencyCode) {
144+
return Currency.getInstance(currencyCode).getSymbol();
145+
}
126146
}

packages/in_app_purchase/in_app_purchase_android/android/src/test/java/io/flutter/plugins/inapppurchase/TranslatorTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import static org.junit.Assert.assertEquals;
88
import static org.junit.Assert.assertNotNull;
99
import static org.junit.Assert.assertNull;
10+
import static org.junit.Assert.assertTrue;
11+
import static org.junit.Assert.fail;
1012
import static org.mockito.Mockito.mock;
1113
import static org.mockito.Mockito.when;
1214

@@ -22,8 +24,10 @@
2224
import java.util.Collections;
2325
import java.util.HashMap;
2426
import java.util.List;
27+
import java.util.Locale;
2528
import java.util.Map;
2629
import org.json.JSONException;
30+
import org.junit.Before;
2731
import org.junit.Test;
2832

2933
public class TranslatorTest {
@@ -32,6 +36,12 @@ public class TranslatorTest {
3236
private static final String PURCHASE_EXAMPLE_JSON =
3337
"{\"orderId\":\"foo\",\"packageName\":\"bar\",\"productId\":\"consumable\",\"purchaseTime\":11111111,\"purchaseState\":0,\"purchaseToken\":\"baz\",\"developerPayload\":\"dummy payload\",\"isAcknowledged\":\"true\", \"obfuscatedAccountId\":\"Account101\", \"obfuscatedProfileId\": \"Profile105\"}";
3438

39+
@Before
40+
public void setup() {
41+
Locale locale = new Locale("en", "us");
42+
Locale.setDefault(locale);
43+
}
44+
3545
@Test
3646
public void fromSkuDetail() throws JSONException {
3747
final SkuDetails expected = new SkuDetails(SKU_DETAIL_EXAMPLE_JSON);
@@ -182,6 +192,17 @@ public void fromBillingResult_debugMessageNull() throws JSONException {
182192
assertEquals(billingResultMap.get("debugMessage"), newBillingResult.getDebugMessage());
183193
}
184194

195+
@Test
196+
public void currencyCodeFromSymbol() {
197+
assertEquals("$", Translator.currencySymbolFromCode("USD"));
198+
try {
199+
Translator.currencySymbolFromCode("EUROPACOIN");
200+
fail("Translator should throw an exception");
201+
} catch (Exception e) {
202+
assertTrue(e instanceof IllegalArgumentException);
203+
}
204+
}
205+
185206
private void assertSerialized(SkuDetails expected, Map<String, Object> serialized) {
186207
assertEquals(expected.getDescription(), serialized.get("description"));
187208
assertEquals(expected.getFreeTrialPeriod(), serialized.get("freeTrialPeriod"));
@@ -194,6 +215,7 @@ private void assertSerialized(SkuDetails expected, Map<String, Object> serialize
194215
assertEquals(expected.getPrice(), serialized.get("price"));
195216
assertEquals(expected.getPriceAmountMicros(), serialized.get("priceAmountMicros"));
196217
assertEquals(expected.getPriceCurrencyCode(), serialized.get("priceCurrencyCode"));
218+
assertEquals("$", serialized.get("priceCurrencySymbol"));
197219
assertEquals(expected.getSku(), serialized.get("sku"));
198220
assertEquals(expected.getSubscriptionPeriod(), serialized.get("subscriptionPeriod"));
199221
assertEquals(expected.getTitle(), serialized.get("title"));

packages/in_app_purchase/in_app_purchase_android/lib/src/billing_client_wrappers/sku_details_wrapper.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class SkuDetailsWrapper {
3838
required this.price,
3939
required this.priceAmountMicros,
4040
required this.priceCurrencyCode,
41+
required this.priceCurrencySymbol,
4142
required this.sku,
4243
required this.subscriptionPeriod,
4344
required this.title,
@@ -91,6 +92,12 @@ class SkuDetailsWrapper {
9192
@JsonKey(defaultValue: '')
9293
final String priceCurrencyCode;
9394

95+
/// [price] localized currency symbol
96+
/// For example, for the US Dollar, the symbol is "$" if the locale
97+
/// is the US, while for other locales it may be "US$".
98+
@JsonKey(defaultValue: '')
99+
final String priceCurrencySymbol;
100+
94101
/// The product ID in Google Play Console.
95102
@JsonKey(defaultValue: '')
96103
final String sku;

packages/in_app_purchase/in_app_purchase_android/lib/src/billing_client_wrappers/sku_details_wrapper.g.dart

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/in_app_purchase/in_app_purchase_android/lib/src/types/google_play_product_details.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ class GooglePlayProductDetails extends ProductDetails {
1818
required double rawPrice,
1919
required String currencyCode,
2020
required this.skuDetails,
21+
required String currencySymbol,
2122
}) : super(
2223
id: id,
2324
title: title,
2425
description: description,
2526
price: price,
2627
rawPrice: rawPrice,
2728
currencyCode: currencyCode,
29+
currencySymbol: currencySymbol,
2830
);
2931

3032
/// Points back to the [SkuDetailsWrapper] object that was used to generate
@@ -43,6 +45,7 @@ class GooglePlayProductDetails extends ProductDetails {
4345
price: skuDetails.price,
4446
rawPrice: ((skuDetails.priceAmountMicros) / 1000000.0).toDouble(),
4547
currencyCode: skuDetails.priceCurrencyCode,
48+
currencySymbol: skuDetails.priceCurrencySymbol,
4649
skuDetails: skuDetails,
4750
);
4851
}

packages/in_app_purchase/in_app_purchase_android/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ 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/plugins/tree/master/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.1.4+1
5+
version: 0.1.4+2
66

77
environment:
88
sdk: ">=2.12.0 <3.0.0"
@@ -19,7 +19,7 @@ dependencies:
1919
collection: ^1.15.0
2020
flutter:
2121
sdk: flutter
22-
in_app_purchase_platform_interface: ^1.0.0
22+
in_app_purchase_platform_interface: ^1.1.0
2323
json_annotation: ^4.0.1
2424
meta: ^1.3.0
2525
test: ^1.16.0

packages/in_app_purchase/in_app_purchase_android/test/billing_client_wrappers/sku_details_wrapper_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ final SkuDetailsWrapper dummySkuDetails = SkuDetailsWrapper(
1717
price: 'price',
1818
priceAmountMicros: 1000,
1919
priceCurrencyCode: 'priceCurrencyCode',
20+
priceCurrencySymbol: r'$',
2021
sku: 'sku',
2122
subscriptionPeriod: 'subscriptionPeriod',
2223
title: 'title',
@@ -139,6 +140,7 @@ Map<String, dynamic> buildSkuMap(SkuDetailsWrapper original) {
139140
'price': original.price,
140141
'priceAmountMicros': original.priceAmountMicros,
141142
'priceCurrencyCode': original.priceCurrencyCode,
143+
'priceCurrencySymbol': original.priceCurrencySymbol,
142144
'sku': original.sku,
143145
'subscriptionPeriod': original.subscriptionPeriod,
144146
'title': original.title,

packages/in_app_purchase/in_app_purchase_android/test/in_app_purchase_android_platform_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ void main() {
108108
expect(response.productDetails.first.description,
109109
dummySkuDetails.description);
110110
expect(response.productDetails.first.price, dummySkuDetails.price);
111+
expect(response.productDetails.first.currencySymbol, r'$');
111112
});
112113

113114
test('should get the correct notFoundIDs', () async {

0 commit comments

Comments
 (0)