Tuesday, 24 November 2009

HSBC Payment Intergration using .Net

The HSBC payment gateway is a pain in the arse to deal with the error messages are very poor, and their support line is next to useless.

The other massive ommision is the lack of any kind of .net intergration support. Java, C, and COM are supported, but no .net or PHP code.

Without being to daunted we dived in and managed to get everything working from our ASP.Net app using the COM dll.

Weeks passed, and the web site was finished and we pushed everything onto the new web server. At this point it all stopped working.

After much scratching of heads, and shouting, we figured out the 32 bit COM dll can not be called from the 64 bit ASP.Net app. Arrrrhhh.

After looking at a number of alternatives (registering using WOW, creating a service for it etc), we eventually managed to make it work from .net.

The solutions not ideal, but it gives you a working 100% managed solution.



Step 1. Use the following Java code to get build the key

import java.io.Console;
import java.lang.*;
import java.util.*;
import com.clearcommerce.CpiTools.security.HashGenerator;
import com.clearcommerce.CpiTools.security.SecCrypto;
import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Vector;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class Extract {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
String encryptedKey = "";
if (args.length == 1)
encryptedKey = args[0];

HexBinaryAdapter hb = new HexBinaryAdapter();
SecCrypto sc = new SecCrypto();

byte abyte0[] = sc.decryptToBinary(encryptedKey);
System.out.println("New Secret Base64 Encoded : " + new String(Base64Coder.encode(abyte0)));
System.out.println("New Secret Hex Encoded : " + hb.marshal(abyte0));
return;
}
catch(Exception ex)
{
System.out.println("Error:" + ex.getMessage());
}
}
}


Step 2. add the following code to your ASP.Ner app.

using System;
using System.Collections.Generic;
using System.Text;

namespace HsbcIntergration
{
internal static class CpiHashing
{

private static readonly byte[] _secret = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

public static string ComputeHash(List inputList)
{
return ComputeHash(inputList, _secret);
}

public static string ComputeHash(List inputList, byte[] secretData)
{
List orderedDataToHash = new List(inputList);
orderedDataToHash.Sort(StringComparer.Ordinal);

StringBuilder sb = new StringBuilder();
foreach (string s in orderedDataToHash)
sb.Append(s);

List dataToHash = new List();
dataToHash.AddRange(Encoding.ASCII.GetBytes(sb.ToString()));
dataToHash.AddRange(secretData);

System.Security.Cryptography.HMAC sha = System.Security.Cryptography.HMACSHA1.Create();
sha.Key = secretData;
return Convert.ToBase64String(sha.ComputeHash(dataToHash.ToArray(), 0, dataToHash.Count));
}
}
}

No comments:

Post a Comment