From 530732646a1bf3e58893c67f78093a89c31e454a Mon Sep 17 00:00:00 2001 From: jeff Date: Mon, 2 Mar 2009 23:57:20 +0000 Subject: [PATCH 1/1] In case it is useful elsewhere --- collect.cgi | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ fake-webpay.conf | 6 +++ 2 files changed, 131 insertions(+) create mode 100755 collect.cgi create mode 100644 fake-webpay.conf diff --git a/collect.cgi b/collect.cgi new file mode 100755 index 0000000..883a506 --- /dev/null +++ b/collect.cgi @@ -0,0 +1,125 @@ +#!/usr/bin/perl -T +#!/usr/bin/perl -Tw + +use strict; +use vars qw( $cgi $self_url $error + $collect_html $collect_template + ); + +use subs qw( print_redirect print_collect collect_default ); +use CGI; +use Text::Template; +use Business::CreditCard; + +my $redirect_url = "http://127.0.0.1/selfservice/verify.cgi"; + +$collect_html = -e 'collect.html' + ? 'collect.html' + : '/usr/local/freeside/collect.html'; + +if ( -e $collect_html ) { + my $collect_txt = Text::Template::_load_text($collect_html) + or die $Text::Template::ERROR; + $collect_txt =~ /^(.*)$/s; #untaint the template source - it's trusted + $collect_txt = $1; + $collect_template = new Text::Template ( TYPE => 'STRING', + SOURCE => $collect_txt, + DELIMITERS => [ '<%=', '%>' ], + ) + or die $Text::Template::ERROR; +} else { + $collect_template = new Text::Template ( TYPE => 'STRING', + SOURCE => &collect_default, + DELIMITERS => [ '<%=', '%>' ], + ) + or die $Text::Template::ERROR; +} + +$cgi = new CGI; + +if ( defined($cgi->param('magic')) && $cgi->param('magic') eq 'process' ) { + + $error = ''; + + $cgi->param('paydate' => $cgi->param( 'month' ). '-'. + $cgi->param( 'year' ) + ); + + my $payinfo = $cgi->param('payinfo'); + $payinfo =~ s/\D//g; + $payinfo =~ /^(\d{13,16})$/ or $error ||= "Invalid card"; + $payinfo = $1; + validate($payinfo) or $error ||= "Invalid card"; + + my %rv = ( map { $_ => scalar($cgi->param($_)) } qw( reference amount ) ); + + unless ( $error ) { + $error = '_decline' unless $payinfo eq '4111111111111111'; + } + + if ( $error eq '_decline' ) { + $rv{status} = '01'; + $rv{message} = 'Declined'; + print_redirect( %rv ); + } elsif ( $error eq '_collect' ) { + print_collect(); + } elsif ( $error ) { + print_collect(); + } else { + $rv{status} = '00'; + $rv{message} = 'Approved'; + print_redirect( %rv ); + } + +} else { + $error = ''; + print_collect(); +} + +sub print_collect { + + $error = "Error: $error" if $error; + + my $r = { $cgi->Vars, 'error' => $error }; + + $r->{self_url} = $cgi->self_url; + + print $cgi->header( '-expires' => 'now' ), + $collect_template->fill_in( PACKAGE => 'FS::SelfService::_signupcgi', + HASH => $r + ); +} + +use Data::Dumper; +sub print_redirect { + my %param = @_; + + my $param = join('&', map { "$_=". $param{$_} } keys %param ); + warn Dumper($param); + print $cgi->redirect( '-uri' => $redirect_url. '?'. $param ); +} + +sub collect_default { #html to use if there is a collect phase + <<'END'; +Pay now +Pay now

+<%= $error %>
+You are about to contact our payment processor to pay <%= $amount %> for +<%= $pkg %>.

+Your transaction reference number is <%= $reference %>

+
+ + + +Card Number:
+ +
+ +END +} + +# subs for the templates... + +package FS::SelfService::_signupcgi; +use HTML::Entities; + diff --git a/fake-webpay.conf b/fake-webpay.conf new file mode 100644 index 0000000..5789913 --- /dev/null +++ b/fake-webpay.conf @@ -0,0 +1,6 @@ + + +AddHandler cgi-script .cgi +Options +ExecCGI + + -- 2.11.0