Payment Integrations

How To Payment In Xero Using C#

If you are looking for Xero integration please read this article.

First, we have to create a Customer Account here.

We need two customer account.

  1. Merchant account to send payment
  2. Customer account to receive payment.

The following elements are required to create a bank transfer.

FromBankAccount: From Account Code

ToBankAccount:  To Account Code

Amount: Payment Amount

The following elements are optional to create a bank transfer.

Date: The date of Transfer YYYY-MM-DD. Defaults to the current date.

Elements for FromBankAccount and ToBankAccount

Code: The Account Code of the Bank Account. If the Code is not included then AccountID is required.

C# Code Example

Create the Helpers folder and make a class file namely XeroApiHelper.cs and add the code in it.

using System;
using Xero.Api.Core;
using Xero.Api.Example.Applications.Partner;
using Xero.Api.Example.Applications.Public;
using Xero.Api.Infrastructure.Interfaces;
using Xero.Api.Infrastructure.OAuth;
using Xero.Api.Example.TokenStores;
using Xero.Api.Serialization;
using System.Configuration;

namespace XeroApplication.Helpers
{
    public class ApplicationSettings
    {
        public string BaseApiUrl { get; set; }
        public Consumer Consumer { get; set; }
        public object Authenticator { get; set; }
        
    }

    public static class XeroApiHelper
    {
        private static ApplicationSettings _applicationSettings;

        static XeroApiHelper()
        {
            // Refer to README.md for details
            //var callbackUrl = "";
            var callbackUrl = ConfigurationManager.AppSettings["callbackUrl"];
            var memoryStore = new MemoryTokenStore();
            var requestTokenStore = new MemoryTokenStore();
            var baseApiUrl = ConfigurationManager.AppSettings["baseApiUrl"];
            //var baseApiUrl = "";

            // Consumer details for Application
            var consumerKey = ConfigurationManager.AppSettings["consumerKey"];
            var consumerSecret = ConfigurationManager.AppSettings["consumerSecret"];

            // Signing certificate details for Partner Applications
            //var signingCertificatePath = @"C:\Dev\your_public_privatekey.pfx";
            //var signingCertificatePassword = "Your_signing_cert_password - leave empty if you didn't set one when creating the cert";

            // Public Application Settings
            var publicConsumer = new Consumer(consumerKey, consumerSecret);

            var publicAuthenticator = new PublicMvcAuthenticator(baseApiUrl, baseApiUrl, callbackUrl, memoryStore, 
                publicConsumer, requestTokenStore);

            var publicApplicationSettings = new ApplicationSettings
                {
                    BaseApiUrl = baseApiUrl,
                    Consumer = publicConsumer,
                    Authenticator = publicAuthenticator
                };
             _applicationSettings = publicApplicationSettings;
        }

        public static ApiUser User()
        {
            return new ApiUser { Name = Environment.MachineName };    
        }

        public static IConsumer Consumer()
        {
            return _applicationSettings.Consumer;
        }

        public static IMvcAuthenticator MvcAuthenticator()
        {
            return (IMvcAuthenticator)_applicationSettings.Authenticator;
        }

        public static IXeroCoreApi CoreApi()
        {
            if (_applicationSettings.Authenticator is IAuthenticator)
            {
                return new XeroCoreApi(_applicationSettings.BaseApiUrl, _applicationSettings.Authenticator as IAuthenticator,
                    _applicationSettings.Consumer, User(), new DefaultMapper(), new DefaultMapper());
            }
           
            return null;
        }
    }
}

first, create a customer account.

Open the HomeController.cs  file and create a new method and add the below code in it.

var api = XeroApiHelper.CoreApi();
Account CreateAccount = new Account
{
    Code = "123",//uniq number
    Name = "sarana13",//uniq name
    Type = Xero.Api.Core.Model.Types.AccountType.Current,
    BankAccountNumber = "456-121-1234567",
    BankAccountType = Xero.Api.Core.Model.Types.BankAccountType.Bank
};
api.Accounts.Create(CreateAccount);

after creating the sender and receiver account, payment can be done easily.

create a new payment method.

var api = XeroApiHelper.CoreApi();
Account FromAccount = new Account();
FromAccount.Code = "124";//Sender Account code


Account ToAccount = new Account();
ToAccount.Code = "1001";//Receiver Account code

BankTransfer payamt = new BankTransfer();
payamt.FromBankAccount = FromAccount;
payamt.ToBankAccount = ToAccount;
payamt.Amount =Convert.ToDecimal(1000);//Amount
payamt.Date = DateTime.Now;
api.BankTransfers.Create(payamt);

You can explore API by the following URL

 

 

 

Sagar Rana

Sagar Rana is a Web Developer in Vision Infotech. He has strong skills and knowledge of ASP.NET C#, ASP.NET MVC, .Net Core, Jquery, JavaScript, WEB API, React.js, ADO.Net, Entity Framework, SQL and different integration like Xero, Stripe, Zoho CRM, Square, PayTM, PayKUN, RazorPay, Quickbook Desktop etc.

View Comments

  • Hmm it seems like your blog ate my first comment (it was super long) so I guess I'll just sum it up what I had written and say, I'm thoroughly enjoying your blog. I as well am an aspiring blog writer but I'm still new to the whole thing. Do you have any tips and hints for beginner blog writers? I'd genuinely appreciate it.

Recent Posts

Testing hk

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Operation

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

TETS NEW

test

2 years ago