From 7ff6381ad82df71f87a228549cd3cfadc35d213a Mon Sep 17 00:00:00 2001 From: Jens Schuppe Date: Fri, 30 Aug 2019 10:33:30 +0200 Subject: [PATCH] [#6] Make contribution status configurable per payment method --- CRM/Twingle/Form/Profile.php | 46 ++++++++++++++++++++++++++ CRM/Twingle/Profile.php | 13 ++++++-- api/v3/TwingleDonation/Submit.php | 2 +- templates/CRM/Twingle/Form/Profile.tpl | 6 ++++ 4 files changed, 64 insertions(+), 3 deletions(-) diff --git a/CRM/Twingle/Form/Profile.php b/CRM/Twingle/Form/Profile.php index fdac078..6d7adf2 100644 --- a/CRM/Twingle/Form/Profile.php +++ b/CRM/Twingle/Form/Profile.php @@ -44,6 +44,14 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form { */ protected static $_paymentInstruments = NULL; + /** + * @var array + * + * A static cache of retrieved contribution statuses found within + * static::getContributionStatusOptions(). + */ + protected static $_contributionStatusOptions = NULL; + /** * @var array * @@ -238,6 +246,14 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form { static::getPaymentInstruments(), // list of options TRUE // is required ); + + $this->add( + 'select', + $pi_name . '_status', + E::ts('Record %1 donations with contribution status', array(1 => $pi_label)), + static::getContributionStatusOptions(), + TRUE + ); } if (CRM_Twingle_Submission::civiSepaEnabled()) { @@ -613,6 +629,36 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form { return self::$_paymentInstruments; } + /** + * Retrieves contribution statuses as options for select form elements. + * + * @return array + * + * @throws \CiviCRM_API3_Exception + */ + public static function getContributionStatusOptions() { + if (!isset(self::$_contributionStatusOptions)) { + $query = civicrm_api3( + 'OptionValue', + 'get', + array( + 'option.limit' => 0, + 'option_group_id' => 'contribution_status', + 'return' => array( + 'value', + 'label', + ) + ) + ); + + foreach ($query['values'] as $contribution_status) { + self::$_contributionStatusOptions[$contribution_status['value']] = $contribution_status['label']; + } + } + + return self::$_contributionStatusOptions; + } + /** * Retrieves active groups used as mailing lists within the system as options * for select form elements. diff --git a/CRM/Twingle/Profile.php b/CRM/Twingle/Profile.php index ff29a74..bd1e7c3 100644 --- a/CRM/Twingle/Profile.php +++ b/CRM/Twingle/Profile.php @@ -201,7 +201,12 @@ class CRM_Twingle_Profile { 'membership_type_id', ), // Add payment methods. - array_keys(static::paymentInstruments()) + array_keys(static::paymentInstruments()), + + // Add contribution status for all payment methods. + array_map(function ($attribute) { + return $attribute . '_status'; + }, array_keys(static::paymentInstruments())) ); } @@ -263,7 +268,11 @@ class CRM_Twingle_Profile { 'contribution_source' => NULL, 'custom_field_mapping' => NULL, 'membership_type_id' => NULL, - )); + ) + // Add contribution status for all payment methods. + + array_fill_keys(array_map(function($attribute) { + return $attribute . '_status'; + }, array_keys(static::paymentInstruments())), CRM_Twingle_Submission::CONTRIBUTION_STATUS_COMPLETED)); } /** diff --git a/api/v3/TwingleDonation/Submit.php b/api/v3/TwingleDonation/Submit.php index a7ae8c8..dafaa85 100644 --- a/api/v3/TwingleDonation/Submit.php +++ b/api/v3/TwingleDonation/Submit.php @@ -632,7 +632,7 @@ function civicrm_api3_twingle_donation_Submit($params) { // Create contribution. $contribution_data += array( - 'contribution_status_id' => CRM_Twingle_Submission::CONTRIBUTION_STATUS_COMPLETED, + 'contribution_status_id' => $profile->getAttribute($params['payment_instrument_id'] . '_status', CRM_Twingle_Submission::CONTRIBUTION_STATUS_COMPLETED), 'receive_date' => $params['confirmed_at'], ); $contribution = civicrm_api3('Contribution', 'create', $contribution_data); diff --git a/templates/CRM/Twingle/Form/Profile.tpl b/templates/CRM/Twingle/Form/Profile.tpl index ae79e64..72c9716 100644 --- a/templates/CRM/Twingle/Form/Profile.tpl +++ b/templates/CRM/Twingle/Form/Profile.tpl @@ -151,8 +151,14 @@ {foreach key=pi_name item=pi_label from=$payment_instruments} + + + {capture assign="pi_contribution_status"}{$pi_name}_status{/capture} + + + {/foreach}
{$form.$pi_name.label} {$form.$pi_name.html}{$form.$pi_contribution_status.label}{$form.$pi_contribution_status.html}