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

[in_app_purchase] Added priceCurrencySymbol to SkuDetailsWrapper #4114

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
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.1.4+2

* Added price currency symbol to SkuDetailsWrapper.

## 0.1.4+1

* Fixed typos.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
import com.android.billingclient.api.SkuDetails;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Currency;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

/** Handles serialization of {@link com.android.billingclient.api.BillingClient} related objects. */
/*package*/ class Translator {
Expand All @@ -30,6 +32,7 @@ static HashMap<String, Object> fromSkuDetail(SkuDetails detail) {
info.put("price", detail.getPrice());
info.put("priceAmountMicros", detail.getPriceAmountMicros());
info.put("priceCurrencyCode", detail.getPriceCurrencyCode());
info.put("priceCurrencySymbol", currencySymbolFromCode(detail.getPriceCurrencyCode()));
info.put("sku", detail.getSku());
info.put("type", detail.getType());
info.put("subscriptionPeriod", detail.getSubscriptionPeriod());
Expand Down Expand Up @@ -123,4 +126,21 @@ static HashMap<String, Object> fromBillingResult(BillingResult billingResult) {
info.put("debugMessage", billingResult.getDebugMessage());
return info;
}

/**
* Gets the symbol of for the given currency code for the default {@link Locale.Category#DISPLAY
* DISPLAY} locale. For example, for the US Dollar, the symbol is "$" if the default locale is the
* US, while for other locales it may be "US$". If no symbol can be determined, the ISO 4217
* currency code is returned.
*
* @param currencyCode the ISO 4217 code of the currency
* @return the symbol of this currency code for the default {@link Locale.Category#DISPLAY
* DISPLAY} locale
* @exception NullPointerException if <code>currencyCode</code> is null
* @exception IllegalArgumentException if <code>currencyCode</code> is not a supported ISO 4217
* code.
*/
static String currencySymbolFromCode(String currencyCode) {
return Currency.getInstance(currencyCode).getSymbol();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -22,8 +24,10 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.json.JSONException;
import org.junit.Before;
import org.junit.Test;

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

@Before
public void setup() {
Locale locale = new Locale("en", "us");
Locale.setDefault(locale);
}

@Test
public void fromSkuDetail() throws JSONException {
final SkuDetails expected = new SkuDetails(SKU_DETAIL_EXAMPLE_JSON);
Expand Down Expand Up @@ -182,6 +192,17 @@ public void fromBillingResult_debugMessageNull() throws JSONException {
assertEquals(billingResultMap.get("debugMessage"), newBillingResult.getDebugMessage());
}

@Test
public void currencyCodeFromSymbol() {
assertEquals("$", Translator.currencySymbolFromCode("USD"));
try {
Translator.currencySymbolFromCode("EUROPACOIN");
fail("Translator should throw an exception");
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException);
}
}

private void assertSerialized(SkuDetails expected, Map<String, Object> serialized) {
assertEquals(expected.getDescription(), serialized.get("description"));
assertEquals(expected.getFreeTrialPeriod(), serialized.get("freeTrialPeriod"));
Expand All @@ -194,6 +215,7 @@ private void assertSerialized(SkuDetails expected, Map<String, Object> serialize
assertEquals(expected.getPrice(), serialized.get("price"));
assertEquals(expected.getPriceAmountMicros(), serialized.get("priceAmountMicros"));
assertEquals(expected.getPriceCurrencyCode(), serialized.get("priceCurrencyCode"));
assertEquals("$", serialized.get("priceCurrencySymbol"));
assertEquals(expected.getSku(), serialized.get("sku"));
assertEquals(expected.getSubscriptionPeriod(), serialized.get("subscriptionPeriod"));
assertEquals(expected.getTitle(), serialized.get("title"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class SkuDetailsWrapper {
required this.price,
required this.priceAmountMicros,
required this.priceCurrencyCode,
required this.priceCurrencySymbol,
required this.sku,
required this.subscriptionPeriod,
required this.title,
Expand Down Expand Up @@ -91,6 +92,12 @@ class SkuDetailsWrapper {
@JsonKey(defaultValue: '')
final String priceCurrencyCode;

/// [price] localized currency symbol
/// For example, for the US Dollar, the symbol is "$" if the locale
/// is the US, while for other locales it may be "US$".
@JsonKey(defaultValue: '')
final String priceCurrencySymbol;

/// The product ID in Google Play Console.
@JsonKey(defaultValue: '')
final String sku;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ class GooglePlayProductDetails extends ProductDetails {
required double rawPrice,
required String currencyCode,
required this.skuDetails,
required String currencySymbol,
}) : super(
id: id,
title: title,
description: description,
price: price,
rawPrice: rawPrice,
currencyCode: currencyCode,
currencySymbol: currencySymbol,
);

/// Points back to the [SkuDetailsWrapper] object that was used to generate
Expand All @@ -43,6 +45,7 @@ class GooglePlayProductDetails extends ProductDetails {
price: skuDetails.price,
rawPrice: ((skuDetails.priceAmountMicros) / 1000000.0).toDouble(),
currencyCode: skuDetails.priceCurrencyCode,
currencySymbol: skuDetails.priceCurrencySymbol,
skuDetails: skuDetails,
);
}
Expand Down
4 changes: 2 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,7 @@ 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/plugins/tree/master/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.1.4+1
version: 0.1.4+2

environment:
sdk: ">=2.12.0 <3.0.0"
Expand All @@ -19,7 +19,7 @@ dependencies:
collection: ^1.15.0
flutter:
sdk: flutter
in_app_purchase_platform_interface: ^1.0.0
in_app_purchase_platform_interface: ^1.1.0
json_annotation: ^4.0.1
meta: ^1.3.0
test: ^1.16.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ final SkuDetailsWrapper dummySkuDetails = SkuDetailsWrapper(
price: 'price',
priceAmountMicros: 1000,
priceCurrencyCode: 'priceCurrencyCode',
priceCurrencySymbol: r'$',
sku: 'sku',
subscriptionPeriod: 'subscriptionPeriod',
title: 'title',
Expand Down Expand Up @@ -139,6 +140,7 @@ Map<String, dynamic> buildSkuMap(SkuDetailsWrapper original) {
'price': original.price,
'priceAmountMicros': original.priceAmountMicros,
'priceCurrencyCode': original.priceCurrencyCode,
'priceCurrencySymbol': original.priceCurrencySymbol,
'sku': original.sku,
'subscriptionPeriod': original.subscriptionPeriod,
'title': original.title,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ void main() {
expect(response.productDetails.first.description,
dummySkuDetails.description);
expect(response.productDetails.first.price, dummySkuDetails.price);
expect(response.productDetails.first.currencySymbol, r'$');
});

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