]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/Circ/CreditCard.pm
LP#1322341: Count part holds on records
[Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / Application / Circ / CreditCard.pm
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>
7 #
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.
12 #
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;
21
22 use Business::CreditCard;
23 use Business::OnlinePayment;
24 use UUID::Tiny qw/:std/;
25 use Locale::Country;
26
27 use OpenILS::Event;
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";
32
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 {
38     no strict 'refs';
39
40     my $argshash = shift;
41     my $funcname = "bop_args_" . $argshash->{processor};
42     return &{$funcname}($argshash) if defined &{$funcname};
43     return ();
44 }
45
46 # Provide default arguments for calls using the AuthorizeNet processor
47 sub bop_args_AuthorizeNet {
48     my $argshash = shift;
49     if ($argshash->{server}) {
50         return (
51             # One might provide "test.authorize.net" here.
52             Server => $argshash->{server},
53         );
54     }
55     else {
56         return ();
57     }
58 }
59
60 # Provide default arguments for calls using the PayPal processor
61 sub bop_args_PayPal {
62     my $argshash = shift;
63     return (
64         Username => $argshash->{login},
65         Password => $argshash->{password},
66         Signature => $argshash->{signature}
67     );
68 }
69
70 # Provide default arguments for calls using the PayflowPro processor
71 sub bop_args_PayflowPro {
72     my $argshash = shift;
73     return (
74         "vendor" => $argshash->{vendor},
75         "partner" => $argshash->{partner} || "PayPal" # reasonable default?
76     );
77 }
78
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
96
97 sub process_payment {
98     my ($argshash) = @_;
99
100     # Confirm some required arguments.
101     return OpenILS::Event->new('BAD_PARAMS')
102         unless $argshash
103             and $argshash->{cc}
104             and $argshash->{amount}
105             and $argshash->{expiration}
106             and $argshash->{ou};
107
108     # Used to test argshash->{processor} here, but now that's handled earlier.
109
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};
114
115     # A valid patron_id is also required.
116     my $e = new_editor();
117     my $patron = $e->retrieve_actor_user(
118         [
119             $argshash->{patron_id},
120             {
121                 flesh        => 1,
122                 flesh_fields => { au => ["mailing_address", "card"] }
123             }
124         ]
125     ) or return $e->event;
126
127     return dispatch($argshash, $patron);
128 }
129
130 sub prepare_bop_content {
131     my ($argshash, $patron, $cardtype) = @_;
132
133     my %content;
134     foreach (qw/
135         login
136         password
137         description
138         first_name
139         last_name
140         amount
141         expiration
142         cvv2
143         address
144         city
145         state
146         zip
147         country/) {
148         if (exists $argshash->{$_}) {
149             $content{$_} = $argshash->{$_};
150         }
151     }
152     
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;
157     
158     $content{first_name} ||= $patron->first_given_name;
159     $content{last_name}  ||= $patron->family_name;
160
161     $content{FirstName}    = $content{first_name};   # kludge mcugly for PP
162     $content{LastName}     = $content{last_name};
163
164     # makes patron barcode accessible in CC payment records
165     my $bc = ($patron->card) ? $patron->card->barcode : '';
166     $content{description}  = "$bc " . ($content{description} || '');
167
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;
175     }
176
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;
181
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/-//;
188     }
189
190     %content;
191 }
192
193 sub dispatch {
194     my ($argshash, $patron) = @_;
195     
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");
201
202         return new OpenILS::Event("CREDIT_PROCESSOR_INVALID_CC_NUMBER");
203     }
204
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;
211
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"
217         );
218     }
219
220     $logger->debug(
221         "applying payment via processor '" . $argshash->{processor} . "'"
222     );
223
224     # Find B:OP constructor arguments specific to our payment processor.
225     my %bop_args = get_bop_args_filler($argshash);
226
227     # We're assuming that all B:OP processors accept this argument to the
228     # constructor.
229     $bop_args{test_transaction} = $argshash->{testmode};
230
231     my $transaction = new Business::OnlinePayment(
232         $argshash->{processor}, %bop_args
233     );
234
235     my %content = prepare_bop_content($argshash, $patron, $cardtype);
236     $transaction->content(%content);
237
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;
241
242     my $payload = {
243         "processor" => $argshash->{"processor"}, "card_type" => $cardtype
244     };
245
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($_);
250     }
251
252     my $event_name;
253
254     if ($transaction->is_success) {
255         $logger->info($argshash->{processor} . " payment succeeded");
256         $event_name = "SUCCESS";
257     } else {
258         $logger->info($argshash->{processor} . " payment failed");
259         $event_name = "CREDIT_PROCESSOR_DECLINED_TRANSACTION";
260     }
261
262     return new OpenILS::Event($event_name, "payload" => $payload);
263 }
264
265
266 1;