From 3e1ff55366455f544f1d275b7f791118ad88df1d Mon Sep 17 00:00:00 2001
From: Matthew Wire <mjw@mjwconsult.co.uk>
Date: Sat, 15 Aug 2020 00:44:19 +0100
Subject: [PATCH] Add workaround for #17777 so receive_date is not updated on
 contribution (<5.29). Wrap workaround for order_reference in (<5.27) block

---
 api/v3/Mjwpayment.php | 30 +++++++++++++++++++++++++-----
 1 file changed, 25 insertions(+), 5 deletions(-)

diff --git a/api/v3/Mjwpayment.php b/api/v3/Mjwpayment.php
index 5a67213..c75676e 100644
--- a/api/v3/Mjwpayment.php
+++ b/api/v3/Mjwpayment.php
@@ -408,13 +408,33 @@ function civicrm_api3_mjwpayment_create_payment($params) {
     civicrm_api3('Payment', 'cancel', $params);
     $params['total_amount'] = $amount;
   }
+  // @todo #17777 - we store receive_date so we can fix it later
+  if (version_compare(CRM_Utils_System::version(), '5.29', '<')) {
+    $contributionReceiveDate = (string) civicrm_api3('Contribution', 'getvalue', [
+      'id' => $params['contribution_id'],
+      'return' => 'receive_date'
+    ]);
+  }
+
   $trxn = CRM_Financial_BAO_Payment::create($params);
 
-  // @todo Payment.create didn't support order_reference param until CiviCRM version 5.27 (https://github.com/civicrm/civicrm-core/pull/17278)
-  civicrm_api3('FinancialTrxn', 'create', [
-    'id' => $trxn->id,
-    'order_reference' => $params['order_reference'],
-  ]);
+  if (version_compare(CRM_Utils_System::version(), '5.27', '<')) {
+    // @todo Payment.create didn't support order_reference param until CiviCRM version 5.27 (https://github.com/civicrm/civicrm-core/pull/17278)
+    civicrm_api3('FinancialTrxn', 'create', [
+      'id' => $trxn->id,
+      'order_reference' => $params['order_reference'],
+    ]);
+  }
+
+  if (version_compare(CRM_Utils_System::version(), '5.29', '<')) {
+    // @todo Fix contribution receive date as it should not be updated by Payment.create https://github.com/civicrm/civicrm-core/pull/17777
+    $sql = 'UPDATE civicrm_contribution SET receive_date="%2" WHERE id=%1';
+    $sqlParams = [
+      1 => [$params['contribution_id'], 'Positive'],
+      2 => [CRM_Utils_Date::isoToMysql($contributionReceiveDate), 'Date']
+    ];
+    CRM_Core_DAO::executeQuery($sql, $sqlParams);
+  }
 
   $values = [];
   _civicrm_api3_object_to_array_unique_fields($trxn, $values[$trxn->id]);
-- 
GitLab