Commerce Payment API Reference
Corecommerce
Overview
Section titled “Overview”The Payment API processes financial transactions during checkout. Implement IPaymentPlugin for custom payment gateways. The payment flow follows authorize, capture, void, and refund steps.
Namespace
Section titled “Namespace”Optimizely.Commerce.Order
Key interfaces
Section titled “Key interfaces”| Interface | Purpose |
|---|---|
IPaymentPlugin | Custom payment gateway integration |
IPaymentManagerFacade | Manage payment methods |
IPayment | Represents a payment on an order |
IPaymentPlugin
Section titled “IPaymentPlugin”Implement this interface to integrate a payment gateway.
ProcessPayment(IOrderGroup, IPayment)
Section titled “ProcessPayment(IOrderGroup, IPayment)”Processes a payment transaction.
Parameters
| Name | Type | Description |
|---|---|---|
orderGroup | IOrderGroup | The cart or order |
payment | IPayment | Payment to process |
Returns: PaymentProcessingResult
Implement a custom payment plugin
public class CustomPaymentGateway : IPaymentPlugin
{
public PaymentProcessingResult ProcessPayment(
IOrderGroup orderGroup,
IPayment payment)
{
var transactionType = payment.TransactionType;
switch (transactionType)
{
case TransactionType.Authorization:
return Authorize(payment);
case TransactionType.Capture:
return Capture(payment);
case TransactionType.Void:
return VoidPayment(payment);
case TransactionType.Credit:
return Refund(payment);
default:
return PaymentProcessingResult.CreateUnsuccessfulResult(
"Unsupported transaction type.");
}
}
private PaymentProcessingResult Authorize(IPayment payment)
{
// Call external gateway
payment.AuthorizationCode = "AUTH-12345";
return PaymentProcessingResult
.CreateSuccessfulResult("Authorized.");
}
private PaymentProcessingResult Capture(IPayment payment)
{
payment.TransactionID = "TXN-67890";
return PaymentProcessingResult
.CreateSuccessfulResult("Captured.");
}
private PaymentProcessingResult VoidPayment(IPayment payment)
{
return PaymentProcessingResult
.CreateSuccessfulResult("Voided.");
}
private PaymentProcessingResult Refund(IPayment payment)
{
return PaymentProcessingResult
.CreateSuccessfulResult("Refunded.");
}
} Transaction types
Section titled “Transaction types”| TransactionType | Description |
|---|---|
Authorization | Verifies funds and places a hold |
Capture | Charges the authorized amount |
Sale | Combines authorization and capture |
Void | Cancels an authorization before capture |
Credit | Refunds a captured payment |
Payment flow
Section titled “Payment flow”Authorization → Capture → (optional) Credit ↓ VoidIPayment properties
Section titled “IPayment properties”| Property | Type | Description |
|---|---|---|
PaymentId | int | Unique payment identifier |
PaymentMethodId | Guid | Payment method reference |
PaymentMethodName | string | Display name |
Amount | decimal | Transaction amount |
Status | string | Current payment status |
TransactionType | string | Authorization, Capture, Void, Credit |
TransactionID | string | Gateway transaction ID |
AuthorizationCode | string | Authorization code from gateway |
ValidationCode | string | Validation code |
BillingAddress | IOrderAddress | Billing address |
ProviderTransactionID | string | Provider-specific transaction ID |
PaymentProcessingResult
Section titled “PaymentProcessingResult”| Property | Type | Description |
|---|---|---|
IsSuccessful | bool | Whether the transaction succeeded |
Message | string | Result message or error description |
| Factory method | Description |
|---|---|
CreateSuccessfulResult(string) | Creates a successful result with message |
CreateUnsuccessfulResult(string) | Creates a failed result with error message |
IPaymentManagerFacade
Section titled “IPaymentManagerFacade”GetPaymentMethodsByMarket(MarketId)
Section titled “GetPaymentMethodsByMarket(MarketId)”Returns available payment methods for a market.
Parameters
| Name | Type | Description |
|---|---|---|
marketId | MarketId | Target market |
Returns: IEnumerable<PaymentMethodDto.PaymentMethodRow>
Add payment to cart
public void AddPayment(ICart cart, Guid paymentMethodId, decimal amount)
{
var payment = cart.CreateCardPayment();
payment.PaymentMethodId = paymentMethodId;
payment.Amount = amount;
payment.TransactionType = TransactionType.Authorization.ToString();
cart.GetFirstForm().Payments.Add(payment);
_orderRepository.Save(cart);
} Registering a payment plugin
Section titled “Registering a payment plugin”Register in dependency injection
services.AddTransient<IPaymentPlugin, CustomPaymentGateway>(); Common exceptions
Section titled “Common exceptions”| Exception | Cause |
|---|---|
PaymentException | Gateway returned an error |
InvalidOperationException | Payment method not configured for market |
ArgumentException | Invalid amount or missing required fields |
PaymentAuthorizationException | Authorization declined by gateway |