Xero Payment Integration In C#


In this article, we will learn how to implement a Xero payment gateway in NET Web application using C#.

Let’s begin.

Go to https://developer.xero.com/

Click on Get Started button to start implementation. Click on the “free Xero account” link and do the registration. After successfully completed registration, you will get mail for activation. From there you need an active account by clicking the link in Email. (set password)

It redirects you to developer website back, if not, open this link (https://developer.xero.com/myapps)

Click on New App > Public App > Fill all information and click on Create App

To get Consumer Key and Consumer Secret click on the application which you just created.

Now, Install “Xero.API.SDK” SDK in your application

Go to Tools > NuGet Package Manager > Manage NuGet Packages For Solutions.

Click on Browse Tab and search for “Xero.API.SDK”. Click on search NuGet and click to Install button.

After installation of SDK, Set consumer key, consumer secret, Base URL and Call Bask URL in web.config.

You can explore API by following URL, and Code sample also available here.

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()
            var callbackUrl = ConfigurationManager.AppSettings["callbackUrl"];
            var memoryStore = new MemoryTokenStore();
            var requestTokenStore = new MemoryTokenStore();
            var baseApiUrl = ConfigurationManager.AppSettings["baseApiUrl"];
              // Consumer details for Application
            var consumerKey = ConfigurationManager.AppSettings["consumerKey"];
            var consumerSecret = ConfigurationManager.AppSettings["consumerSecret"];
            // 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;

Open the HomeController.cs file and add the below code in it.

private IMvcAuthenticator _authenticator;
private ApiUser _user;
public HomeController()
    _user = XeroApiHelper.User();

    _authenticator = XeroApiHelper.MvcAuthenticator();
public ActionResult Connect()
    var authorizeUrl = _authenticator.GetRequestTokenAuthorizeUrl(_user.Name);
    return Redirect(authorizeUrl);
public ActionResult Authorize(string oauth_token, string oauth_verifier, string org)
    var accessToken = _authenticator.RetrieveAndStoreAccessToken(_user.Name, oauth_token, oauth_verifier, org);
    if (accessToken == null)
        return View("NoAuthorized");

    return View(accessToken);

open the Index.cshtml file and add the code in it.

@Html.ActionLink("Connect to xero", "Connect", "Home")

Create the Authorize.cshtml and add the code in it.

<h3>Token Key: @Model.TokenKey</h3>
<h3>Token Secret: @Model.TokenSecret</h3>
<h3>Expires At: @Model.ExpiresAt</h3>

1 Comment

  1. Devin

    What’s up to every body, it’s my first go to see of this weblog; this blog includes awesome and genuinely good stuff designed for visitors.|


Submit a Comment

Your email address will not be published. Required fields are marked *


Select Categories