Skip to content

moduit-engineering/xendit-dotnet

Repository files navigation

Xendit .NET Library

NuGet MIT licensed Release Build

This library is the abstraction of Xendit API for access from applications written with C# .NET.

Please note that this library is not official from Xendit. You can refer to Xendit Github for the complete list of their official API client libraries.

Table of Contents

  1. Installation
  2. API Documentation
  3. Requirements
  4. Usage
  5. Disbursement Services
  6. Invoice services
  7. Virtual Account Services
  8. Batch Disbursement Services
  9. E-Wallet Services
  10. Exception Handling
  11. Contributing
  12. TODO

Installation

We recommended you to install using our NuGet Package, however you can clone this repo and embed it into your projects.

API Documentation

Please check Xendit API Reference.

Requirements

This library is built on .NET Standard 2.0. You only need to make sure that your code is compatible with .NET Standard 2.0, if not, you can build this project on other .NET version easily.

Usage

You need to use secret API key in order to use functionality in this library. The key can be obtained from your Xendit Dashboard.

using Xendit.ApiClient;    
    
public class Program
{    
    public static void Main()    
    {    
        var xendit = new XenditClient("PUT YOUR API KEY HERE");    
    
        // The rest of the code...    
    }    
}    

For complete configuration, you can use the XenditClient constructor that accepts XenditConfiguration.

Example: Create a specified Fixed Virtual Account (assuming your code is asynchronous, otherwise you can easily call .Result or .GetAwaiter().GetResult() after the async method depending on your case).

using System.Threading.Tasks;    
using Xendit.ApiClient.Constants;    
using Xendit.ApiClient.VirtualAccount;    
    
public class Program
{    
    public static void Main()    
    {    
        MainBusiness().Wait();    
    }    
    
    private async Task MainBusiness()    
    {    
        var xendit = new XenditClient("PUT YOUR API KEY HERE");    
    
        // You can create non-specified fixed VA number by not providing `VirtualAccountNumber` property value.    
        var requestedVA = new XenditVACreateRequest    
        {    
            ExternalId = "VA_fixed-1234567890",    
            Name = "Steve Woznike",    
            BankCode = XenditVABankCode.MANDIRI,    
            VirtualAccountNumber = "9999000002"    
        };
    
        var va = await xendit.VirtualAccount.CreateAsync(requestedVA);    
    }    
}    

Note: in a real project, we recommend to use dependency injection to create XenditClient object as singleton, you can also use IXenditClient interface for your unit tests.

Disbursement Services

Create a disbursement

var requestedDisbursement = new XenditDisbursementCreateRequest    
{    
    ExternalId = "My-Disb-1234567890",    
    Amount = 125000,    
    BankCode = XenditDisbursementBankCode.MANDIRI,    
    AccountHolderName = "Andilau Sinarasa",    
    AccountNumber = "1234567890",    
    Description = "Disbursement description."
};    
    
var disbursement = await xendit.Disbursement.CreateAsync(requestedDisbursement);    

Get banks with available disbursement service

var availableDisbBanks = await xendit.Disbursement.GetAvailableBanksAsync();    

Get a disbursement by external ID

var disbursement = await xendit.Disbursement.GetByExternalIdAsync("EXAMPLE_EXTERNAL_ID");    

Get a disbursement by ID

var disbursement = await xendit.Disbursement.GetByIdAsync("EXAMPLE_ID");    

Back to top

Invoice services

Create an invoice

var requestedInvoice = new XenditInvoiceCreateRequest    
{    
    ExternalId = "My-Invoice-1234567890",    
    Amount = 75000,    
    PayerEmail = "customer@example.com",    
    Description = "Invoice for a random gadget"
};    
    
var invoice = await xendit.Invoice.CreateAsync(requestedInvoice);    

You can bind the invoice to a Virtual Account by providing Virtual Account Id to property VANumberId of XenditInvoiceCreateRequest above.

Get an invoice by ID

var invoice = await xendit.Invoice.GetAsync("EXAMPLE_ID");    

Get all invoices

var options = new XenditInvoiceOptions    
{    
    Limit = 3,    
    Statuses = new List<XenditInvoiceStatus> { XenditInvoiceStatus.SETTLED, XenditInvoiceStatus.EXPIRED }    
};    
    
var invoices = await xendit.Invoice.GetAllAsync(options);    

Expire an invoice

var invoice = await xendit.Invoice.ExpireAsync("EXAMPLE_ID");    

Back to top

Virtual Account Services

Create a fixed virtual account

Closed virtual account
    
new XenditVACreateRequest    
{    
    ExternalId = "my-VA-fixed-closed-1234568",    
    BankCode = XenditVABankCode.BNI,    
    Name = "VA Test",    
    IsClosedVA = true,    
    ExpectedAmount = 1350000    
};    
Opened virtual account
    
