Xdot.Paymob.CashIn.DependencyInjection 1.0.7

This package has a SemVer 2.0.0 package version: 1.0.7+build.259.
dotnet add package Xdot.Paymob.CashIn.DependencyInjection --version 1.0.7                
NuGet\Install-Package Xdot.Paymob.CashIn.DependencyInjection -Version 1.0.7                
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="Xdot.Paymob.CashIn.DependencyInjection" Version="1.0.7" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Xdot.Paymob.CashIn.DependencyInjection --version 1.0.7                
#r "nuget: Xdot.Paymob.CashIn.DependencyInjection, 1.0.7"                
#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.
// Install Xdot.Paymob.CashIn.DependencyInjection as a Cake Addin
#addin nuget:?package=Xdot.Paymob.CashIn.DependencyInjection&version=1.0.7

// Install Xdot.Paymob.CashIn.DependencyInjection as a Cake Tool
#tool nuget:?package=Xdot.Paymob.CashIn.DependencyInjection&version=1.0.7                

Xdot.Paymob

GitHub Actions Status xdot-paymob NuGet Package Downloads

.NET SDK to help you integrate with the Paymob’s payment gateway. to help you integrate with the Paymob’s payment gateway.

if you like this work, please consider give the project star 🌟

Features

  • Supporting .NET Standard 2.0+, .NET 5+, .NET Core 2.0+, and .NET Framework 4.6.1+.
  • Automatic retries - The library automatically retries requests on intermittent failures.
  • Manage authentication tokens - The library manage authenticate (using the configured ApiKey), token caching and invalidation. so you don't need to provide it for each request and the library provide it in each request need it for you.
  • You can access the full response property.
  • Handle API DateTime responses correctly - all responses use DateTimeOffset.
  • Ability to swap the configuration (API key, hmac, ...) during runtime.

Installation

Using the .NET CLI tools:

dotnet add package Xdot.Paymob.CashIn.DependencyInjection

Using the NuGet CLI:

nuget install Xdot.Paymob.CashIn.DependencyInjection

Using the Package Manager Console:

Install-Package Xdot.Paymob.CashIn.DependencyInjection

Usage

Configuration Dependency Injection

Configure the library in Startup.cs with these helper methods. This will inject IPaymobCashInBroker (used to call the Paymob API), IPaymobCashInAuthenticator (used to authenticate and manage authentication token), and configure options.

services.AddPaymobCashIn(config => {
    config.ApiKey = "Api Key for your Paymob’s accont";
    config.Hmac = "Hmac secret for your Paymob’s accont",
});

// Alert: ApiKey and Hmac is a sensitive settings make sure to store them into
// a secret manager (Azure key vault for example).
// DON'T STORE SECRETS IN CODE

  • Then you can inject IPaymobCashInBroker to your service and use to to call the Paymob API.

Here's the details of what the IPaymobCashInBroker has to offer:

Method Description
CreateOrderAsync See: https://docs.paymob.com/docs/accept-standard-redirect#2-order-registration-api
RequestPaymentKeyAsync See: https://docs.paymob.com/docs/accept-standard-redirect#3-payment-key-request
CreateWalletPayAsync See: https://docs.paymob.com/docs/mobile-wallets#pay-request
CreateKioskPayAsync See: https://docs.paymob.com/docs/kiosk-payments
CreateCashCollectionPayAsync See: https://docs.paymob.com/docs/cash-collection
CreateSavedTokenPayAsync See: https://docs.paymob.com/docs/pay-with-saved-token
GetTransactionAsync Get transaction by id.
GetTransactionsPageAsync Get transactions page.
GetOrderAsync Get an order by id.
GetOrdersPageAsync Get orders page.
CreateIframeSrc Helper method to create iframe src url
Validate Helper method to verify callback content with your hmac secret see: https://docs.paymob.com/docs/transaction-webhooks#hmac-authentication

Simple Example

  • Create a payment
public class CashInService
{
    private readonly IPaymobCashInBroker _broker;

    public CashInService(IPaymobCashInBroker broker)
    {
        _broker = broker;
    }

    public async Task<string> RequestCardPaymentKey()
    {
        // Create order.
        var amountCents = 1000; // 10 LE
        var orderRequest = CashInCreateOrderRequest.CreateOrder(amountCents);
        var orderResponse = await _broker.CreateOrderAsync(orderRequest);

        // Request card payment key.
        var billingData = new CashInBillingData(
            firstName: "Mahmoud",
            lastName: "Shaheen",
            phoneNumber: "010000000",
            email: "someone@gmail.com");

        var paymentKeyRequest = new CashInPaymentKeyRequest(
            integrationId: 123, // change this
            orderId: orderResponse.Id,
            billingData: billingData,
            amountCents: amountCents);

        var paymentKeyResponse = await _broker.RequestPaymentKeyAsync(paymentKeyRequest);

        // Create iframe src.
        return _broker.CreateIframeSrc(iframeId: "1234", token: paymentKeyResponse.PaymentKey);
    }
}
  • Define the callback
private static readonly JsonSerializerOptions SerializerOptions = new(JsonSerializerDefaults.Web)
{
    NumberHandling = JsonNumberHandling.AllowReadingFromString,
};

[HttpPost("cashin-callback")]
public ActionResult CashInCallback(
    [FromQuery] string hmac,
    [FromBody] CashInCallback callback,
    IPaymobCashInBroker broker
)
{
    if (callback.Type is null || callback.Obj is null)
    {
        throw new InvalidOperationException("Unexpected transaction callback.");
    }

    var content = ((JsonElement) callback.Obj).GetRawText();

    switch (callback.Type.ToUpperInvariant())
    {
        case CashInCallbackTypes.Transaction:
        {
            var transaction = JsonSerializer.Deserialize<CashInCallbackTransaction>(content, SerializerOptions)!;
            var valid = broker.Validate(transaction, hmac);

            if (!valid)
            {
                return BadRequest();
            }

            // TODO: Handle transaction.

            return Ok();
        }
        case CashInCallbackTypes.Token:
        {
            var token = JsonSerializer.Deserialize<CashInCallbackToken>(content, SerializerOptions)!;
            var valid = broker.Validate(token, hmac);

            if (!valid)
            {
                return BadRequest();
            }

            // TODO: Handle token.

            return Ok();
        }
        default:
            throw new InvalidOperationException($"Unexpected {nameof(CashInCallbackTypes)} = {callback.Type}");
    }
}

License

This project is licensed under the Apache 2.0 license.

Contact

If you have any suggestions, comments or questions, please feel free to contact me on:

Email: mxshaheen@gmail.com

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
1.0.7 1,168 10/25/2023
1.0.6 992 10/15/2022
1.0.5 664 6/28/2022
1.0.0 808 1/2/2022
0.0.0-preview.0.30 157 12/6/2021
0.0.0-preview.0.27 154 12/6/2021
0.0.0-preview.0.26 157 10/2/2021
0.0.0-preview.0.23 164 9/5/2021
0.0.0-preview.0.21 153 9/1/2021
0.0.0-preview.0.10 152 8/25/2021
0.0.0-preview.0.9 169 8/20/2021
0.0.0-preview.0.7 140 8/18/2021
0.0.0-preview.0.4 171 8/12/2021
0.0.0-preview.0 159 8/11/2021