Skip to content

Commerce Payment API Reference

advanced
📜Corecommerce

The Payment API processes financial transactions during checkout. Implement IPaymentPlugin for custom payment gateways. The payment flow follows authorize, capture, void, and refund steps.

Optimizely.Commerce.Order

InterfacePurpose
IPaymentPluginCustom payment gateway integration
IPaymentManagerFacadeManage payment methods
IPaymentRepresents a payment on an order

Implement this interface to integrate a payment gateway.

Processes a payment transaction.

Parameters

NameTypeDescription
orderGroupIOrderGroupThe cart or order
paymentIPaymentPayment to process

Returns: PaymentProcessingResult

Implement a custom payment plugin
csharp
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.");
  }
}
TransactionTypeDescription
AuthorizationVerifies funds and places a hold
CaptureCharges the authorized amount
SaleCombines authorization and capture
VoidCancels an authorization before capture
CreditRefunds a captured payment
Authorization → Capture → (optional) Credit
Void
PropertyTypeDescription
PaymentIdintUnique payment identifier
PaymentMethodIdGuidPayment method reference
PaymentMethodNamestringDisplay name
AmountdecimalTransaction amount
StatusstringCurrent payment status
TransactionTypestringAuthorization, Capture, Void, Credit
TransactionIDstringGateway transaction ID
AuthorizationCodestringAuthorization code from gateway
ValidationCodestringValidation code
BillingAddressIOrderAddressBilling address
ProviderTransactionIDstringProvider-specific transaction ID
PropertyTypeDescription
IsSuccessfulboolWhether the transaction succeeded
MessagestringResult message or error description
Factory methodDescription
CreateSuccessfulResult(string)Creates a successful result with message
CreateUnsuccessfulResult(string)Creates a failed result with error message

Returns available payment methods for a market.

Parameters

NameTypeDescription
marketIdMarketIdTarget market

Returns: IEnumerable<PaymentMethodDto.PaymentMethodRow>

Add payment to cart
csharp
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);
}
Register in dependency injection
csharp
services.AddTransient<IPaymentPlugin, CustomPaymentGateway>();
ExceptionCause
PaymentExceptionGateway returned an error
InvalidOperationExceptionPayment method not configured for market
ArgumentExceptionInvalid amount or missing required fields
PaymentAuthorizationExceptionAuthorization declined by gateway