[IC] Surepay Intergration - the continuing globalsub saga...

ben schlaver ben@closetgenius.com
Wed, 25 Apr 2001 03:06:43 -0700


This version of the surepay globalsub processes cleanly thru the Interchange
system generating the order and associated emails, but i'm not noticing the
orders arriving in the SurePay Online Manager.. like i do with there
SampleCGI...

??

#!/usr/bin/perl
#
# Sample CGI script for PurePayments(tm) Perl SDK API
# Copyright (c) 1999 iMall Incorporated
#
# $Id: SampleCGI.pl,v 1.18 2001/02/15 20:08:50 jsheu Exp $
#
#---------------------------------------------------------------------------
-----------------------


# Connection routine for Surepay
# by mark@summersault.com with code reused and inspired by
#	Mike Heins <mike@minivend.com>
#	webmaster@nameastar.net
#   Jeff Nappi <brage@cyberhighway.net>
#   Paul Delys <paul@gi.alaska.edu>
#  Edited by Ray Desjardins <ray@dfwmicrotech.com>
# surepay ben@closetgenius.com

# Edited only for references to Minivend, changed to Interchange
# by Mike Heins. Thanks, guys!

Variable SUREPAY_HELP <<EOV

 1. Modify interchange.cfg to use this file.

	#include globalsub/surepay

 2. Modify catalog.cfg to set the server and your Surepay account info


 # Username and password
 Variable MV_PAYMENT_ID      YourSurePayID
 Variable MV_PAYMENT_SECRET  YourSurepayPassword
 Variable MV_PAYMENT_MODE    custom surepay

 3. Make sure CreditCardAuto is off (default in Interchange demos)

 4. Restart Interchange.

EOV

GlobalSub <<EOS
sub surepay {

require 'PurePaymentsSDK.pl';
require 'cgi-lib.pl';

$prog = 'surepay';     # this script
$mcfg = 'merchant.cfg';     # Merchant config file (contains ID, password,
gateway mode)

local($timeout)   = 15;
local($SIG{ALRM}) = sub { die "$timeout sec timeout"; };

EOF
$trailer = "</BODY></HTML>\n";

print "Content-Type: text/html\n\n";

&ReadParse(*in);
foreach (split(/&/,$ENV{QUERY_STRING}))
{
  my ($n,$v) = split(/=/);
  $in{$n} = $v if $n && $v;
}

# Production System
$host_live = 'xml.surepay.com';
$port_live = 443;
$path_live = '/';

# Test System
$host_test = 'xml.test.surepay.com';
$port_test = 443;
$path_test = '/';

# Open merchant.cfg if exists
if (-e $mcfg)
{
  open (F,$mcfg);
  while (<F>)
  {
    chop;
    next if /^#/;
    s/#.*//;
    s/\s+$//;
    my ($n,$v) = split(/\s+/,$_,2);
    $in{$n} = $v if $n;
  }
  close F;
}

if ($in{mode} eq 'live')
{
  $in{host} ||= $host_live;
  $in{port} ||= $port_live;
  $in{path} ||= $path_live;
}
elsif ($in{mode} eq 'test')
{
  $in{host} ||= $host_test;
  $in{port} ||= $port_test;
  $in{path} ||= $path_test;
}
$in{gtype}    ||= 'https';  # (http|https|test)
$in{numitems} ||= 5;        # Max number of line items to allow
$in{dumpxml}  ||= 'N';      # Display request/response xml if eq 'Y'
$in{debug}    ||= 0;        # Additional debug info if > 0

# Option to read all input parameters from config file
if (!$in{submit} && $in{config} && -e $in{config})
{
  open (F,$in{config});
  while (<F>)
  {
    chop;
    next if /^#/;
    s/#.*//;
    my ($n,$v) = split(/\s+/,$_,2);
    $in{$n} = $v if $n;
  }
  close F;
}

my ($csp,$cspmid) = split(/\s+/,$in{ppmid});
if ($cspmid =~ /\d+/)
{
  $in{csp} = $csp;
  $in{cspmid} = $cspmid;
}
else
{
  $in{csp} = '';
  $in{cspmid} = $in{ppmid};
}

if ($in{submit} eq 'AUTHORIZE')
{
  &run_auth;
}


exit;

sub run_auth
{

if ($in{bdefault} eq 'Y')
{
  foreach
('fullname','address1','address2','address3','city','state','zip','country',
           'phone','fax','email')
  {
    $in{"b$_"} = $in{"s$_"};
  }
}

eval {

my $reqManager = RequestManager::new($in{gtype},$in{'data session
host'},$in{port},$in{path},$in{debug},'');
my $billAddress = Address::new('billing',
                               'fullname' => $in{'b_fname b_lname'},
			       'email'    => $in{email},
			       'eveningphone' => $in{b_phone},
			       'fax'      => $in{fax},
			       'address1' => $in{b_address1},
			       'address2' => $in{b_address2},
			       'address3' => $in{b_address3},
			       'city'     => $in{b_city},
			       'state'    => $in{b_state},
			       'country'  => $in{b_country},
			       'zip'      => $in{b_zip});
my $shipAddress = Address::new('shipping',
                               'fullname' => $in{'fname lname'},
                               'email'    => $in{email},
			       'eveningphone' => $in{phone_day},
			       'fax'      => $in{fax},
			       'address1' => $in{address1},
			       'address2' => $in{address2},
			       'address3' => $in{address3},
			       'city'     => $in{city},
			       'state'    => $in{state},
			       'country'  => $in{country},
			       'zip'      => $in{zip});

my ($pi,$id);
if ($in{ptype} eq 'cc') # CreditCard
{
  $pi =
CreditCard::new($in{mv_credit_card_number},$in{'mv_credit_card_exp_month/mv_
credit_card_exp_year'},$in{0},$in{0},$billAddress);  #
number,expiration,address
}
elsif ($in{ptype} eq 'tc') # TeleCheck
{
  $in{isbusiness} = ($in{isbusiness} eq 'true')? 'true' : 'false';
  $id = Identification::new('driverslicense',$in{dlnum},$in{dlst});  #
type,number,issuedBy
  $pi =
TeleCheck::new($in{cnum},$in{micr},$in{isbusiness},$billAddress,$id);
}
else
{
  $pi = FlashCash::new($billAddress);
}
$ptype = $pi->getPaymentType;

foreach ('shipping','tax','total') { $in{$_} =~ s/[^\d\.\-]//g; }

my $extAmt = 0.;
my ($i,$lineitem,@lineitems);
foreach $i (1..$in{numitems})
{
  next unless $in{sku.$i} && $in{quan.$i};
  $lineitem =
LineItem::new($in{sku.$i},$in{desc.$i},$in{quan.$i},0,$in{price.$i});
  $extAmt += $lineitem->getQuantity * $lineitem->getUnitPrice;
  push(@lineitems,$lineitem);
}
my $totalAmt = $extAmt + $in{shipping} + $in{tax};

my $obj;
if ($in{ptype} eq 'cf') # FlashCash: compute fee
{
  $obj = ComputeFee::new($in{bcountry},'',$in{shipping},$in{tax});
}
elsif ($in{ptype} eq 'fc') # FlashCash: initiate payment
{
  $obj =
InitiatePayment::new($in{ordernumber},$in{orderdesc},$in{ponumber},,
		              $in{shipping},$in{tax},$shipAddress,$pi);
}
else # CreditCard or TeleCheck
{
  $obj =
Auth::new($in{order_number},$in{description},$in{ponumber},'',$in{authcode},
		   $in{'data session host'},$in{refurl},$in{browser},
		   $in{shipping},$in{tax}, $in{ecommerce}, $in{securitytype},
		   $shipAddress,$pi);
}
if ($in{gto} || $in{gfrom} || $in{gmsg})
{
  $obj->addGift($in{gto},$in{gfrom},$in{gmsg}) unless $in{ptype} eq 'cf';
}
foreach $lineitem (@lineitems) { $obj->addLineItem($lineitem); }
if ($in{total} && $in{total} != $totalAmt) { $obj->setTotal($in{total}); }
$total = $obj->getTotal;
$num   = scalar @{$obj->getLineItems};

print "<PRE>\n" if $in{debug} > 0;
my $req  =
Request::new($in{csp},$in{sdkversion},$in{sdktype},$in{cspmid},$in{password}
,$obj);
print $req->toString  if $in{debug} > 0;

alarm $timeout;
my $res  = $reqManager->submit($req);
alarm 0;

print $res->toString if $in{debug} > 0;
print "</PRE>\n" if $in{debug} > 0;

my $ores = $res->getFirstResponse;
$pres = ref $ores;
$failure    = $ores->getFailure;        # true/false
$failureMsg = $ores->getFailureMsg;     # message if failure=true
if ($pres eq 'AuthResponse')
{
  $status   = $ores->getAuthStatus;     # status  if failure=false
  $authCode = $ores->getAuthCode;       # authorization code if transaction
approved
  $avs      = $ores->getAVS;            # avs
  $cvv2result = $ores->getcvv2result;	# CVV2 result
  $tid      = $ores->getTransactionID;  # transaction id
  $status ||= 'ERROR';
  $status  .= ": " . $failureMsg if $failure eq 'true';
}
elsif ($pres eq 'ComputeFeeResponse')
{
  $fee = $ores->getFeeAmount;
  $due = $ores->getDueAmount;
  $exp = $ores->getExpirationDate;
  $exc = $ores->getExchangeRate;
  $pid = '';
  $tid = 'compute fee only';
  $status  = ($failure eq 'true')? $failureMsg : '';
}
elsif ($pres eq 'InitiatePaymentResponse')
{
  $fee = $ores->getFeeAmount;
  $due = $ores->getDueAmount;
  $exp = $ores->getExpirationDate;
  $exc = $ores->getExchangeRate;
  $pid = $ores->getPayID;
  $tid = $ores->getTransactionID;
  $status  = ($failure eq 'true')? $failureMsg : '';
}


if ($in{dumpxml} eq 'Y')
{
  my $smsg = &xml_display($req->toXML);
  my $rmsg = &xml_display($res->toXML);
  print "<P>Request:<PRE>$smsg</PRE>\n";
  print "<P>Response:<PRE>$rmsg</PRE>\n";
}

};
$@ =~ s/ at .*//;

$status = "ERROR: $@\n" if $@;

my $hidden = '';
foreach (sort keys %in)
{
  next if $_ eq 'password';
  next unless $in{$_} =~ /\S/;
  $hidden .= "<INPUT TYPE=hidden NAME=$_ VALUE=\"$in{$_}\">\n";
}

print <<EOF;
$header
<P><TABLE WIDTH=50% BORDER=1 CELLSPACING=0 CELLPADDING=3>
EOF
if ($in{debug} > 0) { print <<EOF;
<TR><TD>Merchant<TD>$in{ppmid}</TR>
<TR valign=top><TD>Gateway<TD>$in{gtype}
$in{mode}<BR>$in{host}:$in{port}<BR>$in{path}</TR>
EOF
}
print <<EOF;
<TR><TD>Payment Type<TD>$ptype&nbsp;</TR>
<TR><TD>Invoice Number<TD>$in{ordernumber}&nbsp;</TR>
<TR><TD>Total Amount<TD>$total&nbsp;</TR>
<TR><TD># Line Items<TD>$num&nbsp;</TR>
EOF

if ($ptype eq 'FlashCash')
{
  print "<TR><TD>Status<TD>$status&nbsp;</TR>\n" if $status;
  print <<EOF;
<TR><TD>Fee Amount<TD>$fee&nbsp;</TR>
<TR><TD>Due Amount<TD>$due&nbsp;</TR>
<TR><TD>Expiration<TD>$exp&nbsp;</TR>
<TR><TD>Exchange Rate<TD>$exc&nbsp;</TR>
<TR><TD>Pay ID<TD>$pid&nbsp;</TR>
<TR><TD>Transaciton ID<TD>$tid&nbsp;</TR>
EOF
}
else
{
  print <<EOF;
<TR><TD>Auth Status<TD>$status&nbsp;</TR>
<TR><TD>Auth Code<TD>$authCode&nbsp;</TR>
<TR><TD>AVS<TD>$avs&nbsp;</TR>
<TR><TD>CVV2 Result<TD>$cvv2result&nbsp;</TR>
<TR><TD>Transaction ID<TD>$tid&nbsp;</TR>
EOF
}

sub xml_display {
  my $s = shift;
  $s =~ s#(<[^/])#\n$1#g;
  $s =~ s#(</?)([^ !/>]*)#$1^1a^$2^1b^#g;
  $s =~ s#\b(\w+)=#^2a^$1^2b^=#g;
  $s =~ s#="([^"]*)"#="^3a^$1^3b^"#g;
  $s =~ s#<#&lt;#g;
  $s =~ s#>#&gt;#g;
  $s =~ s#\^1a\^#<FONT COLOR=ff0000>#g;
  $s =~ s#\^2a\^#<FONT COLOR=0000ff>#g;
  $s =~ s#\^3a\^#<FONT COLOR=00bb00>#g;
  $s =~ s#\^1b\^#</FONT>#g;
  $s =~ s#\^2b\^#</FONT>#g;
  $s =~ s#\^3b\^#</FONT>#g;
  return $s;
}