Sample Code - Generating the Payment Request Form Programmatically

To generate the payment request form using Java (JSP under Tomcat) please see the code below:

<!--
Sample JSP code for Payment Pages
Calculating the x_fp_hash with SHA-1
Tested on Tomcat5/Mac OS X 10.5
-->
<html>
<head>
 <title>Sample JSP Payment Form with SHA-1</title>
 <style type="text/css">
   label {
      display: block;
      margin: 5px 0px;
      color: #AAA;
   }
   input {
      display: block;
   }
   input[type=submit] {
      margin-top: 20px;
   }

 </style>

</head>
<body>

<%@ page import="java.util.Random" %>
<%@ page import="javax.crypto.Mac" %>
<%@ page import="javax.crypto.SecretKey" %>
<%@ page import="javax.crypto.spec.SecretKeySpec" %>

<%
// x_login and transactionKey should be taken from Payment Page settings
String x_login        = "###-###-##-##"; // aka Payment Page ID
String transactionKey = "#############"; // aka Transaction Key
String x_amount       = "19.99";

// Generate a random sequence number
Random generator = new Random();
int x_fp_sequence = generator.nextInt(1000);

// Generate the timestamp
// Make sure this will be in UTC
long x_fp_timestamp = System.currentTimeMillis()/1000;

// Use Java Cryptography functions to generate the x_fp_hash value
// generate secret key for HMAC-SHA1 using the transaction key
SecretKey key = new SecretKeySpec(transactionKey.getBytes(), "HmacSHA1");

// Get instance of Mac object implementing HMAC-SHA1, and
// Initialize it with the above secret key
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(key);

// process the input string
String inputstring = x_login + "^" + x_fp_sequence + "^" +
x_fp_timestamp + "^" + x_amount + "^";
byte[] result = mac.doFinal(inputstring.getBytes());

// convert the result from byte[] to hexadecimal format
StringBuffer strbuf = new StringBuffer(result.length * 2);
for(int i=0; i< result.length; i++)
   {
       if(((int) result[i] & 0xff) < 0x10)
           strbuf.append("0");
       strbuf.append(Long.toString((int) result[i] & 0xff, 16));
   }
String x_fp_hash = strbuf.toString();
%>
<form action="https://checkout.globalgatewaye4.firstdata.com/payment" method="post">

<label>x_login</label>
 <input name="x_login" value="<%= x_login %>" />
 <label>x_fp_sequence</label>
 <input name="x_fp_sequence" value="<%= x_fp_sequence %>" />
 <label>x_fp_timestamp</label>
 <input name="x_fp_timestamp" value="<%= x_fp_timestamp %>" />
 <label>x_amount</label>
 <input name="x_amount" value="<%= x_amount %>" />
 <label>x_fp_hash (with SHA-1)</label>
 <input name="x_fp_hash" value="<%= x_fp_hash %>" size="40"/>
 <input name="x_show_form" value="PAYMENT_FORM" type="hidden" />
 <input type="submit" value="Checkout" />
</form>

</body>
</html>

  

To generate the payment request form using Perl please see the code below:

#!/usr/bin/perl -w

use strict;


# Prerequisites
# In order to be able to calculate the HMAC MD5 digest, we use a function hmac_md5_hex below.
#
#  1. Install Digest::MD5 from http://cpan.uwinnipeg.ca/dist/Digest-Perl-MD5
#  2. Install Digest::HMAC_MD5 from http://theoryx5.uwinnipeg.ca/pub/CPAN/authors/id/G/GA/GAAS/Digest-HMAC-1.01.tar.gz
# Other perl packages may be available
#
# With CPAN :
#
# (sudo) perl -MCPAN -e shell;
# install Digest::MD5
# install Digest::HMAC_MD5
# install Digest::SHA1
# exit
#
# Note: Digest::SHA1
# this will also install Digest::HMAC_SHA1

use Digest::HMAC_SHA1 qw(hmac_sha1_hex);

print "content-type: text/html\n\n";

print <<END_HEADER;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>
      Payment Pagest: Sample Perl Payment Form with SHA-1
    </title>
    <style type="text/css">
      label {
          display: block;
          margin: 5px 0px;
          color: #AAA;
       }
       input {
          display: block;
       }
       input[type=submit] {
          margin-top: 20px;
       }
    </style>

  </head>
  <body>

  <h1>
    Payment Pages: Sample Perl Payment Form with SHA-1
  </h1>

  <form action="https://checkout.globalgatewaye4.firstdata.com/payment" method="POST">
END_HEADER


      my $x_amount = "595.99";
      my $x_login = "WSP-#####-##";  # Take from Payment Page ID in Payment Pages interface
      my $transaction_key = "###############"; # Take from Payment Pages configuration interface
      my $x_currency_code = "USD"; # Needs to agree with the currency of the payment page
      my $x_fp_sequence = int(rand 5000) + 1000;
      my $x_fp_timestamp = time; #  needs to be in UTC. Make sure webserver produces UTC

      #  The values that contribute to x_fp_hash
      my $hmac_data = $x_login . "^" . $x_fp_sequence . "^" . $x_fp_timestamp . "^" . $x_amount . "^" . $x_currency_code;

      my $x_fp_hash = hmac_sha1_hex($hmac_data, $transaction_key);

      print('<label>x_login</label> <input name="x_login" value="' . $x_login . '">' );
      print('<label>x_amount</label> <input name="x_amount" value="' . $x_amount . '">' );
      print('<label>x_fp_sequence</label> <input name="x_fp_sequence" value="' . $x_fp_sequence . '">' );
      print('<label>x_fp_timestamp</label> <input name="x_fp_timestamp" value="' . $x_fp_timestamp . '">' );
      print('<label>x_fp_hash (with SHA-1)</label> <input name="x_fp_hash" value="' . $x_fp_hash . '" size="50">' );
      print('<label>x_currency_code</label> <input type="hidden" name="x_currency_code" value="' . $x_currency_code . '">');

     print <<END_REST
      <input type="hidden" name="x_show_form" value="PAYMENT_FORM">
      <input type="submit" value="Pay with Payment Pages">
    </form>

  </body>
</html>
END_REST

 

To generate the payment request form using PHP please see the code below:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>
      Payment Pages: Sample PHP Payment Form
    </title>
<style type="text/css">
  label {
      display: block;
      margin: 5px 0px;
      color: #AAA;
   }
   input {
      display: block;
   }
   input[type=submit] {
      margin-top: 20px;
   }
</style>

  </head>
  <body>
  <h1>
    Payment Pages: Sample PHP Payment Form
  </h1>

  <form action="https://checkout.globalgatewaye4.firstdata.com/payment" method="POST">

<?php
      $x_login = "WSP-#####-##";  //  Take from Payment Page ID in Payment Pages interface
      $transaction_key = "#############"; // Take from Payment Pages configuration interface
      $x_amount = "595.99";
      $x_currency_code = "USD"; // Needs to agree with the currency of the payment page
      srand(time()); // initialize random generator for x_fp_sequence
      $x_fp_sequence = rand(1000, 100000) + 123456;
      $x_fp_timestamp = time(); // needs to be in UTC. Make sure webserver produces UTC

      // The values that contribute to x_fp_hash
      $hmac_data = $x_login . "^" . $x_fp_sequence . "^" . $x_fp_timestamp . "^" . $x_amount . "^" . $x_currency_code;
      $x_fp_hash = hash_hmac('MD5', $hmac_data, $transaction_key);
      
      echo ('<label>x_login</label><input name="x_login" value="' . $x_login . '">' );
      echo ('<label>x_amount</label><input name="x_amount" value="' . $x_amount . '">' );
      echo ('<label>x_fp_sequence</label><input name="x_fp_sequence" value="' . $x_fp_sequence . '">' );
      echo ('<label>x_fp_timestamp</label><input name="x_fp_timestamp" value="' . $x_fp_timestamp . '">' );
      echo ('<label>x_fp_hash</label><input name="x_fp_hash" value="' . $x_fp_hash . '" size="50">' );
      echo ('<label>x_currency_code</label><input name="x_currency_code" value="' . $x_currency_code . '">');
?>

      <input type="hidden" name="x_show_form" value="PAYMENT_FORM"/>
      <input type="submit" value="Pay with Payment Pages"/>
    </form>

  </body>
</html>

 

To generate the payment request form using Ruby on Rails please see the code below:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>
      Payment Pagest Form Example - Ruby on Rails
    </title>
  </head>
  <body>

  <h1>
    Payment Pages Form Example - Ruby on Rails
  </h1>
  <p>
    Disclaimer: this sample program is meant for guidance. There are no warranties about its functionality.
  </p>
  <%= form_tag "https://checkout.globalgatewaye4.firstdata.com/payment", :method=>:post %>
  <%
     # The payment data.
     x_amount = "595.99"
     x_login = "WSP-#####-##"  #  Take from Payment Page ID in Payment Pages interface
     transaction_key = "########################" # Take from Payment Pages configuration interface
     x_currency_code = "USD" #  Needs to agree with the currency of the payment page
     x_fp_sequence = ((rand*100000).to_i + 2000).to_s
     x_fp_timestamp = Time.now.to_i.to_s # needs to be in UTC. Make sure webserver produces UTC
     
     # The values that contribute to x_fp_hash
     hmac_data = "#{x_login}^#{x_fp_sequence}^#{x_fp_timestamp}^#{x_amount}^#{x_currency_code}"

     # calculate HMAC-SHA1
     x_fp_hash = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new("sha1"),transaction_key, hmac_data)

     # or, to calculate HMAC-MD5, comment out above and uncomment below
     #x_fp_hash = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new("md5"),transaction_key, hmac_data)
  %>

  <%= hidden_field_tag 'x_amount', x_amount %>
  <%= hidden_field_tag 'x_login', x_login %>
  <%= hidden_field_tag 'x_fp_sequence', x_fp_sequence %>
  <%= hidden_field_tag :x_fp_timestamp, x_fp_timestamp %>
  <%= hidden_field_tag :x_currency_code, x_currency_code %>
  <%= hidden_field_tag :x_fp_hash, x_fp_hash %>
  <%= hidden_field_tag :x_show_form, "PAYMENT_FORM" %>
    &  <%= submit_tag  "Pay Now" %>
  </form>

<body>
</html>
Powered by Zendesk