diff --git a/CRM/Core/Payment/MJWIPNTrait.php b/CRM/Core/Payment/MJWIPNTrait.php index a66dd158e09299e3c5edaa3325415bcbd0fc31c1..b922ed2d253b1b17c125b4a2c9d75d21b10fc302 100644 --- a/CRM/Core/Payment/MJWIPNTrait.php +++ b/CRM/Core/Payment/MJWIPNTrait.php @@ -95,22 +95,23 @@ trait CRM_Core_Payment_MJWIPNTrait { /** * Record a refunded contribution + * This function ONLY supports full refunds + * * @param array $params * * @throws \CiviCRM_API3_Exception */ protected function updateContributionRefund($params) { - $this->checkRequiredParams('updateContributionRefund', ['id', 'total_amount'], $params); - - if ($params['total_amount'] > 0) { - $params['total_amount'] = -$params['total_amount']; - } + $this->checkRequiredParams('updateContributionRefund', ['id', 'payment_trxn_id'], $params); if (empty($params['cancel_date'])) { $params['cancel_date'] = date('YmdHis'); } + $params['contribution_status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Refunded'); civicrm_api3('Contribution', 'create', $params); + + $this->updatePaymentTrxnID($params['id'], $params['payment_trxn_id']); } /** @@ -251,12 +252,17 @@ trait CRM_Core_Payment_MJWIPNTrait { // @fixme: There needs to be a better way to do this!! // Contribution trxn_id = invoice_id, payment trxn_id = charge_id // but calling completetransaction does not allow us to do that. + // @fixme: 2! Payment.get does not support the sort option so we can't do limit=1,sort=id DESC $payment = civicrm_api3('Payment', 'get', [ 'contribution_id' => $contributionID, - 'options' => ['limit' => 1, 'sort' => "id DESC"], ]); + if (empty($payment['count'])) { + $this->exception('No payments found for contribution ID: ' . $contributionID); + } + krsort($payment['values']); + $paymentID = CRM_Utils_Array::first($payment['values'])['id']; civicrm_api3('FinancialTrxn', 'create', [ - 'id' => $payment['id'], + 'id' => $paymentID, 'trxn_id' => $trxnID, ]); }