Skip to content

russkyc/paymongo-sharp

Repository files navigation

Paymongo.Sharp - Effortless Paymongo integration for .NET

Nuget

Paymongo is a powerful payment platform that provides a full suite of financial tools for businesses and more. With the Paymongo.Sharp, you can integrate payment processing into your .NET applications, allowing you to securely accept payments, manage transactions, and more.

This client wrapper is designed to make it easy for .NET developers to interact with the Paymongo API. It provides a simple, intuitive interface that abstracts the API, allowing you to access a typed client and focus on building your Paymongo-integrated application faster.

⬇️ Installation



Nuget

Install package from: Nuget using nuget cli or the nuget package manager.


Add Imports

using Paymongo.Sharp;

Initialize Client

var client = new PaymongoClient(apiKey: "<api_key or public_key>");

⭐ Payments Demo

Cli (Console)

WPF (With WebView 2 Control)

Blazor

This nuget package is not limited to these samples, it also supports the all .NET platforms including mono, android, and others.

✨ What API Actions are currently Supported

This client is in active development and features are slowly being implemented but not all of them are supported as of now. You can track the support for all of Paymongo's official API actions below:

Feature Support Table (Paymongo.Sharp Version: 1.0.0+)

API Resource Implementation Status
Checkout Full
Links Full
Payment Intent Full
Payment Method Full
Payments Full
Card Installments Full
Refunds Full
Sources Full
QR PH Full
Webhooks Full
⚠️ Customers Partial
Child Merchant Unavailable, Planned
File Record Unavailable, Planned
Platforms Unavailable, Planned
Related Consumer Unavailable, Planned
Requirements Unavailable, Planned
Subscriptions Unavailable, Planned
Treasury Unavailable, Planned

💥 Breaking Changes (V1.0.0+)

  • All amount related properties should now be single number values, eg; 100.00 should be represented as 10000, to help with this there is a new extension method ToLongAmount() that can be used to convert decimal values to the correct amount format.
decimal amount = 100.00m;
// Convert to long amount (single number value in with centavos)
long longAmount = amount.ToLongAmount(); // 10000

📓 Basic Client API Reference

If you want to get started with this Paymongo client, you can check the refernces for basic usage below.

Checkout

  • Create Checkout Session
  • Retrieve a Checkout Session
  • Expire a Checkout Session

Create a Checkout Session

// We create a new Checkout object
// This one includes the minimal required values
Checkout checkout = new Checkout() {
    Description = "Test Checkout",
    LineItems = new [] {
        new LineItem {
            Name = "item_name",
            Quantity = 1,
            Currency = Currency.Php,
            Amount = 3500
        }
    },
    PaymentMethodTypes = new [] {
        PaymentMethod.GCash,
        PaymentMethod.Card,
        PaymentMethod.Paymaya
    }
};

// We use the PaymongoClient from earlier
// This returns the Checkout object with the new server info for checkout url, id, and others
Checkout checkoutResult = await client.Checkouts.CreateCheckoutAsync(checkout);

Retrieve a Checkout Session

// We use the PaymongoClient from earlier
// Lets assume that the checkout id is "12345678"
// This returns a Checkout object from the server
Checkout checkoutResult = await client.Checkouts.RetrieveCheckoutAsync("12345678");

Expire a Checkout Session

// We use the PaymongoClient from earlier
// Lets assume that the checkout id is "12345678"
// This expires the checkout on the server and returns the expired Checkout object
Checkout checkoutResult = await client.Checkouts.ExpireCheckoutAsync("12345678");

For full Checkout API reference, please see: Checkout Session Resource


Payment Intent

  • Create Payment Intent
  • Retrieve a Payment Intent
  • Attach to a Payment Intent

Create a Payment Intent

// Create a new PaymentIntent object with minimal required values
PaymentIntent paymentIntent = new PaymentIntent
{
    Amount = 10000,
    Currency = Currency.Php,
    PaymentMethodAllowed =
    [
        PaymentMethod.Card,
        PaymentMethod.Paymaya
    ],
    PaymentMethodOptions = new PaymentMethodOption()
    {
        Card = new Card()
    }
};

// Use the PaymongoClient to create the payment intent
PaymentIntent paymentIntentResult = await client.PaymentIntents.CreatePaymentIntentAsync(paymentIntent);

Retrieve a Payment Intent

// Let's assume we have a payment intent id from the previous step
const string paymentIntentId = "pi_WENqK7d5L3XN9YQzEt39B3oF";

PaymentIntent getPaymentIntent = await client.PaymentIntents.RetrievePaymentIntentAsync(paymentIntentId);

