made event handlers for cart/sku

created baseline emsbadge and emsticket
added completePurchase() and denyPurchase() utility methods for transaction
This commit is contained in:
JT Smith 2008-03-19 20:18:00 +00:00
parent f3fd67378f
commit 2a2e683dd9
9 changed files with 884 additions and 170 deletions

View file

@ -101,7 +101,7 @@ sub create {
=head2 delete ()
Deletes this cart and all cartItems contained in it.
Deletes this cart and removes all cartItems contained in it. Also see onCompletePurchase() and empty().
=cut
@ -117,7 +117,7 @@ sub delete {
=head2 empty ()
Removes all items from this cart.
Removes all items from this cart. Also see onCompletePurchase() and delete().
=cut
@ -322,6 +322,23 @@ sub new {
#-------------------------------------------------------------------
=head2 onCompletePurchase ()
Calls onCompletePurchase() on all the items in the cart. Then deletes all the items in the cart without calling $item->remove() on them which would affect inventory levels. See also delete() and empty().
=cut
sub onCompletePurchase {
my $self = shift;
foreach my $item (@{$self->getItems}) {
$item->getSku->completePurchase($item);
$item->delete;
}
$self->delete;
}
#-------------------------------------------------------------------
=head2 update ( properties )
Sets properties in the cart.

View file

@ -65,12 +65,27 @@ sub create {
$cart->session->db->write('insert into cartItem (quantity, cartId, assetId, itemId, dateAdded) values (1,?,?,?,now())', [$cart->getId, $sku->getId, $itemId]);
my $self = $class->new($cart, $itemId);
$self->update({asset=>$sku});
$sku->adjustQuantityAvailable(-1);
$sku->onAdjustQuantityInCart($self, 1);
return $self;
}
#-------------------------------------------------------------------
=head2 delete ( )
Removes this item from the cart without calling $sku->onRemoveFromCart which would adjust inventory levels. See also remove().
=cut
sub delete {
my $self = shift;
$self->cart->session->db->deleteRow("cartItem","itemId",$self->getId);
undef $self;
return undef;
}
#-------------------------------------------------------------------
=head2 get ( [ property ] )
Returns a duplicated hash reference of this objects data.
@ -204,15 +219,14 @@ sub new {
=head2 remove ( )
Removes this item from the cart.
Removes this item from the cart and calls $sku->onRemoveFromCart. See also delete().
=cut
sub remove {
my $self = shift;
$self->cart->session->db->deleteRow("cartItem","itemId",$self->getId);
undef $self;
return undef;
$self->getSku->onRemoveFromCart($self);
return $self->delete;
}
@ -229,18 +243,18 @@ The number to set the quantity to. Zero or less will remove the item from cart.
=cut
sub setQuantity {
my ($self, $quantity) = @_;
my ($self, $newQuantity) = @_;
my $id = id $self;
my $currentQuantity = $self->get("quantity");
if ($quantity > $self->getSku->getMaxAllowedInCart) {
if ($newQuantity > $self->getSku->getMaxAllowedInCart) {
WebGUI::Error::Shop::MaxOfItemInCartReached->throw(error=>"Cannot have that many of this item in cart.");
}
if ($quantity <= 0) {
if ($newQuantity <= 0) {
return $self->remove;
}
$properties{$id}{quantity} = $quantity;
$self->getSku->adjustQuantityAvailable($currentQuantity + $quantity);
$properties{$id}{quantity} = $newQuantity;
$self->cart->session->db->setRow("cartItem","itemId", $properties{$id});
$self->getSku->onAdjustQuantityInCart($self, $newQuantity - $currentQuantity);
}
#-------------------------------------------------------------------

View file

@ -26,6 +26,17 @@ This package keeps records of every puchase made.
use WebGUI::Shop::Transaction;
my $transaction = WebGUI::Shop::Transaction->new($session, $id);
# typical transaction goes like this:
my $transaction = WebGUI::Shop::Transaction->create({ cart=>$cart, paymentMethod=>$paymentMethod, paymentAddress=>$address});
my ($transactionNumber, $status, $message) = $paymentMethod->tryTransaction;
if ($status eq "somekindofsuccess") {
$transaction->completePurchase($cart, $transactionNumber, $status, $message);
}
else {
$transaction->denyPurchase($transactionNumber, $status, $message);
}
=head1 METHODS
@ -56,6 +67,41 @@ sub addItem {
#-------------------------------------------------------------------
=head2 completePurchase ( cart, transactionCode, statusCode, statusMessage )
See also denyPurchase(). Completes a purchase by updating the transaction as a success, and clearing the cart of it's items.
=head3 cart
A reference to the current cart that's full of items just purchased.
=head3 transactionCode
The transaction id or code given by the payment gateway.
=head3 statusCode
The status code that came back from the payment gateway when trying to process the payment.
=head3 statusMessage
The extended status message that came back from the payment gateway when trying to process the payment.
=cut
sub completePurchase {
my ($self, $cart, $transactionCode, $statusCode, $statusMessage) = @_;
$cart->completePurchase;
$self->update({
transactionCode => $transactionCode,
isSuccessful => 1,
statusCode => $statusCode,
statusMessage => $statusMessage,
});
}
#-------------------------------------------------------------------
=head2 create ( session, properties )
Constructor. Creates a new transaction object. Returns a reference to the object.
@ -103,6 +149,36 @@ sub delete {
#-------------------------------------------------------------------
=head2 denyPurchase ( transactionCode, statusCode, statusMessage )
Completes a purchase as a failure. It could be that the user didn't enter their credit cart correctly, or they may have insufficient funds.
=head3 transactionCode
The transaction id or code given by the payment gateway.
=head3 statusCode
The status code that came back from the payment gateway when trying to process the payment.
=head3 statusMessage
The extended status message that came back from the payment gateway when trying to process the payment.
=cut
sub denyPurchase {
my ($self, $transactionCode, $statusCode, $statusMessage) = @_;
$self->update({
isSuccessful => 0,
transactionCode => $transactionCode,
statusCode => $statusCode,
statusMessage => $statusMessage
});
}
#-------------------------------------------------------------------
=head2 formatCurrency ( amount )
Formats a number as a float with two digits after the decimal like 0.00.