From: Jason Stephenson Date: Wed, 25 May 2011 19:32:07 +0000 (-0400) Subject: A first attempt at supporting the SIP fee paid message (37/38). X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=e96ad2abb0ea8009b517ca4335598e1260f252ff;p=evergreen%2Fequinox.git A first attempt at supporting the SIP fee paid message (37/38). This code contains stylistic changes suggested by the community, but still needs major structural/logical improvements. Signed-off-by: Jason Stephenson --- diff --git a/Open-ILS/examples/oils_sip.xml.example b/Open-ILS/examples/oils_sip.xml.example index 3111626b32..1cbeac4b7d 100644 --- a/Open-ILS/examples/oils_sip.xml.example +++ b/Open-ILS/examples/oils_sip.xml.example @@ -81,7 +81,7 @@ - + diff --git a/Open-ILS/src/perlmods/lib/OpenILS/SIP.pm b/Open-ILS/src/perlmods/lib/OpenILS/SIP.pm index fe312ffd2f..a309db7c78 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/SIP.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/SIP.pm @@ -14,6 +14,7 @@ use OpenILS::SIP::Transaction; use OpenILS::SIP::Transaction::Checkout; use OpenILS::SIP::Transaction::Checkin; use OpenILS::SIP::Transaction::Renew; +use OpenILS::SIP::Transaction::FeePayment; use OpenSRF::System; use OpenILS::Utils::Fieldmapper; @@ -408,23 +409,34 @@ sub end_patron_session { } -#sub pay_fee { -# my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type, -# $pay_type, $fee_id, $trans_id, $currency) = @_; -# my $trans; -# my $patron; -# -# $trans = new ILS::Transaction::FeePayment; -# -# $patron = new ILS::Patron $patron_id; -# -# $trans->transaction_id($trans_id); -# $trans->patron($patron); -# $trans->ok(1); -# -# return $trans; -#} -# +sub pay_fee { + my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type, + $pay_type, $fee_id, $trans_id, $currency) = @_; + + my $xact = OpenILS::SIP::Transaction::FeePayment->new(authtoken => $self->{authtoken}); + my $patron = $self->find_patron($patron_id); + + if (!$patron) { + $xact->screen_msg("Invalid Patron Barcode '$patron_id'"); + $xact->ok(0); + return $xact; + } + + $xact->patron($patron); + $xact->sip_currency($currency); + $xact->fee_amount($fee_amt); + $xact->sip_fee_type($fee_type); + $xact->transaction_id($trans_id); + # We don't presently use these, but we might in the future. + $xact->patron_password($patron_pwd); + $xact->fee_id($fee_id); + $xact->sip_payment_type($pay_type); + + $xact->do_fee_payment(); + + return $xact; +} + #sub add_hold { # my ($self, $patron_id, $patron_pwd, $item_id, $title_id, # $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_; diff --git a/Open-ILS/src/perlmods/lib/OpenILS/SIP/Transaction.pm b/Open-ILS/src/perlmods/lib/OpenILS/SIP/Transaction.pm index eeb9fafc80..a4813cb147 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/SIP/Transaction.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/SIP/Transaction.pm @@ -18,7 +18,7 @@ my %fields = ( item => undef, desensitize => 0, alert => '', - transation_id => undef, + transaction_id => undef, sip_fee_type => '01', # Other/Unknown fee_amount => undef, sip_currency => 'CAD', diff --git a/Open-ILS/src/perlmods/lib/OpenILS/SIP/Transaction/FeePayment.pm b/Open-ILS/src/perlmods/lib/OpenILS/SIP/Transaction/FeePayment.pm new file mode 100644 index 0000000000..14c5998760 --- /dev/null +++ b/Open-ILS/src/perlmods/lib/OpenILS/SIP/Transaction/FeePayment.pm @@ -0,0 +1,110 @@ +# --------------------------------------------------------------- +# Copyright (C) 2011 Merrimack Valley Library Consortium +# Jason Stephenson + +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# --------------------------------------------------------------- +# +# An object to handle fee payment +# + +package OpenILS::SIP::Transaction::FeePayment; + +use warnings; +use strict; + +use POSIX qw(strftime); + +use OpenILS::SIP; +use OpenILS::SIP::Transaction; +use OpenILS::SIP::Msg qw/:const/; +use Sys::Syslog qw(syslog); + +use OpenILS::Application::AppUtils; +my $U = 'OpenILS::Application::AppUtils'; + + +our @ISA = qw(OpenILS::SIP::Transaction); + +# Most fields are handled by the Transaction superclass +my %fields = ( + sip_payment_type => undef, + fee_id => 0, + patron_password => undef, + ); + +sub new { + my $class = shift; + + my $self = $class->SUPER::new(@_); + + foreach my $element (keys %fields) { + $self->{_permitted}->{$element} = $fields{$element}; + } + + @{$self}{keys %fields} = values %fields; + return bless $self, $class; +} + +sub do_fee_payment { + my $self = shift; + my $results = $U->simplereq('open-ils.actor', 'open-ils.actor.user.transactions.history.have_balance', $self->{authtoken}, $self->patron->internal_id); + if (ref $results eq 'ARRAY') { + syslog('LOG_INFO', scalar @$results . " bills found for " . $self->patron->internal_id); + my $payment = $self->fee_amount; + foreach my $bill (@{$results}) { + syslog('LOG_INFO', 'bill '. $bill->id . ' amount ' . $bill->balance_owed); + if (!$self->transaction_id) { + if ($bill->balance_owed >= $payment) { + $self->pay_bill($bill->id, $payment); + $payment = 0; + } + else { + $self->pay_bill($bill->id, $bill->balance_owed); + $payment -= $bill->balance_owed; + } + last unless ($payment); + } + elsif ($self->transaction_id == $bill->id) { + $self->pay_bill($bill->id, $payment); + $payment = 0; + last; + } + } + if ($payment == $self->fee_amount) { + $self->ok(0); + if (!$self->transaction_id) { + $self->screen_msg("Payment failed: you have no bills."); + } + else { + $self->screen_msg("Payment failed: transaction " + $self->transaction_id + " not found."); + } + } + else { + $self->ok(1); + } + } + else { + $self->ok(0); + $self->screen_msg($results->{descr}); + } +} + +sub pay_bill { + my ($self, $bill, $amount) = @_; + my $user = $self->patron->{user}; + my $r = $U->simplereq('open-ils.circ', 'open-ils.circ.money.payment', $self->{authtoken}, + { payment_type => "cash_payment", userid => $user->id, note => "via SIP2", + payments => [[$bill, $amount ]]}, $user->last_xact_id); +} + + +1;