-
-
Save dcomartin/535030622b322a9d970bd78afdc896dd 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 class PayOrderHandler | |
| { | |
| private readonly DbContext _db; | |
| public PayOrderHandler(DbContext db) | |
| { | |
| _db = db; | |
| } | |
| public async Task Handle(PayOrder cmd) | |
| { | |
| using var trx = _db.BeginTransaction(); | |
| _db.Inbox.Add(new InboxMessage(cmd.MessageId)); | |
| var order = _db.Orders.Find(cmd.OrderId); | |
| order.MarkPaymentPending(cmd.PaymentAttemptId); | |
| _db.Outbox.Add(new OutboxMessage( | |
| type: "ChargePayment", | |
| key: cmd.PaymentAttemptId, | |
| payload: new { cmd.OrderId, cmd.Amount } | |
| )); | |
| await _db.SaveChangesAsync(); | |
| await trx.CommitAsync(); | |
| } | |
| } | |
| public class ChargePaymentHandler | |
| { | |
| private readonly PaymentGateway _paymentGateway; | |
| private readonly DbContext _db; | |
| public ChargePaymentHandler(PaymentGateway paymentGateway, DbContext db) | |
| { | |
| _paymentGateway = paymentGateway; | |
| _db = db; | |
| } | |
| public async Task Handle(ChargePayment cmd) | |
| { | |
| var receipt = await _paymentGateway.Charge( | |
| orderId: cmd.OrderId, | |
| amount: cmd.Amount, | |
| idempotencyKey: cmd.MessageId // if supported | |
| ); | |
| using var trx = _db.BeginTransaction(); | |
| var order = _db.Orders.Find(cmd.OrderId); | |
| order.MarkPaid(receipt.Id); | |
| await _db.SaveChangesAsync(); | |
| await trx.CommitAsync(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment