From 520fdb9f8d940f9cd8cda4a5fd72b7096643ddf0 Mon Sep 17 00:00:00 2001 From: Marc Michalsky forumZFD Date: Mon, 22 Feb 2021 18:25:19 +0100 Subject: [PATCH] implement soft credits --- CRM/TwingleCampaign/Utils/APIWrapper.php | 75 ++++++++++++++++++++++-- twinglecampaign.php | 11 +++- 2 files changed, 77 insertions(+), 9 deletions(-) diff --git a/CRM/TwingleCampaign/Utils/APIWrapper.php b/CRM/TwingleCampaign/Utils/APIWrapper.php index 334d1f1..bed4113 100644 --- a/CRM/TwingleCampaign/Utils/APIWrapper.php +++ b/CRM/TwingleCampaign/Utils/APIWrapper.php @@ -1,5 +1,7 @@ getApiRequestSig(); + if ($request == '3.twingledonation.submit') { + + $response = $event->getResponse(); + + // Create soft credit for contribution + $contribution = $response['values']['contribution'] + [array_key_first($response['values']['contribution'])]; + if (array_key_exists('campaign_id', $contribution)) { + try { + $twingle_event = civicrm_api3( + 'TwingleEvent', + 'getsingle', + ['id' => $contribution['campaign_id']] + )['values']; + $response['values']['soft_credit'] = + self::createSoftCredit($contribution, $twingle_event)['values']; + $event->setResponse($response); + } catch (CiviCRM_API3_Exception $e) { + // Do nothing + } + } + } + } + /** * ## Map donation to Campaign * This functions tries to map an incoming Twingle donation to a campaign @@ -29,7 +63,6 @@ class CRM_TwingleCampaign_Utils_APIWrapper { * the de.systopia.twingle extension can include the campaign into the * contribution which it will create. * - * @throws CiviCRM_API3_Exception */ public function mapDonation($apiRequest, $callsame) { @@ -53,8 +86,9 @@ class CRM_TwingleCampaign_Utils_APIWrapper { } } elseif (array_key_exists( - 'event', - $apiRequest['params']['custom_fields']) + 'event', + $apiRequest['params']['custom_fields']) && + !empty($apiRequest['params']['custom_fields']['event']) ) { try { $targetCampaign = civicrm_api3( @@ -78,8 +112,6 @@ class CRM_TwingleCampaign_Utils_APIWrapper { } } - // TODO: Soft credits - if (isset($targetCampaign)) { $apiRequest['params']['campaign_id'] = $targetCampaign['values']['id']; } @@ -87,4 +119,35 @@ class CRM_TwingleCampaign_Utils_APIWrapper { return $callsame($apiRequest); } + /** + * ## Create soft credit + * + * This method creates a soft credit for an event initiator. + * + * @param array $contribution + * @param array $event + * + * @return array + */ + private static function createSoftCredit( + array $contribution, + array $event): array { + try { + return civicrm_api3('ContributionSoft', 'create', [ + 'contribution_id' => $contribution['id'], + 'amount' => $contribution['total_amount'], + 'currency' => $contribution['currency'], + 'contact_id' => $event['contact_id'], + 'soft_credit_type' => 'twingle_event', + ]); + } catch (CiviCRM_API3_Exception $e) { + Civi::log()->error( + E::LONG_NAME . + ' could not create TwingleProject: ' . + $e->getMessage(), + $project->getResponse() + ); + } + } + } \ No newline at end of file diff --git a/twinglecampaign.php b/twinglecampaign.php index 2ceb017..515cea0 100644 --- a/twinglecampaign.php +++ b/twinglecampaign.php @@ -14,14 +14,19 @@ require_once 'twinglecampaign.civix.php'; function twinglecampaign_civicrm_config(&$config) { _twinglecampaign_civix_civicrm_config($config); - // This dispatcher adds an event listener to TwingleDonation.submit - // (de.systopia.twingle) and calls an API-Wrapper which maps incoming Twingle - // donations to TwingleCampaigns. + // This dispatchers add event listeners to TwingleDonation.submit + // (de.systopia.twingle) and call an API-Wrapper which maps incoming Twingle + // donations to TwingleCampaigns and create soft credits for event initiators. Civi::dispatcher()->addListener( 'civi.api.prepare', ['CRM_TwingleCampaign_Utils_APIWrapper', 'PREPARE'], -100 ); + Civi::dispatcher()->addListener( + 'civi.api.respond', + ['CRM_TwingleCampaign_Utils_APIWrapper', 'RESPOND'], + -100 + ); }