FahdCloud.ThirdParty.PaymobIntegrationV2 2.0.1

dotnet add package FahdCloud.ThirdParty.PaymobIntegrationV2 --version 2.0.1
                    
NuGet\Install-Package FahdCloud.ThirdParty.PaymobIntegrationV2 -Version 2.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FahdCloud.ThirdParty.PaymobIntegrationV2" Version="2.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FahdCloud.ThirdParty.PaymobIntegrationV2" Version="2.0.1" />
                    
Directory.Packages.props
<PackageReference Include="FahdCloud.ThirdParty.PaymobIntegrationV2" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add FahdCloud.ThirdParty.PaymobIntegrationV2 --version 2.0.1
                    
#r "nuget: FahdCloud.ThirdParty.PaymobIntegrationV2, 2.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=FahdCloud.ThirdParty.PaymobIntegrationV2&version=2.0.1
                    
Install FahdCloud.ThirdParty.PaymobIntegrationV2 as a Cake Addin
#tool nuget:?package=FahdCloud.ThirdParty.PaymobIntegrationV2&version=2.0.1
                    
Install FahdCloud.ThirdParty.PaymobIntegrationV2 as a Cake Tool

PaymobIntegration Library

The FahdCloud.ThirdParty.PaymobIntegrationV2 library is a .NET library designed to simplify integration with the Paymob payment gateway. It enables developers to create checkout URLs for payment transactions and retrieve payment details efficiently, using dependency injection and modern C# practices.

Features

  • Generate checkout URLs for initiating Paymob payment transactions.
  • Retrieve payment details for completed transactions.
  • Supports authentication with Paymob API using API keys.
  • Caches authentication tokens to optimize performance.
  • Validates request data to ensure compliance with Paymob API requirements.
  • Handles HTTP requests with robust error handling.

Installation

  1. Add the Library to Your Project:

    • Install the FahdCloud.ThirdParty.PaymobIntegrationV2 NuGet package:
      dotnet add package FahdCloud.ThirdParty.PaymobIntegrationV2
      
    • Alternatively, clone or download the library from the repository and add it to your solution.
  2. Set Up Dependency Injection: Register the library's services in your application's dependency injection container using the AddPaymobClient extension method.

     using Microsoft.EntityFrameworkCore;
    
     var builder = WebApplication.CreateBuilder(args);
    
     builder.Services.AddControllers();
     // Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
     builder.Services.AddOpenApi();
    
     // Add PaymobClient
     services.AddPaymobClient();
    
     var app = builder.Build();
    
     // Configure the HTTP request pipeline.
     if (app.Environment.IsDevelopment())
     {
         app.MapOpenApi();
     }
    
     app.UseHttpsRedirection();
    
     app.UseAuthorization();
    
     app.MapControllers();
    
     app.Run();
    
    

Usage

Below is an example of how to use the FahdCloud.ThirdParty.PaymobIntegrationV2 library to create a checkout URL and retrieve payment details.

using System.Text.Json;
using Microsoft.Extensions.DependencyInjection;
using FahdCloud.ThirdParty.PaymobIntegrationV2;
using FahdCloud.ThirdParty.PaymobIntegrationV2.Configuration;
using FahdCloud.ThirdParty.PaymobIntegrationV2.Models.Requests;

var services = new ServiceCollection();
services.AddPaymobClient();

var paymobConfig = new PaymobConfig
{
    BaseUrl = "https://accept.paymob.com",
    ApiKey = "your-api-key", // Replace with your Paymob API key
    PublicKey = "your-public-key", // Replace with your Paymob public key
    SecretKey = "your-secret-key" // Replace with your Paymob secret key
};

var serviceProvider = services.BuildServiceProvider();
var paymobClient = serviceProvider.GetService<IPaymobClient>();

// Create a checkout URL
var checkoutResponse = await paymobClient.CreateCheckoutUrlAsync(CreateDummyRequest(), paymobConfig);
Console.WriteLine($"Checkout URL: {checkoutResponse.checkOutUrl}");

// Retrieve payment details
Console.Write("Enter Merchant Order Id: ");
string merchantOrderId = Console.ReadLine()!.Trim();
var paymentDetailsRequest = new PaymentDetailsRequest { merchant_order_id = merchantOrderId };
var paymentDetails = await paymobClient.GetPaymentDetails(paymentDetailsRequest, paymobConfig);
Console.WriteLine($"Payment Details: {JsonSerializer.Serialize(paymentDetails)}");

// Example dummy request for checkout
static PaymentTransactionRequest CreateDummyRequest()
{
    return new PaymentTransactionRequest
    {
        amount = 10000, // Amount in cents (e.g., 100.00 EGP)
        currency = "EGP",
        redirection_url = "https://yourwebsite.com/redirect",
        // Add other required fields as per Paymob API
    };
}

Note: Replace your-api-key, your-public-key, and your-secret-key with your actual Paymob API credentials. Implement YourCacheServiceImplementation for ICacheService (e.g., using in-memory or distributed caching like Redis).

Configuration

The PaymobConfig class is used to configure the library with your Paymob API credentials and base URL.

var config = new PaymobConfig
{
    BaseUrl = "https://accept.paymob.com", // Paymob API base URL
    ApiKey = "your-api-key",              // Your Paymob API key
    SecretKey = "your-secret-key",        // Your Paymob secret key
    PublicKey = "your-public-key"         // Your Paymob public key
};

Obtain these credentials from your Paymob merchant account dashboard.

Requirements

  • .NET 6.0 or later
  • The FahdCloud.ThirdParty.PaymobIntegrationV2 NuGet package (includes all necessary dependencies)
  • A caching implementation for ICacheService (e.g., in-memory or distributed cache)

Error Handling

The library includes robust error handling:

  • Throws ArgumentNullException for null inputs.
  • Throws ValidationException for invalid request data.
  • Throws HttpRequestException for failed API calls with detailed error messages.
  • Throws JsonException for deserialization errors.

Contributing

Contributions are welcome! Please submit a pull request or open an issue to suggest improvements or report bugs.

License

This library is licensed under the MIT License. See the LICENSE file for details.

Contact

For support or inquiries, visit the Paymob API documentation at https://docs.paymob.com.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
2.0.1 31 5/27/2025
1.0.0 30 5/26/2025