Home > Uncategorized > #Verify a bank account number in C# using #Stripe

#Verify a bank account number in C# using #Stripe

fitness-bank

Say you collect payout data from your affiliates or partners, and you would like to make sure that the bank details are correct, so that you do not have the embarrassment and cost of a bounced payment.

Here is a trick on how to do this, using Stripe – This is not actually using Stripe to make the payout, but it’s using Stripe Connect to verify the bank account details.

First set up a Stripe account, then create a sub account via Stripe connect. You will need this account id, and your private key. Do not use your main account id, since your bank account details will be overwritten.

Here’s the main code;

public static string Verify(string BankCountry, string BankSortCode, string BankAccountNumber)
{
var wc = new WebClient();
string strResult;
try
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; // comparable to modern browsers
var myCreds = new NetworkCredential(PrivateKey, “”);
wc.Credentials = myCreds;
strUrl = string.Format(strUrl, stripe_user_id);
var strPostdata = “”;
// to-do: Possible-bug: Does this add new bank accounts every time you try to update the account?
if (BankAccountNumber != null && !BankAccountNumber.StartsWith(“*”))
{
strPostdata += “&external_account[object]=bank_account”;
strPostdata += “&external_account[country]=” + BankCountry;
strPostdata += “&external_account[currency]=” + CurrencyFromBankCountry(BankCountry);
strPostdata += “&external_account[routing_number]=” + BankSortCode.Replace(“-“, “”).Replace(” “, “”);
strPostdata += “&external_account[account_number]=” + BankAccountNumber;
}
wc.Headers[“Content-Type”] = “application/x-www-form-urlencoded”;
strResult = wc.UploadString(strUrl, strPostdata);
}
catch (WebException exception)
{
strResult = GetExceptionDetails(exception);
var jError = JObject.Parse(strResult);
thrownew Exception(jError[“error”][“message”].ToString());
}
var userJson = GetAccountDetails();
var jUser = JObject.Parse(userJson);
foreach (var bankAccont in jUser[“bank_accounts”][“data”])
{
var strStripeLast4 = bankAccont[“last4”].ToString();
var strPassedLast4 = BankAccountNumber.Substring(BankAccountNumber.Length – 4);
if (strPassedLast4 == strStripeLast4)
{
return bankAccont.ToString();
}
}
return”Unknown failure”;
}
You’ll also need this to pull back the bank details (i.e. bank name) from Stripe:
private static string GetAccountDetails()
{
/*
-u sk_test_jCTjZ7050Z9zbRyj3me2JFEW:
*/
var wc = new WebClient();
string strResult;
try
{
var myCreds = new NetworkCredential(PrivateKey, “”);
wc.Credentials = myCreds;
strUrl += stripe_user_id;
strResult = wc.DownloadString(strUrl);
}
catch (WebException exception)
{
strResult = GetExceptionDetails(exception);
}
return strResult;
}
And two helper methods;
– GetExceptionDetails helps you get a descriptive error message rather than just a HTTP status code when something goes wrong.
private static string GetExceptionDetails(WebException exception)
{
string strResult = “”;
if (exception.Response != null)
{
var responseStream = exception.Response.GetResponseStream();
if (responseStream != null)
{
using (var reader = new StreamReader(responseStream))
{
strResult = reader.ReadToEnd();
}
}
}
return strResult;
}
And this, – which could be made a little neater using RegionInfo in System.Globalisation, but served for my purposes;
privatestaticstring CurrencyFromBankCountry(string BankCountry)
{
switch (BankCountry)
{
case”US”: return”USD”;
case”IE”: return”EUR”;
case”GB”: return”GBP”;
case”AU”: return”AUD”;
case”CA”: return”CAD”;
case”DK”: return”DKK”;
case”FI”: return”EUR”;
case”FR”: return”EUR”;
case”JP”: return”JPY”;
case”NO”: return”NOK”;
case”SG”: return”SGD”;
case”ES”: return”EUR”;
case”SE”: return”SEK”;
case”AT”: return”EUR”;
case”BE”: return”EUR”;
case”DE”: return”EUR”;
case”HK”: return”HKD”;
case”IT”: return”EUR”;
case”LU”: return”EUR”;
case”NL”: return”EUR”;
case”PT”: return”EUR”;
}
throw new Exception(“Unrecognised bank country”);
}
Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: