Skip to content

Commit

Permalink
PROD-230: Run contribution status update listener when payment apis a…
Browse files Browse the repository at this point in the history
…re called
  • Loading branch information
Muhammad Shahrukh committed May 14, 2024
1 parent 5cbd614 commit 3efe2d7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
55 changes: 55 additions & 0 deletions Civi/Financeextras/APIWrapper/Payment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Civi\Financeextras\APIWrapper;

use Civi\Financeextras\Event\ContributionPaymentUpdatedEvent;

class Payment {

public static function Prepare($event) {
$requestSignature = $event->getApiRequestSig();
$request = $event->getApiRequest();

if ($requestSignature === '3.payment.delete' && !empty($request['params']['id'])) {
$eftParams = ['entity_table' => 'civicrm_contribution', 'financial_trxn_id' => $request['params']['id'], 'return' => ['entity_id']];
$entity = civicrm_api3('EntityFinancialTrxn', 'getsingle', $eftParams);
if(!empty($entity['entity_id'])) {
$session = \CRM_Core_Session::singleton();
$session->set('contributionIdForDeletedPayment', $entity['entity_id']);
}
}
}

public static function Respond($event) {
$requestSignature = $event->getApiRequestSig();
$contributionId = 0;

switch ($requestSignature) {
case '3.payment.create':
$request = $event->getApiRequest();
$contributionId = !empty($request['params']['contribution_id']) ? $request['params']['contribution_id'] : 0;
break;
case '3.payment.cancel':
$request = $event->getApiRequest();
if (!empty($request['params']['id'])) {
$eftParams = ['entity_table' => 'civicrm_contribution', 'financial_trxn_id' => $request['params']['id'], 'return' => ['entity_id']];
$entity = civicrm_api3('EntityFinancialTrxn', 'getsingle', $eftParams);
$contributionId = !empty($entity['entity_id']) ? $entity['entity_id'] : 0;
}
break;
case '3.payment.delete':
$session = \CRM_Core_Session::singleton();
$contributionId = !empty($session->get('contributionIdForDeletedPayment')) ? $session->get('contributionIdForDeletedPayment') : 0;
$session->set('contributionIdForDeletedPayment', 0);
break;
}

if (!empty($contributionId)) {
\Civi::dispatcher()->dispatch(
ContributionPaymentUpdatedEvent::NAME,
new ContributionPaymentUpdatedEvent($contributionId)
);
}
}

}
2 changes: 2 additions & 0 deletions financeextras.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ function financeextras_civicrm_config(&$config) {
Civi::dispatcher()->addListener('civi.api.respond', ['Civi\Financeextras\APIWrapper\SearchDisplayRun', 'respond'], -100);
Civi::dispatcher()->addSubscriber(new Civi\Financeextras\Event\Subscriber\CreditNoteInvoiceSubscriber());
Civi::dispatcher()->addListener('civi.api.respond', ['Civi\Financeextras\APIWrapper\Contribution', 'respond'], -101);
Civi::dispatcher()->addListener('civi.api.prepare', ['Civi\Financeextras\APIWrapper\Payment', 'prepare'], -102);
Civi::dispatcher()->addListener('civi.api.respond', ['Civi\Financeextras\APIWrapper\Payment', 'respond'], -103);
Civi::dispatcher()->addListener('fe.contribution.received_payment', ['\Civi\Financeextras\Event\Listener\ContributionPaymentUpdatedListener', 'handle']);
Civi::dispatcher()->addListener('civi.api.prepare', ['Civi\Financeextras\APIWrapper\BatchListPage', 'preApiCall']);
Civi::dispatcher()->addListener('civi.token.list', 'financeextras_register_tokens');
Expand Down

0 comments on commit 3efe2d7

Please sign in to comment.