mirror of
https://github.com/signalapp/Signal-Server
synced 2025-12-27 15:29:10 +00:00
Missing Money.nanos should be treated as 0
This commit is contained in:
committed by
ravi-signal
parent
b2dd315177
commit
efde8a31f9
@@ -80,7 +80,9 @@ public class SubscriptionCurrencyUtil {
|
||||
* BigDecimal)
|
||||
*/
|
||||
static BigDecimal convertGoogleMoneyToApiAmount(final Money money) {
|
||||
final BigDecimal fractionalComponent = BigDecimal.valueOf(money.getNanos()).scaleByPowerOfTen(-9);
|
||||
final BigDecimal fractionalComponent = money.getNanos() == null
|
||||
? BigDecimal.ZERO
|
||||
: BigDecimal.valueOf(money.getNanos()).scaleByPowerOfTen(-9);
|
||||
final BigDecimal amount = BigDecimal.valueOf(money.getUnits()).add(fractionalComponent);
|
||||
return convertConfiguredAmountToApiAmount(money.getCurrencyCode(), amount);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright 2025 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
package org.whispersystems.textsecuregcm.subscriptions;
|
||||
|
||||
import com.google.api.services.androidpublisher.model.Money;
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.assertj.core.data.Percentage;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class SubscriptionCurrencyUtilTest {
|
||||
|
||||
@Test
|
||||
void convertGoogleMoneyToApiAmount() {
|
||||
Money money = new Money();
|
||||
money.setCurrencyCode("USD");
|
||||
money.setUnits(4L);
|
||||
|
||||
BigDecimal amt = SubscriptionCurrencyUtil.convertGoogleMoneyToApiAmount(money);
|
||||
Assertions.assertThat(amt).isCloseTo(BigDecimal.valueOf(400), Percentage.withPercentage(0.0001));
|
||||
|
||||
money.setNanos(990000000);
|
||||
amt = SubscriptionCurrencyUtil.convertGoogleMoneyToApiAmount(money);
|
||||
Assertions.assertThat(amt).isCloseTo(BigDecimal.valueOf(499), Percentage.withPercentage(0.0001));
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user