PayPoint Integration with 3D Secure
Right, I am going to write a quick blog about PayPoint payment gateway, since I know for a fact there isn't too much information on the net concerning this API. I am not going to go into full details on how integrate the gateway, just my major stumbling block.
When using 3D Secure (you know that form that pops up from the bank when you buy stuff online) you call a function called threeDSecureEnrolmentRequest([args]). You give that function a bunch of parameters, such as your username, password andsome details about the order, such as total and the card details etc etc. This goes off to PayPoint and they give you back a token and some other variables that you use in a hidden field of a form, which you then post to them. I am not going into the background of this form, it's stupid. We want to worry about this token that we post to PayPoint. The function returns basically a URL with a query string tacked onto the end. So it would look something like this:
http://domain.com?MD=VALUE&TermUrl=VALUE&acs=VALUE&PaReq=THIS_IS_A_STRING_OF_CHARACTERS_WHICH_COULD_INCLUDE_AN_=_SIGNThe fact that they return a URL is a problem in the first place. The PaReq value (which we need to post back to PayPoint) is a jumble of characters, and not necessarily just alphanumeric characters. So you can't use the parse_url() function available in PHP, since this helpfully encodes some characters for you, rendering the string useless to post back to PayPoint. So, Matt and I together decided to manually break the string up using the following code:
$paypoint_response = threeDSecureEnrolmentRequest([crap load of
parameters here]);
// Get the query string from the end of the URL
list($url, $query_string) = explode('?', $paypoint_response);
$parts = explode('&', $query_string);
// Some where to store the query string values
$data = array();
// Loop over each bit and place keys and value into an array
foreach($parts as $part)
{
// Explode at the equals sign, to get key and value
list($key, $value) = explode('=', $part)
$data[$key] = $value;
}
The code above all looked well and good. But occasionally the token would fail and PayPoint send a response back saying they didn't recognise the token (PaReq). This had baffled me for a while. It turns out that PayPoint sometimes put an equals sign at the end of the token, meaning the code above will remove this. Since I explode on "=". So, we had to come up with another way, and between us, Phil, Matt and myself, the code below was born, it was mostly Matt and Phil.
foreach ($parts as $p)
{
$temp = explode('=', $p);
$key = array_shift($temp);
$value = implode('=', $temp);
$data[$key] = $value;
}