Attach to a Payment Intent

// Attach a payment method to an existing PaymentIntent
const string paymentIntentId = "pi_WENqK7d5L3XN9YQzEt39B3oF";

PaymentIntentAttachment paymentIntentAttachment = new PaymentIntentAttachment
{
    PaymentMethod = PaymentMethod.Card,
    ReturnUrl = "https://google.com"
};

PaymentIntent paymentIntentResult = await client.PaymentIntents.AttachToPaymentIntentAsync(paymentIntentId, paymentIntentAttachment);

For full Payment API reference, please see: The Payment Intent Object, (Pre-Authorization) Capture, (Pre-Authorization) Cancel


Payment Method

  • Create a Payment Method
  • Retrieve a Payment Method
  • Update a Payment Method
  • Retrieve list of Payment Methods

Create a Payment Method

// We create a new PaymentMethod object
PaymentMethod paymentMethod = new PaymentMethod {
    Type = PaymentMethodType.GCash,
    Billing = new Billing {
        Name = "Test Name",
        Email = "test@paymongo.com",
        Phone = "+639123456789",
        Address = new Address {
            Line1 = "Test Address 1",
            Line2 = "Test Address 2",
            PostalCode = "1234",
            State = "Test State",
            City = "Test City",
            Country = "PH"
        }
    }
};

// Use the PaymongoClient from earlier
PaymentMethod paymentMethodResult = await client.PaymentMethods.CreatePaymentMethodAsync(paymentMethod);

Retrieve a Payment Method

// Use the PaymongoClient from earlier
// Let's assume the payment method id is "pm_12345678"
PaymentMethod paymentMethodResult = await client.PaymentMethods.RetrievePaymentMethodAsync("pm_12345678");

Update a Payment Method

// Retrieve the payment method first
PaymentMethod paymentMethodResult = await client.PaymentMethods.RetrievePaymentMethodAsync("pm_12345678");

// Update properties as needed
paymentMethodResult.Cvc = "424";

// Update the payment method on the server
PaymentMethod updatedPaymentMethodResult = await client.PaymentMethods.UpdatePaymentMethodAsync(paymentMethodResult);

Retrieve list of Payment Methods

// Use the PaymongoClient from earlier
IEnumerable<PaymentMethod> paymentMethods = await client.PaymentMethods.RetrievePaymentMethodsAsync();

For full Payment Method API reference, please see: The Payment Method Object


Payments

  • Create a Payment
  • List all Payments
  • Retrieve a Payment

List All Payments

// We use the PaymongoClient from earlier
// This returns a list of Payment objects
IEnumerable<Payment> paymentsResult = await client.Payments.ListAllPaymentsAsync();

Retrieve a Payment

// We use the PaymongoClient from earlier
// Lets assume that the payment id is "12345678"
// This returns a Payment object from the server
Payment paymentResult = await client.Payments.RetrievePaymentAsync("12345678");

For full Payments API reference, please see: Payment Resource


Subscriptions (Plans)

  • Create Plan
  • Retrieve a Plan
  • Update a Plan
  • Retrieve Lis of Plans

For full Subscriptions(Plans) API reference, please see: Plan Resource


Links

  • Create a Link
  • Retrieve a Link
  • Get Link by Reference Number
  • Archive a Link
  • Unarchive a Link

Create a Link

// We create a new Link object
// This one includes the minimal required values
Link link = new Link {
    Description = "New Link",
    Amount = 100000,
    Currency = Currency.Php
};

// We use the PaymongoClient from earlier
// This returns the Link object with the new server info for checkout url, id, and others
Link linkResult = await client.Links.CreateLinkAsync(link);

Retrieve a Link

// We use the PaymongoClient from earlier
// Lets assume that the link id is "12345678"
// This returns a Link object from the server
Link linkResult = await client.Links.RetrieveLinkAsync("12345678");

Get Link by Reference Number

// We use the PaymongoClient from earlier
// Lets assume that the link id is "ABCD1234"
// This returns a Link object from the server
Link linkResult = await client.Links.GetLinkByReferenceNumberAsync("ABCD1234");

Archive a Link

// We use the PaymongoClient from earlier
// Lets assume that the Link id is "12345678"
// This returns the updated archived Link object
Link linkResult = await client.Links.ArchiveLinkAsync("12345678");

Unarchive a Link

// We use the PaymongoClient from earlier
// Lets assume that the payment id is "12345678"
// This returns the updated un-archived Link object
Link linkResult = await client.Links.UnarchiveLinkAsync("12345678");

For full Links API reference, please see: Links Resource


Webhooks

  • Create a Webhook
  • List all Webhooks
  • Retrieve a Webhook
  • Disable a Webhook
  • Enable a Webhook
  • Update a Webhook

Create a Webhook

// Create a new Webhook object with required values
Webhook webhook = new Webhook {
    Url = "https://www.example.com/webhook",
    Events = new[] { "source.chargeable", "payment.paid" }
};

// Use the PaymongoClient to create the webhook
Webhook created = await client.Webhooks.CreateWebhookAsync(webhook);

Retrieve a Webhook

// Retrieve a webhook by its ID
Webhook retrieved = await client.Webhooks.RetrieveWebhookAsync("wh_12345678");

List Webhooks

// List all webhooks
IEnumerable<Webhook> webhooks = await client.Webhooks.ListWebhooksAsync();

Update a Webhook

// Update an existing webhook's URL
Webhook webhook = await client.Webhooks.RetrieveWebhookAsync("wh_12345678");
webhook.Url = "https://www.example.com/updated";
Webhook updated = await client.Webhooks.UpdateWebhookAsync(webhook);

Enable a Webhook

// Enable a webhook by its ID
Webhook enabled = await client.Webhooks.EnableWebhookAsync("wh_12345678");

Disable a Webhook

// Disable a webhook by its ID
Webhook disabled = await client.Webhooks.DisableWebhookAsync("wh_12345678");

For full Webhook API reference, please see: Webhook Resource


Refunds

  • Create a Refund
  • Retrieve a Refund
  • List all Refunds

Create a Refund

// Create a new Refund object with the minimal required values
Refund refund = new Refund {
    Amount = 10000,
    PaymentId = "payment_id_12345678",
    Currency = Currency.Php,
    Notes = "Test refund"
};

// Use the PaymongoClient from earlier
// This returns the Refund object with server info
Refund refundResult = await client.Refunds.CreateRefundAsync(refund);

Retrieve a Refund

// Use the PaymongoClient from earlier
// Assume the refund id is "refund_id_12345678"
// This returns a Refund object from the server
Refund refundResult = await client.Refunds.RetrieveRefundAsync("refund_id_12345678");

List All Refunds

// Use the PaymongoClient from earlier
// Optionally filter by payment id and set a limit
IEnumerable<Refund> refunds = await client.Refunds.ListAllRefundsAsync(paymentId: "payment_id_12345678", limit: 10);

For full Refunds API reference, please see: Refund Resource


Customers

  • Create a Customer
  • Retrieve a Customer
  • Edit a Customer
  • Delete a Customer
  • Retrieve the Payment Methods of a Customer
  • Delete a Payment Method of a Customer

Create a Customer

// We create a new Customer object
// This one includes the minimal required values
Customer customer = new Customer() {
    FirstName = "First Name",
    LastName = "Last Name",
    Email = "testcustomermail@mail.com",
    Phone = "+639234735258",
    DefaultDevice = Device.Email
};

// We use the PaymongoClient from earlier
// This returns a Customer object from the server with an Id as confirmation
Customer customerResult = await client.Customers.CreateCustomerAsync(customer);

Retrieve a Customer

// We use the PaymongoClient from earlier
// Lets assume that the customer email is "customer@mail.com"
// Lets assume that the customer phone number is "+639876543210"
// This returns a Customer object from the server
Customer customerResult = await client.Customers.RetrieveCustomerAsync("customer@mail.com", "+639876543210");

Edit a Customer

// First lets get the customer we want to edit
Customer customerResult = await client.Customers.RetrieveCustomerAsync("customer@mail.com", "+639876543210");

// Lets edit some of the customer information
customerResult.FirstName = "New First Name";
customerResult.LastName = "New Last Name";

// After this update executes, this returns the Customer object with the updated information
var editCustomerResult = await client.Customers.EditCustomerAsync(customerResult);

Delete a Customer

// We use the PaymongoClient from earlier
// Lets assume that the customer id is "12345678"
// This returns true if the customer is deleted successfuly
bool deletedCustomerResult = await client.Customers.DeleteCustomerAsync("12345678");

For full Customers API reference, please see: Customer Resource


Sources (Gcash and GrabPay Checkout)

  • Create a Source
  • Retrieve a Source

Create a Source

