Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created January 20, 2026 23:10
Show Gist options
  • Select an option

  • Save dcomartin/bd83c7fe29e0782115803b5224636b3b to your computer and use it in GitHub Desktop.

Select an option

Save dcomartin/bd83c7fe29e0782115803b5224636b3b to your computer and use it in GitHub Desktop.
public static class PayInvoice
{
public sealed record Command(Guid InvoiceId, string PaymentMethodToken);
public sealed class Handler
{
private readonly AppDbContext _db;
private readonly StripeClient _stripe;
public Handler(AppDbContext db, StripeClient stripe)
{
_db = db;
_stripe = stripe;
}
public async Task Handle(Command cmd)
{
var invoice = await _db.Invoices.FindAsync(cmd.InvoiceId)
?? throw new InvalidOperationException("Invoice not found");
var payment = new Payment(Guid.NewGuid(), invoice.Id, invoice.Total);
_db.Payments.Add(payment);
// Provider detail stays here, in the slice.
var chargeResult = await _stripe.ChargeAsync(
amount: invoice.Total.Amount,
currency: invoice.Total.Currency,
token: cmd.PaymentMethodToken);
if (chargeResult.Succeeded)
{
payment.MarkSucceeded();
invoice.ApplyPayment(invoice.Total);
}
else
{
payment.MarkFailed();
}
await _db.SaveChangesAsync();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment