Module banking complet en lecture seule via Powens (ex-Budget Insight)
pour détecter automatiquement les paiements clients et arrêter les
relances dès qu'une facture est payée. Réservé plans Pro / Business,
kill switch global BANKING_ENABLED désactivé en prod tant que le KYC
Powens n'est pas validé (cf. .claude/deploy-memory.md).
Backend (apps/api)
- PowensClient bas niveau : init user, code temporaire 30s, build
webview URL, list/get/delete connections, accounts, transactions,
vérif HMAC SHA-256 timing-safe pour webhook.
- BankingService : ensurePowensUser (chiffrement token via Adonis
encryption / APP_KEY), createWebviewUrl avec state HMAC anti-CSRF
(TTL 10 min), handleCallback (upsert connection + accounts +
fire-and-forget mail + sync 90j + reconcile), disconnect (DELETE
Powens + soft-revoke en DB), setReconciliationMode.
- Réconciliation : match transactions ↔ factures sur montant exact
+ label normalisé (numero ou nom client, NFD strip + alphanum).
Confiance HIGH (label matche) vs LOW (montant seul). Mode auto +
HIGH → invoice.status=paid + bonus rubis + cancel relances +
enqueuePaymentThanks (client) + sendInvoiceAutoPaidNotification
(user). Mode manual ou LOW → match_status='suggested' (UI V2).
- Webhook /webhooks/powens : vérif HMAC, lookup org par
powens_user_id, dispatch CONNECTION_SYNCED / NEW_TRANSACTIONS /
USER_SYNC_ENDED → sync incrémental 7j + reconcile, CONNECTION_ERROR
/ SCA_REQUIRED → update state + last_error. Réponse 200 immédiate
puis processing fire-and-forget pour ne pas timeout côté Powens.
- 4 migrations : bank_connections, bank_accounts, bank_transactions
+ colonnes powens_user_id (chiffré APP_KEY) et reconciliation_mode
sur organizations.
- 2 templates React Email : BankConnectedEmail (post-connection,
récap comptes + lien settings) et InvoiceAutoPaidNotificationEmail
(notif user après match auto, lien direct facture + libellé
bancaire détecté). Toujours en branding Rubis (notif Rubis → user,
jamais marque blanche).
- 2 commandes ace : banking:reconcile (rejoue le reconcile sans
reconnecter la banque) et banking:simulate-payment (injecte une
bank_transaction synthétique qui matche une facture, pour test E2E
sans devoir attendre un vrai virement sandbox).
- Kill switch isBankingEnabled() : flag BANKING_ENABLED + check des
credentials Powens. Endpoint public GET /banking/status renvoie
{ enabled }, /banking/powens/init throw 503 banking_disabled si OFF.
- Fix handler exceptions : UNIQUE violation composite (org, X)
rapporte désormais la vraie colonne en faute (numero/slug/…) avec
message lisible « Le numéro de facture "F2026-0013" existe déjà »,
au lieu d'un message ambigu sur organization_id.
Frontend (apps/web)
- /parametres : nouvelle SettingsSection "Banque" gated par kill
switch + plan Pro/Business. Si Free → upsell card avec CTA vers
/parametres/abonnement. Si Pro/Business sans banque → CTA "Connecter
une banque". Si banque connectée → carte avec accounts (IBAN
masqué FR76 **** **** **** 1234), solde, last sync, bouton
Déconnecter. Toggle Manuel/Auto pour reconciliation_mode.
- /parametres/banque/success : nouvelle route dédiée post-callback
avec badge ✓ animé + halo glow rubis, récap des comptes
synchronisés, 2 CTAs ("Voir mes paramètres" / "Retour dashboard"),
note sécurité "lecture seule, aucun déplacement de fonds".
- Hooks : useBankingStatus, useBankConnections (avec opt-out via
{ enabled }), useInitBanking, useDisconnectBank, useBankingSettings,
useUpdateBankingSettings.
Infrastructure (k3s)
- ConfigMap rubis-api-config : BANKING_ENABLED='false' par défaut,
BANKING_PROVIDER='powens', POWENS_DOMAIN='rubis',
POWENS_API_BASE_URL='https://rubis.biapi.pro/2.0/',
POWENS_REDIRECT_URI='https://app.rubis.pro/api/v1/banking/powens/callback'.
- Secret rubis-app-secrets : 3 nouvelles clés POWENS_CLIENT_ID,
POWENS_CLIENT_SECRET, POWENS_WEBHOOK_SECRET (valeurs sandbox posées
via kubectl patch, à remplacer post-KYC).
Sécurité
- Token Powens chiffré au repos via Adonis encryption (AES-256-GCM,
clé APP_KEY).
- State HMAC SHA-256 signé sur APP_KEY pour le flow webview
(anti-CSRF + porte l'org_id à travers le redirect).
- Webhook HMAC SHA-256 sur header BI-Signature avec
POWENS_WEBHOOK_SECRET, comparaison timing-safe.
- IBAN masqué côté API (transformer).
- Scope par org sur tous les endpoints (anti-IDOR).
- Rate limiting via le middleware Adonis existant.
- Idempotence DB : UNIQUE (org, powens_connection_id), (connection,
powens_account_id), (account, powens_id) → rejouer un event ou un
callback ne pose pas de problème.
Documentation
- /docs/tech/banking-setup.md : procédure complète setup dev avec
Cloudflare Quick Tunnel, compte sandbox Powens, whitelist URLs.
- /.claude/deploy-memory.md : section "Banking (Powens) — activation
prod" avec procédure en 6 étapes (KYC → secrets → ConfigMap →
flip flag → smoke test), snippet kubectl patch pour rotation
ciblée de secrets.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
138 lines
5.5 KiB
TypeScript
138 lines
5.5 KiB
TypeScript
/*
|
|
|--------------------------------------------------------------------------
|
|
| Environment variables service
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
| The `Env.create` method creates an instance of the Env service. The
|
|
| service validates the environment variables and also cast values
|
|
| to JavaScript data types.
|
|
|
|
|
*/
|
|
|
|
import { Env } from '@adonisjs/core/env'
|
|
|
|
export default await Env.create(new URL('../', import.meta.url), {
|
|
// Node
|
|
NODE_ENV: Env.schema.enum(['development', 'production', 'test'] as const),
|
|
PORT: Env.schema.number(),
|
|
HOST: Env.schema.string({ format: 'host' }),
|
|
LOG_LEVEL: Env.schema.string(),
|
|
|
|
// App
|
|
APP_KEY: Env.schema.secret(),
|
|
APP_URL: Env.schema.string({ format: 'url', tld: false }),
|
|
|
|
// Session
|
|
SESSION_DRIVER: Env.schema.enum(['cookie', 'memory', 'database'] as const),
|
|
|
|
// Database
|
|
DB_CONNECTION: Env.schema.enum.optional(['postgres', 'sqlite'] as const),
|
|
PG_HOST: Env.schema.string.optional({ format: 'host' }),
|
|
PG_PORT: Env.schema.number.optional(),
|
|
PG_USER: Env.schema.string.optional(),
|
|
PG_PASSWORD: Env.schema.string.optional(),
|
|
PG_DB_NAME: Env.schema.string.optional(),
|
|
|
|
// Redis (BullMQ + cache)
|
|
REDIS_HOST: Env.schema.string.optional({ format: 'host' }),
|
|
REDIS_PORT: Env.schema.number.optional(),
|
|
REDIS_PASSWORD: Env.schema.string.optional(),
|
|
|
|
// Storage (MinIO via S3 driver)
|
|
DRIVE_DISK: Env.schema.enum.optional(['s3', 'fs'] as const),
|
|
S3_ENDPOINT: Env.schema.string.optional({ format: 'url', tld: false }),
|
|
S3_REGION: Env.schema.string.optional(),
|
|
S3_BUCKET: Env.schema.string.optional(),
|
|
S3_ACCESS_KEY: Env.schema.string.optional(),
|
|
S3_SECRET_KEY: Env.schema.string.optional(),
|
|
S3_FORCE_PATH_STYLE: Env.schema.boolean.optional(),
|
|
|
|
// Mail
|
|
MAIL_FROM_ADDRESS: Env.schema.string.optional(),
|
|
MAIL_FROM_NAME: Env.schema.string.optional(),
|
|
MAIL_DRIVER: Env.schema.enum.optional(['smtp', 'resend'] as const),
|
|
SMTP_HOST: Env.schema.string.optional({ format: 'host' }),
|
|
SMTP_PORT: Env.schema.number.optional(),
|
|
RESEND_API_KEY: Env.schema.string.optional(),
|
|
|
|
// OCR
|
|
OCR_PROVIDER: Env.schema.enum.optional(['mock', 'mistral'] as const),
|
|
MISTRAL_API_KEY: Env.schema.string.optional(),
|
|
|
|
// Stripe — secret key + webhook signing secret. Optional en dev sans
|
|
// billing actif. La commande `stripe:setup` et le webhook handler les
|
|
// exigent au runtime.
|
|
STRIPE_SECRET_KEY: Env.schema.string.optional(),
|
|
STRIPE_WEBHOOK_SECRET: Env.schema.string.optional(),
|
|
|
|
// Web (URL du SPA pour redirects post-checkin)
|
|
WEB_URL: Env.schema.string.optional({ format: 'url', tld: false }),
|
|
|
|
// Landing public (lien dans le footer des emails — branding)
|
|
LANDING_URL: Env.schema.string.optional({ format: 'url', tld: false }),
|
|
|
|
// Auth
|
|
ACCESS_TOKEN_TTL_MINUTES: Env.schema.number.optional(),
|
|
REFRESH_TOKEN_TTL_DAYS: Env.schema.number.optional(),
|
|
COOKIE_DOMAIN: Env.schema.string.optional(),
|
|
COOKIE_SECURE: Env.schema.boolean.optional(),
|
|
|
|
// Google SSO (Ally)
|
|
GOOGLE_CLIENT_ID: Env.schema.string.optional(),
|
|
GOOGLE_CLIENT_SECRET: Env.schema.string.optional(),
|
|
GOOGLE_CALLBACK_URL: Env.schema.string.optional({ format: 'url', tld: false }),
|
|
|
|
// Microsoft SSO (Ally)
|
|
MICROSOFT_CLIENT_ID: Env.schema.string.optional(),
|
|
MICROSOFT_CLIENT_SECRET: Env.schema.string.optional(),
|
|
MICROSOFT_TENANT: Env.schema.string.optional(),
|
|
MICROSOFT_CALLBACK_URL: Env.schema.string.optional({ format: 'url', tld: false }),
|
|
|
|
/*
|
|
|----------------------------------------------------------
|
|
| Variables for configuring the limiter package
|
|
|----------------------------------------------------------
|
|
*/
|
|
LIMITER_STORE: Env.schema.enum(['redis', 'memory'] as const),
|
|
|
|
/*
|
|
|----------------------------------------------------------
|
|
| Sentry — error monitoring (cf. apps/api/start/sentry.ts)
|
|
|----------------------------------------------------------
|
|
| Optionnels en dev local. Si non définis, Sentry est no-op.
|
|
*/
|
|
SENTRY_DSN_API: Env.schema.string.optional(),
|
|
APP_VERSION: Env.schema.string.optional(),
|
|
|
|
/*
|
|
|----------------------------------------------------------
|
|
| Banking — agrégation bancaire (lecture seule, AISP)
|
|
|----------------------------------------------------------
|
|
| V1 : un seul provider supporté (Powens). On garde les flags
|
|
| BANKING_ENABLED / BANKING_PROVIDER pour pouvoir kill-switch
|
|
| la feature en prod sans redéploiement de code et pour
|
|
| anticiper un éventuel multi-provider (Bridge, Tink…).
|
|
|
|
|
| Flux Powens : on init un user Powens par organization, on
|
|
| génère un code temporaire, on ouvre la webview Powens, le
|
|
| user choisit sa banque, Powens redirige sur POWENS_REDIRECT_URI
|
|
| (qui pointe sur notre API), on stocke la connection.
|
|
|
|
|
| En dev : POWENS_REDIRECT_URI doit pointer sur un tunnel HTTPS
|
|
| (Cloudflare Quick Tunnel, ngrok, …) parce que Powens refuse
|
|
| http://. Voir /docs/tech/banking-setup.md.
|
|
|
|
|
| POWENS_DOMAIN = slug du domaine (ex : 'rubis-sandbox').
|
|
| POWENS_API_BASE_URL = URL complète optionnelle pour override
|
|
| (sinon calculée : https://<slug>.biapi.pro/2.0/).
|
|
*/
|
|
BANKING_ENABLED: Env.schema.boolean.optional(),
|
|
BANKING_PROVIDER: Env.schema.enum.optional(['powens'] as const),
|
|
POWENS_DOMAIN: Env.schema.string.optional(),
|
|
POWENS_API_BASE_URL: Env.schema.string.optional({ format: 'url', tld: false }),
|
|
POWENS_CLIENT_ID: Env.schema.string.optional(),
|
|
POWENS_CLIENT_SECRET: Env.schema.secret.optional(),
|
|
POWENS_REDIRECT_URI: Env.schema.string.optional({ format: 'url', tld: false }),
|
|
POWENS_WEBHOOK_SECRET: Env.schema.secret.optional(),
|
|
})
|