// We create a new Source object
// This one includes the minimal required values
Source source = new Source {
    Amount = 100000,
    Billing = new Billing {
        Name = "TestName",
        Email = "test@paymongo.com",
        Phone = "9734534443",
        Address = new Address {
            Line1 = "TestAddress1",
            Line2 = "TestAddress2",
            PostalCode = "4506",
            State = "TestState",
            City = "TestCity",
            Country = "PH"
        }
    },
    Redirect = new Redirect {
        Success = "http://127.0.0.1",
        Failed = "http://127.0.0.1"
    },
    Type = SourceType.GCash,
    Currency = Currency.Php
};

// We use the PaymongoClient from earlier
// This returns a Source object from the server
// containing the redirect object(with checkout url) and other info
Source sourceResult = await client.Sources.CreateSourceAsync(source);

Retrieve a Source

// We use the PaymongoClient from earlier
// Lets assume that the Source id is "12345678"
// This returns a Source object from the server
Link sourceResult = await client.Sources.RetrieveSourceAsync("12345678");

For full Sources API reference, please see: The Sources Object


Installments

  • List Installment Plans

List Card Installment Plans

// We use the PaymongoClient from earlier
// Let's assume the amount is 100000 (in centavos)
// This returns a list of available InstallmentPlan objects for the given amount
IEnumerable<InstallmentPlan> plans = await client.CardInstallments.ListInstallmentPlansAsync(100000);

For full Installments API reference, please see: List Installment Plans


QR PH

  • Create a Static QR PH Code

Create a Static QR PH Code

// We create a new QrPhCode object
// This one includes the minimal required values
QrPhCode qrPh = new QrPhCode() {
  MobileNumber = "+639123456789",
  Kind = QrCodeKind.Instore
};

// We use the PaymongoClient from earlier
// This returns a Source object from the server
// containing the redirect object(with checkout url) and other info
QrPhCode qrPhResult = await client.QrPh.CreateStaticQrPhCodeAsync(qrPh);

For full QR PH API reference, please see: Create a Static QR PH Code


Treasury

Treasury has a couple of sub sections

Wallet
  • Retrieve Wallet by ID
  • Retrieve Wallet Accounts

For full Transaction History API reference, please see: Wallet Account Resource

Send Money
  • Create a Wallet Transaction
  • Retrieve List of all Receiving Institutions

For full Send Money API reference, please see: Wallet Transaction Resource

Disbursemenet
  • Create a Batch Transaction

For full Transaction History API reference, please see: Batch Transaction Resource

Transaction History
  • Retrieve a Wallet Transaction By ID
  • Retrieve List of Wallet Transactions
  • Retrieve List of Batches
  • Retrieve Batch Object

For full Transaction History API reference, please see: Retrieve a Wallet Transaction By ID


Future Plans

Fluent Builder

Something that I have in mind that might be easier to work with, for context let's compare the current api usage and the future fluent builder implementation for the Checkouts client.

1. Current
var client = new PaymongoClient(apiKey: "<api_key>");

Checkout checkout = new Checkout() {
    Description = "Test Checkout",
    LineItems = new [] {
        new LineItem {
            Name = "Item Name",
            Quantity = 1,
            Currency = Currency.Php,
            Amount = 3500
        }
    },
    PaymentMethodTypes = new [] {
        PaymentMethod.GCash,
        PaymentMethod.Card,
        PaymentMethod.Paymaya
    }
};

Checkout checkoutResult = await client.Checkouts.CreateCheckoutAsync(checkout);
2. Future Fluent Builder
var client = new PaymongoClient(apiKey: "<api_key>");

Checkout checkoutResult = await CheckoutBuilder
                                    .WithDescription("Test Checkout")
                                    .WithLineItem(
                                        LineItemBuilder.WithName("Item Name")
                                            .WithQuantity(1)
                                            .WithAmount(3500)
                                            .WithCurrency(Currency.Php)
                                    )
                                    .WithPaymentMethod(PaymentMethod.Gcash)
                                    .WithPaymentMethod(PaymentMethod.Card)
                                    .WithPaymentMethod(PaymentMethod.Paymaya)
                                    .CreateCheckoutAsync();

Other Plans

What do you think should be implemented in future versions of the client? Let your ideas be known and open an issue with a [feature-request] tag and it might make it into future updates. Or, if you tried something that works and is awesome try opening a pull request and if all is good, your contribution can be implemented into the project!


❤️ Donate

This is free and available for everyone to use, but still requires time for development and maintenance. By choosing to donate, you are not only helping develop this project, but you are also helping me dedicate more time for creating more tools that help the community ❤️

🎉 Special Thanks

This project is made easier to develop by Jetbrains! They have provided Licenses to their IDE's to support development of this open-source project.

JetBrains Logo (Main) logo.