diff --git a/docs/upgrades/upgrade_6.8.7-6.99.0.pl b/docs/upgrades/upgrade_6.8.7-6.99.0.pl index 8376d99da..25d26209c 100644 --- a/docs/upgrades/upgrade_6.8.7-6.99.0.pl +++ b/docs/upgrades/upgrade_6.8.7-6.99.0.pl @@ -132,7 +132,7 @@ sub addWorkflow { "WebGUI::Workflow::Activity::TrashExpiredEvents", "WebGUI::Workflow::Activity::CreateCronJob", "WebGUI::Workflow::Activity::DeleteExpiredSessions", "WebGUI::Workflow::Activity::ExpireGroupings", "WebGUI::Workflow::Activity::PurgeOldAssetRevisions", "WebGUI::Workflow::Activity::ExpireSubscriptionCodes", "WebGUI::Workflow::Activity::PurgeOldTrash", - "WebGUI::Workflow::Activity::GetSyndicatedContent"], + "WebGUI::Workflow::Activity::GetSyndicatedContent", "WebGUI::Workflow::Activity::ProcessRecurringPayments"], "WebGUI::User"=>["WebGUI::Workflow::Activity::CreateCronJob"], "WebGUI::VersionTag"=>["WebGUI::Workflow::Activity::CommitVersionTag", "WebGUI::Workflow::Activity::RollbackVersionTag", "WebGUI::Workflow::Activity::TrashVersionTag", "WebGUI::Workflow::Activity::CreateCronJob"] @@ -146,6 +146,8 @@ sub addWorkflow { my $activity = $workflow->addActivity("WebGUI::Workflow::Activity::CleanTempStorage", "pbwfactivity0000000001"); $activity->set("title","Delete temp files older than 24 hours"); $activity->set("storageTimeout",60*60*24); + $activity = $workflow->addActivity("WebGUI::Workflow::Activity::ProcessRecurringPayments", "pbwfactivity0000000013"); + $activity->set("title", "Process Recurring Payments"); $activity = $workflow->addActivity("WebGUI::Workflow::Activity::CleanFileCache", "pbwfactivity0000000002"); $activity->set("title","Prune cache larger than 100MB"); $activity->set("sizeLimit", 1000000000); diff --git a/lib/WebGUI/Workflow/Activity/ProcessRecurringPayments.pm b/lib/WebGUI/Workflow/Activity/ProcessRecurringPayments.pm new file mode 100644 index 000000000..0054ddfe1 --- /dev/null +++ b/lib/WebGUI/Workflow/Activity/ProcessRecurringPayments.pm @@ -0,0 +1,137 @@ +package WebGUI::Workflow::Activity::ProcessRecurringPayments; + + +=head1 LEGAL + + ------------------------------------------------------------------- + WebGUI is Copyright 2001-2006 Plain Black Corporation. + ------------------------------------------------------------------- + Please read the legal notices (docs/legal.txt) and the license + (docs/license.txt) that came with this distribution before using + this software. + ------------------------------------------------------------------- + http://www.plainblack.com info@plainblack.com + ------------------------------------------------------------------- + +=cut + +use strict; +use base 'WebGUI::Workflow::Activity'; +use WebGUI::Commerce::Payment; +use WebGUI::Commerce::Transaction; +use WebGUI::Commerce::Item; +use WebGUI::Mail::Send; + +=head1 NAME + +Package WebGUI::Workflow::Activity::ProcessRecurringPayments + +=head1 DESCRIPTION + +Deals with recurring payments from the subscription system. + +=head1 SYNOPSIS + +See WebGUI::Workflow::Activity for details on how to use any activity. + +=head1 METHODS + +These methods are available from this class: + +=cut + + +#------------------------------------------------------------------- + +=head2 definition ( session, definition ) + +See WebGUI::Workflow::Activity::defintion() for details. + +=cut + +sub definition { + my $class = shift; + my $session = shift; + my $definition = shift; + my $i18n = WebGUI::International->new($session, "Commerce"); + push(@{$definition}, { + name=>$i18n->get("process recurring payments"), + properties=> {} + }); + return $class->SUPER::definition($session,$definition); +} + + + +#------------------------------------------------------------------- +sub getDuration { + my $self = shift; + my $duration = shift; + return $self->session->datetime->addToDate(0,0,0,7) if $duration eq 'Weekly'; + return $self->session->datetime->addToDate(0,0,0,14) if $duration eq 'BiWeekly'; + return $self->session->datetime->addToDate(0,0,0,28) if $duration eq 'FourWeekly'; + return $self->session->datetime->addToDate(0,0,1,0) if $duration eq 'Monthly'; + return $self->session->datetime->addToDate(0,0,3,0) if $duration eq 'Quarterly'; + return $self->session->datetime->addToDate(0,0,6,0) if $duration eq 'HalfYearly'; + return $self->session->datetime->addToDate(0,1,0,0) if $duration eq 'Yearly'; +} + +#------------------------------------------------------------------- + +=head2 execute ( ) + +See WebGUI::Workflow::Activity::execute() for details. + +=cut + +sub execute { + my $self = shift; + my @recurringTransactions = $self->session->db->buildArray("select transactionId from transaction where recurring=1 and status='Completed'"); + my (@unprocessed, @ok, @failed, @fatal); + foreach (@recurringTransactions) { + my $transaction = WebGUI::Commerce::Transaction->new($self->session,$_); + my $itemProperties = $transaction->getItems->[0]; + my $item = WebGUI::Commerce::Item->new($self->session, $itemProperties->{itemId}, $itemProperties->{itemType}); + my $time = time; + $time -= $transaction->get('initDate'); + my $term = int($time / $self->getDuration($item->duration)) + 1; + if ($term > $transaction->lastPayedTerm) { + my $payment = WebGUI::Commerce::Payment->load($self->session, $transaction->gateway); + $transaction->gatewayId; + my $status = $payment->getRecurringPaymentStatus($self->session, $transaction->gatewayId, $term); + my $output = $item->name." (tid: ".$transaction->get('transactionId').") "; + $output .= " by user ".WebGUI::User->new($self->session, $transaction->get(userId))->username." (uid: ".$transaction->get(userId).") "; + $output .= " for term ". sprintf('% 6d', $term)." "; + $output .= " -> ".$transaction->gateway.": (".$transaction->gatewayId.")\t"; + unless ($payment->resultCode) { + unless (defined $status) { + $output .= "NOT PROCESSED YET"; + push (@unprocessed, $output); + } elsif ($status->{resultCode} eq '0') { + $output .= "OK"; + push (@ok, $output); + $item->handler($transaction->get(userId)) unless ($term == 1); + $transaction->lastPayedTerm($term); + } else { + $output .= "PAYMENT FAILED: ".$status->{resultCode}; + push (@failed, $output); + } + } else { + $output .= "FATAL ERROR: ".$payment->resultMessage." (".$payment->errorCode.")"; + } + } + } + my $message = "FAILED PAYMENTS:\n-----------------------------\n".join("\n", @failed)."\n\n\n"; + $message .= "UNPROCESSED PAYMENTS:\n-----------------------------\n".join("\n", @unprocessed)."\n\n\n"; + $message .= "FATAL ERRORS:\n-----------------------------\n".join("\n",@fatal)."\n\n\n"; + $message .= "SUCCESFUL PAYMENTS:\n-----------------------------\n".join("\n", @ok)."\n\n\n"; + my $mail = WebGUI::Mail::Send->new($self->session, { + to=>$self->session->setting->get("commerceSendDailyReportTo"), + subject=>'Daily recurring payments report' + }); + $mail->addText($message); + $mail->send; +} + +1; + diff --git a/lib/WebGUI/i18n/English/Commerce.pm b/lib/WebGUI/i18n/English/Commerce.pm index 06de2ac55..f7a1cd742 100755 --- a/lib/WebGUI/i18n/English/Commerce.pm +++ b/lib/WebGUI/i18n/English/Commerce.pm @@ -1,6 +1,11 @@ package WebGUI::i18n::English::Commerce; our $I18N = { + 'process recurring payments' => { + message => q|Process Recurring Payments|, + lastUpdated => 0, + context => q|the title for the workflow activity that processes recurring payments| + }, 'commerce settings' => { message => q|Commerce|, lastUpdated => 1101772584, diff --git a/sbin/Hourly/ProcessRecurringPayments.pm b/sbin/Hourly/ProcessRecurringPayments.pm deleted file mode 100644 index 0f78e4ab7..000000000 --- a/sbin/Hourly/ProcessRecurringPayments.pm +++ /dev/null @@ -1,73 +0,0 @@ -package Hourly::ProcessRecurringPayments; - -use strict; -use WebGUI::SQL; -use WebGUI::Commerce::Payment; -use WebGUI::Commerce::Transaction; -use WebGUI::Commerce::Item; -use WebGUI::DateTime; -use WebGUI::Session; - -sub _getDuration { - my $duration = shift; - - return addToDate(0,0,0,7) if $duration eq 'Weekly'; - return addToDate(0,0,0,14) if $duration eq 'BiWeekly'; - return addToDate(0,0,0,28) if $duration eq 'FourWeekly'; - return addToDate(0,0,1,0) if $duration eq 'Monthly'; - return addToDate(0,0,3,0) if $duration eq 'Quarterly'; - return addToDate(0,0,6,0) if $duration eq 'HalfYearly'; - return addToDate(0,1,0,0) if $duration eq 'Yearly'; -} - -sub process { - my @recurringTransactions = WebGUI::SQL->buildArray("select transactionId from transaction where recurring=1 and status='Completed'"); - - my (@unprocessed, @ok, @failed, @fatal); - foreach (@recurringTransactions) { - my $transaction = WebGUI::Commerce::Transaction->new($_); - my $itemProperties = $transaction->getItems->[0]; - my $item = WebGUI::Commerce::Item->new($itemProperties->{itemId}, $itemProperties->{itemType}); - my $time = time; - $time -= $transaction->get('initDate'); - my $term = int($time / _getDuration($item->duration)) + 1; - - if ($term > $transaction->lastPayedTerm) { - my $payment = WebGUI::Commerce::Payment->load($transaction->gateway); - - $transaction->gatewayId; - my $status = $payment->getRecurringPaymentStatus($transaction->gatewayId, $term); - - my $output = $item->name." (tid: ".$transaction->get('transactionId').") "; - $output .= " by user ".WebGUI::User->new($transaction->get(userId))->username." (uid: ".$transaction->get(userId).") "; - $output .= " for term ". sprintf('% 6d', $term)." "; - $output .= " -> ".$transaction->gateway.": (".$transaction->gatewayId.")\t"; - unless ($payment->resultCode) { - unless (defined $status) { - $output .= "NOT PROCESSED YET"; - push (@unprocessed, $output); - } elsif ($status->{resultCode} eq '0') { - $output .= "OK"; - push (@ok, $output); - $item->handler($transaction->get(userId)) unless ($term == 1); - $transaction->lastPayedTerm($term); - } else { - $output .= "PAYMENT FAILED: ".$status->{resultCode}; - push (@failed, $output); - } - } else { - $output .= "FATAL ERROR: ".$payment->resultMessage." (".$payment->errorCode.")"; - } - } - } - - my $message = "FAILED PAYMENTS:\n-----------------------------\n".join("\n", @failed)."\n\n\n"; - $message .= "UNPROCESSED PAYMENTS:\n-----------------------------\n".join("\n", @unprocessed)."\n\n\n"; - $message .= "FATAL ERRORS:\n-----------------------------\n".join("\n",@fatal)."\n\n\n"; - $message .= "SUCCESFUL PAYMENTS:\n-----------------------------\n".join("\n", @ok)."\n\n\n"; - - WebGUI::Mail::send($session{setting}{commerceSendDailyReportTo}, 'Daily recurring payments report', $message); -} - -1; -