diff --git a/lib/WebGUI/Shop/Cart.pm b/lib/WebGUI/Shop/Cart.pm index 837fee2e5..cdd92b542 100644 --- a/lib/WebGUI/Shop/Cart.pm +++ b/lib/WebGUI/Shop/Cart.pm @@ -353,6 +353,52 @@ sub onCompletePurchase { #------------------------------------------------------------------- +=head2 readyForCheckout ( ) + +Returns whether all the required properties of the the cart are set. + +=cut + +sub readyForCheckout { + my $self = shift; + + # Check if the shipping address is set and correct + my $address = $self->getShippingAddress; + return 0 if WebGUI::Error->caught; + + # Check if the ship driver is chosen and existant + my $ship = $self->getShipper; + return 0 if WebGUI::Error->caught; + + # Check if the cart has items + return 0 unless scalar @{ $self->getItems }; + + # All checks passed so return true + return 1; +} + +#------------------------------------------------------------------- + +=head2 requiresRecurringPayment ( ) + +Returns whether this cart needs to be checked out with a paydriver that can handle recurring payments. + +=cut + +sub requiresRecurringPayment { + my $self = shift; + + # Look for recurring items in the cart + foreach my $item (@{ $self->getItems }) { + return 1 if $item->getSku->isRecurring; + } + + # No recurring items in cart so return false + return 0; +} + +#------------------------------------------------------------------- + =head2 update ( properties ) Sets properties in the cart. diff --git a/lib/WebGUI/Shop/Pay.pm b/lib/WebGUI/Shop/Pay.pm index 5c4997d81..6ee411ff9 100644 --- a/lib/WebGUI/Shop/Pay.pm +++ b/lib/WebGUI/Shop/Pay.pm @@ -102,17 +102,22 @@ A WebGUI::Shop::Cart object. A WebGUI::Error::InvalidParam exception will be th =cut sub getOptions { - my ($self, $cart) = @_; + my $self = shift; + my $cart = shift; + WebGUI::Error::InvalidParam->throw(error => q{Need a cart.}) unless defined $cart and $cart->isa("WebGUI::Shop::Cart"); my $session = $cart->session; + my $recurringRequired = $cart->requiresRecurringPayment; my %options = (); foreach my $gateway (@{ $self->getPaymentGateways() }) { - $options{$gateway->getId} = { - label => $gateway->get("label"), - button => $gateway->getButton($cart), - }; + if (!$recurringRequired || $gateway->handlesRecurring) { + $options{$gateway->getId} = { + label => $gateway->get("label"), + button => $gateway->getButton( $cart ), + }; + } } return \%options; } @@ -340,7 +345,13 @@ sub www_selectPaymentGateway { ); } - # All the output stuff is just a placeholder until it's templated. + # Check if the cart is ready for checkout + unless ($cart->readyForCheckout) { + $session->http->setRedirect( $session->url->page('shop=cart;method=view') ); + return ''; + } + + # All the output stuff below is just a placeholder until it's templated. my $output .= $i18n->echo('Choose one of the following payment gateways to check out:'); $output .= '