1 # --------------------------------------------------------------------
2 # Copyright (C) 2008 Niles Ingalls
3 # Niles Ingalls <nilesi@zionsville.lib.in.us>
4 # Bill Erickson <erickson@esilibrary.com>
5 # Joe Atzberger <jatzberger@esilibrary.com>
6 # Lebbeous Fogle-Weekley <lebbeous@esilibrary.com>
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License
10 # as published by the Free Software Foundation; either version 2
11 # of the License, or (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 # --------------------------------------------------------------------
18 package OpenILS::Application::Circ::CreditCard;
19 use base qw/OpenSRF::Application/;
20 use strict; use warnings;
22 use Business::CreditCard;
23 use Business::OnlinePayment;
24 use UUID::Tiny qw/:std/;
28 use OpenSRF::Utils::Logger qw/:logger/;
29 use OpenILS::Utils::CStoreEditor qw/:funcs/;
30 use OpenILS::Application::AppUtils;
31 my $U = "OpenILS::Application::AppUtils";
33 # Given the argshash from process_payment(), this helper function just finds
34 # a function in the current namespace named "bop_args_{processor}" and calls
35 # it with $argshash as an argument, returning the result, or returning an
36 # empty hash if it can't find such a function.
37 sub get_bop_args_filler {
41 my $funcname = "bop_args_" . $argshash->{processor};
42 return &{$funcname}($argshash) if defined &{$funcname};
46 # Provide default arguments for calls using the AuthorizeNet processor
47 sub bop_args_AuthorizeNet {
49 if ($argshash->{server}) {
51 # One might provide "test.authorize.net" here.
52 Server => $argshash->{server},
60 # Provide default arguments for calls using the PayPal processor
64 Username => $argshash->{login},
65 Password => $argshash->{password},
66 Signature => $argshash->{signature}
70 # Provide default arguments for calls using the PayflowPro processor
71 sub bop_args_PayflowPro {
74 "vendor" => $argshash->{vendor},
75 "partner" => $argshash->{partner} || "PayPal" # reasonable default?
79 # argshash (Hash of arguments with these keys):
80 # patron_id: Not a barcode, but a patron's internal ID
81 # ou: Org unit where transaction happens
82 # processor: Payment processor to use
83 # (AuthorizeNet/PayPal/PayflowPro)
84 # cc: credit card number
85 # cvv2: 3 or 4 digits from back of card
86 # amount: transaction value
87 # action: optional (default: Normal Authorization)
88 # first_name: optional (default: patron's first_given_name field)
89 # last_name: optional (default: patron's family_name field)
90 # address: optional (default: patron's street1 field + street2)
91 # city: optional (default: patron's city field)
92 # state: optional (default: patron's state field)
93 # zip: optional (default: patron's zip field)
94 # country: optional (some processor APIs: 2 letter code.)
95 # description: optional
100 # Confirm some required arguments.
101 return OpenILS::Event->new('BAD_PARAMS')
104 and $argshash->{amount}
105 and $argshash->{expiration}
108 # Used to test argshash->{processor} here, but now that's handled earlier.
110 # At least the following (derived from org unit settings) are required.
111 return OpenILS::Event->new('CREDIT_PROCESSOR_BAD_PARAMS')
112 unless $argshash->{login}
113 and $argshash->{password};
115 # A valid patron_id is also required.
116 my $e = new_editor();
117 my $patron = $e->retrieve_actor_user(
119 $argshash->{patron_id},
122 flesh_fields => { au => ["mailing_address", "card"] }
125 ) or return $e->event;
127 return dispatch($argshash, $patron);
130 sub prepare_bop_content {
131 my ($argshash, $patron, $cardtype) = @_;
148 if (exists $argshash->{$_}) {
149 $content{$_} = $argshash->{$_};
153 $content{action} = $argshash->{action} || "Normal Authorization";
154 $content{type} = $cardtype; #'American Express', 'VISA', 'MasterCard'
155 $content{card_number} = $argshash->{cc};
156 $content{customer_id} = $patron->id;
158 $content{first_name} ||= $patron->first_given_name;
159 $content{last_name} ||= $patron->family_name;
161 $content{FirstName} = $content{first_name}; # kludge mcugly for PP
162 $content{LastName} = $content{last_name};
164 # makes patron barcode accessible in CC payment records
165 my $bc = ($patron->card) ? $patron->card->barcode : '';
166 $content{description} = "$bc " . ($content{description} || '');
168 # Especially for the following fields, do we need to support different
169 # mapping of fields for different payment processors, particularly ones
170 # in other countries?
171 if(!$content{address}) {
172 $content{address} = $patron->mailing_address->street1;
173 $content{address} .= ", " . $patron->mailing_address->street2
174 if $patron->mailing_address->street2;
177 $content{city} ||= $patron->mailing_address->city;
178 $content{state} ||= $patron->mailing_address->state;
179 $content{zip} ||= $patron->mailing_address->post_code;
180 $content{country} ||= $patron->mailing_address->country;
182 # Yet another fantastic kludge. country2code() comes from Locale::Country.
183 # PayPal must have 2 letter country field (ISO 3166) that's uppercase.
184 if (length($content{country}) > 2 && $argshash->{processor} eq 'PayPal') {
185 $content{country} = uc country2code($content{country});
186 } elsif($argshash->{processor} eq "PayflowPro") {
187 ($content{request_id} = create_uuid_as_string(UUID_V4)) =~ s/-//;
194 my ($argshash, $patron) = @_;
196 # The validate() sub is exported by Business::CreditCard.
197 if (!validate($argshash->{cc})) {
198 # Although it might help a troubleshooter, it's probably not a good
199 # idea to put the credit card number in the log file.
200 $logger->info("Credit card number invalid");
202 return new OpenILS::Event("CREDIT_PROCESSOR_INVALID_CC_NUMBER");
205 # cardtype() also comes from Business::CreditCard. It is not certain that
206 # a) the card type returned by this method will be suitable input for
207 # a payment processor, nor that
208 # b) it is even necessary to supply this argument to processors in all
209 # cases. Testing this with several processors would be a good idea.
210 (my $cardtype = cardtype($argshash->{cc})) =~ s/ card//i;
212 if (lc($cardtype) eq "unknown") {
213 $logger->info("Credit card number passed validate(), " .
214 "yet cardtype() returned $cardtype");
215 return new OpenILS::Event(
216 "CREDIT_PROCESSOR_INVALID_CC_NUMBER", "note" => "cardtype $cardtype"
221 "applying payment via processor '" . $argshash->{processor} . "'"
224 # Find B:OP constructor arguments specific to our payment processor.
225 my %bop_args = get_bop_args_filler($argshash);
227 # We're assuming that all B:OP processors accept this argument to the
229 $bop_args{test_transaction} = $argshash->{testmode};
231 my $transaction = new Business::OnlinePayment(
232 $argshash->{processor}, %bop_args
235 my %content = prepare_bop_content($argshash, $patron, $cardtype);
236 $transaction->content(%content);
238 # submit() does not return a value, although crashing is possible here
239 # with some bad input depending on the payment processor.
240 $transaction->submit;
243 "processor" => $argshash->{"processor"}, "card_type" => $cardtype
246 # Put the values of any of these fields into the event payload, if present.
247 foreach (qw/authorization correlationid avs_code request_id
248 server_response cvv2_response cvv2_code error_message order_number/) {
249 $payload->{$_} = $transaction->$_ if $transaction->can($_);
254 if ($transaction->is_success) {
255 $logger->info($argshash->{processor} . " payment succeeded");
256 $event_name = "SUCCESS";
258 $logger->info($argshash->{processor} . " payment failed");
259 $event_name = "CREDIT_PROCESSOR_DECLINED_TRANSACTION";
262 return new OpenILS::Event($event_name, "payload" => $payload);