Skip to content

Commit

Permalink
CIVIEWAY-11: Avoid redefining pseudoconstants.
Browse files Browse the repository at this point in the history
  • Loading branch information
agileware-dev committed Jun 25, 2018
1 parent 24452fe commit 088f22d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 36 deletions.
12 changes: 5 additions & 7 deletions au_com_agileware_ewayrecurring.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ function __construct( $mode, &$paymentProcessor )
$this->_mode = $mode; // live or test
$this->_paymentProcessor = $paymentProcessor;
$this->_processorName = ts('eWay Recurring');
defineContributionStatuses();
}

/**
Expand Down Expand Up @@ -272,7 +271,7 @@ function doDirectPayment( &$params )
'CRM_Contribute_DAO_Contribution',
$params['contributionID'],
'contribution_status_id',
PENDING_CONTRIBUTION_STATUS_ID
_contribution_status_id('Pending')
);

// Save the eWay customer token in the recurring contribution's processor_id field
Expand Down Expand Up @@ -704,7 +703,6 @@ function updateSubscriptionBillingInfo(&$message = '', $params = array()) {
// Flush to cache to work around this.
CRM_Core_PseudoConstant::flush();


// Build the customer info for eWAY
$expireYear = substr ($params['year'], 2, 2);
$expireMonth = sprintf('%02d', (int) $params['month']); // Pad month with zeros
Expand Down Expand Up @@ -751,10 +749,10 @@ function updateSubscriptionBillingInfo(&$message = '', $params = array()) {

// We shouldn't be allowed to update the details for completed or cancelled payments
switch($contribution['contribution_status_id']) {
case COMPLETED_CONTRIBUTION_STATUS_ID:
case _contribution_status_id('Completed'):
throw new Exception(ts('Attempted to update billing details for a completed contribution.'));
break;
case CANCELLED_CONTRIBUTUTION_STATUS_ID:
case _contribution_status_id('Cancelled'):
throw new Exception(ts('Attempted to update billing details for a cancelled contribution.'));
break;
default:
Expand All @@ -764,11 +762,11 @@ function updateSubscriptionBillingInfo(&$message = '', $params = array()) {
$result = $this->updateCustomerToken( $customerinfo, $params );

// Updating the billing details should fixed failed contributions
if(FAILED_CONTRIBUTION_STATUS_ID == $contribution['contribution_status_id']) {
if(_contribution_status_id('Failed') == $contribution['contribution_status_id']) {
CRM_Core_DAO::setFieldValue( 'CRM_Contribute_DAO_ContributionRecur',
$contribution['id'],
'contribution_status_id',
IN_PROGRESS_CONTRIBUTION_STATUS_ID );
_contribution_status_id('In Progress') );
}

CRM_Core_DAO::setFieldValue( 'CRM_Contribute_DAO_ContributionRecur',
Expand Down
6 changes: 5 additions & 1 deletion eWAYRecurring.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@

include_once 'au_com_agileware_ewayrecurring.class.php';

function _contribution_status_id($name) {
return CRM_Utils_Array::key($name, \CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'));
}

function ewayrecurring_civicrm_buildForm ($formName, &$form) {
if ($formName == 'CRM_Contribute_Form_ContributionPage_Amount') {
if(!($page_id = $form->getVar('_id')))
Expand Down Expand Up @@ -374,4 +378,4 @@ function _ewayrecurring_fix_installments(CRM_Queue_TaskContext $ctx, $schema) {
/* Because we can't rely on PHP having anonymous functions. */
function _ewayrecurring_get_pp_id($processor) {
return $processor['id'];
}
}
43 changes: 16 additions & 27 deletions eWAYRecurring.process.inc
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,6 @@ define('CONTRIBUTION_FAILURE_THRESHOLD', 3);
// TODO: Variable receipt title based on interval and unit?
define('RECEIPT_SUBJECT_TITLE', 'Recurring Donation');

/**
* Define contribution statues as constants.
*/
function defineContributionStatuses() {
// Contribution status IDs.
if(!defined("COMPLETED_CONTRIBUTION_STATUS_ID")) {
$contributionStatues = CRM_Contribute_PseudoConstant::contributionStatus();
define('COMPLETED_CONTRIBUTION_STATUS_ID', CRM_Utils_Array::key("Completed", $contributionStatues));
define('PENDING_CONTRIBUTION_STATUS_ID', CRM_Utils_Array::key("Pending", $contributionStatues));
define('CANCELLED_CONTRIBUTION_STATUS_ID', CRM_Utils_Array::key("Cancelled", $contributionStatues));
define('FAILED_CONTRIBUTION_STATUS_ID', CRM_Utils_Array::key("Failed", $contributionStatues));
define('IN_PROGRESS_CONTRIBUTION_STATUS_ID', CRM_Utils_Array::key("In Progress", $contributionStatues));
}
}

