75 fichiers, ~1 mois de travail depuis f222861 (17/07). Contenu :
Onboarding self-service — jamais exécuté de bout en bout
OnboardingController, StripeWebhookController, StripeService, ResendEmailService
+ IEmailService (10 templates), EmailTemplates/, TrialLifecycleService (Hangfire),
PasswordTokenHelper, SlugHelper, 5 DTOs. Essai 14 j, Stripe customer/Checkout/Tax,
mot de passe oublié + invitation user, plafond IA d'essai.
Schéma Postgres v3 — passe pendant que la base est vide
ContentEmbedding + index HNSW (vector_cosine_ops), IEmbeddingService +
GoogleEmbeddingService (gemini-embedding-001, 768 dims), Deployment/Dockerfile.postgres
(postgis 3.4.3 + pgvector 0.8.6, épinglé par digest — un tag mobile rejouerait le
warning de collation glibc). Colonnes Resource : StoragePath, FileName,
IncludeInAiKnowledge, AiIndexStatus + nouveaux ResourceType ajoutés EN FIN d'enum.
Guide IA
Champs Guide* sur Instance + InstanceDTO, AssistantService lit la configuration client
dans les 4 blocs de prompt (ton codé en dur retiré, règle hors-sujet ajoutée aux deux
variantes qui n'en avaient pas), IHttpClientFactory à la place des new HttpClient().
Table VisitorQuestion + ConversationId sur AiChatRequest.
Stats
Rétention unifiée à 13 mois (instances ET plans), VisitEventPurgeService.
Nettoyage
IsStepLocked / IsHiddenInitially / FactContent supprimés de GuidedStep — IsStepLocked
rendait une étape définitivement infranchissable même après réussite.
SectionMap allégé (-57 lignes).
Tests
SectionParcoursControllerTests, FakeConfiguration, FakeEmailService.
ContentEmbedding a cassé 116 tests sur 124 (EF InMemory ne connaît pas Vector) :
l'entité est exclue quand le provider n'est pas Npgsql. Conséquence assumée —
le vector store n'est couvert par aucun test. dotnet test 124/124.
10 migrations EF. Base locale à jour, dotnet build 0 erreur.
Rien n'est en prod : la bascule Mongo → Postgres est décrite dans DOCS/STATUS.md §1quinquies.
⚠️ appsettings.json contient les clés Stripe (test) et Resend (prod) en clair — à rotationner.
133 lines
5.0 KiB
C#
133 lines
5.0 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using ManagerService.Data;
|
|
using ManagerService.Services;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Logging;
|
|
using NSwag.Annotations;
|
|
using Stripe;
|
|
using Stripe.Checkout;
|
|
|
|
namespace ManagerService.Controllers
|
|
{
|
|
[AllowAnonymous]
|
|
[ApiController, Route("api/webhooks/stripe")]
|
|
[OpenApiTag("Webhooks", Description = "Stripe webhook events")]
|
|
public class StripeWebhookController : ControllerBase
|
|
{
|
|
private readonly MyInfoMateDbContext _myInfoMateDbContext;
|
|
private readonly StripeService _stripeService;
|
|
private readonly IEmailService _emailService;
|
|
private readonly IConfiguration _configuration;
|
|
private readonly ILogger<StripeWebhookController> _logger;
|
|
|
|
public StripeWebhookController(
|
|
MyInfoMateDbContext myInfoMateDbContext,
|
|
StripeService stripeService,
|
|
IEmailService emailService,
|
|
IConfiguration configuration,
|
|
ILogger<StripeWebhookController> logger)
|
|
{
|
|
_myInfoMateDbContext = myInfoMateDbContext;
|
|
_stripeService = stripeService;
|
|
_emailService = emailService;
|
|
_configuration = configuration;
|
|
_logger = logger;
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<ObjectResult> HandleWebhook()
|
|
{
|
|
var json = await new StreamReader(Request.Body).ReadToEndAsync();
|
|
|
|
Event stripeEvent;
|
|
try
|
|
{
|
|
stripeEvent = _stripeService.ConstructWebhookEvent(json, Request.Headers["Stripe-Signature"]);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogWarning(ex, "Stripe webhook signature verification failed");
|
|
return new BadRequestObjectResult("Invalid signature");
|
|
}
|
|
|
|
try
|
|
{
|
|
switch (stripeEvent.Type)
|
|
{
|
|
case "checkout.session.completed":
|
|
await HandleCheckoutSessionCompleted((Session)stripeEvent.Data.Object);
|
|
break;
|
|
case "invoice.payment_failed":
|
|
await HandleInvoicePaymentFailed((Invoice)stripeEvent.Data.Object);
|
|
break;
|
|
default:
|
|
_logger.LogInformation("Unhandled Stripe event type {Type}", stripeEvent.Type);
|
|
break;
|
|
}
|
|
|
|
return new OkObjectResult("ok");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Error handling Stripe event {Type}", stripeEvent.Type);
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
private async Task HandleCheckoutSessionCompleted(Session session)
|
|
{
|
|
var instanceId = session.Metadata != null && session.Metadata.TryGetValue("instanceId", out var id)
|
|
? id
|
|
: null;
|
|
|
|
var instance = instanceId != null
|
|
? _myInfoMateDbContext.Instances.FirstOrDefault(i => i.Id == instanceId)
|
|
: _myInfoMateDbContext.Instances.FirstOrDefault(i => i.StripeCustomerId == session.CustomerId);
|
|
|
|
if (instance == null)
|
|
{
|
|
_logger.LogWarning("checkout.session.completed: no matching instance for customer {CustomerId}", session.CustomerId);
|
|
return;
|
|
}
|
|
|
|
instance.IsTrialActive = false;
|
|
instance.StripeSubscriptionId = session.SubscriptionId;
|
|
_myInfoMateDbContext.SaveChanges();
|
|
|
|
var user = _myInfoMateDbContext.Users.FirstOrDefault(u => u.InstanceId == instance.Id);
|
|
if (user != null)
|
|
await _emailService.SendPaymentConfirmedEmailAsync(user.Email, user.FirstName);
|
|
|
|
await _emailService.NotifyAdminAsync(
|
|
"Conversion essai -> payant",
|
|
$"Instance: {instance.Name} ({instance.Id})\nSubscription: {instance.StripeSubscriptionId}");
|
|
}
|
|
|
|
private async Task HandleInvoicePaymentFailed(Invoice invoice)
|
|
{
|
|
var instance = _myInfoMateDbContext.Instances.FirstOrDefault(i => i.StripeCustomerId == invoice.CustomerId);
|
|
if (instance == null)
|
|
{
|
|
_logger.LogWarning("invoice.payment_failed: no matching instance for customer {CustomerId}", invoice.CustomerId);
|
|
return;
|
|
}
|
|
|
|
var user = _myInfoMateDbContext.Users.FirstOrDefault(u => u.InstanceId == instance.Id);
|
|
if (user != null)
|
|
{
|
|
var managerAppUrl = _configuration["AppUrls:ManagerApp"];
|
|
await _emailService.SendPaymentFailedEmailAsync(user.Email, user.FirstName, $"{managerAppUrl}/billing");
|
|
}
|
|
|
|
await _emailService.NotifyAdminAsync(
|
|
"Paiement échoué",
|
|
$"Instance: {instance.Name} ({instance.Id})");
|
|
}
|
|
}
|
|
}
|