Showing posts with label xmlapi. Show all posts
Showing posts with label xmlapi. Show all posts

Monday, November 18, 2013

Token or crn xml payment using NAB

Download for payment response code

testUrl = "https://transact.nab.com.au/test/xmlapi/payment";
liveUrl = "https://transact.nab.com.au/xmlapi/payment";

NAB Transact XML API  Public Test Account Details
Merchant ID: XYZ0010
Transaction Password: abcd1234 


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xmlparser;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author Pritom K Mondal
 */
public class NabTokenPayment {
    public static void main(String[] args) throws Exception {
        NabTokenPayment nabTokenPayment = new NabTokenPayment();
        nabTokenPayment.pay();
    }
    
    private void pay() throws Exception {
        String testURL = "https://transact.nab.com.au/xmlapidemo/periodic";
        String liveURL = "https://transact.nab.com.au/xmlapi/periodic";
        String xml = getTokenPaymentXml();
        URL url = new URL(testURL); 
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();           
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setInstanceFollowRedirects(false); 
        connection.setRequestMethod("POST"); 
        connection.setRequestProperty("Content-Type", "text/xml"); 
        connection.setRequestProperty("charset", "utf-8");
        connection.setRequestProperty("Content-Length", "" + Integer.toString(xml.getBytes().length));
        connection.setUseCaches (false);

        DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());
        wr.writeBytes(xml);
        wr.flush();
        wr.close();
        
        System.out.println("Response code from nab: " + connection.getResponseCode());

        BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line, responseText = "";
        while ((line = br.readLine()) != null) {
            responseText += line;
        }
        br.close();
        
        System.out.println("Response: \n" + responseText);
    }
    
    private String getTokenPaymentXml() throws Exception {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssZ");
        String messageTimestamp = dateFormat.format(new Date());
        String messageID = MD5(messageTimestamp);
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
            "<NABTransactMessage>\n" +
                "<MessageInfo>\n" +
                    "<messageID>" + messageID + "</messageID>\n" +
                    "<messageTimestamp>" + messageTimestamp + "</messageTimestamp>\n" +
                    "<timeoutValue>60</timeoutValue>\n" +
                    "<apiVersion>spxml-4.2</apiVersion>\n" +
                "</MessageInfo>\n" +
                "<MerchantInfo>\n" +
                    "<merchantID>XYZ0010</merchantID>\n" +
                    "<password>abcd1234</password>\n" +
                "</MerchantInfo>\n" +
                "<RequestType>Periodic</RequestType>\n" +
                "<Periodic>\n" +
                    "<PeriodicList count=\"1\">\n" +
                        "<PeriodicItem ID=\"1\">\n" +
                            "<actionType>trigger</actionType>\n" +
                            "<periodicType>8</periodicType>\n" +
                            "<crn>47e8cb6ddc6ba08890d4</crn>\n" +
                            "<transactionReference>Test Trigger CC Payment</transactionReference>\n" +
                            "<amount>1200</amount>\n" +
                            "<currency>AUD</currency>\n" +
                        "</PeriodicItem>\n" +
                    "</PeriodicList>\n" +
                "</Periodic>\n" +
            "</NABTransactMessage>";
        return xml;
    }
    
    private String convertedToHex(byte[] data) {
        StringBuffer buf = new StringBuffer();       
        for (int i = 0; i < data.length; i++) {
            int halfOfByte = (data[i] >>> 4) & 0x0F;
            int twoHalfBytes = 0;
            do {
                if ((0 <= halfOfByte) && (halfOfByte <= 9)) {
                    buf.append( (char) ('0' + halfOfByte) );
                } else {
                    buf.append( (char) ('a' + (halfOfByte - 10)) );
                }
                halfOfByte = data[i] & 0x0F;
            } while(twoHalfBytes++ < 1);
        }
        return buf.toString();
    }

    public String MD5(String text) 
            throws NoSuchAlgorithmException, UnsupportedEncodingException 
    {
        MessageDigest md;
        md = MessageDigest.getInstance("MD5");
        byte[] md5 = new byte[64];
        md.update(text.getBytes("iso-8859-1"), 0, text.length());
        md5 = md.digest();
        return convertedToHex(md5);
    }
}

Successful response:


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<NABTransactMessage>
    <MessageInfo>
        <messageID>af7296fcab496a96cf54aef73ff66960</messageID>
        <messageTimestamp>20131911003610525000+660</messageTimestamp>
        <apiVersion>spxml-4.2</apiVersion>
    </MessageInfo>
    <MerchantInfo>
        <merchantID>XYZ0010</merchantID>
    </MerchantInfo>
    <Status>
        <statusCode>0</statusCode>
        <statusDescription>Normal</statusDescription>
    </Status>
    <RequestType>Periodic</RequestType>
    <Periodic>
        <PeriodicList count="1">
            <PeriodicItem ID="1">
                <actionType>trigger</actionType>
                <crn>47e8cb6ddc6ba08890d4</crn>
                <responseCode>00</responseCode>
                <responseText>Approved</responseText>
                <successful>yes</successful>
                <periodicType>8</periodicType>
                <amount>1200</amount>
                <currency>AUD</currency>
                <txnID>983211</txnID>
                <transactionReference>Test Trigger CC Payment</transactionReference>
                <settlementDate>20131119</settlementDate>
                <CreditCardInfo>
                    <pan>444433...111</pan>
                    <expiryDate>09/15</expiryDate>
                    <cardType>6</cardType>
                    <cardDescription>Visa</cardDescription>
                </CreditCardInfo>
            </PeriodicItem>
        </PeriodicList>
    </Periodic>
