From 54146e32a5054efe8743757ad8e5652c13d20629 Mon Sep 17 00:00:00 2001 From: JT Smith Date: Fri, 23 May 2008 18:21:51 +0000 Subject: [PATCH] added recurring transaction cancellation handling --- lib/WebGUI/Asset/Sku.pm | 31 +++++++++++++++++++++++++++- lib/WebGUI/Shop/PayDriver.pm | 18 ++++++++++++++++ lib/WebGUI/Shop/Transaction.pm | 31 ++++++++++++++++++++++++++++ lib/WebGUI/i18n/English/Asset_Sku.pm | 6 ++++++ lib/WebGUI/i18n/English/Shop.pm | 6 ++++++ 5 files changed, 91 insertions(+), 1 deletion(-) diff --git a/lib/WebGUI/Asset/Sku.pm b/lib/WebGUI/Asset/Sku.pm index c49b2ed08..bc8562a91 100644 --- a/lib/WebGUI/Asset/Sku.pm +++ b/lib/WebGUI/Asset/Sku.pm @@ -17,6 +17,8 @@ package WebGUI::Asset::Sku; use strict; use Tie::IxHash; use base 'WebGUI::Asset'; +use WebGUI::International; +use WebGUI::Mail::Send; use WebGUI::Shop::Cart; @@ -402,6 +404,32 @@ sub onAdjustQuantityInCart { #------------------------------------------------------------------- +=head2 onCancelRecurring ( item ) + +Called when a user or a store admin stops a recurring payment from recurring. This allows for any accounting work that needs to be accounted for happens. By default sends an email to shop managers to let them know that the recurrence has been stopped. + +=head3 item + +Receives a reference to the WebGUI::Shop::TransactionItem so it can determine things like itemId and quantity if it needs them for book keeping purposes. + +=cut + +sub onCancelRecurring { + my ($self, $item) = @_; + my $session = $self->session; + my $i18n = WebGUI::International->new($session, "Shop"); + my $mail = WebGUI::Mail::Send->new($session, { + toGroup => $self->session->setting->get('groupIdAdminCommerce'), + subject => $i18n->get('shop notice'), + }); + my $message = sprintf $i18n->get('cancel recurring message','Asset_Sku'), $item->transaction->get('orderNumber'), $item->get('configuredTitle'), $item->transaction->get('username'); + $mail->addText($message); + $mail->queue; + return undef; +} + +#------------------------------------------------------------------- + =head2 onCompletePurchase ( item ) Called just after payment has been made. It allows for privileges to be given, or bookkeeping @@ -422,7 +450,7 @@ sub onCompletePurchase { =head2 onRefund ( item ) -Called by a transaction upon issuing a refund for this item. Extend to do extra book keeping or restocking. +Called by a transaction upon issuing a refund for this item. Extend to do extra book keeping or restocking. If this is a recurring item, then onCancelRecurring() will also be called. =head3 item @@ -432,6 +460,7 @@ The WebGUI::Shop::TransactionItem being refunded. sub onRefund { my ($self, $item) = @_; + $self->onCancelRecurring($item); return undef; } diff --git a/lib/WebGUI/Shop/PayDriver.pm b/lib/WebGUI/Shop/PayDriver.pm index 18b407df9..7cb2d2d04 100644 --- a/lib/WebGUI/Shop/PayDriver.pm +++ b/lib/WebGUI/Shop/PayDriver.pm @@ -64,6 +64,24 @@ sub _buildObj { } +#------------------------------------------------------------------- + +=head2 cancelRecurringPayment ( transaction ) + +Cancels a recurring transaction. Returns an array containing ( isSuccess, gatewayStatus, gatewayError). Needs to be overridden by subclasses capable of dealing with recurring payments. + +=head3 transaction + +The instanciated recurring transaction object. + +=cut + +sub cancelRecurringPayment { + my $self = shift; + my $transaction = shift; + WebGUI::Error::OverrideMe->throw(); +} + #------------------------------------------------------------------- =head2 className ( ) diff --git a/lib/WebGUI/Shop/Transaction.pm b/lib/WebGUI/Shop/Transaction.pm index 6ad088809..010a36723 100644 --- a/lib/WebGUI/Shop/Transaction.pm +++ b/lib/WebGUI/Shop/Transaction.pm @@ -13,6 +13,7 @@ use WebGUI::Shop::Admin; use WebGUI::Shop::AddressBook; use WebGUI::Shop::Credit; use WebGUI::Shop::TransactionItem; +use WebGUI::Shop::Pay; =head1 NAME @@ -68,6 +69,21 @@ sub addItem { #------------------------------------------------------------------- +=head2 cancelRecurring ( ) + +Cancel a recurring transaction, and calls onCancelRecurring in whatever sku is attached to this transaction. + +=cut + +sub cancelRecurring { + my ($self) = @_; + $self->getPaymentGateway->cancelRecurringPayment($self); + my ($item) = $self->getItems; + $item->getSku->onCancelRecurring($item); +} + +#------------------------------------------------------------------- + =head2 completePurchase ( transactionCode, statusCode, statusMessage ) See also denyPurchase(). Completes a purchase by updating the transaction as a success, and calling onCompletePurchase on all the skus in the transaction. @@ -321,6 +337,21 @@ sub getItems { return \@itemsObjects; } +#------------------------------------------------------------------- + +=head2 getPaymentGateway () + +Returns a reference to the payment gateway attached to this transaction. + +=cut + +sub getPaymentGateway { + my ($self) = @_; + my $pay = WebGUI::Shop::Pay->new($self->session); + return $pay->getPaymentGateway($self->get('paymentDriverId')); +} + + #------------------------------------------------------------------- =head2 new ( session, transactionId ) diff --git a/lib/WebGUI/i18n/English/Asset_Sku.pm b/lib/WebGUI/i18n/English/Asset_Sku.pm index 999d41d61..18fb4232c 100644 --- a/lib/WebGUI/i18n/English/Asset_Sku.pm +++ b/lib/WebGUI/i18n/English/Asset_Sku.pm @@ -9,6 +9,12 @@ our $I18N = { context => q|a help label| }, + 'cancel recurring message' => { + message => q|Recurring transaction %s with an item named %s for a user named %s has been cancelled.|, + lastUpdated => 0, + context => q|a notification email sent to shop owners|, + }, + 'shop' => { message => q|Shop|, lastUpdated => 0, diff --git a/lib/WebGUI/i18n/English/Shop.pm b/lib/WebGUI/i18n/English/Shop.pm index 1bd2a07ee..321ec5541 100644 --- a/lib/WebGUI/i18n/English/Shop.pm +++ b/lib/WebGUI/i18n/English/Shop.pm @@ -3,6 +3,12 @@ package WebGUI::i18n::English::Shop; use strict; our $I18N = { + 'shop notice' => { + message => q|Shop Notice|, + lastUpdated => 0, + context => q|an email subject heading for generic shop notification emails|, + }, + 'mixed items warning' => { message => q|You are not able to check out with both recurring and non-recurring items in your cart. You may have either one recurring item, or as many non-recurring items as you want in your cart at checkout time. If you need to purchase both, then please purchase them under separate transactions.|, lastUpdated => 0,