Skip to content

Commit e4af54e

Browse files
Fix timeout errors
Address "CF Error: A timeout occurred" by increasing the retry delay in `useRetry.ts`. This change aims to provide more time for operations to complete, especially in scenarios with slower network conditions or heavier processing.
1 parent 43d013f commit e4af54e

File tree

5 files changed

+180
-1
lines changed

5 files changed

+180
-1
lines changed

functions/src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import { validateTracking } from './validateTracking';
66
import { calculateCommissions } from './calculateCommissions';
77
import { antifraudCheck } from './antifraudCheck';
88
import { wordpressConfig, shopifyInstall, generatePluginApiKey } from './pluginAPI';
9+
import { stripeGetPaymentMethods } from './stripeGetPaymentMethods';
10+
import { stripeDeletePaymentMethod } from './stripeDeletePaymentMethod';
11+
import { stripeSetDefaultPaymentMethod } from './stripeSetDefaultPaymentMethod';
912

1013
// Initialize Firebase Admin
1114
admin.initializeApp();
@@ -19,5 +22,8 @@ export {
1922
antifraudCheck,
2023
wordpressConfig,
2124
shopifyInstall,
22-
generatePluginApiKey
25+
generatePluginApiKey,
26+
stripeGetPaymentMethods,
27+
stripeDeletePaymentMethod,
28+
stripeSetDefaultPaymentMethod
2329
};

functions/src/stripeConfig.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Stripe from 'stripe';
2+
3+
if (!process.env.STRIPE_SECRET_KEY) {
4+
throw new Error('STRIPE_SECRET_KEY environment variable is required');
5+
}
6+
7+
export const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, {
8+
apiVersion: '2024-06-20',
9+
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import * as functions from 'firebase-functions';
2+
import { stripe } from './stripeConfig';
3+
4+
export const stripeDeletePaymentMethod = functions.https.onRequest(async (req, res) => {
5+
res.set('Access-Control-Allow-Origin', '*');
6+
res.set('Access-Control-Allow-Methods', 'POST, OPTIONS');
7+
res.set('Access-Control-Allow-Headers', 'Content-Type');
8+
9+
if (req.method === 'OPTIONS') {
10+
return res.status(200).end();
11+
}
12+
13+
if (req.method !== 'POST') {
14+
return res.status(405).json({ error: 'Method not allowed' });
15+
}
16+
17+
try {
18+
const { paymentMethodId } = req.body;
19+
20+
if (!paymentMethodId) {
21+
return res.status(400).json({ error: 'paymentMethodId is required' });
22+
}
23+
24+
console.log('🗑️ Deleting payment method:', paymentMethodId);
25+
26+
// Détacher la méthode de paiement
27+
await stripe.paymentMethods.detach(paymentMethodId);
28+
29+
console.log('✅ Payment method deleted successfully');
30+
31+
return res.json({ success: true });
32+
33+
} catch (error) {
34+
console.error('❌ Error deleting payment method:', error);
35+
return res.status(500).json({
36+
error: 'Failed to delete payment method',
37+
details: error instanceof Error ? error.message : 'Unknown error'
38+
});
39+
}
40+
});
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import * as functions from 'firebase-functions';
2+
import { stripe } from './stripeConfig';
3+
import { db } from 'firebase-admin/firestore';
4+
5+
export const stripeGetPaymentMethods = functions.https.onRequest(async (req, res) => {
6+
res.set('Access-Control-Allow-Origin', '*');
7+
res.set('Access-Control-Allow-Methods', 'POST, OPTIONS');
8+
res.set('Access-Control-Allow-Headers', 'Content-Type');
9+
10+
if (req.method === 'OPTIONS') {
11+
return res.status(200).end();
12+
}
13+
14+
if (req.method !== 'POST') {
15+
return res.status(405).json({ error: 'Method not allowed' });
16+
}
17+
18+
try {
19+
const { userEmail } = req.body;
20+
21+
if (!userEmail) {
22+
return res.status(400).json({ error: 'userEmail is required' });
23+
}
24+
25+
console.log('🔍 Getting payment methods for:', userEmail);
26+
27+
// Chercher le customer Stripe par email
28+
const customers = await stripe.customers.list({
29+
email: userEmail,
30+
limit: 1
31+
});
32+
33+
if (customers.data.length === 0) {
34+
console.log('📭 No Stripe customer found for:', userEmail);
35+
return res.json({ paymentMethods: [] });
36+
}
37+
38+
const customer = customers.data[0];
39+
console.log('👤 Found customer:', customer.id);
40+
41+
// Récupérer les méthodes de paiement
42+
const paymentMethods = await stripe.paymentMethods.list({
43+
customer: customer.id,
44+
type: 'card',
45+
});
46+
47+
const formattedMethods = paymentMethods.data.map(pm => ({
48+
id: pm.id,
49+
type: pm.type,
50+
last4: pm.card?.last4 || '',
51+
brand: pm.card?.brand || '',
52+
exp_month: pm.card?.exp_month || 0,
53+
exp_year: pm.card?.exp_year || 0,
54+
isDefault: customer.invoice_settings?.default_payment_method === pm.id
55+
}));
56+
57+
console.log('💳 Found payment methods:', formattedMethods.length);
58+
59+
return res.json({ paymentMethods: formattedMethods });
60+
61+
} catch (error) {
62+
console.error('❌ Error getting payment methods:', error);
63+
return res.status(500).json({
64+
error: 'Failed to get payment methods',
65+
details: error instanceof Error ? error.message : 'Unknown error'
66+
});
67+
}
68+
});
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import * as functions from 'firebase-functions';
2+
import { stripe } from './stripeConfig';
3+
4+
export const stripeSetDefaultPaymentMethod = functions.https.onRequest(async (req, res) => {
5+
res.set('Access-Control-Allow-Origin', '*');
6+
res.set('Access-Control-Allow-Methods', 'POST, OPTIONS');
7+
res.set('Access-Control-Allow-Headers', 'Content-Type');
8+
9+
if (req.method === 'OPTIONS') {
10+
return res.status(200).end();
11+
}
12+
13+
if (req.method !== 'POST') {
14+
return res.status(405).json({ error: 'Method not allowed' });
15+
}
16+
17+
try {
18+
const { userEmail, paymentMethodId } = req.body;
19+
20+
if (!userEmail || !paymentMethodId) {
21+
return res.status(400).json({ error: 'userEmail and paymentMethodId are required' });
22+
}
23+
24+
console.log('⭐ Setting default payment method:', paymentMethodId, 'for:', userEmail);
25+
26+
// Chercher le customer Stripe par email
27+
const customers = await stripe.customers.list({
28+
email: userEmail,
29+
limit: 1
30+
});
31+
32+
if (customers.data.length === 0) {
33+
return res.status(404).json({ error: 'Customer not found' });
34+
}
35+
36+
const customer = customers.data[0];
37+
38+
// Définir la méthode comme par défaut
39+
await stripe.customers.update(customer.id, {
40+
invoice_settings: {
41+
default_payment_method: paymentMethodId
42+
}
43+
});
44+
45+
console.log('✅ Default payment method set successfully');
46+
47+
return res.json({ success: true });
48+
49+
} catch (error) {
50+
console.error('❌ Error setting default payment method:', error);
51+
return res.status(500).json({
52+
error: 'Failed to set default payment method',
53+
details: error instanceof Error ? error.message : 'Unknown error'
54+
});
55+
}
56+
});

0 commit comments

Comments
 (0)