-
Notifications
You must be signed in to change notification settings - Fork 13
staging country code error #1100
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
Merged
kushagrasarathe
merged 1 commit into
peanut-wallet-dev
from
fix/-staging-country-code-error
Aug 15, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
import { countryData, countryCodeMap } from '@/components/AddMoney/consts' | ||
|
||
/** | ||
* Extracts the country name from an IBAN by parsing the first 2 characters (country code) | ||
* @param iban - The IBAN string (with or without spaces) | ||
* @returns The country name if found, null if invalid IBAN or country not found | ||
*/ | ||
export const getCountryFromIban = (iban: string): string | null => { | ||
// Remove spaces and convert to uppercase | ||
const cleanIban = iban.replace(/\s/g, '').toUpperCase() | ||
|
||
// Extract the first 2 characters as country code | ||
const countryCode = cleanIban.substring(0, 2) | ||
|
||
// Try to find country by 2-letter code directly in countryData | ||
let country = countryData.find((c) => c.type === 'country' && c.id === countryCode) | ||
|
||
// If not found, get the 3-letter code and try that | ||
if (!country) { | ||
const threeLetterCode = getCountryCodeForWithdraw(countryCode) | ||
if (threeLetterCode !== countryCode) { | ||
country = countryData.find((c) => c.type === 'country' && c.id === threeLetterCode) | ||
} | ||
} | ||
|
||
return country ? country.title : null | ||
} | ||
|
||
/** | ||
* Validates a US bank account number with comprehensive checks | ||
* @param accountNumber - The bank account number to validate | ||
* @returns Object with isValid boolean and error message if invalid | ||
*/ | ||
export const validateUSBankAccount = (accountNumber: string) => { | ||
// Remove spaces and hyphens for validation | ||
const cleanAccountNumber = accountNumber.replace(/[\s-]/g, '') | ||
|
||
// Check if contains only digits | ||
if (!/^\d+$/.test(cleanAccountNumber)) { | ||
return { | ||
isValid: false, | ||
error: 'Account number must contain only digits', | ||
} | ||
} | ||
|
||
// Check minimum length (US bank accounts are typically 6-17 digits) | ||
if (cleanAccountNumber.length < 6) { | ||
return { | ||
isValid: false, | ||
error: 'Account number must be at least 6 digits', | ||
} | ||
} | ||
|
||
// Check maximum length | ||
if (cleanAccountNumber.length > 17) { | ||
return { | ||
isValid: false, | ||
error: 'Account number cannot exceed 17 digits', | ||
} | ||
} | ||
|
||
// Check for obviously invalid patterns | ||
if (/^0+$/.test(cleanAccountNumber)) { | ||
return { | ||
isValid: false, | ||
error: 'Account number cannot be all zeros', | ||
} | ||
} | ||
|
||
return { | ||
isValid: true, | ||
error: null, | ||
} | ||
} | ||
|
||
/** | ||
* Validates a Mexican CLABE (Clave Bancaria Estandarizada) account number | ||
* CLABE is exactly 18 digits with a specific structure and check digit validation | ||
* @param accountNumber - The CLABE account number to validate | ||
* @returns Object with isValid boolean and error message if invalid | ||
*/ | ||
export const validateMXCLabeAccount = (accountNumber: string) => { | ||
// Remove spaces and hyphens for validation | ||
const cleanAccountNumber = accountNumber.replace(/[\s-]/g, '') | ||
|
||
// Check if contains only digits | ||
if (!/^\d+$/.test(cleanAccountNumber)) { | ||
return { | ||
isValid: false, | ||
error: 'CLABE must contain only digits', | ||
} | ||
} | ||
|
||
// CLABE must be exactly 18 digits | ||
if (cleanAccountNumber.length !== 18) { | ||
return { | ||
isValid: false, | ||
error: 'CLABE must be exactly 18 digits', | ||
} | ||
} | ||
|
||
// Check for obviously invalid patterns | ||
if (/^0+$/.test(cleanAccountNumber)) { | ||
return { | ||
isValid: false, | ||
error: 'CLABE cannot be all zeros', | ||
} | ||
} | ||
|
||
// Validate CLABE check digit using the official algorithm | ||
const digits = cleanAccountNumber.split('').map(Number) | ||
const weights = [3, 7, 1, 3, 7, 1, 3, 7, 1, 3, 7, 1, 3, 7, 1, 3, 7] | ||
|
||
let sum = 0 | ||
for (let i = 0; i < 17; i++) { | ||
sum += digits[i] * weights[i] | ||
} | ||
|
||
const remainder = sum % 10 | ||
const calculatedCheckDigit = remainder === 0 ? 0 : 10 - remainder | ||
const providedCheckDigit = digits[17] | ||
|
||
if (calculatedCheckDigit !== providedCheckDigit) { | ||
return { | ||
isValid: false, | ||
error: 'CLABE check digit is invalid', | ||
} | ||
} | ||
|
||
return { | ||
isValid: true, | ||
error: null, | ||
} | ||
} | ||
|
||
// Returns the 3-letter country code for the given country code | ||
export const getCountryCodeForWithdraw = (country: string) => { | ||
// If the input is already a 3-digit code and exists in the map, return it | ||
if (countryCodeMap[country]) { | ||
return country | ||
} | ||
|
||
// If the input is a 2-digit code, find the corresponding 3-digit code | ||
const threeDigitCode = Object.keys(countryCodeMap).find((key) => countryCodeMap[key] === country) | ||
|
||
return threeDigitCode || country | ||
} | ||
Zishan-7 marked this conversation as resolved.
Show resolved
Hide resolved
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.