-
-
Save dcomartin/bd83c7fe29e0782115803b5224636b3b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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