implement soft credits
This commit is contained in:
parent
86a718273e
commit
520fdb9f8d
2 changed files with 77 additions and 9 deletions
|
@ -1,5 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use CRM_TwingleCampaign_ExtensionUtil as E;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* # APIWrapper class for TwingleDonation.submit
|
* # APIWrapper class for TwingleDonation.submit
|
||||||
* This wrapper maps an incoming contribution to a campaign created by the
|
* This wrapper maps an incoming contribution to a campaign created by the
|
||||||
|
@ -10,7 +12,8 @@ class CRM_TwingleCampaign_Utils_APIWrapper {
|
||||||
private static $campaign_id;
|
private static $campaign_id;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ## Callback method for event listener
|
* ## PREPARE callback method for event listener
|
||||||
|
*
|
||||||
* @param $event
|
* @param $event
|
||||||
*/
|
*/
|
||||||
public static function PREPARE($event) {
|
public static function PREPARE($event) {
|
||||||
|
@ -21,6 +24,37 @@ class CRM_TwingleCampaign_Utils_APIWrapper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ## RESPOND callback method for event listener
|
||||||
|
*
|
||||||
|
* @param $event
|
||||||
|
*/
|
||||||
|
public static function RESPOND($event) {
|
||||||
|
$request = $event->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
|
* ## Map donation to Campaign
|
||||||
* This functions tries to map an incoming Twingle donation to a 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
|
* the de.systopia.twingle extension can include the campaign into the
|
||||||
* contribution which it will create.
|
* contribution which it will create.
|
||||||
*
|
*
|
||||||
* @throws CiviCRM_API3_Exception
|
|
||||||
*/
|
*/
|
||||||
public function mapDonation($apiRequest, $callsame) {
|
public function mapDonation($apiRequest, $callsame) {
|
||||||
|
|
||||||
|
@ -54,7 +87,8 @@ class CRM_TwingleCampaign_Utils_APIWrapper {
|
||||||
}
|
}
|
||||||
elseif (array_key_exists(
|
elseif (array_key_exists(
|
||||||
'event',
|
'event',
|
||||||
$apiRequest['params']['custom_fields'])
|
$apiRequest['params']['custom_fields']) &&
|
||||||
|
!empty($apiRequest['params']['custom_fields']['event'])
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
$targetCampaign = civicrm_api3(
|
$targetCampaign = civicrm_api3(
|
||||||
|
@ -78,8 +112,6 @@ class CRM_TwingleCampaign_Utils_APIWrapper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Soft credits
|
|
||||||
|
|
||||||
if (isset($targetCampaign)) {
|
if (isset($targetCampaign)) {
|
||||||
$apiRequest['params']['campaign_id'] = $targetCampaign['values']['id'];
|
$apiRequest['params']['campaign_id'] = $targetCampaign['values']['id'];
|
||||||
}
|
}
|
||||||
|
@ -87,4 +119,35 @@ class CRM_TwingleCampaign_Utils_APIWrapper {
|
||||||
return $callsame($apiRequest);
|
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()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -14,14 +14,19 @@ require_once 'twinglecampaign.civix.php';
|
||||||
function twinglecampaign_civicrm_config(&$config) {
|
function twinglecampaign_civicrm_config(&$config) {
|
||||||
_twinglecampaign_civix_civicrm_config($config);
|
_twinglecampaign_civix_civicrm_config($config);
|
||||||
|
|
||||||
// This dispatcher adds an event listener to TwingleDonation.submit
|
// This dispatchers add event listeners to TwingleDonation.submit
|
||||||
// (de.systopia.twingle) and calls an API-Wrapper which maps incoming Twingle
|
// (de.systopia.twingle) and call an API-Wrapper which maps incoming Twingle
|
||||||
// donations to TwingleCampaigns.
|
// donations to TwingleCampaigns and create soft credits for event initiators.
|
||||||
Civi::dispatcher()->addListener(
|
Civi::dispatcher()->addListener(
|
||||||
'civi.api.prepare',
|
'civi.api.prepare',
|
||||||
['CRM_TwingleCampaign_Utils_APIWrapper', 'PREPARE'],
|
['CRM_TwingleCampaign_Utils_APIWrapper', 'PREPARE'],
|
||||||
-100
|
-100
|
||||||
);
|
);
|
||||||
|
Civi::dispatcher()->addListener(
|
||||||
|
'civi.api.respond',
|
||||||
|
['CRM_TwingleCampaign_Utils_APIWrapper', 'RESPOND'],
|
||||||
|
-100
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue