Skip to content

Commit 56d3dc8

Browse files
Refactor and Tax Adaptation (#17)
* chore: add sections translations * feat: add ssr locale function * refactor: language selector component * feat: add all home translation * feat: add i18n in terms and privacy page * chore: translate all auth domain * chore: add i18n in payment area * chore: add dashboard translation * feat: add portugues i18n * chore: add currency in client * chore: refactor constants * refactor: create optional tax adapt currency * chore: solving conflits 2
1 parent 416ee55 commit 56d3dc8

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

src/services/stripe.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { loadStripe } from '@stripe/stripe-js';
22
import Stripe from 'stripe';
33

4+
import { calculateCurrencyAmount } from '@/utils/calculateCurrencyAmount';
5+
46
export default class StripeService {
57
private stripe: Stripe;
68

@@ -36,9 +38,9 @@ export default class StripeService {
3638
metadata: { userId, plan },
3739
};
3840

39-
if (currency && currency !== 'usd') {
41+
if (currency) {
4042
const unitAmount = price.unit_amount;
41-
const newUnitAmount = unitAmount ? unitAmount * 6 : 0;
43+
const newUnitAmount = calculateCurrencyAmount(String(unitAmount), currency);
4244

4345
sessionData.line_items = [{
4446
price_data: {

src/utils/calculateCurrencyAmount.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const currencyRates: { [key: string]: number } = {
2+
usd: 1,
3+
brl: 6,
4+
};
5+
6+
export const calculateCurrencyAmount = (amount: string, currency: string = 'usd'): number => {
7+
const rate = currencyRates[currency.toLowerCase()];
8+
9+
if (rate === undefined) {
10+
throw new Error(`Currency not supported: ${currency}`);
11+
}
12+
13+
return Number(amount) * rate;
14+
};

src/utils/transformPurchasePlansDTO.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { calculateCurrencyAmount } from "./calculateCurrencyAmount";
2+
13
export interface InputData {
24
id: string;
35
productName: string;
@@ -21,7 +23,7 @@ interface Plan {
2123

2224
type PlanDetails = Omit<Plan, 'priceMonthly' | 'priceAnnual' | 'idMonthly' | 'idAnnual'>;
2325

24-
export function transformPurchasePlansDTO(data: InputData[], translate: (key: string) => string, currency?: string): Plan[] {
26+
export function transformPurchasePlansDTO(data: InputData[], translate: (key: string) => string, currency: string): Plan[] {
2527
const planDetails: Record<string, PlanDetails> = {
2628
"Starter": {
2729
id: "starter",
@@ -60,8 +62,8 @@ export function transformPurchasePlansDTO(data: InputData[], translate: (key: st
6062

6163
const plansMap: Record<string, Plan> = {};
6264

63-
const setPlanPrice = (planName: string, interval: 'month' | 'year', amount: string, id: string, currency: string = 'usd'): void => {
64-
const newUnitAmount = currency !== 'usd' ? Number(amount) * 6 : amount;
65+
const setPlanPrice = (planName: string, interval: 'month' | 'year', amount: string, id: string, currency: string): void => {
66+
const newUnitAmount = calculateCurrencyAmount(amount, currency);
6567

6668

6769
if (interval === 'month') {

0 commit comments

Comments
 (0)