This resource is quite uncommon in Techblogs, the main reason why I have decided to startup my blog with this particular post.
Many a time, we are required to integrate a payment gateway, and sometimes we get to provide a request URL to capture data from Gateway Servers. But when the HTTP REQUEST method is POST and content type is TEXT/XML, and the we are unable to process this with the usual $_POST[”] to get our post values and manipulate.
Before moving ahead, I may need to note that $_POST[”] will only return results for FORM-DATA and X-WWW-FORM-URLENCODED but not for APPLICATION/XML or TEXT/XML.
When we have to return results for TEXT/XML or APPLICATION/XML, then we may need to utilise SIMPLEXML to get the NODE values from the result. In doing this, there are two simple lines of code we need to call.
[php]
$postData = file_get_contents(‘php://input’);
$xml=simplexml_load_string($postData);
[/php]
Please take note of the following assumptions for the script to be relevant:
The communication mode is HTTP POST (not SOAP)
The content type is text/xml.
The POST request will contain only XML
The request will be a RAW POST directly to stream and NOT in a parameter.
Here’s my php script to call results and return a response status of “0”.
[php]
<?php
$postData = file_get_contents(‘php://input’);
$xml = simplexml_load_string($postData);
if(!empty($xml->MerchantReference)){
$merchRef=$xml->Customers[0]->Customer[0]->MerchantReference;
$custRef=$xml->Customers[0]->Customer[0]->CustReference;
$username=$xml->Customers[0]->Customer[0]->ServiceUsername;
$password=$xml->Customers[0]->Customer[0]->ServicePassword;
$db->setQuery(Check if customer exists in database);
if($db->countResultset() == 0)
{
header("Content-type: text/xml");
echo "<?xml version=’1.0′ encoding=’UTF-8′?>";
echo "<CustomerInformationResponse>";
echo "<MerchantReference>".$merchRef."</MerchantReference>";
echo "<Customers>";
echo "<Customer>";
echo "<Status>0</Status>";
echo "<CustReference>".$custRef."</CustReference>";
echo "<FirstName/>";
echo "<LastName/>";
echo "<OtherName/>";
echo "<Email/>";
echo "<Phone/>";
echo "<ThirdPartyCode/>";
echo "<StatusMessage>Customer is valid</StatusMessage>";
echo "</Customer>";
echo "</Customers>";
echo "</CustomerInformationResponse>";
exit;
}
?>
[/php]
A sample of the HTTP POST REQUEST
[sourcecode language=”plain”]
<CustomerInformationRequest xmlns:ns2="http://test.url.com/" xmlns:ns3="http://www.w3.org/2003/05/soap-envelope">
<ServiceUrl>MY URL</ServiceUrl>
<ServiceUsername>12345</ServiceUsername>
<ServicePassword>abcdef</ServicePassword>
<RouteId>HTTPGENERICv31</RouteId>
<Service>bill</Service>
<MerchantReference>123456</MerchantReference>
<CustReference>abcdef</CustReference>
<PaymentItemCategoryCode/>
<RequestReference/>
<TerminalId/>
<Amount>0</Amount>
<FtpUsername/>
<FtpPassword/>
</CustomerInformationRequest>
[/sourcecode]
The expected response if properly implemented after data manipulation.
[sourcecode language=”plain”]
<CustomerInformationResponse>
<MerchantReference>3527</MerchantReference >
<Customers>
<Customer>
<Status>0</Status>
<CustReference>4565</CustReference>
<FirstName></FirstName>
<LastName></LastName>
<OtherName></OtherName>
<Email></Email>
<Phone></Phone>
<ThirdPartyCode/>
<StatusMessage>Customer is Valid</StatusMessage>
</Customer>
</Customers>
</CustomerInformationResponse>
[/sourcecode]
Now, you may want to submit this data into a database or notify the customer via SMS when this transaction is captured.
This will be discussed in detail on my next post on “file_get_contents”.