From 8b8974c368f49fd857cc0daae312d9a4f1b03096 Mon Sep 17 00:00:00 2001 From: Martin Kamerbeek Date: Mon, 24 Mar 2008 21:44:31 +0000 Subject: [PATCH] Added check for cart completeness and recurring payments. --- lib/WebGUI/Shop/Cart.pm | 46 +++++++++++++++++++++++++++++++ lib/WebGUI/Shop/Pay.pm | 23 ++++++++++++---- lib/WebGUI/Shop/PayDriver/Cash.pm | 30 +++++++++++++++++++- 3 files changed, 92 insertions(+), 7 deletions(-) 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 .= ''; foreach my $payOption ( values %{$self->getOptions( $cart )} ) { diff --git a/lib/WebGUI/Shop/PayDriver/Cash.pm b/lib/WebGUI/Shop/PayDriver/Cash.pm index 09264064a..44f3f0f5f 100644 --- a/lib/WebGUI/Shop/PayDriver/Cash.pm +++ b/lib/WebGUI/Shop/PayDriver/Cash.pm @@ -7,6 +7,26 @@ use WebGUI::Exception; use base qw/WebGUI::Shop::PayDriver/; +#------------------------------------------------------------------- +sub canCheckoutCart { + my $self = shift; + my $cart = $self->getCart; + + return 0 unless $cart->readyForCheckout; + return 0 if $cart->requiresRecurringPayment; + + return 1; +} + +#------------------------------------------------------------------- +sub credentialsOkay { + my $self = shift; + + return 0 unless $self->getBillingAddress; + + return 1; +} + #------------------------------------------------------------------- sub definition { @@ -92,12 +112,14 @@ sub getCartTemplateVariables { my $sku = $item->getSku; $sku->applyOptions( $item->get('options') ); + # Item properties my $itemProperties = $item->get; $itemProperties->{ itemName } = $sku->get('title'); $itemProperties->{ itemUrl } = $sku->getUrl; $itemProperties->{ itemPrice } = $cart->formatCurrency( $sku->getPrice ); $itemProperties->{ totalItemPrice } = $cart->formatCurrency( $sku->getPrice * $item->get('quantity') ); + # Custom item shipping address my $address = eval { $item->getShippingAddress }; $itemProperties->{ itemShippingAddres } = $address->getHtmlFormatted unless (WebGUI::Error->caught); @@ -185,7 +207,11 @@ sub www_pay { my $i18n = WebGUI::International->new($session, 'PayDriver_Cash'); my $var; - my $billingAddress = $self->getBillingAddress( $session->scratch->get( 'ShopPayDriverCash_billingAddressId' ) ); + # Make sure we can checkout the cart + return "" unless $self->canCheckoutCart; + + # Make sure all required credentials have been supplied + return $self->www_getCredentials unless $self->credentialsOkay; # Generate a receipt and send it if enabled. if ( $self->get('sendReceipt') ) { @@ -207,6 +233,8 @@ sub www_pay { $receipt->queue; } + my $billingAddress = $self->getBillingAddress( $session->scratch->get( 'ShopPayDriverCash_billingAddressId' ) ); + # Create a transaction and complete the purchase my $transaction = WebGUI::Shop::Transaction->create( $session, { cart => $cart,