default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / fs_selfservice / FS-SelfService / cgi / verify.cgi
1 #!/usr/bin/perl -T
2 #!/usr/bin/perl -Tw
3
4 use strict;
5 use vars qw( $cgi $self_url $error
6              $verify_html $verify_template
7              $success_html $success_template
8              $decline_html $decline_template
9            );
10
11 use subs qw( print_verify print_okay print_decline
12              verify_default success_default decline_default
13            );
14 use CGI;
15 use Text::Template;
16 use FS::SelfService qw( capture_payment );
17
18 $verify_html =  -e 'verify.html'
19                   ? 'verify.html'
20                   : '/usr/local/freeside/verify.html';
21 $success_html = -e 'verify_success.html'
22                   ? 'success.html'
23                   : '/usr/local/freeside/success.html';
24 $decline_html = -e 'verify_decline.html'
25                   ? 'decline.html'
26                   : '/usr/local/freeside/decline.html';
27
28
29 if ( -e $verify_html ) {
30   my $verify_txt = Text::Template::_load_text($verify_html)
31     or die $Text::Template::ERROR;
32   $verify_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
33   $verify_txt = $1;
34   $verify_template = new Text::Template ( TYPE => 'STRING',
35                                           SOURCE => $verify_txt,
36                                           DELIMITERS => [ '<%=', '%>' ],
37                                         )
38     or die $Text::Template::ERROR;
39 } else {
40   $verify_template = new Text::Template ( TYPE => 'STRING',
41                                           SOURCE => &verify_default,
42                                           DELIMITERS => [ '<%=', '%>' ],
43                                         )
44     or die $Text::Template::ERROR;
45 }
46
47 if ( -e $success_html ) {
48   my $success_txt = Text::Template::_load_text($success_html)
49     or die $Text::Template::ERROR;
50   $success_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
51   $success_txt = $1;
52   $success_template = new Text::Template ( TYPE => 'STRING',
53                                            SOURCE => $success_txt,
54                                            DELIMITERS => [ '<%=', '%>' ],
55                                          )
56     or die $Text::Template::ERROR;
57 } else {
58   $success_template = new Text::Template ( TYPE => 'STRING',
59                                            SOURCE => &success_default,
60                                            DELIMITERS => [ '<%=', '%>' ],
61                                          )
62     or die $Text::Template::ERROR;
63 }
64
65 if ( -e $decline_html ) {
66   my $decline_txt = Text::Template::_load_text($decline_html)
67     or die $Text::Template::ERROR;
68   $decline_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
69   $decline_txt = $1;
70   $decline_template = new Text::Template ( TYPE => 'STRING',
71                                            SOURCE => $decline_txt,
72                                            DELIMITERS => [ '<%=', '%>' ],
73                                          )
74     or die $Text::Template::ERROR;
75 } else {
76   $decline_template = new Text::Template ( TYPE => 'STRING',
77                                            SOURCE => &decline_default,
78                                            DELIMITERS => [ '<%=', '%>' ],
79                                          )
80     or die $Text::Template::ERROR;
81 }
82
83 $cgi = new CGI;
84
85 my $rv = capture_payment(
86            data => { 'manual' => 1,
87                      map { $_ => scalar($cgi->param($_)) } $cgi->param
88                    },
89            url  => $cgi->self_url,
90            cancel => ($cgi->param('cancel') ? 1 : 0),
91 );
92
93 $error = $rv->{error};
94
95 if ( $error eq '_cancel' ) {
96   print_okay(%$rv);
97 } elsif ( $error eq '_decline' ) {
98   print_decline();
99 } elsif ( $error ) {
100   print_verify();
101 } else {
102   print_okay(%$rv);
103 }
104
105
106 sub print_verify {
107
108   $error = "Error: $error" if $error;
109
110   my $r = { $cgi->Vars, 'error' => $error };
111
112   $r->{self_url} = $cgi->self_url;
113
114   print $cgi->header( '-expires' => 'now' ),
115         $verify_template->fill_in( PACKAGE => 'FS::SelfService::_signupcgi',
116                                    HASH    => $r
117                                  );
118 }
119
120 sub print_decline {
121   print $cgi->header( '-expires' => 'now' ),
122         $decline_template->fill_in();
123 }
124
125 sub print_okay {
126   my %param = @_;
127
128   my @success_url = split '/', $cgi->url(-path);
129   pop @success_url;
130
131   my $success_url  = join '/', @success_url;
132   if ($param{session_id}) {
133     my $session_id = lc($param{session_id});
134     $success_url .= "/selfservice.cgi?action=myaccount&session=$session_id";
135   } else {
136     $success_url .= '/signup.cgi?action=success';
137   }
138
139   if ( $param{error} eq '_cancel' ) {
140     # then the payment was canceled, so don't show a message, just redirect
141     # (during signup, you really need a separate landing page for this case)
142     print $cgi->redirect($success_url);
143   } else {
144     print $cgi->header( '-expires' => 'now' ),
145           $success_template->fill_in( HASH => { success_url => $success_url } );
146   }
147 }
148
149 sub success_default { #html to use if you don't specify a success file
150   <<'END';
151 <HTML><HEAD><TITLE>Signup successful</TITLE></HEAD>
152 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>Signup successful</FONT><BR><BR>
153 Thanks for signing up!
154 <BR><BR>
155 <SCRIPT TYPE="text/javascript">
156   window.top.location="<%= $success_url %>";
157 </SCRIPT>
158 </BODY></HTML>
159 END
160 }
161
162 sub verify_default { #html to use for verification response
163   <<'END';
164 <HTML><HEAD><TITLE>Processing error</TITLE></HEAD>
165 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>Processing error</FONT><BR><BR>
166 There has been an error processing your account.  Please contact customer
167 support.
168 </BODY></HTML>
169 END
170 }
171
172 sub decline_default { #html to use if there is a decline
173   <<'END';
174 <HTML><HEAD><TITLE>Processing error</TITLE></HEAD>
175 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>Processing error</FONT><BR><BR>
176 There has been an error processing your account.  Please contact customer
177 support.
178 </BODY></HTML>
179 END
180 }
181
182 # subs for the templates...
183
184 package FS::SelfService::_signupcgi;
185 use HTML::Entities;
186