From ac9da104be82316d1eb1698e083c022a0c0157d9 Mon Sep 17 00:00:00 2001
From: Matthew Wire <mjw@mjwconsult.co.uk>
Date: Mon, 16 May 2022 11:11:28 +0100
Subject: [PATCH] Check if refund already recorded

---
 CRM/Core/Payment/MJWIPNTrait.php     | 2 +-
 CRM/Mjwshared/Form/PaymentRefund.php | 9 +++++++--
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/CRM/Core/Payment/MJWIPNTrait.php b/CRM/Core/Payment/MJWIPNTrait.php
index b414c3b..d20be4a 100644
--- a/CRM/Core/Payment/MJWIPNTrait.php
+++ b/CRM/Core/Payment/MJWIPNTrait.php
@@ -528,7 +528,7 @@ trait CRM_Core_Payment_MJWIPNTrait {
    */
   protected function updateContributionRefund($params) {
     $this->checkRequiredParams('updateContributionRefund', ['contribution_id', 'total_amount'], $params);
-    $params['payment_processor_id'] = $this->_paymentProcessor->getID();
+    $params['payment_processor_id'] = $this->getPaymentProcessor()->getID();
     civicrm_api3('Mjwpayment', 'create_payment', $params);
   }
 
diff --git a/CRM/Mjwshared/Form/PaymentRefund.php b/CRM/Mjwshared/Form/PaymentRefund.php
index 82cc426..76f94fc 100644
--- a/CRM/Mjwshared/Form/PaymentRefund.php
+++ b/CRM/Mjwshared/Form/PaymentRefund.php
@@ -111,8 +111,13 @@ class CRM_Mjwshared_Form_PaymentRefund extends CRM_Core_Form {
           'payment_processor_id' => $payment['payment_processor_id'],
         ];
 
-        // Record the refund in CiviCRM
-        civicrm_api3('Mjwpayment', 'create_payment', $refundPaymentParams);
+        $refundPayment = civicrm_api3('Payment', 'get', [
+          'trxn_id' => $refund['refund_trxn_id'],
+        ]);
+        if (empty($refundPayment['count'])) {
+          // Record the refund in CiviCRM
+          civicrm_api3('Mjwpayment', 'create_payment', $refundPaymentParams);
+        }
         $message = E::ts('Refund was processed successfully.');
 
         if ($formValues['cancel_participants'] && !empty($participantIDs)) {
-- 
GitLab