diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index 98b05d6df..ac7b8b715 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -18,6 +18,7 @@ - fixed: Bad International macro calls in Gallery Template - fixed: more i18n (Shop tags) - fixed: i18nagain + - fixed: Cash available as a payment option to everyone 7.5.17 - fixed: Payment Methods Hover Help Incomplete diff --git a/lib/WebGUI/Shop/Pay.pm b/lib/WebGUI/Shop/Pay.pm index ce25870cb..97b4f78de 100644 --- a/lib/WebGUI/Shop/Pay.pm +++ b/lib/WebGUI/Shop/Pay.pm @@ -91,7 +91,10 @@ sub getDrivers { =head2 getOptions ( $cart ) -Returns a list of options for the user to pay to. It is a hash of hashrefs, with the key of the primary hash being the paymentGatewayId of the driver, and sub keys of label and button. +Returns a set of options for the user to pay to. It is a hash of +hashrefs, with the key of the primary hash being the paymentGatewayId +of the driver, and sub keys of label and button. The hash will only +contain payment gateways that this user is allowed to use. =head3 $cart @@ -110,6 +113,7 @@ sub getOptions { my %options = (); foreach my $gateway (@{ $self->getPaymentGateways() }) { + next unless $gateway->canUse; if (!$recurringRequired || $gateway->handlesRecurring) { $options{$gateway->getId} = { label => $gateway->get("label"), diff --git a/lib/WebGUI/Shop/PayDriver.pm b/lib/WebGUI/Shop/PayDriver.pm index 8819fdd59..6847d0af9 100644 --- a/lib/WebGUI/Shop/PayDriver.pm +++ b/lib/WebGUI/Shop/PayDriver.pm @@ -10,6 +10,7 @@ use WebGUI::Inbox; use WebGUI::International; use WebGUI::HTMLForm; use WebGUI::Macro; +use WebGUI::User; use WebGUI::Shop::Cart; use JSON; @@ -81,10 +82,52 @@ sub cancelRecurringPayment { my $self = shift; my $transaction = shift; WebGUI::Error::OverrideMe->throw(); -} +} #------------------------------------------------------------------- +=head2 canUse ( user ) + +Checks to see if the user can use this Payment Driver. + +=head3 user + +A hashref containing user information. The user referenced will be checked +to see if they can use the Payment Driver. If missing, then $session->user +will be used. + +=head4 userId + +A userId used to build a user object. + +=head4 user + +A user object that will be used directly. + +=cut + +sub canUse { + my $self = shift; + my $user = shift; + my $userObject; + if (!defined $user or ref($user) ne 'HASH') { + $userObject = $self->session->user; + } + else { + if (exists $user->{user}) { + $userObject = $user->{user}; + } + elsif (exists $user->{userId}) { + $userObject = WebGUI::User->new($self->session, $user->{userId}); + } + else { + WebGUI::Error::InvalidParam->throw(error => q{Must provide user information}) + } + } + return $userObject->isInGroup($self->get('groupToUse')); +} + #------------------------------------------------------------------- + =head2 className ( ) Accessor for the className of the object. This is the name of the driver that is used diff --git a/t/Shop/PayDriver.t b/t/Shop/PayDriver.t index a8c4e4e32..70ee008be 100644 --- a/t/Shop/PayDriver.t +++ b/t/Shop/PayDriver.t @@ -31,7 +31,7 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -my $tests = 46; +my $tests = 49; plan tests => 1 + $tests; #---------------------------------------------------------------------------- @@ -459,6 +459,21 @@ cmp_deeply( ); +####################################################################### +# +# canUse +# +####################################################################### + +$session->user({userId => 3}); +ok($driver->canUse, 'canUse: session->user is used if no argument is passed'); +ok(!$driver->canUse({userId => 1}), 'canUse: userId explicit works, visitor cannot use this driver'); + +TODO: { + local $TODO = 'tests for canUse'; + ok(0, 'Test other users and groups'); +} + ####################################################################### # # delete