https://github.com/xshaheen/xdot-paymob
.NET SDK to help you integrate with the Paymob’s payment gateway.
https://github.com/xshaheen/xdot-paymob
csharp dotnet dotnet-core payment
Last synced: 5 months ago
JSON representation
.NET SDK to help you integrate with the Paymob’s payment gateway.
- Host: GitHub
- URL: https://github.com/xshaheen/xdot-paymob
- Owner: xshaheen
- License: apache-2.0
- Created: 2021-08-11T08:10:09.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2025-03-17T16:53:52.000Z (over 1 year ago)
- Last Synced: 2025-09-24T01:35:49.144Z (9 months ago)
- Topics: csharp, dotnet, dotnet-core, payment
- Language: C#
- Homepage:
- Size: 290 KB
- Stars: 31
- Watchers: 1
- Forks: 6
- Open Issues: 15
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE.txt
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
# Xdot.Paymob
[](https://github.com/xshaheen/xdot-paymob/actions) [](https://www.nuget.org/packages/Xdot.Paymob.CashIn)
.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-core-cli-tools]:
```sh
dotnet add package Xdot.Paymob.CashIn.DependencyInjection
```
Using the [NuGet CLI][nuget-cli]:
```sh
nuget install Xdot.Paymob.CashIn.DependencyInjection
```
Using the [Package Manager Console][package-manager-console]:
```powershell
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.
```c#
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
```
- If you don't use Microsoft DI, Use the base package [Xdot.Paymob.CashIn][cash-in-package] and configure your DI to the
equivalent to [this configuration][di-config-ref].
---
- 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
```c#
public class CashInService
{
private readonly IPaymobCashInBroker _broker;
public CashInService(IPaymobCashInBroker broker)
{
_broker = broker;
}
public async Task 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
```c#
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(content, SerializerOptions)!;
var valid = broker.Validate(transaction, hmac);
if (!valid)
{
return BadRequest();
}
// TODO: Handle transaction.
return Ok();
}
case CashInCallbackTypes.Token:
{
var token = JsonSerializer.Deserialize(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
[cash-in-package]: https://www.nuget.org/packages/Xdot.Paymob.CashIn/
[dotnet-core-cli-tools]: https://docs.microsoft.com/en-us/dotnet/core/tools/
[nuget-cli]: https://docs.microsoft.com/en-us/nuget/tools/nuget-exe-cli-reference
[package-manager-console]: https://docs.microsoft.com/en-us/nuget/tools/package-manager-console
[di-config-ref]: https://github.com/xshaheen/xdot-paymob/blob/main/src/CashIn.DependencyInjection/ServiceCollectionExtensions.cs#L58