From 4dadf2c02b1957e2df163393ef8000c4f617c23b Mon Sep 17 00:00:00 2001 From: automated-signal <37887102+automated-signal@users.noreply.github.com> Date: Mon, 28 Jul 2025 17:00:13 -0500 Subject: [PATCH] Donations: Select default currency via user's phone number Co-authored-by: Scott Nonnenberg --- ACKNOWLEDGMENTS.md | 24 +++++++++++++++++++++ package.json | 1 + pnpm-lock.yaml | 8 +++++++ ts/components/Preferences.stories.tsx | 1 + ts/components/PreferencesDonateFlow.tsx | 4 +++- ts/components/PreferencesDonations.tsx | 4 ++++ ts/services/donations.ts | 8 +++++++ ts/state/smart/PreferencesDonations.tsx | 12 ++++++++++- ts/test-node/services/donations_test.ts | 28 +++++++++++++++++++++++++ ts/types/PhoneNumber.ts | 4 +++- 10 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 ts/test-node/services/donations_test.ts diff --git a/ACKNOWLEDGMENTS.md b/ACKNOWLEDGMENTS.md index c1b2d54bc1..30c1ec34e2 100644 --- a/ACKNOWLEDGMENTS.md +++ b/ACKNOWLEDGMENTS.md @@ -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. diff --git a/package.json b/package.json index 14f638af80..7c90ed5f73 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3d16c20876..c2903baf85 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -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 diff --git a/ts/components/Preferences.stories.tsx b/ts/components/Preferences.stories.tsx index 43f0443d4f..ad2343a9e5 100644 --- a/ts/components/Preferences.stories.tsx +++ b/ts/components/Preferences.stories.tsx @@ -218,6 +218,7 @@ function renderDonationsPane(props: { i18n={i18n} contentsRef={props.contentsRef} clearWorkflow={action('clearWorkflow')} + initialCurrency="USD" isStaging page={props.page} setPage={props.setPage} diff --git a/ts/components/PreferencesDonateFlow.tsx b/ts/components/PreferencesDonateFlow.tsx index 7849d3f5b5..08c3b74495 100644 --- a/ts/components/PreferencesDonateFlow.tsx +++ b/ts/components/PreferencesDonateFlow.tsx @@ -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; @@ -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(); - const [currency, setCurrency] = useState(); + const [currency, setCurrency] = useState(initialCurrency); const [cardExpiration, setCardExpiration] = useState(''); const [cardNumber, setCardNumber] = useState(''); const [cardCvc, setCardCvc] = useState(''); diff --git a/ts/components/PreferencesDonations.tsx b/ts/components/PreferencesDonations.tsx index 3cc29a7cf7..d1718cb7bc 100644 --- a/ts/components/PreferencesDonations.tsx +++ b/ts/components/PreferencesDonations.tsx @@ -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({ { + 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'); + }); + }); +}); diff --git a/ts/types/PhoneNumber.ts b/ts/types/PhoneNumber.ts index 9024415682..e363e33678 100644 --- a/ts/types/PhoneNumber.ts +++ b/ts/types/PhoneNumber.ts @@ -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(