Thawani Pay SDK for .NET

Recently I was working on a project in .NET where I needed to integrate Thawani Payment Gateway (Oman Based Payment Gateway). There API documentation is great but there isn’t any official library in .NET. Many of the new developers might find it hard to start consuming the APIs, generating the JSON, etc.

I wrapped all such calls into a project and sharing it with you all, which you can install from nuget directly into your project.

The project’s source code can be found on GitHub.

Here is the sample code from the project:

For creating Payment Sessions

var checkout = new ThawaniPaySDK.Checkout();
//If you have your API Keys and want to use the Production instead of UAT
var checkout = new ThawaniPaySDK.Checkout("apiKey", "publicKey");
var products = new List<ThawaniPaySDK.Models.CommonModels.ProductModel>()
{
    new ThawaniPaySDK.Models.CommonModels.ProductModel()
    {
        name = "Product 1",
        quantity = 1,
        unit_amount = 100
    }
};

var sessionModel = new ThawaniPaySDK.Models.CheckoutModels.CheckoutSessionCreateRequestModel()
{
    success_url = "https://domain.com/success/",
    cancel_url = "https://domain.com/cancel/",
    mode = "payment",
    client_reference_id = "",
    customer_id = "", //If you have a customer id, the customer can save their cards. We will see how you can initiate a payment intent using a saved card
    products = products,
    metadata = new ThawaniPaySDK.Models.CommonModels.MetadataModel()
    {
        customer_email = "example@domain.com", //Mandatory
        customer_name = "Customer Name", //Mandatory
        customer_phone = "968 12345678", //Mandatory
        customer_id = "", //Optional
        order_id = "" //Optional
    }
};

var sessionInfo = checkout.CreateSession(sessionModel);
var url = checkout.GeneratePaymentURL(sessionInfo.data.session_id);

The payment status of the Checkout Session, one of paidunpaid, or cancelled. You can use this value to decide when to fulfill your customer’s order.

For creating a Customer (This is required before you can make Payment Intents)

var customer = new ThawaniPaySDK.Customer();
var customerCreateModel = customer.CreateCustomer(new ThawaniPaySDK.Models.CustomerModels.CustomerCreateRequestModel()
{
    client_customer_id = "",
    //A unique Identification from you, it could be an Email or a UserId.
    //It would later be used for retrieval of the Customer Info
});

var customerDeleteModel = customer.DeleteCustomer("customer_id");
//The above will delete customer

var paymentMethods = new ThawaniPaySDK.PaymentMethods();
var customerPaymentMethods = paymentMethods.ListCustomerPaymentMethods("customer_id");
//This above will list out all of the cards with their Masked Numbers, NickName, Brand and an Id (Payment Method Id)

var paymentMethodDeletedModel = paymentMethods.DeleteCustomerPaymentMethod("payment_method_id");
//The above will delete Payment Method

For creating a Payment Intent

var paymentIntent = new ThawaniPaySDK.PaymentIntent();
//If you have your API Keys and want to use the Production instead of UAT
var paymentIntent = new ThawaniPaySDK.PaymentIntent("apiKey", "publicKey");
var paymentIntentModel = paymentIntent.CreatePaymentIntent(new ThawaniPaySDK.Models.PaymentIntentModels.CreatePaymentIntentRequestModel()
{
    amount = 100,
    client_reference_id = "", //The Customer Id
    metadata = new ThawaniPaySDK.Models.CommonModels.MetadataModel()
    {
        customer_email = "example@domain.com", //Mandatory
        customer_name = "Customer Name", //Mandatory
        customer_phone = "968 12345678", //Mandatory
        customer_id = "", //Optional
        order_id = "" //Optional
    },
    payment_method_id = "", //The Card Id saved at Thawani
    return_url = "https://domain.com/returnURL"
});

var paymentIntentConfirmModel = paymentIntent.ConfirmPaymentIntent(new ThawaniPaySDK.Models.PaymentIntentModels.ConfirmPaymentIntentRequestModel()
{
    amount = 100,
    payment_method_id = "", //The Card Id saved at Thawani
},
paymentIntentModel.data.id);

var paymentIntentURL = paymentIntentConfirmModel.data.next_action.url;

var paymentIntentRetrieveModel = paymentIntent.RetrievePaymentIntent(paymentIntentModel.data.id);

Status of this PaymentIntent, one of requires_payment_methodrequires_confirmationrequires_actioncancelled, or succeeded

For further details, you can check Thawani’s official docs page.


Buy me coffee

I would like to continue supporting the community. Showing your support would encourage me more and keep this going on.

$5.00

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s