dotnet web sql business etc

16 березня, 2011

IBAN validation: C# implementation

IBAN validation checksum (a basic ISO 7064 mod 97-10 calculation where the remainder must equal 1).

Let's recall the steps of IBAN validation:

1.Move the four initial characters to the end of the string
2.Replace the letters in the string with digits, expanding the string as necessary, such that A=10, B=11 and Z=35.
3.Convert the string to an integer and mod-97 the entire number

And below is implementation of IBAN validation written in C#:
   public static bool ValidateIBAN(string ibanValue)
        {
            if (System.Text.RegularExpressions.Regex.IsMatch(ibanValue, "^[A-Z0-9]"))
            {
                ibanValue = ibanValue.Replace(" ", String.Empty);
                string iban =

                ibanValue.Substring(4, ibanValue.Length - 4) + ibanValue.Substring(0, 4);
                int asciiShift = 55;

                StringBuilder sb = new StringBuilder();
                foreach (char c in iban)
                {
                    int v;
                    if (Char.IsLetter(c))
                    {
                        v = c - asciiShift;
                    }
                    else
                    {
                        v = int.Parse(c.ToString());
                    }
                    sb.Append(v);
                }

                string checkSumString = sb.ToString();
                int checksum = int.Parse(checkSumString.Substring(0, 1));
                for (int i = 1; i < checkSumString.Length; i++)
                {
                    int v = int.Parse(checkSumString.Substring(i, 1));
                    checksum *= 10;
                    checksum += v;
                    checksum %= 97;
                }
                return checksum == 1;
            }
            else
            {
                return false;
            }

            }

Мітки: , ,

15 березня, 2011

Integration with online payment systems: howto

This is a pretty generic task for a team working on payment module for a big website. So here are some tips on how to approach this task in case you come across one.

1. Make sure you have been supplied with test data (links for staging environment, test login/password, test accounts);

2. Investigate the specifications, note: you absolutely have to clear up all possible question addressing technical support of the payment provider as early as possible. Here are examples of questions you may address: supported currencies, requirements towards all of the parameters submitted in the request, security mechanisms (for example MD5 signature when receiving back notification call), etc;

3. Create an integration prototype implementing the basic flow: submitting data to service, receiving the response/notification, make sure that you have made at least one successful transaction with your prototype, as well as checked reject/error flows (for that you need test data, mentioned in point 1)

4.  Implement integration within your system, test all possible success/reject/error flows. Make sure you take into account connection timeouts as well when for some reason provider site may become unavailable.

5. Before deploying integration functionality to production environment, get production data (links to live environment, live login/password, live accounts) and inform the provider support about the date of your deploy.

6. When going live with new provider integration, it is important to also make some live transactions using real card/wallet/voucher/bank account. Ask you business department to make such transactions, and communicate with them closely regarding the tests outcome.

And finally, enable new payment option for your users, sit back and enjoy the soaring numbers of transactions coming through. Good luck!

Мітки:

IBAN (International Bank Account Number): definition and validation

IBAN is unique identifier of a customer bank account within the international bank systems.

Validation techniques:
1) has length of no more than 34 symbols
2) includes only big letters (A-Z), and digits (0-9), no slashes, spaces, hyphens, etc.
3) first two characters are country ISO code  mo(DE for Germany, FR for France, etc.)
4) satisfies a special validation algorithm

IBAN validation algorithm:
1) First 4 characters of the IBAN are transferred to the end of the string
2) All characters are transaformed to numeric representation as follows: digits are converted to corresponding numbers ("3" -> 3), A to 10, B to 11, .. and Z to 35.
3) The created number is divided by mod 97. If the remained 1 is obtained from the division then the IBAN is valid.

Мітки: ,

SQL 2008: send a .csv file using sp_send_db_mail

In SQL server 2008 you can send results of a query stored in a .csv formatted file attachment. To do that, you have to play around with sp_send_dbmail stored procedure parameters. Here is approximate example of what such script would look like:
exec sp_send_dbmail 
      @recipients = 'email1@gmail.com;email2@gmail.com'
    , @query = 'SELECT TOP 100 * FROM [torders] ORDER BY [date] DESC'
    , @execute_query_database = 'my_database'
    , @attach_query_result_as_file = 1
    , @query_attachment_filename = 'Last10orders.csv'
    , @query_result_separator = ','
    , @query_result_no_padding = 1


The resulting file will arrive, but the values will be crammed into one cell. To obtain a trule comma-separated data: save the file on to your computer, open Microsoft Excel application, open the file. In the text import wizard window, set 1 select file type delimited, in the step 2 select delimiter: comma (diselect tab), save the resulting file as .csv type.

The problem preventing from obtaining a well-formatted .csv file in the beginning is the fact that SQL saves files in Unicode format, and there is not parameter for sp_send_dbmail to indicate this explicitly (like for SQL 2000 xp_sendmail, parameter @ansi_attachment=true), so a small workaround in needed.

Мітки: ,