Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created February 4, 2026 13:44
Show Gist options
  • Select an option

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

Select an option

Save dcomartin/535030622b322a9d970bd78afdc896dd to your computer and use it in GitHub Desktop.
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