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
|
||||
|
||||
use CRM_TwingleCampaign_ExtensionUtil as E;
|
||||
|
||||
/**
|
||||
* # APIWrapper class for TwingleDonation.submit
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* ## Callback method for event listener
|
||||
* ## PREPARE callback method for event listener
|
||||
*
|
||||
* @param $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
|
||||
* 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()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue