-- ============================================================ -- V2.3 — Hub de paiement Wave : intentions_paiement -- Chaque paiement Wave est initié depuis UnionFlow. -- Auteur: UnionFlow Team | BCEAO/OHADA compliant -- ============================================================ CREATE TABLE IF NOT EXISTS intentions_paiement ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), utilisateur_id UUID NOT NULL, organisation_id UUID, -- NULL pour abonnements UnionFlow SA montant_total DECIMAL(14,2) NOT NULL CHECK (montant_total > 0), code_devise VARCHAR(3) NOT NULL DEFAULT 'XOF', type_objet VARCHAR(30) NOT NULL, -- COTISATION|ADHESION|EVENEMENT|ABONNEMENT_UNIONFLOW statut VARCHAR(20) NOT NULL DEFAULT 'INITIEE', -- Wave API wave_checkout_session_id VARCHAR(255) UNIQUE, wave_launch_url VARCHAR(1000), wave_transaction_id VARCHAR(100), -- Traçabilité des objets payés (JSON: [{type,id,montant},...]) objets_cibles TEXT, date_creation TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, date_expiration TIMESTAMP, -- TTL 30 min date_completion TIMESTAMP, -- Métadonnées BaseEntity actif BOOLEAN NOT NULL DEFAULT TRUE, date_modification TIMESTAMP, cree_par VARCHAR(255), modifie_par VARCHAR(255), version BIGINT NOT NULL DEFAULT 0, CONSTRAINT fk_intention_utilisateur FOREIGN KEY (utilisateur_id) REFERENCES utilisateurs(id), CONSTRAINT fk_intention_organisation FOREIGN KEY (organisation_id) REFERENCES organisations(id) ON DELETE SET NULL, CONSTRAINT chk_intention_type CHECK (type_objet IN ('COTISATION','ADHESION','EVENEMENT','ABONNEMENT_UNIONFLOW')), CONSTRAINT chk_intention_statut CHECK (statut IN ('INITIEE','EN_COURS','COMPLETEE','EXPIREE','ECHOUEE')), CONSTRAINT chk_intention_devise CHECK (code_devise ~ '^[A-Z]{3}$') ); CREATE INDEX idx_intention_utilisateur ON intentions_paiement(utilisateur_id); CREATE INDEX idx_intention_statut ON intentions_paiement(statut); CREATE INDEX idx_intention_wave_session ON intentions_paiement(wave_checkout_session_id); CREATE INDEX idx_intention_expiration ON intentions_paiement(date_expiration); -- Supprimer les champs paiement redondants de cotisations (centralisés dans intentions_paiement) ALTER TABLE cotisations DROP COLUMN IF EXISTS methode_paiement, DROP COLUMN IF EXISTS reference_paiement; -- Ajouter le lien cotisation → intention de paiement ALTER TABLE cotisations ADD COLUMN IF NOT EXISTS intention_paiement_id UUID, ADD CONSTRAINT fk_cotisation_intention FOREIGN KEY (intention_paiement_id) REFERENCES intentions_paiement(id) ON DELETE SET NULL; COMMENT ON TABLE intentions_paiement IS 'Hub centralisé Wave : chaque paiement est initié depuis UnionFlow avant appel API Wave'; COMMENT ON COLUMN intentions_paiement.objets_cibles IS 'JSON: liste des objets couverts par ce paiement — ex: 3 cotisations mensuelles'; COMMENT ON COLUMN intentions_paiement.wave_checkout_session_id IS 'ID de session Wave — clé de réconciliation sur réception webhook';