</NABTransactMessage>

Error response:


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<NABTransactMessage>
    <MessageInfo>
        <messageID>f8c4385e4beb4a7c177e8b8aaa124e15</messageID>
        <messageTimestamp>20131911004348303000+660</messageTimestamp>
        <apiVersion>spxml-4.2</apiVersion>
    </MessageInfo>
    <MerchantInfo>
        <merchantID>XYZ0010</merchantID>
    </MerchantInfo>
    <Status>
        <statusCode>0</statusCode>
        <statusDescription>Normal</statusDescription>
    </Status>
    <RequestType>Periodic</RequestType>
    <Periodic>
        <PeriodicList count="1">
            <PeriodicItem ID="1">
                <actionType>trigger</actionType>
                <crn>47e8cb6ddc6ba08890d4</crn>
                <responseCode>04</responseCode>
                <responseText>Pick Up Card</responseText>
                <successful>no</successful>
                <periodicType>8</periodicType>
                <amount>304</amount>
                <currency>AUD</currency>
                <txnID>983222</txnID>
                <transactionReference>Test Trigger CC Payment</transactionReference>
                <settlementDate>20131119</settlementDate>
                <CreditCardInfo>
                    <pan>444433...111</pan>
                    <expiryDate>09/15</expiryDate>
                    <cardType>6</cardType>
                    <cardDescription>Visa</cardDescription>
                </CreditCardInfo>
            </PeriodicItem>
        </PeriodicList>
    </Periodic>
</NABTransactMessage>

Wednesday, July 3, 2013

NAB Xmlapi payment using php

Download the pdf document for transaction codes and other information

testUrl = "https://transact.nab.com.au/test/xmlapi/payment";
liveUrl = "https://transact.nab.com.au/xmlapi/payment";

NAB Transact XML API – Public Test Account Details
Merchant ID: XYZ0010
Transaction Password: abcd1234

Xml sent to NAB for processing

$nabXml = "<NABTransactMessage>
    <MerchantInfo>
        <merchantID>XYZ0010</merchantID>
        <password>abcd1234</password>
    </MerchantInfo>
    <RequestType>Payment</RequestType>
    <Payment>
        <TxnList count='1'>
            <Txn ID='1'>
                <txnType>0</txnType>
                <txnSource>23</txnSource>
                <amount>5000</amount>
                <currency>AUD</currency>
                <purchaseOrderNo>100000</purchaseOrderNo>
                <CreditCardInfo>
                    <cardNumber>4444333322221111</cardNumber>
                    <cvv>123</cvv>
                    <expiryDate>01/15</expiryDate>
                </CreditCardInfo>
            </Txn>
        </TxnList>
    </Payment>
</NABTransactMessage>";

Php code to execute payment using xml api

$result = makeCurlCall(
    $testNab, /* CURL URL */
    "POST", /* CURL CALL METHOD */
    array( /* CURL HEADERS */
        "Content-Type: text/xml; charset=utf-8",
        "Accept: text/xml",
        "Pragma: no-cache",
        "Content_length: ".strlen(trim($nabXml))
    ),
    null, /* CURL GET PARAMETERS */
    $nabXml /* CURL POST PARAMETERS AS XML */
);

makeCurlCall function

function makeCurlCall($url, $method = "GET", $headers = null, $gets = null, $posts = null) {
    $ch = curl_init();
    if($gets != null)
    {
        $url.="?".(http_build_query($gets));
    }
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    if($posts != null)
    {
        curl_setopt($ch, CURLOPT_POSTFIELDS, $posts);
    }
    if($method == "POST") {
        curl_setopt($ch, CURLOPT_POST, true);
    } else if($method == "PUT") {
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    } else if($method == "HEAD") {
        curl_setopt($ch, CURLOPT_NOBODY, true);
    }
    if($headers != null && is_array($headers))
    {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    }
    $response = curl_exec($ch);
    $code = curl_getinfo($ch,CURLINFO_HTTP_CODE);

    curl_close($ch);
    return array(
        "code" => $code,
        "response" => $response
    );
}

And the response from NAB is like this, parse the xml received from nab


<? xml version = "1.0" encoding = "UTF-8" standalone = "no" ?>
<NABTransactMessage>
    <MessageInfo>
        <messageID/>
        <messageTimestamp>20130307120339466000+600</messageTimestamp>
        <apiVersion/>
    </MessageInfo>
    <RequestType>Payment</RequestType>
    <MerchantInfo>
        <merchantID>XYZ0010</merchantID>
    </MerchantInfo>
    <Status>
        <statusCode>000</statusCode>
        <statusDescription>Normal</statusDescription>
    </Status>
    <Payment>
        <TxnList count="1">
            <Txn ID="1">
                <txnType>0</txnType>
                <txnSource>23</txnSource>
                <amount>2500</amount>
                <currency>AUD</currency>
                <purchaseOrderNo>8547963513-1372817009</purchaseOrderNo>
                <approved>Yes</approved>
                <responseCode>00</responseCode>
                <responseText>Approved</responseText>
                <settlementDate>20130703</settlementDate>
                <txnID>503578</txnID>
                <authID/>
                <CreditCardInfo>
                    <pan>444433...111</pan>
                    <expiryDate>01/15</expiryDate>
                    <cardType>6</cardType>
                    <cardDescription>Visa</cardDescription>
                </CreditCardInfo>
            </Txn>
        </TxnList>
    </Payment>
</NABTransactMessage>
https://docs.google.com/file/d/0B5nZNPW48dpFNmVpdi1PRHkyd2c/edit?usp=sharing