From bea412dd398c8a6a379172c695bd0f10061db626 Mon Sep 17 00:00:00 2001
From: Matthew Wire <mjw@mjwconsult.co.uk>
Date: Mon, 16 May 2022 11:38:20 +0100
Subject: [PATCH] Add a lock around recording refund payment

---
 CRM/Core/Payment/StripeIPN.php | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/CRM/Core/Payment/StripeIPN.php b/CRM/Core/Payment/StripeIPN.php
index 1fd5fd62..94174f9c 100644
--- a/CRM/Core/Payment/StripeIPN.php
+++ b/CRM/Core/Payment/StripeIPN.php
@@ -655,7 +655,18 @@ class CRM_Core_Payment_StripeIPN {
       $refundParams['cancelled_payment_id'] = $cancelledPaymentID;
     }
 
-    $this->updateContributionRefund($refundParams);
+    $lock = Civi::lockManager()->acquire('data.contribute.contribution.' . $refundParams['contribution_id']);
+    if (!$lock->isAcquired()) {
+      \Civi::log()->error('Could not acquire lock to record refund for contribution: ' . $refundParams['contribution_id']);
+    }
+    $refundPayment = civicrm_api3('Payment', 'get', [
+      'trxn_id' => $refund['refund_trxn_id'],
+      'total_amount' => $refundParams['total_amount'],
+    ]);
+    if (empty($refundPayment['count'])) {
+      $this->updateContributionRefund($refundParams);
+    }
+    $lock->release();
     return 'OK - refund recorded';
   }
 
-- 
GitLab