new XenditVACreateRequest    
{    
    ExternalId = "my-VA-fixed-closed-1234568",    
    BankCode = XenditVABankCode.BNI,    
    Name = "VA Test",    
};    
// Just unassign `IsClosedVA` and `ExpectedAmount` to create Opened VA.    
 var requestedVA = new XenditVACreateRequest    
{    
    ExternalId = "my-VA-fixed-closed-1234568",    
    BankCode = XenditVABankCode.BNI,    
    Name = "VA Test",    
    IsClosedVA = true,    
    ExpectedAmount = 1350000
};    
    
var va = await xendit.VirtualAccount.CreateAsync(requestedVA);    

Get banks with available virtual account service

var availableVABanks = await xendit.VirtualAccount.GetAvailableBanksAsync();    

Get a fixed virtual account by ID

var va = await xendit.VirtualAccount.GetAsync("EXAMPLE_ID");    

Expire a fixed virtual account

var expiredVA = await xendit.VirtualAccount.ExpireAsync("EXAMPLE_ID");    

Back to top

Batch Disbursement Services

Batch disbursement item

var item = new XenditBatchDisbursementCreateRequestItem    
{    
    ExternalId = "My Disb item Id 1",    
    Amount = 10000,    
    BankCode = XenditDisbursementBankCode.BRI_SYR,    
    AccountHolderName = "Buye Loku",    
    AccountNumber = "1234567890",    
    Description = "Description for the item disbursement",    
    EmailTo = new string[] { "buye.loku@example.com", "email2@example.com" }    
};    

Create a batch disbursement

var batchDisbursement = await xendit.Disbursement.CreateBatchAsync(new XenditBatchDisbursementCreateRequest    
{    
    Reference = "My reference Id",    
    Disbursements = new List<XenditBatchDisbursementCreateRequestItem> { /* put the items here */ }    
});    

Get banks with available disbursement service

var availableDisbBanks = await xendit.Disbursement.GetAvailableBanksAsync();    

Back to top

E-Wallet Services

Create a LinkAja payment

var linkAjaPaymentItems = new List<XenditEWalletCreateLinkAjaPaymentRequestItem>
{
    new XenditEWalletCreateLinkAjaPaymentRequestItem
    {
        Id = "id_test_karton123",
        Name = "Kertas Karton",
        Price = 15000,
        Quantity = 2
    }
};

var linkAjaPayment = new XenditEWalletCreateLinkAjaPaymentRequest
{
    ExternalId = "linkaja-ewallet-1234",
    Amount = 30000,
    CallbackUrl = "https://example.com/callback_url",
    RedirectUrl = "https://example.com/redirect_url",
    Phone = "089911111111",
    Items = linkAjaPaymentItems
};

var linkAjaPaymentResponse = await xendit.EWallet.CreateLinkAjaPaymentAsync(linkAjaPayment);

Create a Dana payment

var danaPayment = new XenditEWalletCreateDanaPaymentRequest
{
    ExternalId = "dana-ewallet-1234",
    Amount = 25000,
    CallbackUrl = "https://example.com/callback_url",
    RedirectUrl = "https://example.com/redirect_url"
};

var danaPaymentResponse = await xendit.EWallet.CreateDanaPaymentAsync(danaPayment);

Create an OVO payment

var ovoPayment = new XenditEWalletCreateOvoPaymentRequest
{
    ExternalId = "ovo-ewallet-1234",
    Amount = 12500,
    Phone = "088889998888"
};

var ovoPaymentResponse = await xendit.EWallet.CreateOvoPaymentAsync(ovoPayment);

You can use other Xendit API version for OVO using ApiVersion property of XenditEWalletCreateOvoPaymentRequest, by default it uses the latest version. You can access the predefined versions using XenditEWalletOvoVersion class. If you are using Visual Studio or Visual Studio Code, you should be able to see the summary of each version.

Get an e-wallet payment

var payment = await xendit.EWallet.GetPaymentStatusAsync("ovo-ewallet-1234", XenditEWalletType.OVO);

Back to top

Exception Handling

This library throws exception with type XenditHttpResponseException when the API response is not successful (non 2xx HTTP status code).
There are some properties of this serializable exception that you can use to track problems related with API request and response.
In order to get response content to know what Xendit API says about the response, you can just use Content property, you may also want to deserialize it for your need.

Normally these exception's properties are enough to investigate what really happened to the request and response of the corresponding API call, although a bit expensive.

Contributing

TODO

  1. There are some Xendit's API endpoints that haven't been implemented to this library:

    • Balance
    • Credit Card
    • Cardless Transaction
    • Retail Outlets
    • Recurring Payments
    • Payouts
  2. CI/CD

We welcome any contributions to this project. :)