initial import
[Business-OnlinePayment-PaymentsGateway.git] / PaymentsGateway.pm
1 package Business::OnlinePayment::PaymentsGateway;
2
3 use strict;
4 use Carp;
5 use Business::OnlinePayment
6 use Net::SSLeay qw(sslcat);
7 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $DEBUG);
8
9 require Exporter;
10
11 @ISA - qw( Exporter AutoLoader Business::OnlinePayment);
12 @EXPORT = qw();
13 @EXPORT_OK = qw();
14 $VERSION = '0.01';
15
16 $DEBUG = 0;
17
18 sub set_defaults {
19   my $self = shift;
20   $self->server('');
21   $self->port( 5050 + 1000 * $self->test_transaction() );
22   #$self->build_subs();
23 }
24
25 sub map_fields {
26   my $self = shift;
27   my %content = $self->content();
28
29   #ACTION MAP
30   my %actions = (
31     'normal authorization' => 0,
32     'authorization only'   => 1,
33     'post authorization'   => 2,
34     'credit'               => 3,
35   );
36
37   my %types = (
38     'visa'             => 10,
39     'mastercard'       => 10,
40     'american express' => 10,
41     'discover'         => 10,
42     'cc'               => 10,
43     'check'            => 20,
44   );
45
46   #pg_type/action = action + type  
47
48   $self->transaction_type( $actions{ lc($content{'action'}) } 
49                            + $types{ lc($content{'type'  }) }    );
50
51   #$self->content(%content);
52 }
53
54 sub revmap_fields {
55     my($self, %map) = @_;
56     my %content = $self->content();
57     foreach(keys %map) {
58         $content{$_} = ref($map{$_})
59                          ? ${ $map{$_} }
60                          : $content{$map{$_}};
61     }
62     $self->content(%content);
63 }
64
65 sub submit {
66   my $self = shift;
67   $self->map_fields();
68
69   my %content = $self->content();
70
71   $self->revmap_fields( 
72     'pg_merchant_id'                   => 'login',
73     'pg_password'                      => 'password',
74     'pg_transaction_type'              => \($self->transaction_type()),
75     #'pg_merchant_data_1'
76     #...
77     #'pg_merchant_data_9'
78     'pg_total_amount'                  => 'amount',
79     #'pg_sales_tax_amount'
80     'pg_consumer_id'                   => 'customer_id',
81     'ecom_consumerorderid'             => 'invoice_number', #???
82     #'ecom_walletid'                    =>
83     'pg_billto_postal_name_company'    => 'company', #????
84     'ecom_billto_postal_name_first'    => 'first_name', #????
85     'ecom_billto_postal_name_last'     => 'last_name', # ????
86     'ecom_billto_postal_street_line1'  => 'address',
87     #'ecom_billto_postal_street_line2'
88     'ecom_billto_postal_city'          => 'city',
89     'ecom_billto_postal_stateprov'     => 'state',
90     'ecom_billto_postal_postalcode'    => 'zip',
91     'ecom_billto_postal_countrycode'   => 'country',
92     'ecom_billto_telecom_phone_number' => 'phone',
93     'ecom_billto_online_email'         => 'email',
94     #'pg_billto_ssn'
95     #'pg_billto_dl_number'
96     #'pg_billto_dl_state'
97     'ecom_payment_check_trn'           => 'routing_code',
98     'ecom_payment_check_account'       => 'account_number',
99     'ecom_payment_check_account_type'  => \'C', #checking
100     #'ecom_payment_check_checkno'       =>
101   );
102
103   # name (first_name & last_name ) ?
104   # fax
105
106   # card_number exp_date
107
108   #account_number routing_code bank_name
109
110   my @fields = qw( pg_merchant_id pg_password pg_transaction_type ),
111                ( map { "pg_merchant_$_" } (1..9) ),
112                qw( pg_total_amount pg_sales_tax_amount pg_consumer_id
113                    ecom_consumerorderid ecom_walletid
114                    pg_billto_postal_name_company
115                    ecom_billto_postal_name_first ecom_billto_postal_name_last
116                    ecom_billto_postal_street_line1
117                    ecom_billto_postal_street_line2
118                    ecom_billto_postal_city ecom_billto_postal_stateprov
119                    ecom_billto_postal_postalcode ecom_billto_postal_countrycode
120                    ecom_billto_telecom_phone_number ecom_billto_online_email
121                    pg_billto_ssn pg_billto_dl_number pg_billto_dl_state
122                );
123
124   if ( $content{'type'} =~ /^check$/i ) {
125     push @fields, qw( ecom_payment_check_trn
126                       ecom_payment_check_account
127                       ecom_payment_check_account_type );
128   } else {
129     croak $content{$type}. ' not (yet) supported';
130   }
131
132   my $request = join("\n", map { "$_=$content{$_}" } @fields ). "\n";
133
134   my $reply = sslcat( $self->server(), $self->port(), $request );
135
136   my %response = map { /^(\w+)=(.*)$/
137                          or warn "can't parse response line: $_";
138                        ($1, $2);
139                      } split(/\n/, $reply);
140
141   if ( $response{'pg_response_type'} eq 'A' ) {
142     $self->is_success(1);
143     $self->response_code($response{'pg_response_code'});
144     $self->authorization($response{'pg_authorization_code'});
145   } else {
146     $self->is_success(0);
147     $self->response_code($response{'pg_response_code'});
148     $self->error_message($response{'pg_response_description'});
149   }
150 }
151
152 1;
153
154 #pod goes here
155