Plaid Payment Integration in ASP.NET MVC

Introduction

In this article, we will learn how to implement a Plaid payment Integration in ASP.NET Web application.

Let’s begin.

Go to https://plaid.com/

Click on “Get API Keys” and to make registration. After successfully completed Registration user will get mail for activation. From there user has to an active account by clicking the link in Email.

Click on Confirm button then the below page appears.

Click on Account > Keys

You will get the ClientID, Secrets, and public_key

Set ClientID, Secrets, and public_key in web.config.

C# Code Example

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

private const string PublicTokenURL = "https://sandbox.plaid.com/sandbox/public_token/create";
private const string AccessTokenURL = "https://sandbox.plaid.com/item/public_token/exchange";
public static string clientId = ConfigurationManager.AppSettings["ClientId"];
public static string clientSecret = ConfigurationManager.AppSettings["ClientSecret"];
public static string PublicKey = ConfigurationManager.AppSettings["PublicKey"];


public async System.Threading.Tasks.Task<ActionResult> Index()
{
    try
    {
        string result = string.Empty;
        AccessTokenRequest data = new AccessTokenRequest();
        publictokenRequest generatepublictoken = new publictokenRequest();

        generatepublictoken.public_key = PublicKey;
        generatepublictoken.institution_id = "ins_3";
        generatepublictoken.initial_products = new string[] { "auth" };
        string accessdata = JsonConvert.SerializeObject(generatepublictoken);
        var httpWebRequest = (HttpWebRequest)WebRequest.Create(PublicTokenURL);
        httpWebRequest.Method = "POST";
        httpWebRequest.ContentType = "application/json";
        if (httpWebRequest.Method == "POST")
        {
            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                streamWriter.Write(accessdata);
                streamWriter.Flush();
                streamWriter.Close();
            }
        }
        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        if (httpResponse.StatusCode == HttpStatusCode.OK)
        {
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                result = streamReader.ReadToEnd();
            }
        }

        var GetRateModel = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponcePublictoken>(result);

        data.client_id = clientId;
        data.secret = clientSecret;
        data.public_token = GetRateModel.public_token;
        string BillingData = JsonConvert.SerializeObject(data);
        var httpWebRequestAccessToken = (HttpWebRequest)WebRequest.Create(AccessTokenURL);
        httpWebRequestAccessToken.Method = "POST";
        httpWebRequestAccessToken.ContentType = "application/json";
        if (httpWebRequestAccessToken.Method == "POST")
        {
            using (var streamWriter = new StreamWriter(httpWebRequestAccessToken.GetRequestStream()))
            {
                streamWriter.Write(BillingData);
                streamWriter.Flush();
                streamWriter.Close();
            }
        }
       var httpResponse1 = (HttpWebResponse)httpWebRequestAccessToken.GetResponse();
        if (httpResponse1.StatusCode == HttpStatusCode.OK)
        {
            using (var streamReader = new StreamReader(httpResponse1.GetResponseStream()))
            {
                result = streamReader.ReadToEnd();
            }
        }
        var getAccessToken = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponceAccessToken>(result);
        ViewBag.Token = getAccessToken.access_token;
    }
    catch (WebException webex)
    {
        WebResponse errResp = webex.Response;
        using (Stream respStream = errResp.GetResponseStream())
        {
            StreamReader reader = new StreamReader(respStream);
           var result = reader.ReadToEnd();
        }
    }
   
    return View();
}

Open the Models folder and make a class and add the code in it.

public class AccessTokenRequest
{
    public string client_id { get; set; }
    public string secret { get; set; }
    public string public_token { get; set; }
}

public class publictokenRequest
{
    public string public_key { get; set; }
    public string institution_id { get; set; }
    public string[] initial_products { get; set; }
}

public class ResponcePublictoken
{
    public string public_token { get; set; }
    public string request_id { get; set; }
}

public class ResponceAccessToken
{
    public string access_token { get; set; }
    public string item_id { get; set; }
    public string request_id { get; set; }
}

That’s it, you are ready to use the Plaid Payment Integration.

Submit a Comment

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

Subscribe

Select Categories