Skip to content
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

Refactor and Tax Adaptation #17

Merged
merged 14 commits into from
Feb 3, 2025
6 changes: 4 additions & 2 deletions src/services/stripe.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { loadStripe } from '@stripe/stripe-js';
import Stripe from 'stripe';

import { calculateCurrencyAmount } from '@/utils/calculateCurrencyAmount';

export default class StripeService {
private stripe: Stripe;

Expand Down Expand Up @@ -36,9 +38,9 @@ export default class StripeService {
metadata: { userId, plan },
};

if (currency && currency !== 'usd') {
if (currency) {
const unitAmount = price.unit_amount;
const newUnitAmount = unitAmount ? unitAmount * 6 : 0;
const newUnitAmount = calculateCurrencyAmount(String(unitAmount), currency);

sessionData.line_items = [{
price_data: {
Expand Down
14 changes: 14 additions & 0 deletions src/utils/calculateCurrencyAmount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const currencyRates: { [key: string]: number } = {
usd: 1,
brl: 6,
};

export const calculateCurrencyAmount = (amount: string, currency: string = 'usd'): number => {
const rate = currencyRates[currency.toLowerCase()];

if (rate === undefined) {
throw new Error(`Currency not supported: ${currency}`);
}

return Number(amount) * rate;
};
8 changes: 5 additions & 3 deletions src/utils/transformPurchasePlansDTO.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { calculateCurrencyAmount } from "./calculateCurrencyAmount";

export interface InputData {
id: string;
productName: string;
Expand All @@ -21,7 +23,7 @@ interface Plan {

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

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

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

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


if (interval === 'month') {
Expand Down
Loading