ffzg/recall_notices.pl: added --interval and --dedup
[koha.git] / opac / opac-account-pay-paypal-return.pl
index d2be6d3..76f6d21 100755 (executable)
 # with Koha; if not, write to the Free Software Foundation, Inc.,
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
-use strict;
-use warnings;
+use Modern::Perl;
 use utf8;
 
 use CGI;
 use HTTP::Request::Common;
 use LWP::UserAgent;
-use URL::Encode qw(url_params_mixed);
+use URI;
 
 use C4::Auth;
 use C4::Output;
 use C4::Accounts;
-use C4::Members;
+use Koha::Acquisition::Currencies;
 use Koha::Database;
+use Koha::Patrons;
 
 my $cgi = new CGI;
 
@@ -49,10 +49,12 @@ my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
     }
 );
 
+my $active_currency = Koha::Acquisition::Currencies->get_active;
+
 my $token    = $cgi->param('token');
 my $payer_id = $cgi->param('PayerID');
 my $amount   = $cgi->param('amount');
-my @accountlines = $cgi->param('accountlines');
+my @accountlines = $cgi->multi_param('accountlines');
 
 my $ua = LWP::UserAgent->new;
 
@@ -75,22 +77,35 @@ my $nvp_params = {
     'PAYERID'                        => $payer_id,
     'TOKEN'                          => $token,
     'PAYMENTREQUEST_0_AMT'           => $amount,
+    'PAYMENTREQUEST_0_CURRENCYCODE'  => $active_currency->currency,
 };
 
 my $response = $ua->request( POST $url, $nvp_params );
 
-my $error;
+my $error = q{};
 if ( $response->is_success ) {
-    my $params = url_params_mixed( $response->decoded_content );
 
-    if ( $params->{ACK} eq "Success" ) {
-        $amount = $params->{PAYMENTINFO_0_AMT};
+    my $urlencoded = $response->content;
+    my %params = URI->new( "?$urlencoded" )->query_form;
+
+
+    if ( $params{ACK} eq "Success" ) {
+        $amount = $params{PAYMENTINFO_0_AMT};
+
+        my $account = Koha::Account->new( { patron_id => $borrowernumber } );
+        my @lines = Koha::Account::Lines->search(
+            {
+                accountlines_id => { -in => \@accountlines }
+            }
+        );
 
-        my $accountlines_rs = Koha::Database->new()->schema()->resultset('Accountline');
-        foreach my $accountlines_id ( @accountlines ) {
-            my $accountline = $accountlines_rs->find( $accountlines_id );
-            makepayment( $accountlines_id, $borrowernumber, undef, $accountline->amountoutstanding, undef, undef, 'PayPal' );
-        }
+        $account->pay(
+            {
+                amount => $amount,
+                lines  => \@lines,
+                note   => 'PayPal'
+            }
+        );
     }
     else {
        $error = "PAYPAL_ERROR_PROCESSING";
@@ -98,11 +113,12 @@ if ( $response->is_success ) {
 
 }
 else {
-    $error => "PAYPAL_UNABLE_TO_CONNECT";
+    $error = "PAYPAL_UNABLE_TO_CONNECT";
 }
 
+my $patron = Koha::Patrons->find( $borrowernumber );
 $template->param(
-    borrower    => GetMemberDetails($borrowernumber),
+    borrower    => $patron->unblessed,
     accountview => 1
 );