diff --git a/CRM/Twingle/Form/Profile.php b/CRM/Twingle/Form/Profile.php index dc54b39..ea83455 100644 --- a/CRM/Twingle/Form/Profile.php +++ b/CRM/Twingle/Form/Profile.php @@ -379,6 +379,13 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form { FALSE, // is not required array('class' => 'crm-select2 huge') ); + $this->add( + 'text', + 'membership_postprocess_call', + E::ts('API Call for Membership Postprocessing'), + FALSE + ); + $this->addRule('membership_postprocess_call', E::ts("The API call must have the form 'Entity.Action'."), 'regex', '/^[A-Za-z_]+[.][A-Za-z_]+$/'); $this->add( 'text', // field type diff --git a/CRM/Twingle/Profile.php b/CRM/Twingle/Profile.php index 5c98333..e61f4df 100644 --- a/CRM/Twingle/Profile.php +++ b/CRM/Twingle/Profile.php @@ -219,6 +219,7 @@ class CRM_Twingle_Profile { 'custom_field_mapping', 'membership_type_id', 'membership_type_id_recur', + 'membership_postprocess_call', ), // Add payment methods. array_keys(static::paymentInstruments()), diff --git a/api/v3/TwingleDonation/Submit.php b/api/v3/TwingleDonation/Submit.php index 52de247..505b1fc 100644 --- a/api/v3/TwingleDonation/Submit.php +++ b/api/v3/TwingleDonation/Submit.php @@ -667,11 +667,51 @@ function civicrm_api3_twingle_donation_Submit($params) { } if (!empty($membership_type_id)) { $membership = civicrm_api3('Membership', 'create', array( - 'contact_id' => $contact_id, - 'membership_type_id' => $membership_type_id, + 'contact_id' => $contact_id, + 'membership_type_id' => $membership_type_id, )); - $result_values['membership'] = $membership; + + // call the postprocess API + $postprocess_call = $profile->getAttribute('membership_postprocess_call'); + if (!empty($postprocess_call)) { + list($pp_entity, $pp_action) = explode('.', $postprocess_call, 1); + try { + // gather the contribution IDs + $recurring_contribution_id = $contribution_id = ''; + if (isset($mandate)) { + if ($mandate['type'] == 'RCUR') { + $recurring_contribution_id = $mandate['entity_id']; + } elseif ($mandate['type'] == 'OOFF') { + $contribution_id = $mandate['entity_id']; + } + } else { + if (isset($contribution_recur['id'])) { + $recurring_contribution_id = $contribution_recur['id']; + } + if (isset($contribution['id'])) { + $contribution_id = $contribution['id']; + } + } + + // run the call + civicrm_api3($pp_entity, $pp_action, [ + 'membership_id' => $membership['id'], + 'contact_id' => $contact_id, + 'organization_id' => isset($organisation_id) ? $organisation_id : '', + 'contribution_id' => $contribution_id, + 'recurring_contribution_id' => $recurring_contribution_id, + ]); + + // refresh membership data + $result_values['membership'] = civicrm_api3('Membership', 'getsingle', $membership['id']); + + } catch (Exception $ex) { + // TODO: more error handling? + Civi::log()->debug("Twingle membership postprocessing call {$pp_entity}.{$pp_action} has failed: " . $ex->getMessage()); + } + } + } $result = civicrm_api3_create_success($result_values); diff --git a/templates/CRM/Twingle/Form/Profile.hlp b/templates/CRM/Twingle/Form/Profile.hlp index 81045c2..8a89223 100644 --- a/templates/CRM/Twingle/Form/Profile.hlp +++ b/templates/CRM/Twingle/Form/Profile.hlp @@ -38,6 +38,17 @@ {ts domain="de.systopia.twingle"}Select which financial type to use for recurring contributions.{/ts} {/htxt} +{htxt id='id-membership-postprocessing-call'} + {ts domain="de.systopia.twingle"}Some organisations have specific conventions on how a membership should be created. Since the Twingle-API can only create a "bare bone" membership object, you can enter a API Call (as 'Entity.Action') to adjust any newly created membership to your organisation's needs.{/ts} + {ts domain="de.systopia.twingle"}The API call would receive the following parameters:
membership_id
: The ID of the newly created membershipcontact_id
: The ID of the contact involvedorganization_id
: The ID of the contact's organisation, potentially emptycontribution_id
: The ID contribution received, potentially emptyrecurring_contribution_id
: The ID of the recurring contribution. If empty, this was only a one-off donation.Map Twingle custom fields to CiviCRM custom fields using the following format (each assignment in a separate line):
twingle_field_1=custom_123diff --git a/templates/CRM/Twingle/Form/Profile.tpl b/templates/CRM/Twingle/Form/Profile.tpl index 3878454..17e0c87 100644 --- a/templates/CRM/Twingle/Form/Profile.tpl +++ b/templates/CRM/Twingle/Form/Profile.tpl @@ -236,6 +236,27 @@
twingle_field_2=custom_789