Mastering Multiple Payment Gateways in .NET Core: Seamlessly Integrating Cards, UPIs, and Online Banking
Strategy and Abstract Factory for Grouping Payment Methods 🎯
In today’s digital age, offering multiple payment methods is a must for any business. Customers want flexibility — whether it’s credit cards, UPI, or online banking. But what happens when you need to integrate various payment gateways within a .NET Core application? 🤔
Don’t worry — I’ve got you covered! In this blog, we’ll dive into how to implement multiple payment gateways in .NET Core, grouped by payment types (cards, UPI, online banking). And yes, I’ll throw in some code snippets to keep things practical! Ready to level up your payment integration skills? Let’s get started!
🔥 Don’t Miss Out! Explore More Exciting Bytes Here!
🔍https://dotnet-fullstack-dev.blogspot.com/ and dive into the content!
Why Multiple Payment Gateways? 🔄
Before jumping into the technical details, let’s understand why it’s crucial to implement multiple gateways. Integrating multiple gateways offers several advantages:
- Higher Conversion Rates: Customers are more likely to complete a purchase if their preferred payment method is available.
- Reduced Payment Failures: If one gateway is down, others can step in, ensuring smooth transactions.
- Global Reach: Support for various payment methods (cards, UPI, banking) can cater to customers worldwide.
Sounds like a win-win, right? Let’s now tackle how we can implement this!
Strategy for Grouping Payment Methods 🎯
We’ll focus on grouping payment gateways by categories such as:
- Cards: Visa, Mastercard, American Express (including specific bank integrations like HDFC, ICICI).
- UPI: Google Pay, PhonePe, Paytm.
- Online Banking: Net banking integrations with specific banks like SBI, Axis Bank, etc.
We’ll achieve this in .NET Core using a clean, modular architecture to manage these multiple gateways efficiently.
Step 1: Setting Up the Payment Interfaces ⚙️
First, let’s define some interfaces to abstract our payment gateways. This helps in keeping the code flexible and easy to extend.
public interface IPaymentGateway
{
Task<bool> ProcessPayment(decimal amount);
string GetPaymentMethodType(); // Card, UPI, or Online Banking
}
public interface ICardPaymentGateway : IPaymentGateway
{
string GetCardType(); // Visa, Mastercard, etc.
}
public interface IUPIPaymentGateway : IPaymentGateway
{
string GetUPIProvider(); // GPay, PhonePe, etc.
}
public interface IBankingPaymentGateway : IPaymentGateway
{
string GetBankName(); // SBI, HDFC, etc.
}
These interfaces allow us to group payment gateways by type (card, UPI, banking) while keeping each implementation specific to its provider.
Step 2: Implementing Payment Gateways for Each Group 🛠️
Now, let’s implement some concrete classes for the gateways. We’ll group the gateways by types, like cards (Visa, Mastercard), UPI (Google Pay, PhonePe), and online banking (SBI, Axis Bank).
Cards: Visa and Mastercard Implementations
public class VisaPaymentGateway : ICardPaymentGateway
{
public string GetPaymentMethodType() => "Card";
public string GetCardType() => "Visa";
public async Task<bool> ProcessPayment(decimal amount)
{
// Visa payment processing logic
Console.WriteLine("Processing Visa payment...");
return await Task.FromResult(true);
}
}
public class MastercardPaymentGateway : ICardPaymentGateway
{
public string GetPaymentMethodType() => "Card";
public string GetCardType() => "Mastercard";
public async Task<bool> ProcessPayment(decimal amount)
{
// Mastercard payment processing logic
Console.WriteLine("Processing Mastercard payment...");
return await Task.FromResult(true);
}
}
UPI: Google Pay and PhonePe Implementations
public class GooglePayGateway : IUPIPaymentGateway
{
public string GetPaymentMethodType() => "UPI";
public string GetUPIProvider() => "Google Pay";
public async Task<bool> ProcessPayment(decimal amount)
{
// Google Pay payment processing logic
Console.WriteLine("Processing Google Pay payment...");
return await Task.FromResult(true);
}
}
public class PhonePeGateway : IUPIPaymentGateway
{
public string GetPaymentMethodType() => "UPI";
public string GetUPIProvider() => "PhonePe";
public async Task<bool> ProcessPayment(decimal amount)
{
// PhonePe payment processing logic
Console.WriteLine("Processing PhonePe payment...");
return await Task.FromResult(true);
}
}
Online Banking: SBI and HDFC Implementations
public class SBIPaymentGateway : IBankingPaymentGateway
{
public string GetPaymentMethodType() => "Banking";
public string GetBankName() => "SBI";
public async Task<bool> ProcessPayment(decimal amount)
{
// SBI banking payment processing logic
Console.WriteLine("Processing SBI banking payment...");
return await Task.FromResult(true);
}
}
public class HDFCPaymentGateway : IBankingPaymentGateway
{
public string GetPaymentMethodType() => "Banking";
public string GetBankName() => "HDFC";
public async Task<bool> ProcessPayment(decimal amount)
{
// HDFC banking payment processing logic
Console.WriteLine("Processing HDFC banking payment...");
return await Task.FromResult(true);
}
}
Step 3: Grouping and Managing Payment Gateways 🧩
With our individual payment gateways ready, let’s create a PaymentManager to group these gateways and handle payments dynamically based on user preference.
public class PaymentManager
{
private readonly IEnumerable<IPaymentGateway> _paymentGateways;
public PaymentManager(IEnumerable<IPaymentGateway> paymentGateways)
{
_paymentGateways = paymentGateways;
}
public async Task<bool> ProcessPayment(string methodType, decimal amount)
{
var gateway = _paymentGateways.FirstOrDefault(pg => pg.GetPaymentMethodType() == methodType);
if (gateway != null)
{
return await gateway.ProcessPayment(amount);
}
Console.WriteLine("Payment method not found!");
return false;
}
}
Here, we’re dynamically choosing the payment gateway based on the method type (Card, UPI, or Banking).
Step 4: Dependency Injection in .NET Core 🎯
Finally, let’s tie it all together using Dependency Injection. This allows us to register our various payment gateway implementations and inject them where needed.
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<ICardPaymentGateway, VisaPaymentGateway>();
services.AddScoped<ICardPaymentGateway, MastercardPaymentGateway>();
services.AddScoped<IUPIPaymentGateway, GooglePayGateway>();
services.AddScoped<IUPIPaymentGateway, PhonePeGateway>();
services.AddScoped<IBankingPaymentGateway, SBIPaymentGateway>();
services.AddScoped<IBankingPaymentGateway, HDFCPaymentGateway>();
services.AddScoped<PaymentManager>();
}
Wrapping It Up 🎁
And there you have it! Integrating multiple payment gateways in .NET Core, neatly grouped by payment types — whether it’s cards, UPIs, or online banking. This approach keeps your architecture clean, modular, and easy to extend as your application grows.
By following this strategy, you can offer your customers a seamless checkout experience no matter their preferred payment method. 🚀
Want More?
Stay tuned! In future articles, we’ll dive even deeper into advanced scenarios like handling transaction failures, retries, and integrating third-party payment SDKs.
Until then, keep coding and keep growing! 😊