Generating the payment request form using Perl

Created by Richard Moore, Modified on Wed, 12 Apr, 2023 at 12:02 PM by Richard Moore

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>
      Hosted Checkout: 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>
    Hosted Checkout: Sample Perl Payment Form with SHA-1
  </h1>

  <form action="https://checkout.e-xact.com/pay" 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 = "CAD"; # 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="hidden" name="x_test_request" value="TRUE">

      <input type="submit" value="Pay with Hosted Checkout">
    </form>

  </body>
</html>
END_REST

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article