Skip to content
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

Fix double precision issue and inconsistent return type on Android #885

Merged
merged 2 commits into from
Dec 27, 2019
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: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,10 @@ Each `product` returns from `getProducts()` contains:

Property | iOS | And | Comment
-------- | :-: | :-: | -------
`price` | ✓ | ✓ | Will return localizedPrice on Android (default) or a string price (eg. `1.99`) (iOS).
`price` | ✓ | ✓ | Localized price string, with only number (eg. `1.99`).
`productId` | ✓ | ✓ | Returns a string needed to purchase the item later.
`currency` | ✓ | ✓ | Returns the currency code.
`localizedPrice` | ✓ | ✓ | Use localizedPrice if you want to display the price to the user so you don't need to worry about currency symbols.
`localizedPrice` | ✓ | ✓ | Localized price string, with number and currency symbol (eg. `$1.99`).
`title` | ✓ | ✓ | Returns the title Android and localizedTitle on iOS.
`description` | ✓ | ✓ | Returns the localized description on Android and iOS.
`introductoryPrice` | ✓ | ✓ | Formatted introductory price of a subscription, including its currency sign, such as €3.99.<br>The price doesn't include tax.
Expand Down
13 changes: 11 additions & 2 deletions android/src/main/java/com/dooboolab/RNIap/RNIapModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ObjectAlreadyConsumedException;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -255,7 +256,13 @@ public void onSkuDetailsResponse(BillingResult billingResult, List<SkuDetails> s
for (SkuDetails skuDetails : skuDetailsList) {
WritableMap item = Arguments.createMap();
item.putString("productId", skuDetails.getSku());
item.putDouble("price", skuDetails.getPriceAmountMicros() / 1000000f);
long priceAmountMicros = skuDetails.getPriceAmountMicros();
// Use valueOf instead of constructors.
// See: https://www.javaworld.com/article/2073176/caution--double-to-bigdecimal-in-java.html
BigDecimal priceAmount = BigDecimal.valueOf(priceAmountMicros);
BigDecimal microUnitsDivisor = BigDecimal.valueOf(1000000);
String price = priceAmount.divide(microUnitsDivisor).toString();
item.putString("price", price);
item.putString("currency", skuDetails.getPriceCurrencyCode());
item.putString("type", skuDetails.getType());
item.putString("localizedPrice", skuDetails.getPrice());
Expand All @@ -269,7 +276,9 @@ public void onSkuDetailsResponse(BillingResult billingResult, List<SkuDetails> s
// new
item.putString("iconUrl", skuDetails.getIconUrl());
item.putString("originalJson", skuDetails.getOriginalJson());
item.putDouble("originalPrice", skuDetails.getOriginalPriceAmountMicros() / 1000000f);
BigDecimal originalPriceAmountMicros = BigDecimal.valueOf(skuDetails.getOriginalPriceAmountMicros());
String originalPrice = originalPriceAmountMicros.divide(microUnitsDivisor).toString();
item.putString("originalPrice", originalPrice);
items.pushMap(item);
}

Expand Down