From a1a0800de7c69fe5ee414b79e408ceacd4a1c2c3 Mon Sep 17 00:00:00 2001 From: Mark Wells Date: Thu, 25 Apr 2013 14:45:49 -0700 Subject: [PATCH] changes to support PayPal, #22395 --- FS/FS/ClientAPI/Signup.pm | 28 ++-- FS/FS/Conf.pm | 6 +- FS/FS/Schema.pm | 3 +- FS/FS/agent.pm | 20 +-- FS/FS/cust_main.pm | 5 +- FS/FS/cust_main/Billing_Realtime.pm | 4 +- FS/FS/payby.pm | 1 + FS/FS/payinfo_Mixin.pm | 18 +-- FS/FS/payment_gateway.pm | 15 ++- .../cgi/make_thirdparty_payment.html | 2 +- fs_selfservice/FS-SelfService/cgi/myaccount.html | 7 +- .../FS-SelfService/cgi/myaccount_menu.html | 35 +++-- fs_selfservice/FS-SelfService/cgi/selfservice.cgi | 5 +- fs_selfservice/FS-SelfService/cgi/verify.cgi | 17 ++- httemplate/edit/agent_payment_gateway.html | 1 + httemplate/edit/payment_gateway.html | 141 +++++++++++---------- httemplate/edit/process/payment_gateway.html | 1 + 17 files changed, 180 insertions(+), 129 deletions(-) diff --git a/FS/FS/ClientAPI/Signup.pm b/FS/FS/ClientAPI/Signup.pm index 1dbb20bc7..c3beb69ff 100644 --- a/FS/FS/ClientAPI/Signup.pm +++ b/FS/FS/ClientAPI/Signup.pm @@ -946,15 +946,27 @@ sub capture_payment { } my $cust_main = $cust_pay_pending->cust_main; - my $bill_error = - $cust_main->realtime_botpp_capture( $cust_pay_pending, - %{$packet->{data}}, - apply => 1, - ); + if ( $packet->{cancel} ) { + # the user has chosen not to make this payment + # (probably should be a separate API call, but I don't want to duplicate + # all of the above...which should eventually go away) + my $error = $cust_pay_pending->delete; + # don't show any errors related to this; they're not meaningful + warn "error canceling pending payment $paypendingnum: $error\n" if $error; + return { 'error' => '_cancel', + 'session_id' => $cust_pay_pending->session_id }; + } else { + # create the payment + my $bill_error = + $cust_main->realtime_botpp_capture( $cust_pay_pending, + %{$packet->{data}}, + apply => 1, + ); - return { 'error' => ( $bill_error->{bill_error} ? '_decline' : '' ), - %$bill_error, - }; + return { 'error' => ( $bill_error->{bill_error} ? '_decline' : '' ), + %$bill_error, + }; + } } diff --git a/FS/FS/Conf.pm b/FS/FS/Conf.pm index 6a19ff475..8dbc070a0 100644 --- a/FS/FS/Conf.pm +++ b/FS/FS/Conf.pm @@ -2098,7 +2098,7 @@ and customer address. Include units.', 'section' => 'self-service', 'description' => 'Acceptable payment types for the signup server', 'type' => 'selectmultiple', - 'select_enum' => [ qw(CARD DCRD CHEK DCHK LECB PREPAY BILL COMP) ], + 'select_enum' => [ qw(CARD DCRD CHEK DCHK LECB PREPAY PPAL BILL COMP) ], }, { @@ -2472,7 +2472,7 @@ and customer address. Include units.', 'section' => 'billing', 'description' => 'Available payment types.', 'type' => 'selectmultiple', - 'select_enum' => [ qw(CARD DCRD CHEK DCHK LECB BILL CASH WEST MCRD COMP) ], + 'select_enum' => [ qw(CARD DCRD CHEK DCHK LECB BILL CASH WEST MCRD PPAL COMP) ], }, { @@ -2480,7 +2480,7 @@ and customer address. Include units.', 'section' => 'UI', 'description' => 'Default payment type. HIDE disables display of billing information and sets customers to BILL.', 'type' => 'select', - 'select_enum' => [ '', qw(CARD DCRD CHEK DCHK LECB BILL CASH WEST MCRD COMP HIDE) ], + 'select_enum' => [ '', qw(CARD DCRD CHEK DCHK LECB BILL CASH WEST MCRD PPAL COMP HIDE) ], }, { diff --git a/FS/FS/Schema.pm b/FS/FS/Schema.pm index eb73ccbc8..4f395f285 100644 --- a/FS/FS/Schema.pm +++ b/FS/FS/Schema.pm @@ -3267,7 +3267,8 @@ sub tables_hashref { 'gateway_username', 'varchar', 'NULL', $char_d, '', '', 'gateway_password', 'varchar', 'NULL', $char_d, '', '', 'gateway_action', 'varchar', 'NULL', $char_d, '', '', - 'gateway_callback_url', 'varchar', 'NULL', $char_d, '', '', + 'gateway_callback_url', 'varchar', 'NULL', 255, '', '', + 'gateway_cancel_url', 'varchar', 'NULL', 255, '', '', 'disabled', 'char', 'NULL', 1, '', '', ], 'primary_key' => 'gatewaynum', diff --git a/FS/FS/agent.pm b/FS/FS/agent.pm index 3794d3f1d..9b322093b 100644 --- a/FS/FS/agent.pm +++ b/FS/FS/agent.pm @@ -216,7 +216,7 @@ an attempt will be made to select a gateway suited for the taxes paid on the invoice. The I and I options can be used to influence the choice -as well. Presently only 'CC' and 'ECHECK' methods are meaningful. +as well. Presently only 'CC', 'ECHECK', and 'PAYPAL' methods are meaningful. When the I is 'CC' then the card number in I can direct this routine to route to a gateway suited for that type of card. @@ -246,13 +246,17 @@ sub payment_gateway { } #look for an agent gateway override first - my $cardtype; - if ( $options{method} && $options{method} eq 'CC' && $options{payinfo} ) { - $cardtype = cardtype($options{payinfo}); - } elsif ( $options{method} && $options{method} eq 'ECHECK' ) { - $cardtype = 'ACH'; - } else { - $cardtype = $options{method} || ''; + my $cardtype = ''; + if ( $options{method} ) { + if ( $options{method} eq 'CC' && $options{payinfo} ) { + $cardtype = cardtype($options{payinfo}); + } elsif ( $options{method} eq 'ECHECK' ) { + $cardtype = 'ACH'; + } elsif ( $options{method} eq 'PAYPAL' ) { + $cardtype = 'PayPal'; + } else { + $cardtype = $options{method} + } } my $override = diff --git a/FS/FS/cust_main.pm b/FS/FS/cust_main.pm index 2a4602e19..1cf036551 100644 --- a/FS/FS/cust_main.pm +++ b/FS/FS/cust_main.pm @@ -390,7 +390,7 @@ sub insert { $payby = 'PREP' if $amount; - } elsif ( $self->payby =~ /^(CASH|WEST|MCRD)$/ ) { + } elsif ( $self->payby =~ /^(CASH|WEST|MCRD|PPAL)$/ ) { $payby = $1; $self->payby('BILL'); @@ -2021,7 +2021,8 @@ sub check { if ( $self->paydate eq '' || $self->paydate eq '-' ) { return "Expiration date required" - unless $self->payby =~ /^(BILL|PREPAY|CHEK|DCHK|LECB|CASH|WEST|MCRD)$/; + # shouldn't payinfo_check do this? + unless $self->payby =~ /^(BILL|PREPAY|CHEK|DCHK|LECB|CASH|WEST|MCRD|PPAL)$/; $self->paydate(''); } else { my( $m, $y ); diff --git a/FS/FS/cust_main/Billing_Realtime.pm b/FS/FS/cust_main/Billing_Realtime.pm index 804969b16..1caa3e5af 100644 --- a/FS/FS/cust_main/Billing_Realtime.pm +++ b/FS/FS/cust_main/Billing_Realtime.pm @@ -111,7 +111,7 @@ L for supported gateways. Required arguments in the hashref are I, and I -Available methods are: I, I and I +Available methods are: I, I, I, and I Available optional arguments are: I, I, I, I, I, I, I @@ -317,6 +317,7 @@ my %bop_method2payby = ( 'CC' => 'CARD', 'ECHECK' => 'CHEK', 'LEC' => 'LECB', + 'PAYPAL' => 'PPAL', ); sub realtime_bop { @@ -612,6 +613,7 @@ sub realtime_bop { %$bop_content, 'reference' => $cust_pay_pending->paypendingnum, #for now 'callback_url' => $payment_gateway->gateway_callback_url, + 'cancel_url' => $payment_gateway->gateway_cancel_url, 'email' => $email, %content, #after ); diff --git a/FS/FS/payby.pm b/FS/FS/payby.pm index d1961a58d..e223a050f 100644 --- a/FS/FS/payby.pm +++ b/FS/FS/payby.pm @@ -208,6 +208,7 @@ sub longname { 'CARD' => 'CC', 'CHEK' => 'ECHECK', 'MCRD' => 'CC', + 'PPAL' => 'PAYPAL', ); sub payby2bop { diff --git a/FS/FS/payinfo_Mixin.pm b/FS/FS/payinfo_Mixin.pm index 9879a3abd..82632526d 100644 --- a/FS/FS/payinfo_Mixin.pm +++ b/FS/FS/payinfo_Mixin.pm @@ -44,26 +44,18 @@ For Refunds (cust_refund): For Payments (cust_pay): 'CARD' (credit cards), 'CHEK' (electronic check/ACH), 'LECB' (phone bill billing), 'BILL' (billing), 'PREP' (prepaid card), -'CASH' (cash), 'WEST' (Western Union), or 'MCRD' (Manual credit card) +'CASH' (cash), 'WEST' (Western Union), 'MCRD' (Manual credit card), +'PPAL' (PayPal) 'COMP' (free) is depricated as a payment type in cust_pay =cut -# was this supposed to do something? - -#sub payby { -# my($self,$payby) = @_; -# if ( defined($payby) ) { -# $self->setfield('payby', $payby); -# } -# return $self->getfield('payby') -#} - =item payinfo Payment information (payinfo) can be one of the following types: -Card Number, P.O., comp issuer (4-8 lowercase alphanumerics; think username) or prepayment identifier (see L) +Card Number, P.O., comp issuer (4-8 lowercase alphanumerics; think username) +prepayment identifier (see L), PayPal transaction ID =cut @@ -267,6 +259,8 @@ sub payby_payinfo_pretty { 'Western Union'; #. $self->payinfo; } elsif ( $self->payby eq 'MCRD' ) { 'Manual credit card'; #. $self->payinfo; + } elsif ( $self->payby eq 'PPAL' ) { + 'PayPal transaction#' . $self->order_number; } else { $self->payby. ' '. $self->payinfo; } diff --git a/FS/FS/payment_gateway.pm b/FS/FS/payment_gateway.pm index 4a7585e24..e94a62cf4 100644 --- a/FS/FS/payment_gateway.pm +++ b/FS/FS/payment_gateway.pm @@ -41,7 +41,7 @@ currently supported: =item gateway_namespace - Business::OnlinePayment, Business::OnlineThirdPartyPayment, or Business::BatchPayment -=item gateway_module - Business::OnlinePayment:: module name +=item gateway_module - Business::OnlinePayment:: (or other) module name =item gateway_username - payment gateway username @@ -51,6 +51,14 @@ currently supported: =item disabled - Disabled flag, empty or 'Y' +=item gateway_callback_url - For ThirdPartyPayment only, set to the URL that +the user should be redirected to on a successful payment. This will be sent +as a transaction parameter (named "callback_url"). + +=item gateway_cancel_url - For ThirdPartyPayment only, set to the URL that +the user should be redirected to if they cancel the transaction. PayPal +requires this; other gateways ignore it. + =item auto_resolve_status - For BatchPayment only, set to 'approve' to auto-approve unresolved payments after some number of days, 'reject' to auto-decline them, or null to do nothing. @@ -128,6 +136,7 @@ sub check { || $self->ut_textn('gateway_username') || $self->ut_anything('gateway_password') || $self->ut_textn('gateway_callback_url') # a bit too permissive + || $self->ut_textn('gateway_cancel_url') || $self->ut_enum('disabled', [ '', 'Y' ] ) || $self->ut_enum('auto_resolve_status', [ '', 'approve', 'reject' ]) || $self->ut_numbern('auto_resolve_days') @@ -152,8 +161,8 @@ sub check { } # this little kludge mimics FS::CGI::popurl - $self->gateway_callback_url($self->gateway_callback_url. '/') - if ( $self->gateway_callback_url && $self->gateway_callback_url !~ /\/$/ ); + #$self->gateway_callback_url($self->gateway_callback_url. '/') + # if ( $self->gateway_callback_url && $self->gateway_callback_url !~ /\/$/ ); $self->SUPER::check; } diff --git a/fs_selfservice/FS-SelfService/cgi/make_thirdparty_payment.html b/fs_selfservice/FS-SelfService/cgi/make_thirdparty_payment.html index b5b9eea1f..59ee93b00 100755 --- a/fs_selfservice/FS-SelfService/cgi/make_thirdparty_payment.html +++ b/fs_selfservice/FS-SelfService/cgi/make_thirdparty_payment.html @@ -8,7 +8,7 @@ onSubmit="document.OneTrueForm.process.disabled=true"> diff --git a/fs_selfservice/FS-SelfService/cgi/myaccount.html b/fs_selfservice/FS-SelfService/cgi/myaccount.html index 9ab262261..a6352e07a 100644 --- a/fs_selfservice/FS-SelfService/cgi/myaccount.html +++ b/fs_selfservice/FS-SelfService/cgi/myaccount.html @@ -6,18 +6,13 @@ Hello <%= $name %>!

<%= include('small_custview') %>
-<%= unless ( $access_pkgnum ) { - $OUT .= qq!Balance: \$$balance

!; - } - ''; -%> <%= $OUT .= qq! View All Invoices     !; %> <%= if ( $balance > 0 ) { - if (scalar(grep $_, @hide_payment_fields)) { + if (scalar(grep $_, @hide_payment_fields)) { # this sucks $OUT .= qq! Make a payment

!; } else { $OUT .= qq! Make a payment
!; diff --git a/fs_selfservice/FS-SelfService/cgi/myaccount_menu.html b/fs_selfservice/FS-SelfService/cgi/myaccount_menu.html index 4a31b1258..cf719e849 100644 --- a/fs_selfservice/FS-SelfService/cgi/myaccount_menu.html +++ b/fs_selfservice/FS-SelfService/cgi/myaccount_menu.html @@ -23,37 +23,44 @@ unless ( $access_pkgnum ) { url=>'customer_order_pkg', 'indent'=>2 }; } +my %payby_mode; +@payby_mode{@cust_paybys} = @hide_payment_fields; +# $payby_mode{FOO} is true if FOO is thirdparty, false if it's B::OP, +# nonexistent if it's not supported + if ( $balance > 0 ) { #XXXFIXME "enable selfservice prepay features" flag or something, eventually per-pkg or something really fancy - #XXXFIXME still a bit sloppy for multi-gateway of differing namespace - my $i = 0; - while($i < scalar(@cust_paybys)) { last if $cust_paybys[$i] =~ /^CARD/; $i++ } - if ( $cust_paybys[$i] && $cust_paybys[$i] =~ /^CARD/ ) { + if ( exists( $payby_mode{CARD} ) ) { push @menu, { title => 'Recharge my account with a credit card', - url => $hide_payment_fields[$i] + url => $payby_mode{CARD} ? 'make_thirdparty_payment&payby_method=CC' : 'make_payment', indent => 2, } } - $i = 0; - while($i < scalar(@cust_paybys)) { last if $cust_paybys[$i] =~ /^CHEK/; $i++ } - if ( $cust_paybys[$i] && $cust_paybys[$i] =~ /^CHEK/ ) { + if ( exists( $payby_mode{CHEK} ) ) { push @menu, { title => 'Recharge my account with a check', - url => $hide_payment_fields[$i] + url => $payby_mode{CHEK} ? 'make_thirdparty_payment&payby_method=ECHECK' : 'make_ach_payment', indent => 2, } } - push @menu, { title => 'Recharge my account with a prepaid card', - url => 'recharge_prepay', - indent => 2, - } - if grep(/^PREP/, @cust_paybys); + if ( exists( $payby_mode{PREP} ) ) { + push @menu, { title => 'Recharge my account with a prepaid card', + url => 'recharge_prepay', + indent => 2, + } + } + if ( exists( $payby_mode{PPAL} ) ) { + push @menu, { title => 'Recharge my account with PayPal', + url => 'make_thirdparty_payment&payby_method=PAYPAL', + indent => 2, + } + } } push @menu, diff --git a/fs_selfservice/FS-SelfService/cgi/selfservice.cgi b/fs_selfservice/FS-SelfService/cgi/selfservice.cgi index f7fe308cf..40fe98af2 100755 --- a/fs_selfservice/FS-SelfService/cgi/selfservice.cgi +++ b/fs_selfservice/FS-SelfService/cgi/selfservice.cgi @@ -667,12 +667,15 @@ sub make_thirdparty_payment { } sub post_thirdparty_payment { - $cgi->param('payby_method') =~ /^(CC|ECHECK)$/ + $cgi->param('payby_method') =~ /^(CC|ECHECK|PAYPAL)$/ or die "illegal payby method"; my $method = $1; $cgi->param('amount') =~ /^(\d+(\.\d*)?)$/ or die "illegal amount"; my $amount = $1; + # realtime_collect() returns the result from FS::cust_main->realtime_collect + # which returns realtime_bop() + # which returns a hashref of popup_url, collectitems, and reference my $result = realtime_collect( 'session_id' => $session_id, 'method' => $method, diff --git a/fs_selfservice/FS-SelfService/cgi/verify.cgi b/fs_selfservice/FS-SelfService/cgi/verify.cgi index d9346b897..ff209d2f9 100755 --- a/fs_selfservice/FS-SelfService/cgi/verify.cgi +++ b/fs_selfservice/FS-SelfService/cgi/verify.cgi @@ -87,11 +87,14 @@ my $rv = capture_payment( map { $_ => scalar($cgi->param($_)) } $cgi->param }, url => $cgi->self_url, + cancel => ($cgi->param('cancel') ? 1 : 0), ); $error = $rv->{error}; - -if ( $error eq '_decline' ) { + +if ( $error eq '_cancel' ) { + print_okay(%$rv); +} elsif ( $error eq '_decline' ) { print_decline(); } elsif ( $error ) { print_verify(); @@ -133,8 +136,14 @@ sub print_okay { $success_url .= '/signup.cgi?action=success'; } - print $cgi->header( '-expires' => 'now' ), - $success_template->fill_in( HASH => { success_url => $success_url } ); + if ( $param{error} eq '_cancel' ) { + # then the payment was canceled, so don't show a message, just redirect + # (during signup, you really need a separate landing page for this case) + print $cgi->redirect($success_url); + } else { + print $cgi->header( '-expires' => 'now' ), + $success_template->fill_in( HASH => { success_url => $success_url } ); + } } sub success_default { #html to use if you don't specify a success file diff --git a/httemplate/edit/agent_payment_gateway.html b/httemplate/edit/agent_payment_gateway.html index 4a7cedf79..41a9f3e95 100644 --- a/httemplate/edit/agent_payment_gateway.html +++ b/httemplate/edit/agent_payment_gateway.html @@ -34,6 +34,7 @@ for