wasAsked.

Implement different payment gateways using Factory design pattern

EPAM · Role not specified · technical

Published

Year not specified · System Design

Community answers

Posted by wasAsked editorial collection

The Factory pattern centralises the creation of objects, so client code asks for a payment gateway without knowing which concrete class to instantiate. Step 1 — Common interface public interface IPaymentGateway { string Name { get; } Task<PaymentResult> ChargeAsync(PaymentRequest request, CancellationToken ct); } public sealed record PaymentRequest(string OrderId, decimal Amount, string Currency, string IdempotencyKey); public sealed record PaymentResult(bool Success, string? TransactionId, string? Error); Step 2 — Concrete gateways public sealed class RazorpayGateway(HttpClient http) : IPaymentGateway { public string Name => "razorpay"; public async Task<PaymentResult> ChargeAsync(PaymentRequest r, CancellationToken ct) { // call Razorpay API with r.IdempotencyKey ... return new PaymentResult(true, "rzp_123", null); } } public sealed class StripeGateway(HttpClient http) : IPaymentGateway { public string Name => "stripe"; public async Task<PaymentResult> ChargeAsync(PaymentRequest r, CancellationToken ct) => new(true, "pi_456", null); } public sealed class PayPalGateway(HttpClient http) : IPaymentGateway { public string Name => "paypal"; public async Task<PaymentResult> ChargeAsync(PaymentRequest r, CancellationToken ct) => new(true, "pp_789", null); } Step 3 — Factory public interface IPaymentGatewayFactory { IPaymentGateway Create(string provider); } public sealed class PaymentGatewayFactory(IEnumerable<IPaymentGateway> gateways) : IPaymentGatewayFactory { private readonly Dictionary<string, IPaymentGateway> _gateways = gateways.ToDictionary(g => g.Name, StringComparer.OrdinalIgnoreCase); public IPaymentGateway Create(string provider) => _gateways.TryGetValue(provider, out var gateway) ? gateway : throw new NotSupportedException($"Payment provider '{provider}' is not supported."); } Step 4 — Registration builder.Services.AddHttpClient<RazorpayGateway>(); builder.Services.AddHttpClient<StripeGateway>(); builder.Services.AddHttpClient<PayPalGateway>(); builder.Services.AddTransient<IPaymentGateway>(sp => sp.GetRequiredService<RazorpayGateway>()); builder.Services.AddTransient<IPaymentGateway>(sp => sp.GetRequiredService<StripeGateway>()); builder.Services.AddTransient<IPaymentGateway>(sp => sp.GetRequiredService<PayPalGateway>()); builder.Services.AddScoped<IPaymentGatewayFactory, PaymentGatewayFactory>(); Step 5 — Usage public sealed class CheckoutService(IPaymentGatewayFactory factory) { public Task<PaymentResult> PayAsync(Order order, string provider, CancellationToken ct) => factory.Create(provider).ChargeAsync( new PaymentRequest(order.Id, order.Total, "INR", order.Id + ":payment"), ct); } Why this design is good - Open/Closed: adding a gateway means adding a class and a registration; CheckoutService and the factory do not change. - Client code depends only on IPaymentGateway (Dependency Inversion), making it easy to unit test with a fake gateway. - Selection can come from user choice, configuration, country or fallback rules. - Alternative in .NET 8+: keyed services (AddKeyedTransient<IPaymentGateway, StripeGateway>("stripe") and IServiceProvider.GetRequiredKeyedService) remove the need for a custom factory class. Mention real concerns too: idempotency keys, webhook verification per provider, secrets in Key Vault, and resilience policies on each HttpClient.

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics