In case it is useful elsewhere
[fake-webpay.git] / collect.cgi
1 #!/usr/bin/perl -T
2 #!/usr/bin/perl -Tw
3
4 use strict;
5 use vars qw( $cgi $self_url $error
6              $collect_html $collect_template
7            );
8
9 use subs qw( print_redirect print_collect collect_default );
10 use CGI;
11 use Text::Template;
12 use Business::CreditCard;
13
14 my $redirect_url = "http://127.0.0.1/selfservice/verify.cgi";
15
16 $collect_html = -e 'collect.html'
17                   ? 'collect.html'
18                   : '/usr/local/freeside/collect.html';
19
20 if ( -e $collect_html ) {
21   my $collect_txt = Text::Template::_load_text($collect_html)
22     or die $Text::Template::ERROR;
23   $collect_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
24   $collect_txt = $1;
25   $collect_template = new Text::Template ( TYPE => 'STRING',
26                                            SOURCE => $collect_txt,
27                                            DELIMITERS => [ '<%=', '%>' ],
28                                          )
29     or die $Text::Template::ERROR;
30 } else {
31   $collect_template = new Text::Template ( TYPE => 'STRING',
32                                            SOURCE => &collect_default,
33                                            DELIMITERS => [ '<%=', '%>' ],
34                                          )
35     or die $Text::Template::ERROR;
36 }
37
38 $cgi = new CGI;
39
40 if ( defined($cgi->param('magic')) && $cgi->param('magic') eq 'process' ) {
41
42     $error = '';
43
44     $cgi->param('paydate' => $cgi->param( 'month' ). '-'.
45                              $cgi->param( 'year' )
46                );
47
48     my $payinfo = $cgi->param('payinfo');
49     $payinfo =~ s/\D//g;
50     $payinfo =~ /^(\d{13,16})$/ or $error ||= "Invalid card";
51     $payinfo = $1;
52     validate($payinfo) or $error ||= "Invalid card";
53
54     my %rv = ( map { $_ => scalar($cgi->param($_)) } qw( reference amount ) );
55
56     unless ( $error ) {
57       $error = '_decline' unless $payinfo eq '4111111111111111';
58     }
59     
60     if ( $error eq '_decline' ) {
61       $rv{status} = '01';
62       $rv{message} = 'Declined';
63       print_redirect( %rv );
64     } elsif ( $error eq '_collect' ) {
65       print_collect();
66     } elsif ( $error ) {
67       print_collect();
68     } else {
69       $rv{status} = '00';
70       $rv{message} = 'Approved';
71       print_redirect( %rv );
72     }
73
74 } else {
75   $error = '';
76   print_collect();
77 }
78
79 sub print_collect {
80
81   $error = "Error: $error" if $error;
82
83   my $r = { $cgi->Vars, 'error' => $error };
84
85   $r->{self_url} = $cgi->self_url;
86
87   print $cgi->header( '-expires' => 'now' ),
88         $collect_template->fill_in( PACKAGE => 'FS::SelfService::_signupcgi',
89                                     HASH    => $r
90                                   );
91 }
92
93 use Data::Dumper;
94 sub print_redirect {
95   my %param = @_;
96
97   my $param = join('&', map { "$_=". $param{$_} } keys %param );
98   warn Dumper($param);
99   print $cgi->redirect( '-uri' => $redirect_url. '?'. $param );
100 }
101
102 sub collect_default { #html to use if there is a collect phase
103   <<'END';
104 <HTML><HEAD><TITLE>Pay now</TITLE></HEAD>
105 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>Pay now</FONT><BR><BR>
106 <FONT SIZE="+1" COLOR="#FF00000"><%= $error %></FONT><BR>
107 You are about to contact our payment processor to pay <%= $amount %> for
108 <%= $pkg %>.<BR><BR>
109 Your transaction reference number is <%= $reference %><BR><BR>
110 <FORM NAME="collect_popper" method="post" action="<%= $self_url %>">
111 <INPUT NAME="magic"  TYPE="hidden" VALUE="process">
112 <INPUT NAME="amount" TYPE="hidden" VALUE="<%= $amount %>">
113 <INPUT NAME="reference" TYPE="hidden" VALUE="<%= $reference %>">
114 Card Number:<INPUT NAME="payinfo" TYPE="text" VALUE=""><BR>
115 <INPUT NAME="submit" type="submit" value="Pay now">
116 </FORM>
117 </BODY></HTML>
118 END
119 }
120
121 # subs for the templates...
122
123 package FS::SelfService::_signupcgi;
124 use HTML::Entities;
125