function process_recurring_payments($payment_processor) {
// Create log handler
$log = CRM_Core_Error::createDebugLogger();
Expand All @@ -66,10 +51,10 @@ function process_recurring_payments($payment_processor) {

$log->log("Processing " . count($pending_contributions) . " pending contributions for processor " . $payment_processor['id'], PEAR_LOG_INFO);
foreach ($pending_contributions as $pending_contribution) {
if ($pending_contribution['contribution_recur']->contribution_status_id == CANCELLED_CONTRIBUTION_STATUS_ID) {
if ($pending_contribution['contribution_recur']->contribution_status_id == _contribution_status_id('Cancelled')) {
complete_contribution($pending_contribution['contribution']['id'],
'',
CANCELLED_CONTRIBUTION_STATUS_ID,
_contribution_status_id('Cancelled'),
strtotime($pending_contribution['contribution_recur']->cancel_date));
continue;
}
Expand Down Expand Up @@ -111,7 +96,7 @@ function process_recurring_payments($payment_processor) {
// Mark the transaction as failed and move on.
complete_contribution($pending_contribution['contribution']['id'],
$result->ewayTrxnNumber,
FAILED_CONTRIBUTION_STATUS_ID);
_contribution_status_id('Failed'));
$pending_contribution['contribution_recur']->failure_count++;
} else {
// $log->log("Successfully processed payment for pending contribution ID: " . $pending_contribution['contribution']['id'] . "\n", PEAR_LOG_DEBUG);
Expand Down Expand Up @@ -159,7 +144,7 @@ function process_recurring_payments($payment_processor) {
CRM_Core_DAO::setFieldValue( 'CRM_Contribute_DAO_ContributionRecur',
$pending_contribution['contribution_recur']->id,
'contribution_status_id',
IN_PROGRESS_CONTRIBUTION_STATUS_ID );
_contribution_status_id('In Progress') );

// $log->log("Finished processing contribution ID: " . $pending_contribution['contribution']['id'] . "\n", PEAR_LOG_DEBUG);
}
Expand Down Expand Up @@ -217,7 +202,7 @@ function process_recurring_payments($payment_processor) {
array(
'version' => '3',
'id' => $contribution->id,
'contribution_recur_status_id' => COMPLETED_CONTRIBUTION_STATUS_ID)
'contribution_recur_status_id' => _contribution_status_id('Completed'))
);
}

Expand All @@ -244,7 +229,7 @@ function process_recurring_payments($payment_processor) {
CRM_Core_DAO::setFieldValue( 'CRM_Contribute_DAO_ContributionRecur',
$contribution->id,
'contribution_status_id',
COMPLETED_CONTRIBUTION_STATUS_ID);
_contribution_status_id('Completed'));
CRM_Core_DAO::setFieldValue( 'CRM_Contribute_DAO_ContributionRecur',
$contribution->id,
'end_date',
Expand Down Expand Up @@ -322,7 +307,7 @@ function process_recurring_payments($payment_processor) {

if ($result->ewayTrxnStatus != 'True') {
// Mark transaction as failed
$new_contribution_record['contribution_status_id'] = FAILED_CONTRIBUTION_STATUS_ID;
$new_contribution_record['contribution_status_id'] = _contribution_status_id('Failed');
}
else {
// $log->log("Successfully processed payment for scheduled recurring contribution ID: " . $contribution->id . "\n", PEAR_LOG_DEBUG);
Expand Down Expand Up @@ -357,7 +342,7 @@ function process_recurring_payments($payment_processor) {
$contribution->failure_count++;

// Record a failed contribution in this case.
$new_contribution_record['contribution_status_id'] = FAILED_CONTRIBUTION_STATUS_ID;
$new_contribution_record['contribution_status_id'] = _contribution_status_id('Failed');
$created = civicrm_api3('Contribution', 'create', $new_contribution_record);

$new_contribution_record = reset($created['values']);
Expand All @@ -383,7 +368,7 @@ function process_recurring_payments($payment_processor) {
CRM_Core_DAO::setFieldValue( 'CRM_Contribute_DAO_ContributionRecur',
$contribution->id,
'contribution_status_id',
FAILED_CONTRIBUTION_STATUS_ID
_contribution_status_id('Failed')
);

$log->log("Marked payment {$contribution->id} as failed");
Expand Down Expand Up @@ -413,7 +398,7 @@ function get_pending_recurring_contributions($log, $payment_processor)
// TODO: Stop using the API. For consistency we should use BAOs so we get objects back
$params = array(
'version' => 3,
'contribution_status_id' => PENDING_CONTRIBUTION_STATUS_ID,
'contribution_status_id' => _contribution_status_id('Pending'),
'rowCount' => 100,
'offset' => 0,
'is_test' => $payment_processor['is_test'],
Expand Down Expand Up @@ -564,8 +549,12 @@ function process_eway_payment($soap_client, $managed_customer_id, $amount_in_cen
* @param string $contribution_id The ID of the contribution to mark as complete
* @return object The contribution object
*/
function complete_contribution($contribution_id, $trxn_id = NULL, $status_id = COMPLETED_CONTRIBUTION_STATUS_ID, $c_date = NULL)
function complete_contribution($contribution_id, $trxn_id = NULL, $status_id = NULL, $c_date = NULL)
{
if(!$status_id) {
$status_id = _contribution_status_id('Completed');
}

// Mark the contribution as complete
$contribution = civicrm_api3('Contribution', 'getsingle', array('id' => $contribution_id));

Expand All @@ -574,7 +563,7 @@ function complete_contribution($contribution_id, $trxn_id = NULL, $status_id = C
if(!$c_date)
$c_date = time();

if ($status_id == CANCELLED_CONTRIBUTION_STATUS_ID)
if ($status_id == _contribution_status_id('Cancelled'))
$contribution['cancel_date'] = date('Y-m-d H:i:s', $c_date);
else
$contribution['receive_date'] = date('Y-m-d H:i:s', $c_date);
Expand Down
2 changes: 1 addition & 1 deletion info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<email>[email protected]</email>
</maintainer>
<releaseDate>2018-06-22</releaseDate>
<version>1.5.3</version>
<version>1.5.4</version>
<develStage>stable</develStage>
<compatibility><ver>4.6</ver><ver>4.7</ver><ver>5.2</ver></compatibility>
<comments></comments>
Expand Down

0 comments on commit 088f22d

Please sign in to comment.