Codeblazor.PaymentGateway.Core.Library
1.0.0
dotnet add package Codeblazor.PaymentGateway.Core.Library --version 1.0.0
NuGet\Install-Package Codeblazor.PaymentGateway.Core.Library -Version 1.0.0
<PackageReference Include="Codeblazor.PaymentGateway.Core.Library" Version="1.0.0" />
<PackageVersion Include="Codeblazor.PaymentGateway.Core.Library" Version="1.0.0" />
<PackageReference Include="Codeblazor.PaymentGateway.Core.Library" />
paket add Codeblazor.PaymentGateway.Core.Library --version 1.0.0
#r "nuget: Codeblazor.PaymentGateway.Core.Library, 1.0.0"
#:package Codeblazor.PaymentGateway.Core.Library@1.0.0
#addin nuget:?package=Codeblazor.PaymentGateway.Core.Library&version=1.0.0
#tool nuget:?package=Codeblazor.PaymentGateway.Core.Library&version=1.0.0
Generic Payment Gateway Library
This library provides a flexible and extensible solution for integrating multiple payment gateways into your .NET Core applications. It supports various payment gateways like Razorpay, Paytm, CCAvenue, ICICI, PayU, Atom, SBI ePay, and more, using a factory pattern for easy integration.
Features
- Supports multiple payment gateways: Razorpay, Paytm, CCAvenue, ICICI, PayU, Atom, SBI ePay, WorldLine, etc.
- Factory pattern: Easily switch between different gateways using the factory pattern.
- Configurable: Payment gateway credentials and configurations can be passed via a dictionary.
- Extensible: Add more gateways by implementing the
IPaymentGatewayinterface. - Error handling: Custom exceptions to handle payment gateway-specific errors.
Installation
- Clone the repository or download the source code.
- Add the project as a reference in your .NET Core MVC application.
- Ensure the required dependencies are installed for each gateway (e.g., Razorpay SDK).
dotnet add package Razorpay.Api
Usage
1. Define the Gateway Enum
Define an enum to specify the supported gateways:
public enum GatewayTypeEnum
{
RazorPay,
Paytm,
CCAvenue,
ICICI,
PayU,
Atom,
SBIePay,
WorldLine
}
2. Implement the IPaymentGateway Interface
Each payment gateway implements the IPaymentGateway interface. For example, the RazorpayGateway class:
public class RazorpayGateway : IPaymentGateway
{
private readonly string _apiKey;
private readonly string _apiSecret;
public RazorpayGateway(string apiKey, string apiSecret)
{
_apiKey = apiKey ?? throw new ArgumentNullException(nameof(apiKey));
_apiSecret = apiSecret ?? throw new ArgumentNullException(nameof(apiSecret));
}
public void ProcessPayment(decimal amount)
{
// Razorpay payment processing logic
Console.WriteLine($"Processing payment of {amount} using Razorpay.");
}
public string GetPaymentStatus(string paymentId)
{
// Razorpay payment status logic
return $"Payment status for {paymentId} is Success.";
}
}
3. Payment Gateway Factory
Use the PaymentGatewayFactory to create the appropriate gateway instance based on the GatewayTypeEnum:
public static class PaymentGatewayFactory
{
public static IPaymentGateway Create(GatewayTypeEnum gatewayId, IDictionary<string, string> config)
{
return gatewayId switch
{
GatewayTypeEnum.RazorPay => new RazorpayGateway(config["ApiKey"], config["ApiSecret"]),
GatewayTypeEnum.Paytm => new PaytmGateway(config["MerchantId"], config["SecretKey"]),
GatewayTypeEnum.CCAvenue => new CCAvenueGateway(config["MerchantId"], config["AccessCode"], config["WorkingKey"]),
// Add other gateways...
_ => throw new NotSupportedException("Payment gateway not supported")
};
}
}
4. Example Usage in Controller
In your controller, use the factory to create a gateway instance and process payments:
public class PaymentController : Controller
{
public IActionResult ProcessPayment(string paymentId, decimal amount)
{
var config = new Dictionary<string, string>
{
{ "ApiKey", "your_api_key" },
{ "ApiSecret", "your_api_secret" }
};
var gateway = PaymentGatewayFactory.Create(GatewayTypeEnum.RazorPay, config);
gateway.ProcessPayment(amount);
var status = gateway.GetPaymentStatus(paymentId);
ViewBag.Status = status;
return View();
}
}
5. Handling Exceptions
The library uses custom exceptions (PaymentGatewayException) to handle errors specific to each payment gateway. For example:
public class PaymentGatewayException : Exception
{
public int ErrorCode { get; }
public PaymentGatewayException(string message, int errorCode) : base(message)
{
ErrorCode = errorCode;
}
public PaymentGatewayException(string message, int errorCode, Exception innerException) : base(message, innerException)
{
ErrorCode = errorCode;
}
}
Supported Gateways
Currently, the following gateways are supported:
- Razorpay
- Paytm
- CCAvenue
- ICICI
- PayU
- Atom
- SBI ePay
- WorldLine
Extending the Library
To add support for additional gateways, follow these steps:
- Create a new class that implements the IPaymentGateway interface.
- Implement the methods ProcessPayment and GetPaymentStatus.
- Add the new gateway to the PaymentGatewayFactory.
Example:
public class NewGateway : IPaymentGateway
{
public void ProcessPayment(decimal amount)
{
// New gateway payment logic
}
public string GetPaymentStatus(string paymentId)
{
// New gateway payment status logic
return "Success";
}
}
Then, update the PaymentGatewayFactory:
public static class PaymentGatewayFactory
{
public static IPaymentGateway Create(GatewayTypeEnum gatewayId, IDictionary<string, string> config)
{
return gatewayId switch
{
GatewayTypeEnum.NewGateway => new NewGateway(),
// Other gateways...
_ => throw new NotSupportedException("Payment gateway not supported")
};
}
}
Error Handling
The library uses custom exceptions to handle specific errors from each payment gateway. You can catch these exceptions in your application and handle them accordingly.
Example:
try
{
var gateway = PaymentGatewayFactory.Create(GatewayTypeEnum.RazorPay, config);
gateway.ProcessPayment(amount);
}
catch (PaymentGatewayException ex)
{
// Handle payment gateway error
Console.WriteLine($"Error: {ex.Message}, Code: {ex.ErrorCode}");
}
License
This library is open-source and licensed under the MIT License. See the LICENSE file for more information.
Feel free to modify and extend this library to suit your needs!
This `README.md` file provides detailed information about the library, including setup, usage, and how to extend it with new payment gateways. It should help developers integrate and extend the payment gateway functionality in their .NET Core applications.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Microsoft.Extensions.Configuration (>= 9.0.1)
- Newtonsoft.Json (>= 13.0.3)
- RazorPay.Core (>= 1.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0 | 207 | 1/25/2025 |