Donations: Select default currency via user's phone number

Co-authored-by: Scott Nonnenberg <scott@signal.org>
This commit is contained in:
automated-signal 2025-07-28 17:00:13 -05:00 committed by GitHub
parent ca0d489beb
commit 4dadf2c02b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 91 additions and 3 deletions

View file

@ -3367,6 +3367,30 @@ Signal Desktop makes use of the following open source projects.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
## country-codes-list
MIT License
Copyright (c) 2019 Luciano
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
## credit-card-type
Copyright (c) 2009-2018 Braintree, a division of PayPal, Inc.

View file

@ -141,6 +141,7 @@
"classnames": "2.5.1",
"config": "3.3.12",
"copy-text-to-clipboard": "2.1.0",
"country-codes-list": "2.0.0",
"credit-card-type": "10.0.2",
"dashdash": "2.0.0",
"direction": "1.0.4",

8
pnpm-lock.yaml generated
View file

@ -188,6 +188,9 @@ importers:
copy-text-to-clipboard:
specifier: 2.1.0
version: 2.1.0
country-codes-list:
specifier: 2.0.0
version: 2.0.0
credit-card-type:
specifier: 10.0.2
version: 10.0.2
@ -4545,6 +4548,9 @@ packages:
typescript:
optional: true
country-codes-list@2.0.0:
resolution: {integrity: sha512-KZqq/LBdCD76hQCa6nOx0bA/nIjYly1OtV8+Bbt/4SW+mJEqGk7oZHjUj7PRrV0gXJJKs6Tv2cIntFdofBByvA==}
crc@3.8.0:
resolution: {integrity: sha512-iX3mfgcTMIq3ZKLIsVFAbv7+Mc10kxabAGQb8HvjA1o3T1PIYprbakQ65d3I+2HGHt6nSKkM9PYjgoJO2KcFBQ==}
@ -14627,6 +14633,8 @@ snapshots:
optionalDependencies:
typescript: 5.6.3
country-codes-list@2.0.0: {}
crc@3.8.0:
dependencies:
buffer: 5.7.1

View file

@ -218,6 +218,7 @@ function renderDonationsPane(props: {
i18n={i18n}
contentsRef={props.contentsRef}
clearWorkflow={action('clearWorkflow')}
initialCurrency="USD"
isStaging
page={props.page}
setPage={props.setPage}

View file

@ -65,6 +65,7 @@ const SUPPORT_URL = 'https://support.signal.org/hc/requests/new?desktop';
export type PropsDataType = {
i18n: LocalizerType;
initialCurrency: string;
donationAmountsConfig: OneTimeDonationHumanAmounts | undefined;
lastError: DonationErrorType | undefined;
validCurrencies: ReadonlyArray<string>;
@ -87,6 +88,7 @@ export type PropsType = PropsDataType & PropsActionType & PropsHousekeepingType;
export function PreferencesDonateFlow({
contentsRef,
i18n,
initialCurrency,
donationAmountsConfig,
lastError,
validCurrencies,
@ -106,7 +108,7 @@ export function PreferencesDonateFlow({
const [step, setStep] = useState<'amount' | 'paymentDetails'>('amount');
const [amount, setAmount] = useState<HumanDonationAmount>();
const [currency, setCurrency] = useState<string>();
const [currency, setCurrency] = useState<string>(initialCurrency);
const [cardExpiration, setCardExpiration] = useState('');
const [cardNumber, setCardNumber] = useState('');
const [cardCvc, setCardCvc] = useState('');

View file

@ -47,6 +47,7 @@ type PropsExternalType = {
export type PropsDataType = {
i18n: LocalizerType;
initialCurrency: string;
isStaging: boolean;
page: SettingsPage;
lastError: DonationErrorType | undefined;
@ -452,6 +453,7 @@ function PreferencesReceiptList({
export function PreferencesDonations({
contentsRef,
i18n,
initialCurrency,
isStaging,
page,
workflow,
@ -581,6 +583,7 @@ export function PreferencesDonations({
<PreferencesDonateFlow
contentsRef={contentsRef}
i18n={i18n}
initialCurrency={initialCurrency}
donationAmountsConfig={donationAmountsConfig}
lastError={lastError}
validCurrencies={validCurrencies}
@ -612,6 +615,7 @@ export function PreferencesDonations({
generateDonationReceiptBlob={generateDonationReceiptBlob}
showToast={showToast}
isStaging={isStaging}
initialCurrency={initialCurrency}
page={page}
lastError={lastError}
workflow={workflow}

View file

@ -12,6 +12,7 @@ import {
ReceiptSerial,
ServerPublicParams,
} from '@signalapp/libsignal-client/zkgroup';
import * as countryCodes from 'country-codes-list';
import * as Bytes from '../Bytes';
import * as Errors from '../types/errors';
@ -42,6 +43,7 @@ import type {
} from '../types/Donations';
import { ToastType } from '../types/Toast';
import { NavTab, SettingsPage } from '../types/Nav';
import { getRegionCodeForNumber } from '../util/libphonenumberUtil';
const { createDonationReceipt } = DataWriter;
@ -955,3 +957,9 @@ function isCredentialValid(credential: ReceiptCredential): boolean {
return true;
}
export function phoneNumberToCurrencyCode(e164: string): string {
const regionCode = getRegionCodeForNumber(e164) ?? 'US';
const countryData = countryCodes.findOne('countryCode', regionCode);
return countryData?.currencyCode ?? 'USD';
}

View file

@ -6,7 +6,7 @@ import { useSelector } from 'react-redux';
import type { MutableRefObject } from 'react';
import { getIntl, getTheme } from '../selectors/user';
import { getIntl, getTheme, getUserNumber } from '../selectors/user';
import { getMe } from '../selectors/conversations';
import { PreferencesDonations } from '../../components/PreferencesDonations';
import type { SettingsPage } from '../../types/Nav';
@ -19,6 +19,7 @@ import { getDonationHumanAmounts } from '../../util/subscriptionConfiguration';
import { drop } from '../../util/drop';
import type { OneTimeDonationHumanAmounts } from '../../types/Donations';
import { getPreferredBadgeSelector } from '../selectors/badges';
import { phoneNumberToCurrencyCode } from '../../services/donations';
export const SmartPreferencesDonations = memo(
function SmartPreferencesDonations({
@ -45,6 +46,7 @@ export const SmartPreferencesDonations = memo(
const { clearWorkflow, submitDonation, updateLastError } =
useDonationsActions();
const ourNumber = useSelector(getUserNumber);
const { badges, color, firstName, profileAvatarUrl } = useSelector(getMe);
const badge = getPreferredBadge(badges);
@ -67,6 +69,13 @@ export const SmartPreferencesDonations = memo(
drop(loadDonationAmounts());
}, []);
const currencyFromPhone = ourNumber
? phoneNumberToCurrencyCode(ourNumber).toLowerCase()
: 'usd';
const initialCurrency = validCurrencies.includes(currencyFromPhone)
? currencyFromPhone
: 'usd';
return (
<PreferencesDonations
i18n={i18n}
@ -81,6 +90,7 @@ export const SmartPreferencesDonations = memo(
validCurrencies={validCurrencies}
showToast={showToast}
contentsRef={contentsRef}
initialCurrency={initialCurrency}
isStaging={isStaging}
page={page}
lastError={donationsState.lastError}

View file

@ -0,0 +1,28 @@
// Copyright 2025 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { assert } from 'chai';
import { phoneNumberToCurrencyCode } from '../../services/donations';
describe('donations', () => {
describe('phoneNumberToCurrency', () => {
it('handles US phone number', async () => {
assert.strictEqual(phoneNumberToCurrencyCode('+18055550000'), 'USD');
});
it('handles Canada phone number', async () => {
assert.strictEqual(phoneNumberToCurrencyCode('+17805550000'), 'CAD');
});
it('handles Puerto Rico phone number', async () => {
assert.strictEqual(phoneNumberToCurrencyCode('+17875550000'), 'USD');
});
it('handles Guam phone number', async () => {
assert.strictEqual(phoneNumberToCurrencyCode('+16715550000'), 'USD');
});
it('handles Aruba phone number', async () => {
assert.strictEqual(phoneNumberToCurrencyCode('+2972870550'), 'AWG');
});
it('handles New Zealand phone number', async () => {
assert.strictEqual(phoneNumberToCurrencyCode('+6492221111;'), 'NZD');
});
});
});

View file

@ -40,7 +40,9 @@ export function getCountryCode(
return undefined;
}
return instance.parse(phoneNumber).getCountryCode();
const parsed = instance.parse(phoneNumber);
return parsed.getCountryCode();
} catch (error) {
const errorText = Errors.toLogFormat(error);
log.info(