1 package Business::OnlinePayment::HTTPS;
4 use vars qw($VERSION $DEBUG $ssl_module $skip_NetSSLeay);
7 use base qw(Business::OnlinePayment);
10 $VERSION = eval $VERSION; # modperlstyle: convert the string into a number
18 die if defined($skip_NetSSLeay) && $skip_NetSSLeay;
20 Net::SSLeay->VERSION(1.30);
22 # qw(get_https post_https make_form make_headers);
23 $ssl_module = 'Net::SSLeay';
28 require LWP::UserAgent;
29 require HTTP::Request::Common;
30 require Crypt::SSLeay;
31 #import HTTP::Request::Common qw(GET POST);
32 $ssl_module = 'Crypt::SSLeay';
36 unless ( $ssl_module ) {
37 die "One of Net::SSLeay (v1.30 or later)".
38 " or Crypt::SSLeay (+LWP) is required";
45 Business::OnlinePayment::HTTPS - Base class for HTTPS payment APIs
49 package Business::OnlinePayment::MyProcessor
50 use base qw(Business::OnlinePayment::HTTPS);
57 # pass a list (order is preserved, if your gateway needs that)
58 ($page, $response, %reply_headers)
59 = $self->https_get( field => 'value', ... );
62 my %hash = ( field => 'value', ... );
63 ($page, $response_code, %reply_headers)
64 = $self->https_get( \%hash );
71 This is a base class for HTTPS based gateways, providing useful code for
72 implementors of HTTPS payment APIs.
74 It depends on Net::SSLeay _or_ ( Crypt::SSLeay and LWP::UserAgent ).
80 =item https_get [ \%options ] HASHREF | FIELD => VALUE, ...
82 Accepts parameters as either a hashref or a list of fields and values. In the
83 latter case, ordering is preserved (see L<Tie::IxHash> to do so when passing a
86 Returns a list consisting of the page content as a string, the HTTP response
87 code, and a list of key/value pairs representing the HTTP response headers.
94 # handle optional options hashref
96 if ( scalar(@_) > 1 and ref($_[0]) eq "HASH" ) {
100 # accept a hashref or a list (keep it ordered)
102 if ( ref($_[0]) eq 'HASH' ) {
104 } elsif ( scalar(@_) > 1 ) {
105 tie my %hash, 'Tie::IxHash', @_;
107 } elsif ( scalar(@_) == 1 ) {
110 die "https_get called with no params\n";
113 $opts->{"Content-Type"} ||= "application/x-www-form-urlencoded";
117 if ( ref( $opts->{headers} ) eq "HASH" ) {
118 %headers = %{ $opts->{headers} };
120 $headers{'Host'} ||= $self->server;
122 my $path = $self->path;
123 if ( keys %$post_data ) {
125 #my $u = URI->new("", "https");
126 #$u->query_param(%$post_data);
127 #$path .= '?'. $u->query;
129 $path .= '?'. join('&',
130 map { uri_escape($_).'='. uri_escape($post_data->{$_}) }
137 if ( $ssl_module eq 'Net::SSLeay' ) {
139 import Net::SSLeay qw(get_https make_headers);
140 my $headers = make_headers(%headers);
141 get_https( $self->server, $self->port, $path, $headers, "", $opts->{"Content-Type"} );
143 } elsif ( $ssl_module eq 'Crypt::SSLeay' ) {
145 import HTTP::Request::Common qw(GET);
147 my $url = 'https://'. $self->server;
148 $url .= ':'. $self->port
149 unless $self->port == 443;
152 my $ua = new LWP::UserAgent;
153 my $res = $ua->request( GET( $url ) );
155 #( $res->as_string, # wtf?
158 map { $_ => $res->header($_) } $res->header_field_names
163 die "unknown SSL module $ssl_module";
169 =item https_post [ \%options ] SCALAR | HASHREF | FIELD => VALUE, ...
171 Accepts form fields and values as either a hashref or a list. In the latter
172 case, ordering is preserved (see L<Tie::IxHash> to do so when passing a
175 Also accepts instead a simple scalar containing the raw content.
177 Returns a list consisting of the page content as a string, the HTTP response
178 code, and a list of key/value pairs representing the HTTP response headers.
185 # handle optional options hashref
187 if ( scalar(@_) > 1 and ref($_[0]) eq "HASH" ) {
191 # accept a hashref or a list (keep it ordered)
193 if ( ref($_[0]) eq 'HASH' ) {
195 } elsif ( scalar(@_) > 1 ) {
196 tie my %hash, 'Tie::IxHash', @_;
198 } elsif ( scalar(@_) == 1 ) {
201 die "https_post called with no params\n";
204 $opts->{"Content-Type"} ||= "application/x-www-form-urlencoded";
208 if ( ref( $opts->{headers} ) eq "HASH" ) {
209 %headers = %{ $opts->{headers} };
211 $headers{'Host'} ||= $self->server;
213 if ( $DEBUG && ref($post_data) ) {
214 warn "post data:\n", join('', map { " $_ => ". $post_data->{$_}. "\n" } keys %$post_data );
217 if ( $ssl_module eq 'Net::SSLeay' ) {
219 import Net::SSLeay qw(post_https make_headers make_form);
220 my $headers = make_headers(%headers);
223 no warnings 'uninitialized';
224 warn $self->server. ':'. $self->port. $self->path. "\n";
225 $Net::SSLeay::trace = $DEBUG;
228 my $raw_data = ref($post_data) ? make_form(%$post_data) : $post_data;
229 post_https( $self->server, $self->port, $self->path,
230 $headers, $raw_data, $opts->{"Content-Type"} );
232 } elsif ( $ssl_module eq 'Crypt::SSLeay' ) {
234 import HTTP::Request::Common qw(POST);
236 my $url = 'https://'. $self->server;
237 $url .= ':'. $self->port
238 unless $self->port == 443;
245 my $ua = new LWP::UserAgent;
248 if ( ref($post_data) ) {
249 $res = $ua->request( POST( $url, [ %$post_data ] ) );
251 my $req =new HTTP::Request( 'POST' => $url );
252 $req->content_type( $opts->{"Content-Type"} );
253 $req->content($post_data);
254 $res = $ua->request($req);
257 #( $res->as_string, # wtf?
260 map { $_ => $res->header($_) } $res->header_field_names
265 die "unknown SSL module $ssl_module";
275 L<Business::OnlinePayment>