Protect PAN Request Code Samples

A protect PAN call is made to take a PAN (payment account number) and receive a token value to represent the PAN. For more information, see Protect PAN.

Protect PAN Request - Using Java

The following code sample in Java (without using any third party libraries), calls the Public API 'Protect PAN' request to request a Token for the given PAN (Payment Account Number)


import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class ProtectPANRequestDemo {
 public static void main(String[] args) throws IOException {
  String url = "https://tst05-epapi-na.gsipartners.com/v1.0" +
                "/stores/ENFUS/payments/pan/protect.xml";
  URL request_url = new URL(url);
  /*
  * Create a new HttpURLConnection connection,
  * A URLConnection with support for HTTP-specific features
  * */
  HttpURLConnection http_conn = (HttpURLConnection) request_url.openConnection();
  HttpURLConnection.setFollowRedirects(true);
  try {
     /*
      * Set http connection settings
      * */
     http_conn.setConnectTimeout(10000);
     http_conn.setReadTimeout(15000);
     http_conn.setRequestMethod("POST");
     http_conn.setDoOutput(true);
     http_conn.setRequestProperty("Content-Type","application/xml");
    // below ApiKey should be replaced with provided client specific key
     http_conn.setRequestProperty("ApiKey","rePlace2tHis3wIth4prOviDed5kEy");
     /*
      * Prepare XML to send in Body
      *  */
     String body = "<ProtectPanRequest xmlns=\"http://api.gsicommerce.com/schema/checkout/1.0\">\n"
            +"<PaymentAccountNumber>4111111111111111</PaymentAccountNumber>\n"
            +"<TenderClass>CreditCard</TenderClass>\n"
            +"</ProtectPanRequest>";
     /*
     * Send POST Request to server
     * */
     OutputStream output = new BufferedOutputStream(http_conn.getOutputStream());
     output.write(body.getBytes());
     output.flush();
     output.close();
     System.out.println(String.valueOf(http_conn.getResponseCode()) + " - "
                + http_conn.getResponseMessage());
     /*
      * Get Response from the server
      * */
     System.out.println(readInputStreamToString(http_conn));
     } finally{
       if(http_conn != null) {
          http_conn.disconnect();
       }
     }
  }
/*
* Method will accept connection as input parameter and process response and returns response
* */
    private static String readInputStreamToString(HttpURLConnection connection) {
      String result = null;
      StringBuffer sb = new StringBuffer();
      InputStream is = null;

      try {
        is = new BufferedInputStream(connection.getInputStream());
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String inputLine = "";
        while ((inputLine = br.readLine()) != null) {
           sb.append(inputLine);
                sb.append('\n');
            }
            result = sb.toString();
        }
        catch (Exception e) {
            is = new BufferedInputStream(connection.getErrorStream());
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String inputLine = "";
            try {
                while ((inputLine = br.readLine()) != null) {
                    sb.append(inputLine);
                    sb.append('\n');
                }
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            result = sb.toString();
        }
        finally {
            if (is != null) {
                try {
                    is.close();
                }
                catch (IOException e) {
                    System.out.print("Error closing InputStream");
                }
            }
        }
        return result;
    }
}

Protect PAN Request - Using PHP

The following code sample in PHP (without using any third party libraries), calls the Public API 'Protect PAN' request to request a Token for the given PAN


<?php
/*  required PHP version > 4.3.0 */
/* End point URL for the Protect PAN api call */
$url= 'https://tst05-epapi-na.gsipartners.com/v1.0'
            .'/stores/ENFUS/payments/pan/protect.xml';

/* XML Payload for the Protect PAN request */
$xmlPayloadStr = '<ProtectPanRequest xmlns="http://api.gsicommerce.com/schema/checkout/1.0">
    <PaymentAccountNumber>4111111111111111</PaymentAccountNumber>
    <TenderClass>CreditCard</TenderClass>
    </ProtectPanRequest>';

/* HTTP context option , preparing the http post request
*  $opts is an associative array of associative arrays of HTTP context options
*/
$opts = array('http' =>
    array(
        'method'  => 'POST',
        // below ApiKey should be replaced with provided client specific key
        'header'  => ['Content-type: application/xml',
                'ApiKey:1replace2this3with4Provided5Key'],
        'content' => $xmlPayloadStr,
        # return body even if HTTP status != 200
        'ignore_errors' => true,
    )
);

/* Creates and returns a stream context
 * Context is a set of options that can modify the behavior of a stream
 * Stream is a resource object which exhibits streamable behavior. */
$context  = stream_context_create($opts);
/* Reads entire file/url into a string,
*  using this we can send the http post method with payload to server
*/
$result = file_get_contents($url, false, $context);
// Display the response Code and Response Body for the request
echo '<h2> Protect PAN Response : '.$http_response_header[0]
    .'</h2><pre>'.htmlspecialchars($result, ENT_QUOTES).'</pre>';
?>

Disclaimer

All sample code is provided by Radial for illustrative purposes only. These examples have not been thoroughly tested under all conditions. Radial, therefore, cannot guarantee or imply reliability, serviceability, or function of these programs. All programs contained herein are provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed. We encourage you to research the various libraries and pick one that suits your choice.