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);
23 # qw(get_https post_https make_form make_headers);
24 $ssl_module = 'Net::SSLeay';
29 require LWP::UserAgent;
30 require HTTP::Request::Common;
31 require Crypt::SSLeay;
33 #import HTTP::Request::Common qw(GET POST);
34 $ssl_module = 'Crypt::SSLeay';
38 unless ($ssl_module) {
39 die "One of Net::SSLeay (v1.30 or later)"
40 . " or Crypt::SSLeay (+LWP) is required";
47 Business::OnlinePayment::HTTPS - Base class for HTTPS payment APIs
51 package Business::OnlinePayment::MyProcessor;
52 use base qw(Business::OnlinePayment::HTTPS);
59 # pass a list (order is preserved, if your gateway needs that)
60 ( $page, $response, %reply_headers )
61 = $self->https_get( field => 'value', ... );
64 my %hash = ( field => 'value', ... );
65 ( $page, $response_code, %reply_headers )
66 = $self->https_get( \%hash );
73 This is a base class for HTTPS based gateways, providing useful code
74 for implementors of HTTPS payment APIs.
76 It depends on Net::SSLeay _or_ ( Crypt::SSLeay and LWP::UserAgent ).
82 =item https_get [ \%options ] HASHREF | FIELD => VALUE, ...
84 Accepts parameters as either a hashref or a list of fields and values.
85 In the latter case, ordering is preserved (see L<Tie::IxHash> to do so
86 when passing a hashref).
88 Returns a list consisting of the page content as a string, the HTTP
89 response code, and a list of key/value pairs representing the HTTP
92 The options hashref supports setting headers and Content-Type:
95 headers => { 'X-Header1' => 'value', ... },
96 Content-Type => 'text/namevalue',
104 # handle optional options hashref
106 if ( scalar(@_) > 1 and ref( $_[0] ) eq "HASH" ) {
110 # accept a hashref or a list (keep it ordered)
112 if ( ref( $_[0] ) eq 'HASH' ) {
115 elsif ( scalar(@_) > 1 ) {
116 tie my %hash, 'Tie::IxHash', @_;
119 elsif ( scalar(@_) == 1 ) {
123 die "https_get called with no params\n";
126 $opts->{"Content-Type"} ||= "application/x-www-form-urlencoded";
130 if ( ref( $opts->{headers} ) eq "HASH" ) {
131 %headers = %{ $opts->{headers} };
133 $headers{'Host'} ||= $self->server;
135 my $path = $self->path;
136 if ( keys %$post_data ) {
139 map { uri_escape($_) . '=' . uri_escape( $post_data->{$_} ) }
143 if ( $ssl_module eq 'Net::SSLeay' ) {
145 import Net::SSLeay qw(get_https make_headers);
146 my $headers = make_headers(%headers);
147 get_https( $self->server, $self->port, $path, $headers, "",
148 $opts->{"Content-Type"} );
150 elsif ( $ssl_module eq 'Crypt::SSLeay' ) {
152 import HTTP::Request::Common qw(GET);
154 my $url = 'https://' . $self->server;
155 $url .= ':' . $self->port
156 unless $self->port == 443;
159 my $ua = new LWP::UserAgent;
160 my $res = $ua->request( GET($url) );
163 $res->content, $res->code,
164 map { $_ => $res->header($_) } $res->header_field_names
168 die "unknown SSL module $ssl_module";
172 =item https_post [ \%options ] SCALAR | HASHREF | FIELD => VALUE, ...
174 Accepts form fields and values as either a hashref or a list. In the
175 latter case, ordering is preserved (see L<Tie::IxHash> to do so when
178 Also accepts instead a simple scalar containing the raw content.
180 Returns a list consisting of the page content as a string, the HTTP
181 response code, and a list of key/value pairs representing the HTTP
184 The options hashref supports setting headers and Content-Type:
187 headers => { 'X-Header1' => 'value', ... },
188 Content-Type => 'text/namevalue',
196 # handle optional options hashref
198 if ( scalar(@_) > 1 and ref( $_[0] ) eq "HASH" ) {
202 # accept a hashref or a list (keep it ordered)
204 if ( ref( $_[0] ) eq 'HASH' ) {
207 elsif ( scalar(@_) > 1 ) {
208 tie my %hash, 'Tie::IxHash', @_;
211 elsif ( scalar(@_) == 1 ) {
215 die "https_post called with no params\n";
218 $opts->{"Content-Type"} ||= "application/x-www-form-urlencoded";
222 if ( ref( $opts->{headers} ) eq "HASH" ) {
223 %headers = %{ $opts->{headers} };
225 $headers{'Host'} ||= $self->server;
227 if ( $DEBUG && ref($post_data) ) {
230 map { " $_ => " . $post_data->{$_} . "\n" } keys %$post_data );
233 if ( $ssl_module eq 'Net::SSLeay' ) {
235 import Net::SSLeay qw(post_https make_headers make_form);
236 my $headers = make_headers(%headers);
239 no warnings 'uninitialized';
240 warn $self->server . ':' . $self->port . $self->path . "\n";
241 $Net::SSLeay::trace = $DEBUG;
244 my $raw_data = ref($post_data) ? make_form(%$post_data) : $post_data;
245 post_https( $self->server, $self->port, $self->path, $headers,
246 $raw_data, $opts->{"Content-Type"} );
248 elsif ( $ssl_module eq 'Crypt::SSLeay' ) {
250 import HTTP::Request::Common qw(POST);
252 my $url = 'https://' . $self->server;
253 $url .= ':' . $self->port
254 unless $self->port == 443;
261 my $ua = new LWP::UserAgent;
264 if ( ref($post_data) ) {
265 $res = $ua->request( POST( $url, [%$post_data] ) );
268 my $req = new HTTP::Request( 'POST' => $url );
269 $req->content_type( $opts->{"Content-Type"} );
270 $req->content($post_data);
271 $res = $ua->request($req);
275 $res->content, $res->code,
276 map { $_ => $res->header($_) } $res->header_field_names
280 die "unknown SSL module $ssl_module";
288 L<Business::OnlinePayment>