1-
21import { useAuth } from '@/hooks/useAuth' ;
32import { useCampaigns } from '@/hooks/useCampaigns' ;
43import { useStripePayment } from '@/hooks/useStripePayment' ;
54import { usePaymentMethods } from '@/hooks/usePaymentMethods' ;
65import { useToast } from '@/hooks/use-toast' ;
76import { CampaignFormData } from './useCampaignFormState' ;
8- import { functions } from '@/lib/firebase' ;
9- import { httpsCallable } from 'firebase/functions' ;
107
118export const useCampaignFormSubmission = (
129 formData : CampaignFormData ,
@@ -17,20 +14,19 @@ export const useCampaignFormSubmission = (
1714 const { user } = useAuth ( ) ;
1815 const { createCampaign } = useCampaigns ( ) ;
1916 const { setupPaymentForCampaign } = useStripePayment ( ) ;
20- const { refreshPaymentMethods } = usePaymentMethods ( ) ;
17+ const { refreshPaymentMethods, paymentMethods } = usePaymentMethods ( ) ;
2118 const { toast } = useToast ( ) ;
2219
2320 const redirectToStripeForNewCard = async ( campaignData : CampaignFormData ) => {
24- console . log ( '🎯 NOUVEAU FLOW : Redirection vers Stripe SANS créer la campagne' ) ;
21+ console . log ( '🎯 SUPABASE : Redirection vers Stripe SANS créer la campagne' ) ;
2522
2623 try {
27- // Store campaign data securely with encryption
24+ // Store campaign data securely
2825 import ( '@/utils/secureClientStorage' ) . then ( ( { secureStorage } ) => {
29- secureStorage . setCampaignData ( 'pendingCampaignData' , campaignData , 2 ) ; // 2 hours expiry
26+ secureStorage . setCampaignData ( 'pendingCampaignData' , campaignData , 2 ) ;
3027 console . log ( '🔒 Campaign data stored securely' ) ;
3128 } ) ;
3229
33- // Stocker aussi dans le state pour le flow normal
3430 setPendingCampaignData ( campaignData ) ;
3531
3632 // Rediriger vers Stripe avec un ID temporaire
@@ -53,66 +49,65 @@ export const useCampaignFormSubmission = (
5349 } ;
5450
5551 const createCampaignWithExistingCard = async ( campaignData : CampaignFormData , cardId : string ) => {
56- console . log ( '🎯 Création campagne avec carte existante validée:' , cardId ) ;
57-
58- // Créer la campagne directement finalisée car la carte est déjà validée
59- const campaignId = await createCampaign ( {
60- ...campaignData ,
61- isDraft : false ,
62- paymentConfigured : true ,
63- defaultCommissionRate : 10 ,
64- stripePaymentMethodId : cardId ,
65- } ) ;
52+ console . log ( '🎯 SUPABASE: Création campagne avec carte existante validée:' , cardId ) ;
6653
67- console . log ( '✅ Campagne créée et finalisée:' , campaignId ) ;
68- return campaignId ;
54+ try {
55+ // Utiliser la nouvelle Edge Function Supabase pour finaliser la campagne
56+ const { supabase } = await import ( '@/integrations/supabase/client' ) ;
57+
58+ const { data, error } = await supabase . functions . invoke ( 'finalize-campaign' , {
59+ body : {
60+ campaignData : {
61+ ...campaignData ,
62+ defaultCommissionRate : 10
63+ } ,
64+ cardId
65+ }
66+ } ) ;
67+
68+ if ( error ) {
69+ throw new Error ( error . message || 'Failed to create campaign' ) ;
70+ }
71+
72+ console . log ( '✅ SUPABASE: Campagne créée et finalisée:' , data . campaign . id ) ;
73+ return data . campaign . id ;
74+ } catch ( error ) {
75+ console . error ( '❌ SUPABASE: Erreur création campagne:' , error ) ;
76+ throw error ;
77+ }
6978 } ;
7079
7180 const handleSubmit = async ( e : React . FormEvent ) => {
7281 e . preventDefault ( ) ;
7382 setLoading ( true ) ;
7483
7584 try {
76- console . log ( '🎯 FLOW CORRIGÉ: Validation AVANT création de campagne...' ) ;
77-
78- if ( ! formData . name ) {
79- throw new Error ( 'Le nom de la campagne est requis' ) ;
80- }
85+ console . log ( '🎯 SUPABASE: Validation AVANT création de campagne...' ) ;
8186
82- if ( ! formData . targetUrl ) {
83- throw new Error ( 'L \'URL de destination est requise ' ) ;
87+ if ( ! formData . name || ! formData . targetUrl ) {
88+ throw new Error ( 'Le nom et l \'URL de la campagne sont requis ' ) ;
8489 }
8590
86- // Vérifier les cartes disponibles
87- console . log ( '🔄 Vérification des cartes existantes...' ) ;
91+ // Vérifier les cartes disponibles via Supabase
92+ console . log ( '🔄 SUPABASE: Vérification des cartes existantes...' ) ;
8893 await refreshPaymentMethods ( ) ;
8994
90- // Attendre pour s'assurer de la synchronisation
91- await new Promise ( resolve => setTimeout ( resolve , 500 ) ) ;
92-
93- // Récupérer les données fraîches via Firebase
94- const getPaymentMethods = httpsCallable ( functions , 'stripeGetPaymentMethods' ) ;
95- const freshCardsResponse = await getPaymentMethods ( { userEmail : user ?. email } ) ;
96- const freshCardsData = freshCardsResponse . data as { paymentMethods ?: any [ ] } ;
97-
98- const availableCards = freshCardsData . paymentMethods || [ ] ;
99-
100- console . log ( '💳 Cartes disponibles:' , availableCards . length ) ;
95+ console . log ( '💳 Cartes disponibles:' , paymentMethods . length ) ;
10196
102- if ( availableCards . length === 0 ) {
103- console . log ( '💳 FLOW CORRIGÉ : Aucune carte → Redirection Stripe (PAS de création campagne) ' ) ;
97+ if ( paymentMethods . length === 0 ) {
98+ console . log ( '💳 SUPABASE : Aucune carte → Redirection Stripe' ) ;
10499 await redirectToStripeForNewCard ( formData ) ;
105100 return ;
106101 }
107102
108- // 🔥 CORRECTION: TOUJOURS afficher le sélecteur, même avec une seule carte
109- console . log ( '💳 FLOW CORRIGÉ : Cartes disponibles → TOUJOURS afficher le sélecteur' ) ;
103+ // Afficher le sélecteur de cartes
104+ console . log ( '💳 SUPABASE : Cartes disponibles → Afficher le sélecteur' ) ;
110105 setPendingCampaignData ( formData ) ;
111106 setShowPaymentSelector ( true ) ;
112107 setLoading ( false ) ;
113108
114109 } catch ( error : any ) {
115- console . error ( '❌ Erreur dans le flow corrigé :' , error ) ;
110+ console . error ( '❌ SUPABASE: Erreur dans le flow:' , error ) ;
116111 toast ( {
117112 title : "Erreur" ,
118113 description : error . message || "Impossible de traiter la demande" ,
@@ -127,4 +122,4 @@ export const useCampaignFormSubmission = (
127122 createCampaignWithExistingCard,
128123 redirectToStripeForNewCard,
129124 } ;
130- } ;
125+ } ;
0 commit comments