Added check for cart completeness and recurring payments.

This commit is contained in:
Martin Kamerbeek 2008-03-24 21:44:31 +00:00
parent 283d5a2fed
commit 8b8974c368
3 changed files with 92 additions and 7 deletions

View file

@ -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.

View file

@ -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 .= '<table border="0">';
foreach my $payOption ( values %{$self->getOptions( $cart )} ) {

View file

@ -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,