Skip to content
Open
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
Expand Up @@ -24,6 +24,7 @@
import com.badlogic.gdx.pay.PurchaseManagerConfig;
import com.badlogic.gdx.pay.Transaction;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.ParseException;

Expand Down Expand Up @@ -73,14 +74,16 @@ static Transaction convertReceiptToTransaction(int i, String requestId, Receipt
static Information convertProductToInformation(Product product) {

String priceString = product.getPrice();
Float priceAsFloat = tryParsePrice(priceString);
Float priceAsFloat = tryParsePriceToFloat(priceString);
BigDecimal priceAsBigDecimal = tryParsePriceToBigDecimal(priceString);
return Information.newBuilder()
.localName(product.getTitle())
.localDescription(product.getDescription())
.localPricing(priceString)
.priceCurrencyCode(tryParseCurrency(priceString))
.priceInCents(convertToPriceInCents(priceAsFloat))
.priceAsDouble(priceAsFloat == null ? null : priceAsFloat.doubleValue())
.priceAsBigDecimal(priceAsBigDecimal)
.build();
}

Expand All @@ -103,19 +106,39 @@ static Integer convertToPriceInCents(Float priceAsFloat) {
return priceAsFloat == null ? null : MathUtils.ceilPositive(priceAsFloat * 100);
}

private static Float tryParsePrice(String priceString) {
private static String tryParsePrice(String priceString) {
if (priceString == null || priceString.length() == 0)
return null;
try {
// Remove currency from string
// The ugly way
priceString = priceString.substring(1);

// Remaining should be parseable
// Remove currency from string
// The ugly way
priceString = priceString.substring(1);

// Remaining should be parseable
return priceString;
}

private static Float tryParsePriceToFloat(String priceString) {
priceString = tryParsePrice(priceString);
if (priceString == null)
return null;
try {
return NumberFormat.getInstance().parse(priceString).floatValue();
} catch (ParseException exception) {
// Silenced
}
return null;
}

private static BigDecimal tryParsePriceToBigDecimal(String priceString) {
priceString = tryParsePrice(priceString);
if (priceString == null)
return null;
try {
return new BigDecimal(priceString);
} catch (NumberFormatException exception) {
// Silenced
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

Expand Down Expand Up @@ -231,6 +233,7 @@ private void convertSubscriptionProductToInformation(Information.Builder builder
.priceCurrencyCode(paidForPricingPhase.getPriceCurrencyCode())
.priceInCents((int) paidForPricingPhase.getPriceAmountMicros() / 10_000)
.priceAsDouble(paidForPricingPhase.getPriceAmountMicros() / 1_000_000.0)
.priceAsBigDecimal((new BigDecimal(paidForPricingPhase.getPriceAmountMicros())).divide(BigDecimal.valueOf(1_000_000), 6, RoundingMode.FLOOR).stripTrailingZeros())
;

ProductDetails.PricingPhase freeTrialSubscriptionPhase = getFreeTrialSubscriptionPhase(details.getPricingPhases());
Expand Down Expand Up @@ -287,7 +290,8 @@ private static void convertOneTimeProductToInformation(Information.Builder build
.localPricing(priceString)
.priceCurrencyCode(oneTimePurchaseDetails.getPriceCurrencyCode())
.priceInCents((int) (oneTimePurchaseDetails.getPriceAmountMicros() / 10_000))
.priceAsDouble(oneTimePurchaseDetails.getPriceAmountMicros() / 1_000_000.0);
.priceAsDouble(oneTimePurchaseDetails.getPriceAmountMicros() / 1_000_000.0)
.priceAsBigDecimal((new BigDecimal(oneTimePurchaseDetails.getPriceAmountMicros())).divide(BigDecimal.valueOf(1_000_000), 6, RoundingMode.FLOOR).stripTrailingZeros());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

import org.json.JSONException;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;

class HuaweiPurchaseManagerUtils {
Expand Down Expand Up @@ -68,6 +70,7 @@ static Information buildInformation(ProductInfo productInfo) {
.localPricing(priceString)
.priceCurrencyCode(productInfo.getCurrency())
.priceAsDouble(productInfo.getMicrosPrice() / 1_000_000.0)
.priceAsBigDecimal((new BigDecimal(productInfo.getMicrosPrice())).divide(BigDecimal.valueOf(1_000_000), 6, RoundingMode.FLOOR).stripTrailingZeros())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.robovm.apple.storekit.*;

import javax.annotation.Nullable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
Expand Down Expand Up @@ -535,6 +536,7 @@ public Information getInformation(String identifier) {
.priceCurrencyCode(p.getPriceLocale().getCurrencyCode())
.priceInCents(MathUtils.ceilPositive(p.getPrice().floatValue() * 100))
.priceAsDouble(p.getPrice().doubleValue())
.priceAsBigDecimal(new BigDecimal(p.getPrice().stringValue()))
.freeTrialPeriod(convertToFreeTrialPeriod(p))
.build();
}
Expand Down
18 changes: 18 additions & 0 deletions gdx-pay/src/main/java/com/badlogic/gdx/pay/Information.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.badlogic.gdx.pay;

import javax.annotation.Nullable;
import java.math.BigDecimal;
import java.util.Currency;

/**
Expand All @@ -27,6 +28,7 @@ public final class Information {
@Deprecated
private Integer priceInCents;
private Double priceAsDouble;
private BigDecimal priceAsBigDecimal;

private String priceCurrencyCode;

Expand All @@ -45,6 +47,7 @@ private Information(Builder builder) {
localPricing = builder.localPricing;
priceInCents = builder.priceInCents;
priceAsDouble = builder.priceAsDouble;
priceAsBigDecimal = builder.priceAsBigDecimal;
priceCurrencyCode = builder.priceCurrencyCode;
freeTrialPeriod = builder.freeTrialPeriod;
}
Expand Down Expand Up @@ -96,6 +99,15 @@ public Double getPriceAsDouble() {
return priceAsDouble;
}

/**
* Price (as a BigDecimal).
* <p>Caution: this field could be null, information is not always available! </p>
*/
@Nullable
public BigDecimal getPriceAsBigDecimal() {
return priceAsBigDecimal;
}

/**
* Price currency code.
* <p>Caution:Note that not all PurchaseManagers set this field!</p>
Expand Down Expand Up @@ -169,6 +181,7 @@ public static final class Builder {
@Deprecated
private Integer priceInCents;
private Double priceAsDouble;
private BigDecimal priceAsBigDecimal;
private String priceCurrencyCode;
private FreeTrialPeriod freeTrialPeriod;

Expand Down Expand Up @@ -210,6 +223,11 @@ public Builder priceAsDouble(Double val) {
return this;
}

public Builder priceAsBigDecimal(BigDecimal val) {
priceAsBigDecimal = val;
return this;
}

public Builder priceCurrencyCode(String val) {
priceCurrencyCode = val;
return this;
Expand Down
Loading