double =cut POD fix from Mike Heins
[Vend-Payment-BusinessOnlinePayment.git] / BusinessOnlinePayment.pm
1 # Vend::Payment::BusinessOnlinePayment - Interchange wrapper for
2 #                                        Business::OnlinePayment modules
3 #
4 # Copyright (C) 2004 Ivan Kohler.  All rights reserved.
5 #
6 # This program is free software; you can redistribute it and/or modify it
7 # under the same terms as Perl itself.
8 #
9 # Ivan Kohler <ivan-interchange@420.am>
10
11 package Vend::Payment::BusinessOnlinePayment;
12
13 =head1 NAME
14
15 Vend::Payment::BusinessOnlinePayment - Interchange wrapper for Business::OnlinePayment
16
17 =head1 SYNOPSIS
18
19     &charge=onlinepayment
20
21         or
22
23     [charge mode=onlinepayment param1=value1 param2=value2]
24
25 =head1 PREREQUISITES
26
27   Business::OnlinePayment
28   Business::OnlinePayment:: gateway module
29
30   See L<http://www.420.am/business-onlinepayment/> or 
31   L<http://search.cpan.org/search?query=Business%3A%3AOnlinePayment&mode=module>
32
33 =head1 DESCRIPTION
34
35 This is a wrapper around Business::OnlinePayment for Interchange.
36
37 The Vend::Payment::BusinessOnlinePayment module implements the onlinepayment()
38 routine for use with Interchange.  It is compatible on a call level with the
39 other Interchange payment modules.  In theory (and even usually in practice)
40 you could switch from another gateway to a Business::OnlinePayment supported
41 gateway (or between different Business::OnlinePayment gateways) with a few
42 configuration file changes.
43
44 Business::OnlinePayment is a set of related Perl modules for processing online
45 payments (credit cards, electronic checks, and other payment systems).  It
46 provides a consistant interface for processing online payments, regardless of
47 the gateway backend being used, in the same way that DBI provides an consistant
48 interface to different databases.
49
50 See L<http://www.420.am/business-onlinepayment/> for more information and
51 supported gateways.
52
53 It is hoped that a future version of Interchange will do all credit card
54 processing through Business::OnlinePayment, but this is my no means
55 guaranteed and the timeframe is unknown.  Think ALSA somewhere around
56 Linux 2.2 and you've got the general idea.
57
58 Currently this module is recommended for people with gateway processors
59 unsupported by a native Interchange Vend::Payment:: module and for the
60 adventurous.
61
62 =head1 USAGE
63
64 To enable this module, place this directive in C<interchange.cfg>:
65
66     Require module Vend::Payment::BusinessOnlinePayment
67
68 This I<must> be in interchange.cfg or a file included from it.
69
70 The mode can be named anything, but the C<gateway> parameter must be set
71 to C<onlinepayment>. To make it the default payment gateway for all credit
72 card transactions in a specific catalog, you can set in C<catalog.cfg>:
73
74     Variable   MV_PAYMENT_MODE  onlinepayment
75
76 It uses several of the standard settings from Interchange payment. Any time
77 we speak of a setting, it is obtained either first from the tag/call options,
78 then from an Interchange order Route named for the mode, then finally a
79 default global payment variable, For example, the C<setting> parameter would
80 be specified by:
81
82     [charge mode=onlinepayment setting=value]
83
84 or
85
86     Route onlinepayment setting value
87
88 or 
89
90     Variable MV_PAYMENT_SETTING      value
91
92 The following settings are available:
93
94 =over 4
95
96 =item processor
97
98 Your Business::OnlinePayment processor.
99
100 =item id
101
102 Your Business::OnlinePayment login.
103
104 =item secret
105
106 Your Busienss::OnlinePayment password.
107
108 =item transaction
109
110 The type of transaction to be run. Valid values are:
111
112     Interchange         Business::OnlinePayment
113     ----------------    -----------------------
114         auth            Authorization Only
115         return          Credit
116         reverse
117         sale            Normal Authorization
118         settle          Post Authorization
119         void            Void
120
121 =item test
122
123 Set this true if you wish to operate in test mode.  Make sure to verify
124 that your specific Business::OnlinePayment:: gateway module supports a
125 test mode.
126
127 =back
128
129 In addition, any other processor options are passed to your gateway.  See
130 the documentation for your specific Business::OnlinePayment:: gateway module
131 for details on what options are required, if any.
132
133 =head1 AUTHOR
134
135 Ivan Kohler <ivan-interchange@420.am>
136
137 Initial development of this module was sponsored in part by Simply Marketing,
138 Inc. <http://www.simplymarketinginc.com/>.
139
140 =head1 COPYRIGHT
141
142 Copyright 2004 Ivan Kohler.  All rights reserved.
143
144 This program is free software; you can redistribute it and/or modify it
145 under the same terms as Perl itself.
146
147 =cut
148
149 package Vend::Payment;
150 use strict;
151 use vars qw( $VERSION );
152 use Business::OnlinePayment;
153
154 $VERSION = '0.01';
155
156 my $default_avs = q{You must enter the correct billing address of your credit }.
157                   q{card.  The bank returned the following error: %s};
158
159 my $default_declined = "error: %s. Please call in your order or try again.";
160
161 sub onlinepayment {
162   my ($user, $amount) = @_;
163
164   my $opt = {};
165   my $secret;
166
167   if ( ref($user) ) {
168     $opt = $user;
169     $user = $opt->{id};
170     $secret = $opt->{secret};
171   }
172
173   my $actual = $opt->{actual} || { map_actual() };
174
175   $user ||= charge_param('id')
176     or return ( MStatus => 'failure-hard',
177                 MErrMsg => errmsg('No account id'),
178               );
179
180   $secret ||= charge_param('secret');
181
182   my $precision = $opt->{precision} || 2;
183
184   my $referer = $opt->{referer} || charge_param('referer');
185
186   $actual->{$_} = $opt->{$_}
187     foreach grep defined($opt->{$_}), qw(
188       order_id
189       auth_code
190       mv_credit_card_exp_month
191       mv_credit_card_exp_year
192       mv_credit_card_number
193     );
194
195   $actual->{mv_credit_card_exp_month} =~ s/\D//g;
196   $actual->{mv_credit_card_exp_year} =~ s/\D//g;
197   $actual->{mv_credit_card_exp_year} =~ s/\d\d(\d\d)/$1/;
198   $actual->{mv_credit_card_number} =~ s/\D//g;
199
200   my $exp = sprintf '%02d/%02d', $actual->{mv_credit_card_exp_month},
201                                  $actual->{mv_credit_card_exp_year};
202
203   my %ic2bop = (
204     'auth'    =>  'Authorization Only',
205     'return'  =>  'Credit',
206     #'reverse' =>
207     'sale'    =>  'Normal Authorization',
208     'settle'  =>  'Post Authorization',
209     'void'    =>  'Void',
210   );
211
212   my $action = $ic2bop{$opt->{transaction} || 'sale'};
213
214   $amount = $opt->{total_cost} if $opt->{total_cost};
215         
216   if ( ! $amount ) {
217     $amount = Vend::Interpolate::total_cost();
218     $amount = Vend::Util::round_to_frac_digits($amount, $precision);
219   }
220
221   my $order_id = gen_order_id($opt);
222
223   my $processor = charge_param('processor');
224
225   #processor options!
226   my %ignore = map { $_=>1 } qw(gateway processor id secret transaction );
227   my %options = map  { $_=>1 }
228                 grep { !$ignore{$_} } (
229                                         keys(%$opt),
230                                         map { s/^MV_PAYMENT_//; lc($_); }
231                                           grep { /^MV_PAYMENT_/ }
232                                             keys %$main::Variable
233                                       );
234
235   my $transaction =
236     new Business::OnlinePayment ( $processor, %options );
237
238   $transaction->test_transaction($opt->{test} || charge_param('test'));
239
240   $actual->{$_} =~ s/[\n\r]//g foreach keys %$actual;
241
242   $transaction->content(
243     'type'            => 'CC',
244     'login'           => $user,
245     'password'        => $secret,
246     'action'          => $action,
247     #'description'
248     'amount'          => $amount,
249     'card_number'     => $actual->{mv_credit_card_number},
250     'expiration'      => $exp,
251     'cvv2'            => $actual->{cvv2},
252     'order_number'    => $actual->{order_id},
253     'auth_code'       => $actual->{auth_code},
254     #'recurring_billing'
255     'invoice_number'  => $actual->{mv_order_number},
256     #'customer_id'
257     'last_name'       => $actual->{b_lname},
258     'first_name'      => $actual->{b_fname},
259     'name'            => $actual->{b_fname}. ' '. $actual->{b_lname},
260     'company'         => $actual->{b_company},
261     'address'         => $actual->{b_address},
262     'city'            => $actual->{b_city},
263     'state'           => $actual->{b_state},
264     'zip'             => $actual->{b_zip},
265     'country'         => $actual->{b_country},
266     'ship_last_name'  => $actual->{lname},
267     'ship_first_name' => $actual->{fname},
268     'ship_name'       => $actual->{fname}. ' '. $actual->{lname},
269     'ship_company'    => $actual->{company},
270     'ship_address'    => $actual->{address},
271     'ship_city'       => $actual->{city},
272     'ship_state'      => $actual->{state},
273     'ship_zip'        => $actual->{zip},
274     'ship_country'    => $actual->{country},
275     'referer'         => $referer,
276     'email'           => $actual->{email},
277     'phone'           => $actual->{phone_day},
278   );
279
280   $transaction->submit();
281
282   my %result;
283   if ( $transaction->is_success() ) {
284
285     $result{MStatus} = 'success';
286     $result{'order-id'} = $transaction->order_number || $opt->{'order_id'};
287
288   } else {
289
290     $result{MStatus} = 'failure';
291     delete $result{'order-id'};
292
293     if ( 0 ) { #need a standard Business::OnlinePayment way to ask about AVS
294       $result{MErrMsg} = errmsg(
295         $opt->{'message_avs'} || $default_avs,
296         $transaction->error_message
297       )
298     } else {
299       $result{MErrMsg} = errmsg(
300         $opt->{'message_declined'} || "$processor $default_declined",
301          $transaction->error_message
302       );
303     }
304
305   }
306
307   return %result;
308
309 }
310
311 1;
312