From 33ced7bbc30fb1e0ba5c9af15baf0e66915ca117 Mon Sep 17 00:00:00 2001 From: "B. Endres" Date: Mon, 10 Feb 2020 08:26:55 +0100 Subject: [PATCH] [#19] implementing membership postprocess call (WIP) --- CRM/Twingle/Form/Profile.php | 7 ++++ CRM/Twingle/Profile.php | 1 + api/v3/TwingleDonation/Submit.php | 46 ++++++++++++++++++++++++-- templates/CRM/Twingle/Form/Profile.hlp | 11 ++++++ templates/CRM/Twingle/Form/Profile.tpl | 45 +++++++++++++++++++++++++ 5 files changed, 107 insertions(+), 3 deletions(-) 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:{/ts} +{/htxt} + {htxt id='id-custom_field_mapping'} {ts domain="de.systopia.twingle"}

Map Twingle custom fields to CiviCRM custom fields using the following format (each assignment in a separate line):

twingle_field_1=custom_123
twingle_field_2=custom_789
diff --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 @@ {$form.membership_type_id_recur.label} {$form.membership_type_id_recur.html} + + + {$form.membership_postprocess_call.label} + + + {$form.membership_postprocess_call.html} + {$form.contribution_source.label} @@ -286,3 +307,27 @@ + +{literal} + +{/literal} \ No newline at end of file