generated from compucorp/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
DRUPALMM-207: Add ContributionPayability API for generic payability checks #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
erawat
wants to merge
1
commit into
master
Choose a base branch
from
DRUPALMM-207-contribution-payability-api
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,214 @@ | ||
| <?php | ||
|
|
||
| namespace Civi\Api4\Action\ContributionPayability; | ||
|
|
||
| use Civi\Api4\Contribution; | ||
| use Civi\Api4\Generic\AbstractAction; | ||
| use Civi\Api4\Generic\Result; | ||
| use Civi\Paymentprocessingcore\Payability\PayabilityResult; | ||
| use Civi\Paymentprocessingcore\Service\PayabilityProviderRegistry; | ||
|
|
||
| /** | ||
| * Get the payability status of contributions for a contact. | ||
| * | ||
| * This action queries contributions and uses registered payability providers | ||
| * to determine if each contribution can be paid now or is managed by the | ||
| * payment processor (e.g., recurring subscriptions, payment plans). | ||
| * | ||
| * @method int getContactId() | ||
| * @method $this setContactId(int $contactId) | ||
| * @method array|null getContributionStatus() | ||
| * @method $this setContributionStatus(array $status) | ||
| * @method string|null getStartDate() | ||
| * @method $this setStartDate(string $date) | ||
| * @method string|null getEndDate() | ||
| * @method $this setEndDate(string $date) | ||
| */ | ||
| class GetStatus extends AbstractAction { | ||
|
|
||
| /** | ||
| * Contact ID to check contributions for. | ||
| * | ||
| * @var int | ||
| * @required | ||
| */ | ||
| protected $contactId; | ||
|
|
||
| /** | ||
| * Filter by contribution status names. | ||
| * | ||
| * If not provided, all statuses are included. | ||
| * Example: ['Pending', 'Partially paid'] | ||
| * | ||
| * @var array|null | ||
| */ | ||
| protected $contributionStatus; | ||
|
|
||
| /** | ||
| * Filter contributions received on or after this date. | ||
| * | ||
| * Format: YYYY-MM-DD | ||
| * | ||
| * @var string|null | ||
| */ | ||
| protected $startDate; | ||
|
|
||
| /** | ||
| * Filter contributions received on or before this date. | ||
| * | ||
| * Format: YYYY-MM-DD | ||
| * | ||
| * @var string|null | ||
| */ | ||
| protected $endDate; | ||
|
|
||
| /** | ||
| * Execute the action. | ||
| * | ||
| * @param \Civi\Api4\Generic\Result $result | ||
| * | ||
| * @throws \CRM_Core_Exception | ||
| */ | ||
| public function _run(Result $result) { | ||
| $contributions = $this->loadContributions(); | ||
|
|
||
| if (empty($contributions)) { | ||
| return; | ||
| } | ||
|
|
||
| // Group contributions by payment processor type | ||
| $groupedByProcessor = $this->groupByProcessorType($contributions); | ||
|
|
||
| // Get payability registry | ||
| $registry = $this->getPayabilityRegistry(); | ||
|
|
||
| // Process each processor type | ||
| $payabilityResults = []; | ||
| foreach ($groupedByProcessor as $processorType => $contributionIds) { | ||
| if ($registry->hasProvider($processorType)) { | ||
| $provider = $registry->getProvider($processorType); | ||
| $providerResults = $provider->getPayabilityForContributions($contributionIds); | ||
| $payabilityResults = array_merge($payabilityResults, $providerResults); | ||
| } | ||
| } | ||
|
|
||
| // Build final result set | ||
| foreach ($contributions as $contribution) { | ||
| $contributionId = (int) $contribution['id']; | ||
| $processorType = $contribution['payment_processor_type'] ?? NULL; | ||
|
|
||
| // Base contribution data | ||
| $row = [ | ||
| 'id' => $contributionId, | ||
| 'contact_id' => (int) $contribution['contact_id'], | ||
| 'total_amount' => $contribution['total_amount'], | ||
| 'currency' => $contribution['currency'], | ||
| 'receive_date' => $contribution['receive_date'], | ||
| 'contribution_status' => $contribution['contribution_status_id:name'], | ||
| 'payment_processor_type' => $processorType, | ||
| ]; | ||
|
|
||
| // Add payability info | ||
| if (isset($payabilityResults[$contributionId])) { | ||
| $payability = $payabilityResults[$contributionId]; | ||
| if ($payability instanceof PayabilityResult) { | ||
| $row = array_merge($row, $payability->toArray()); | ||
| } | ||
| else { | ||
| // Handle array format from duck-typed providers | ||
| $row['can_pay_now'] = $payability['can_pay_now'] ?? NULL; | ||
| $row['payability_reason'] = $payability['payability_reason'] ?? NULL; | ||
| $row['payment_type'] = $payability['payment_type'] ?? NULL; | ||
| $row['payability_metadata'] = $payability['payability_metadata'] ?? []; | ||
| } | ||
| } | ||
| else { | ||
| // No provider registered for this processor type | ||
| $row['can_pay_now'] = NULL; | ||
| $row['payability_reason'] = $processorType | ||
| ? "No payability provider registered for processor type: {$processorType}" | ||
| : 'No payment processor associated'; | ||
| $row['payment_type'] = NULL; | ||
| $row['payability_metadata'] = []; | ||
| } | ||
|
|
||
| $result[] = $row; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Load contributions for the contact with filters applied. | ||
| * | ||
| * @return array | ||
| * | ||
| * @throws \CRM_Core_Exception | ||
| */ | ||
| private function loadContributions(): array { | ||
| $query = Contribution::get($this->checkPermissions) | ||
| ->addSelect( | ||
| 'id', | ||
| 'contact_id', | ||
| 'total_amount', | ||
| 'currency', | ||
| 'receive_date', | ||
| 'contribution_status_id:name', | ||
| 'contribution_recur_id', | ||
| 'payment_processor_id', | ||
| 'payment_processor_id.payment_processor_type_id:name' | ||
| ) | ||
| ->addWhere('contact_id', '=', $this->contactId); | ||
|
|
||
| // Apply status filter | ||
| if (!empty($this->contributionStatus)) { | ||
| $query->addWhere('contribution_status_id:name', 'IN', $this->contributionStatus); | ||
| } | ||
|
|
||
| // Apply date filters | ||
| if (!empty($this->startDate)) { | ||
| $query->addWhere('receive_date', '>=', $this->startDate); | ||
| } | ||
| if (!empty($this->endDate)) { | ||
| $query->addWhere('receive_date', '<=', $this->endDate . ' 23:59:59'); | ||
| } | ||
|
|
||
| $contributions = $query->execute()->getArrayCopy(); | ||
|
|
||
| // Normalize processor type field name | ||
| foreach ($contributions as &$contribution) { | ||
| $contribution['payment_processor_type'] = | ||
| $contribution['payment_processor_id.payment_processor_type_id:name'] ?? NULL; | ||
| unset($contribution['payment_processor_id.payment_processor_type_id:name']); | ||
| } | ||
|
|
||
| return $contributions; | ||
| } | ||
|
|
||
| /** | ||
| * Group contribution IDs by payment processor type. | ||
| * | ||
| * @param array $contributions | ||
| * | ||
| * @return array<string, array<int>> | ||
| * Array keyed by processor type, containing arrays of contribution IDs. | ||
| */ | ||
| private function groupByProcessorType(array $contributions): array { | ||
|
Check failure on line 194 in Civi/Api4/Action/ContributionPayability/GetStatus.php
|
||
| $grouped = []; | ||
|
|
||
| foreach ($contributions as $contribution) { | ||
| $processorType = $contribution['payment_processor_type'] ?? '_none_'; | ||
| $grouped[$processorType][] = (int) $contribution['id']; | ||
| } | ||
|
|
||
| return $grouped; | ||
| } | ||
|
|
||
| /** | ||
| * Get the payability provider registry. | ||
| * | ||
| * @return \Civi\Paymentprocessingcore\Service\PayabilityProviderRegistry | ||
| */ | ||
| private function getPayabilityRegistry(): PayabilityProviderRegistry { | ||
| return \Civi::service('paymentprocessingcore.payability_registry'); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| <?php | ||
|
|
||
| namespace Civi\Api4; | ||
|
|
||
| /** | ||
| * ContributionPayability API - Check if contributions can be paid now. | ||
| * | ||
| * This API provides a generic way to check the payability status of | ||
| * contributions across multiple payment processors. Each processor | ||
| * extension registers a PayabilityProvider that implements | ||
| * processor-specific logic for determining if a contribution can be | ||
| * paid immediately or is managed by the payment processor. | ||
| * | ||
| * @searchable none | ||
| * @since 1.0 | ||
| * @package Civi\Api4 | ||
| */ | ||
| class ContributionPayability extends Generic\AbstractEntity { | ||
|
|
||
| /** | ||
| * Get the payability status of contributions for a contact. | ||
| * | ||
| * @param bool $checkPermissions | ||
| * | ||
| * @return \Civi\Api4\Action\ContributionPayability\GetStatus | ||
| */ | ||
| public static function getStatus($checkPermissions = TRUE) { | ||
| return (new Action\ContributionPayability\GetStatus(__CLASS__, __FUNCTION__)) | ||
| ->setCheckPermissions($checkPermissions); | ||
| } | ||
|
|
||
| /** | ||
| * Get field definitions for the entity. | ||
| * | ||
| * @param bool $checkPermissions | ||
| * | ||
| * @return \Civi\Api4\Generic\BasicGetFieldsAction | ||
| */ | ||
| public static function getFields($checkPermissions = TRUE) { | ||
| return (new Generic\BasicGetFieldsAction(__CLASS__, __FUNCTION__, function () { | ||
| return [ | ||
| [ | ||
| 'name' => 'id', | ||
| 'title' => 'Contribution ID', | ||
| 'data_type' => 'Integer', | ||
| 'readonly' => TRUE, | ||
| ], | ||
| [ | ||
| 'name' => 'contact_id', | ||
| 'title' => 'Contact ID', | ||
| 'data_type' => 'Integer', | ||
| 'readonly' => TRUE, | ||
| ], | ||
| [ | ||
| 'name' => 'total_amount', | ||
| 'title' => 'Total Amount', | ||
| 'data_type' => 'Money', | ||
| 'readonly' => TRUE, | ||
| ], | ||
| [ | ||
| 'name' => 'currency', | ||
| 'title' => 'Currency', | ||
| 'data_type' => 'String', | ||
| 'readonly' => TRUE, | ||
| ], | ||
| [ | ||
| 'name' => 'receive_date', | ||
| 'title' => 'Receive Date', | ||
| 'data_type' => 'Timestamp', | ||
| 'readonly' => TRUE, | ||
| ], | ||
| [ | ||
| 'name' => 'contribution_status', | ||
| 'title' => 'Contribution Status', | ||
| 'data_type' => 'String', | ||
| 'readonly' => TRUE, | ||
| ], | ||
| [ | ||
| 'name' => 'payment_processor_type', | ||
| 'title' => 'Payment Processor Type', | ||
| 'data_type' => 'String', | ||
| 'readonly' => TRUE, | ||
| ], | ||
| [ | ||
| 'name' => 'can_pay_now', | ||
| 'title' => 'Can Pay Now', | ||
| 'data_type' => 'Boolean', | ||
| 'readonly' => TRUE, | ||
| 'description' => 'TRUE if user can pay, FALSE if managed by processor, NULL if no provider registered', | ||
| ], | ||
| [ | ||
| 'name' => 'payability_reason', | ||
| 'title' => 'Payability Reason', | ||
| 'data_type' => 'String', | ||
| 'readonly' => TRUE, | ||
| ], | ||
| [ | ||
| 'name' => 'payment_type', | ||
| 'title' => 'Payment Type', | ||
| 'data_type' => 'String', | ||
| 'readonly' => TRUE, | ||
| 'description' => 'Type: one_off, subscription, or payment_plan', | ||
| ], | ||
| [ | ||
| 'name' => 'payability_metadata', | ||
| 'title' => 'Payability Metadata', | ||
| 'data_type' => 'Array', | ||
| 'readonly' => TRUE, | ||
| 'description' => 'Processor-specific metadata', | ||
| ], | ||
| ]; | ||
| }))->setCheckPermissions($checkPermissions); | ||
| } | ||
|
|
||
| /** | ||
| * Define permissions for the entity. | ||
| * | ||
| * @return array | ||
| */ | ||
| public static function permissions() { | ||
| return [ | ||
| 'meta' => ['access CiviCRM'], | ||
| 'default' => ['access CiviContribute'], | ||
| 'getStatus' => ['access CiviContribute'], | ||
| ]; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
array_mergehere is incorrect and will lead to data corruption. When merging arrays with integer keys,array_mergere-indexes the keys, which means the contribution IDs will be lost and replaced with a 0-based index. This will break the mapping between contributions and their payability results.To merge the arrays while preserving the integer keys, you should use
array_replace()or the+operator.array_replace()is generally safer and more explicit about its behavior.