Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions CRM/Paymentprocessingcore/BAO/PaymentAttempt.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static function findByContributionId($contributionId) {
}

$attempt = new self();
$attempt->contribution_id = $contributionId;
$attempt->contribution_id = (string) $contributionId;

/** @var CRM_Paymentprocessingcore_DAO_PaymentAttempt $attempt */
if ($attempt->find(TRUE)) {
Expand All @@ -111,6 +111,7 @@ public static function findByContributionId($contributionId) {
public static function getStatuses() {
return [
'pending' => E::ts('Pending'),
'processing' => E::ts('Processing'),
'completed' => E::ts('Completed'),
'failed' => E::ts('Failed'),
'cancelled' => E::ts('Cancelled'),
Expand Down Expand Up @@ -143,7 +144,7 @@ public static function validateStatus(string $status): void {
* Update payment attempt status with validation.
*
* @param int $id Payment attempt ID
* @param string $status New status: 'pending', 'completed', 'failed', 'cancelled'
* @param string $status New status: 'pending', 'processing', 'completed', 'failed', 'cancelled'
*
* @return void
*
Expand All @@ -158,4 +159,27 @@ public static function updateStatus(int $id, string $status): void {
]);
}

/**
* Atomically update payment attempt status with optimistic locking.
*
* Only updates if the current status matches the expected status.
* This prevents race conditions when multiple workers try to process
* the same payment attempt simultaneously.
*
* @param int $id Payment attempt ID
* @param string $expectedStatus Current expected status
* @param string $newStatus New status to set
*
* @return bool TRUE if update was successful, FALSE if status didn't match
*/
public static function updateStatusAtomic(int $id, string $expectedStatus, string $newStatus): bool {
$result = \Civi\Api4\PaymentAttempt::update(FALSE)
->addWhere('id', '=', $id)
->addWhere('status', '=', $expectedStatus)
->setValues(['status' => $newStatus])
->execute();

return $result->count() > 0;
}

}
Comment thread
erawat marked this conversation as resolved.
6 changes: 3 additions & 3 deletions CRM/Paymentprocessingcore/BAO/PaymentProcessorCustomer.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public static function findByContactAndProcessor(int $contactId, int $paymentPro
}

$customer = new self();
$customer->contact_id = $contactId;
$customer->payment_processor_id = $paymentProcessorId;
$customer->contact_id = (string) $contactId;
$customer->payment_processor_id = (string) $paymentProcessorId;

if ($customer->find(TRUE)) {
/** @var array{id: int, contact_id: int, payment_processor_id: int, processor_customer_id: string, created_date: string} $data */
Expand Down Expand Up @@ -77,7 +77,7 @@ public static function findByProcessorCustomerId(string $processorCustomerId, in

$customer = new self();
$customer->processor_customer_id = $processorCustomerId;
$customer->payment_processor_id = $paymentProcessorId;
$customer->payment_processor_id = (string) $paymentProcessorId;

if ($customer->find(TRUE)) {
/** @var array{id: int, contact_id: int, payment_processor_id: int, processor_customer_id: string, created_date: string} $data */
Expand Down
2 changes: 1 addition & 1 deletion CRM/Paymentprocessingcore/BAO/PaymentWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static function create($params) {
$instance = new $className();
$instance->copyValues($params);
$instance->save();
CRM_Utils_Hook::post($hook, $entityName, $instance->id, $instance);
CRM_Utils_Hook::post($hook, $entityName, (int) $instance->id, $instance);

return $instance;
}
Expand Down
Loading