Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .ai/skills/plugin-payment.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Do not use this skill for order processing business logic outside the payment fl

#### Redirect Payment Flow
6. When `PaymentMethodType == Redirection`: `PostRedirectPayment` returns the URL; the checkout redirects the customer there. The gateway posts back to a return controller action in the plugin.
7. Implement a public storefront controller (`[Area("")]` without `[AuthorizeAdmin]`) as the redirect return handler. The controller receives the gateway callback, updates the `PaymentTransaction`, and redirects to order confirmation.
7. Implement a public storefront controller (`[Area("")]` without `[AuthorizeAdmin]`) as the redirect return handler. The controller receives the gateway callback, records the money with `MarkAsPaidCommand` (or `MarkAsAuthorizedCommand` for an authorization), and redirects to order confirmation. Pass `CreatedBy` = the plugin's system name, `Reference` = the gateway's payment id, and `Amount` = what the gateway reports it took. Never set `PaidAmount` or `TransactionStatus` and save through `IPaymentTransactionService.UpdatePaymentTransaction` for a money operation: the commands also update the order and write the transaction's payment history (`IPaymentTransactionHistoryService`), a direct save leaves the history empty.
8. Register the return controller route in `EndpointProvider` or via a convention route in `StartupApplication.Configure`.

#### Operations: Capture, Refund, Void
Expand Down Expand Up @@ -167,7 +167,7 @@ bool IsPartialRefund { get; }
Set `PaymentMethodType.Standard`. `ProcessPayment` returns `TransactionStatus.Pending`. `PostRedirectPayment` returns `""`. All capture/refund/void methods return an error result. `SkipPaymentInfo` driven from settings.

### Example 2: Redirect Gateway (e.g., Stripe Checkout)
Set `PaymentMethodType.Redirection`. `ProcessPayment` returns an empty result. `PostRedirectPayment` calls the gateway SDK to create a session and returns the session URL. Implement a public return controller that verifies the webhook/callback and calls `IPaymentTransactionService.UpdatePaymentTransaction`.
Set `PaymentMethodType.Redirection`. `ProcessPayment` returns an empty result. `PostRedirectPayment` calls the gateway SDK to create a session and returns the session URL. Implement a public return controller that verifies the webhook/callback and sends `MarkAsPaidCommand` with the gateway's payment id and amount (see `StripeCheckoutService.CreatePaymentTransaction`).

### Example 3: Authorize-and-Capture Gateway
Set `PaymentMethodType.Standard`. `ProcessPayment` calls the gateway authorize API and sets `TransactionStatus.Authorized`. Implement `Capture` to charge the authorization. Return `SupportCapture() => true`. Implement `Void` to cancel the authorization.
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,22 @@ public class CaptureCommandHandler : IRequestHandler<CaptureCommand, IList<strin
private readonly IOrderService _orderService;
private readonly IPaymentService _paymentService;
private readonly IPaymentTransactionService _paymentTransaction;
private readonly IPaymentTransactionHistoryService _paymentTransactionHistoryService;

public CaptureCommandHandler(
IPaymentService paymentService,
IPaymentTransactionService paymentTransaction,
IMediator mediator,
IOrderService orderService,
ILogger<CaptureCommandHandler> logger)
ILogger<CaptureCommandHandler> logger,
IPaymentTransactionHistoryService paymentTransactionHistoryService)
{
_paymentService = paymentService;
_paymentTransaction = paymentTransaction;
_mediator = mediator;
_orderService = orderService;
_logger = logger;
_paymentTransactionHistoryService = paymentTransactionHistoryService;
}

public async Task<IList<string>> Handle(CaptureCommand command, CancellationToken cancellationToken)
Expand All @@ -54,14 +57,23 @@ public async Task<IList<string>> Handle(CaptureCommand command, CancellationToke
if (result.Success)
{
paymentTransaction = await _paymentTransaction.GetById(paymentTransaction.Id);
paymentTransaction.PaidAmount = paymentTransaction.TransactionAmount;
//a capture the gateway settles later is not money in yet: it is paid, and recorded, when it is
var captured = result.NewPaymentStatus == TransactionStatus.Paid
? paymentTransaction.TransactionAmount - paymentTransaction.PaidAmount
: 0;
paymentTransaction.PaidAmount += captured;
paymentTransaction.CaptureTransactionId = result.CaptureTransactionId;
paymentTransaction.CaptureTransactionResult = result.CaptureTransactionResult;
paymentTransaction.TransactionStatus = result.NewPaymentStatus;

await _paymentTransaction.UpdatePaymentTransaction(paymentTransaction);

var order = await _orderService.GetOrderByGuid(paymentTransaction.OrderGuid);
if (paymentTransaction.TransactionStatus == TransactionStatus.Paid)
await _paymentTransactionHistoryService.Record(paymentTransaction, order,
PaymentTransactionEntryType.Capture, captured, command.CreatedBy,
result.CaptureTransactionId);

if (order != null && paymentTransaction.TransactionStatus == TransactionStatus.Paid)
{
order.PaidAmount = paymentTransaction.PaidAmount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ public class MarkAsAuthorizedCommandHandler : IRequestHandler<MarkAsAuthorizedCo
private readonly IMediator _mediator;
private readonly IOrderService _orderService;
private readonly IPaymentTransactionService _paymentTransactionService;
private readonly IPaymentTransactionHistoryService _paymentTransactionHistoryService;

public MarkAsAuthorizedCommandHandler(
IOrderService orderService,
IPaymentTransactionService paymentTransactionService,
IMediator mediator)
IMediator mediator,
IPaymentTransactionHistoryService paymentTransactionHistoryService)
{
_orderService = orderService;
_paymentTransactionService = paymentTransactionService;
_mediator = mediator;
_paymentTransactionHistoryService = paymentTransactionHistoryService;
}

public async Task<bool> Handle(MarkAsAuthorizedCommand request, CancellationToken cancellationToken)
Expand All @@ -36,6 +39,8 @@ public async Task<bool> Handle(MarkAsAuthorizedCommand request, CancellationToke

order.PaymentStatusId = PaymentStatus.Authorized;
await _orderService.UpdateOrder(order);
await _paymentTransactionHistoryService.Record(paymentTransaction, order,
PaymentTransactionEntryType.Authorization, paymentTransaction.TransactionAmount, request.CreatedBy);

//event notification
await _mediator.Publish(new PaymentTransactionMarkAsAuthorizedEvent(paymentTransaction), cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ public class MarkAsPaidCommandHandler : IRequestHandler<MarkAsPaidCommand, bool>
private readonly IMediator _mediator;
private readonly IOrderService _orderService;
private readonly IPaymentTransactionService _paymentTransactionService;
private readonly IPaymentTransactionHistoryService _paymentTransactionHistoryService;

public MarkAsPaidCommandHandler(
IMediator mediator,
IOrderService orderService,
IPaymentTransactionService paymentTransactionService)
IPaymentTransactionService paymentTransactionService,
IPaymentTransactionHistoryService paymentTransactionHistoryService)
{
_mediator = mediator;
_orderService = orderService;
_paymentTransactionService = paymentTransactionService;
_paymentTransactionHistoryService = paymentTransactionHistoryService;
}

public async Task<bool> Handle(MarkAsPaidCommand request, CancellationToken cancellationToken)
Expand All @@ -36,6 +39,8 @@ await _mediator.Send(new CanMarkPaymentTransactionAsPaidQuery { PaymentTransacti
if (!canMarkOrderAsPaid)
throw new GrandException("You can't mark this Payment Transaction as paid");

//what the payment method reports it took, else what was still to pay
var paid = request.Amount ?? paymentTransaction.TransactionAmount - paymentTransaction.PaidAmount;
paymentTransaction.TransactionStatus = TransactionStatus.Paid;
paymentTransaction.PaidAmount = paymentTransaction.TransactionAmount;

Expand All @@ -48,12 +53,8 @@ await _mediator.Send(new CanMarkPaymentTransactionAsPaidQuery { PaymentTransacti
order.PaidDateUtc = DateTime.UtcNow;
await _orderService.UpdateOrder(order);

//add a note
await _orderService.InsertOrderNote(new OrderNote {
Note = "Order has been marked as paid",
DisplayToCustomer = false,
OrderId = order.Id
});
await _paymentTransactionHistoryService.Record(paymentTransaction, order, PaymentTransactionEntryType.Payment,
paid, request.CreatedBy, request.Reference, request.Note);

await _mediator.Send(new CheckOrderStatusCommand { Order = order }, cancellationToken);
if (order.PaymentStatusId == PaymentStatus.Paid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@ public class PartiallyPaidOfflineCommandHandler : IRequestHandler<PartiallyPaidO
private readonly IMediator _mediator;
private readonly IOrderService _orderService;
private readonly IPaymentTransactionService _paymentTransactionService;
private readonly IPaymentTransactionHistoryService _paymentTransactionHistoryService;

public PartiallyPaidOfflineCommandHandler(
IOrderService orderService,
IPaymentTransactionService paymentTransactionService,
IMediator mediator)
IMediator mediator,
IPaymentTransactionHistoryService paymentTransactionHistoryService)
{
_orderService = orderService;
_paymentTransactionService = paymentTransactionService;
_mediator = mediator;
_orderService = orderService;
_paymentTransactionHistoryService = paymentTransactionHistoryService;
}

public async Task<bool> Handle(PartiallyPaidOfflineCommand command, CancellationToken cancellationToken)
Expand Down Expand Up @@ -52,6 +55,8 @@ await _mediator.Send(
order.PaidAmount += amountToPaid;
order.PaymentStatusId = order.PaidAmount >= order.OrderTotal ? PaymentStatus.Paid : PaymentStatus.PartiallyPaid;
await _orderService.UpdateOrder(order);
await _paymentTransactionHistoryService.Record(paymentTransaction, order, PaymentTransactionEntryType.Payment,
amountToPaid, command.CreatedBy, command.Reference, command.Note);

//check order status
await _mediator.Send(new CheckOrderStatusCommand { Order = order }, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class PartiallyRefundCommandHandler : IRequestHandler<PartiallyRefundComm
private readonly IOrderService _orderService;
private readonly IPaymentService _paymentService;
private readonly IPaymentTransactionService _paymentTransactionService;
private readonly IPaymentTransactionHistoryService _paymentTransactionHistoryService;

public PartiallyRefundCommandHandler(
IPaymentService paymentService,
Expand All @@ -30,7 +31,8 @@ public PartiallyRefundCommandHandler(
IOrderMessageProviderService orderMessageProviderService,
ILogger<PartiallyRefundCommandHandler> logger,
IOrderService orderService,
LanguageSettings languageSettings)
LanguageSettings languageSettings,
IPaymentTransactionHistoryService paymentTransactionHistoryService)
{
_paymentService = paymentService;
_paymentTransactionService = paymentTransactionService;
Expand All @@ -39,6 +41,7 @@ public PartiallyRefundCommandHandler(
_orderMessageProviderService = orderMessageProviderService;
_logger = logger;
_languageSettings = languageSettings;
_paymentTransactionHistoryService = paymentTransactionHistoryService;
}

public async Task<IList<string>> Handle(PartiallyRefundCommand command, CancellationToken cancellationToken)
Expand Down Expand Up @@ -88,6 +91,8 @@ await _mediator.Send(
? PaymentStatus.Refunded
: PaymentStatus.PartiallyRefunded;
await _orderService.UpdateOrder(order);
await _paymentTransactionHistoryService.Record(paymentTransaction, order, PaymentTransactionEntryType.Refund,
amountToRefund, command.CreatedBy, command.Reference, command.Note);

//check order status
await _mediator.Send(new CheckOrderStatusCommand { Order = order }, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,23 @@ public class PartiallyRefundOfflineCommandHandler : IRequestHandler<PartiallyRef
private readonly IOrderMessageProviderService _orderMessageProviderService;
private readonly IOrderService _orderService;
private readonly IPaymentTransactionService _paymentTransactionService;
private readonly IPaymentTransactionHistoryService _paymentTransactionHistoryService;

public PartiallyRefundOfflineCommandHandler(
IOrderService orderService,
IPaymentTransactionService paymentTransactionService,
IMediator mediator,
IOrderMessageProviderService orderMessageProviderService,
LanguageSettings languageSettings)
LanguageSettings languageSettings,
IPaymentTransactionHistoryService paymentTransactionHistoryService)
{
_orderService = orderService;
_paymentTransactionService = paymentTransactionService;
_mediator = mediator;
_orderMessageProviderService = orderMessageProviderService;
_languageSettings = languageSettings;
_orderService = orderService;
_paymentTransactionHistoryService = paymentTransactionHistoryService;
}

public async Task<bool> Handle(PartiallyRefundOfflineCommand command, CancellationToken cancellationToken)
Expand Down Expand Up @@ -66,6 +69,8 @@ await _mediator.Send(
? PaymentStatus.Refunded
: PaymentStatus.PartiallyRefunded;
await _orderService.UpdateOrder(order);
await _paymentTransactionHistoryService.Record(paymentTransaction, order, PaymentTransactionEntryType.Refund,
amountToRefund, command.CreatedBy, command.Reference, command.Note);


//check order status
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class PlaceOrderCommandHandler : IRequestHandler<PlaceOrderCommand, Place
private readonly IPaymentService _paymentService;
private readonly PaymentSettings _paymentSettings;
private readonly IPaymentTransactionService _paymentTransactionService;
private readonly IPaymentTransactionHistoryService _paymentTransactionHistoryService;
private readonly IPricingService _pricingService;
private readonly IProductAttributeFormatter _productAttributeFormatter;
private readonly IProductReservationService _productReservationService;
Expand All @@ -80,6 +81,7 @@ public PlaceOrderCommandHandler(
IInventoryManageService inventoryManageService,
IPaymentService paymentService,
IPaymentTransactionService paymentTransactionService,
IPaymentTransactionHistoryService paymentTransactionHistoryService,
ILogger<PlaceOrderCommandHandler> logger,
IOrderCalculationService orderTotalCalculationService,
IShoppingCartSubTotalService shoppingCartSubTotalService,
Expand Down Expand Up @@ -119,6 +121,7 @@ public PlaceOrderCommandHandler(
_inventoryManageService = inventoryManageService;
_paymentService = paymentService;
_paymentTransactionService = paymentTransactionService;
_paymentTransactionHistoryService = paymentTransactionHistoryService;
_logger = logger;
_orderTotalCalculationService = orderTotalCalculationService;
_shoppingCartSubTotalService = shoppingCartSubTotalService;
Expand Down Expand Up @@ -402,6 +405,17 @@ private async Task UpdatePaymentTransaction(PaymentTransaction paymentTransactio
paymentTransaction.TransactionStatus = processPaymentResult.NewPaymentTransactionStatus;
paymentTransaction.Errors.Clear();
await _paymentTransactionService.UpdatePaymentTransaction(paymentTransaction);

//what the payment method did at checkout, kept off the order notes; a pending payment is recorded once it
//is paid
if (paymentTransaction.PaidAmount > 0)
await _paymentTransactionHistoryService.Record(paymentTransaction, null,
PaymentTransactionEntryType.Payment, paymentTransaction.PaidAmount,
paymentTransaction.PaymentMethodSystemName);
else if (paymentTransaction.TransactionStatus == TransactionStatus.Authorized)
await _paymentTransactionHistoryService.Record(paymentTransaction, null,
PaymentTransactionEntryType.Authorization, paymentTransaction.TransactionAmount,
paymentTransaction.PaymentMethodSystemName);
}

private async Task<double?> PrepareCommissionRate(Product product, PlaceOrderContainer details)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class RefundCommandHandler : IRequestHandler<RefundCommand, IList<string>
private readonly IOrderService _orderService;
private readonly IPaymentService _paymentService;
private readonly IPaymentTransactionService _paymentTransactionService;
private readonly IPaymentTransactionHistoryService _paymentTransactionHistoryService;

public RefundCommandHandler(
IMediator mediator,
Expand All @@ -28,7 +29,8 @@ public RefundCommandHandler(
IOrderService orderService,
IOrderMessageProviderService orderMessageProviderService,
ILogger<RefundCommandHandler> logger,
LanguageSettings languageSettings)
LanguageSettings languageSettings,
IPaymentTransactionHistoryService paymentTransactionHistoryService)
{
_mediator = mediator;
_paymentService = paymentService;
Expand All @@ -37,6 +39,7 @@ public RefundCommandHandler(
_orderMessageProviderService = orderMessageProviderService;
_logger = logger;
_languageSettings = languageSettings;
_paymentTransactionHistoryService = paymentTransactionHistoryService;
}


Expand Down Expand Up @@ -70,6 +73,8 @@ public async Task<IList<string>> Handle(RefundCommand command, CancellationToken
? PaymentStatus.Refunded
: PaymentStatus.PartiallyRefunded;
await _orderService.UpdateOrder(order);
await _paymentTransactionHistoryService.Record(paymentTransaction, order, PaymentTransactionEntryType.Refund,
request.AmountToRefund, command.CreatedBy);

//check order status
await _mediator.Send(new CheckOrderStatusCommand { Order = order }, cancellationToken);
Expand Down
Loading
Loading