Mwalker has uploaded a new change for review.
https://gerrit.wikimedia.org/r/115559
Change subject: Script to restart orphaned GC recurring jobs
......................................................................
Script to restart orphaned GC recurring jobs
For those donations which did not recur because of an external problem
such as the wrapping script failing, this script can restart them
AND send a special varient of the thank you message notifying the
donor that something odd happened.
Change-Id: If7e3f30f90e8d25bcb9a5109bc997f6690bb5ed2
---
M sites/all/modules/recurring_globalcollect/recurring_globalcollect.module
M sites/all/modules/recurring_globalcollect/recurring_globalcollect_common.inc
A
sites/all/modules/recurring_globalcollect/recurring_globalcollect_restart_missed.drush.inc.php
3 files changed, 139 insertions(+), 1 deletion(-)
git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/crm
refs/changes/59/115559/1
diff --git
a/sites/all/modules/recurring_globalcollect/recurring_globalcollect.module
b/sites/all/modules/recurring_globalcollect/recurring_globalcollect.module
index c124908..d25ec4a 100644
--- a/sites/all/modules/recurring_globalcollect/recurring_globalcollect.module
+++ b/sites/all/modules/recurring_globalcollect/recurring_globalcollect.module
@@ -395,6 +395,8 @@
$msg['original_currency'] = $contribution_recur['currency'];
$msg['original_gross'] = $msg['gross'];
+ $msg['thank_you_options'] = isset( $options['thank_you_options'] ) ?
$options['thank_you_options'] : array();
+
$msg = wmf_civicrm_normalize_contribution_amounts($msg);
/*
* This array needs to be rekeyed to be consistent with:
diff --git
a/sites/all/modules/recurring_globalcollect/recurring_globalcollect_common.inc
b/sites/all/modules/recurring_globalcollect/recurring_globalcollect_common.inc
index 960487c..0d0fa99 100644
---
a/sites/all/modules/recurring_globalcollect/recurring_globalcollect_common.inc
+++
b/sites/all/modules/recurring_globalcollect/recurring_globalcollect_common.inc
@@ -44,7 +44,9 @@
* LIMIT 1
*
* @param int $limit Number of records to pull. Default is 1.
- * @param string $date Uses strtotime() to parse the date
+ * @param string $date End of period to look for failure retries. Start of
+ * period is this minus recurring_globalcollect_run_missed_days. Uses
+ * strtotime() to parse the date.
*
* @todo The field `civicrm_payment_processor`.`payment_processor_type` should
be set.
* @todo Implement $contributionStatus =
CRM_Contribute_PseudoConstant::contributionStatus( null, 'name' );
diff --git
a/sites/all/modules/recurring_globalcollect/recurring_globalcollect_restart_missed.drush.inc.php
b/sites/all/modules/recurring_globalcollect/recurring_globalcollect_restart_missed.drush.inc.php
new file mode 100644
index 0000000..86d2487
--- /dev/null
+++
b/sites/all/modules/recurring_globalcollect/recurring_globalcollect_restart_missed.drush.inc.php
@@ -0,0 +1,134 @@
+<?php
+
+function recurring_globalcollect_restart_missed_drush_command() {
+ $batch_max = (integer) variable_get('recurring_globalcollect_batch_max',
100);
+ $items = array();
+
+ $items['recurring-globalcollect-restart-missed'] = array(
+ 'description' => 'Restart recurring GlobalCollect payments which are
not recurring but have no failures.',
+ 'options' => array(
+ 'batch' => "Number of contributions to process. Maximum is
$batch_max. If set to zero, nothing will be done.",
+ 'grace' => 'Number of days to ignore. Default is 25',
+ 'limit' => 'Number of days beyond which the donation is too old.
Default 365.',
+ )
+ );
+
+ return $items;
+}
+
+/**
+ * Fires the 'batch_process' method in the recurring_globalcollect module.
+ *
+ */
+function drush_recurring_globalcollect_restart_missed() {
+ $batch_size = intval(drush_get_option('batch',
variable_get('recurring_globalcollect_batch_max', 100)));
+ $grace = intval(drush_get_option('grace', 25));
+ $limit = intval(drush_get_option('limit', 365));
+
+ /*
+ * Donations which 'just stopped' and did not have a scheduled recurring
attempt
+ */
+ watchdog('recurring_globalcollect', 'Attempting to restart up to ' .
$batch_size . ' recurring contribution(s).');
+ $contribution_batch = rgcrm_get_contributions( $limit, $grace, $batch_size
);
+ watchdog(
+ 'recurring_globalcollect',
+ 'Query returned @count messages to process',
+ array('@count' => count($contribution_batch))
+ );
+ $result = recurring_globalcollect_batch_charge(
+ $contribution_batch,
+ array( 'thank_you_options' => array('recurring_damaged' => true) )
+ );
+ $processed = count($result['succeeded']) + count($result['failed']);
+ watchdog(
+ 'recurring_globalcollect',
+ 'Attempted to restart :processed missed contribution(s). :failed
failed to restart',
+ array('processed' => $processed, ':failed' => count($result['failed']))
+ );
+
+ /*
+ * Donations which had a recurring attempt but failed and then missed
their retry date
+ */
+ watchdog('recurring_globalcollect', 'Attempting to restart up to ' .
$batch_size . ' abandoned contribution(s).');
+ $old_limit_days = variable_get('recurring_globalcollect_run_missed_days',
0);
+ try {
+ variable_set('recurring_globalcollect_run_missed_days', $limit);
+ $contribution_batch = recurring_globalcollect_get_failure_retry_batch(
$batch_size );
+ variable_set('recurring_globalcollect_run_missed_days',
$old_limit_days);
+ } catch(Exception $ex) {
+ wmf_common_failmail('recurring_globalcollect', $ex);
+ variable_set('recurring_globalcollect_run_missed_days',
$old_limit_days);
+ }
+ watchdog(
+ 'recurring_globalcollect',
+ 'Query returned @count messages to process',
+ array('@count' => count($contribution_batch))
+ );
+ $result = recurring_globalcollect_batch_charge(
+ $contribution_batch,
+ array( 'thank_you_options' => array('recurring_damaged' => true) )
+ );
+
+ $processed = count($result['succeeded']) + count($result['failed']);
+ watchdog(
+ 'recurring_globalcollect',
+ 'Attempted to restart :processed contribution(s). :failed failed to
restart',
+ array('processed' => $processed, ':failed' => count($result['failed']))
+ );
+
+ // Outputting errors again so they can be picked up by Jenkins.
+ $errors = drush_get_error_log();
+ if (!empty($errors)){
+ echo "\n***ERRORS***";
+ foreach($errors as $error=>$msgarray){
+ echo "\n$error: ";
+ foreach ($msgarray as $count=>$message){
+ echo "\n $message";
+ }
+ }
+ echo "\n\n";
+ exit(drush_get_error());
+ }
+}
+
+function rgcrm_get_contributions( $limit, $grace, $batch ) {
+ $earliest = date('Y-m-d 00:00:00', time() - ( $limit * 60 * 60 * 24 ));
+ $latest = date('Y-m-d 23:59:59', time() - ( $grace * 60 * 60 * 24 ));
+
+ $start_day_of_month = date('j');
+ $end_day_of_month = (date('t') == $start_day_of_month ) ? 31 :
$start_day_of_month;
+
+ $contribution_status_id = civicrm_api_contribution_status('Completed');
+
+ watchdog(
+ 'recurring_globalcollect',
+ 'Looking for contributions to restart in timeframe @min -> @max;
recurring on day of month @ds -> @de ',
+ array('@min' => $earliest, '@max' => $latest, '@ds' =>
$start_day_of_month, '@de' => $end_day_of_month )
+ );
+
+ $query = <<<EOS
+SELECT * FROM civicrm_contribution_recur
+WHERE
+ civicrm_contribution_recur.next_sched_contribution BETWEEN :start AND :end
+ AND civicrm_contribution_recur.trxn_id LIKE 'RECURRING GLOBALCOLLECT%'
+ AND ( civicrm_contribution_recur.end_date IS NULL )
+ AND civicrm_contribution_recur.contribution_status_id = :status_id
+ AND civicrm_contribution_recur.cycle_day BETWEEN :ds AND :de
+ AND civicrm_contribution_recur.frequency_unit = 'month'
+LIMIT :batch
+EOS;
+
+ $dbs = wmf_civicrm_get_dbs();
+ $dbs->push( 'civicrm' );
+
+ $res = db_query( $query, array(
+ ':start' => $earliest,
+ ':end' => $latest,
+ ':ds' => $start_day_of_month,
+ ':de' => $end_day_of_month,
+ ':status_id' => $contribution_status_id,
+ ':batch' => $batch
+ ) );
+
+ return $res->fetchAll();
+}
--
To view, visit https://gerrit.wikimedia.org/r/115559
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: If7e3f30f90e8d25bcb9a5109bc997f6690bb5ed2
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/crm
Gerrit-Branch: master
Gerrit-Owner: Mwalker <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits