diff --git a/CRM/Core/Payment/MJWIPNTrait.php b/CRM/Core/Payment/MJWIPNTrait.php index b922ed2d253b1b17c125b4a2c9d75d21b10fc302..f519aa627740b2f3e22e4f02a20c13ccb0ea0538 100644 --- a/CRM/Core/Payment/MJWIPNTrait.php +++ b/CRM/Core/Payment/MJWIPNTrait.php @@ -94,24 +94,66 @@ trait CRM_Core_Payment_MJWIPNTrait { } /** - * Record a refunded contribution - * This function ONLY supports full refunds + * Record a refund on a contribution + * This wraps around the payment.create API to support earlier releases than features were available + * + * Examples: + * $result = civicrm_api3('Payment', 'create', [ + * 'contribution_id' => 590, + * 'total_amount' => -3, + * 'trxn_date' => 20191105200300, + * 'trxn_result_code' => "Test a refund with fees", + * 'fee_amount' => -0.25, + * 'trxn_id' => "abctx123", + * 'order_reference' => "abcor123", + * ]); + * + * Returns: + * "is_error": 0, + * "version": 3, + * "count": 1, + * "id": 465, + * "values": { + * "465": { + * "id": "465", + * "from_financial_account_id": "7", + * "to_financial_account_id": "6", + * "trxn_date": "20191105200300", + * "total_amount": "-3", + * "fee_amount": "-0.25", + * "net_amount": "", + * "currency": "USD", + * "is_payment": "1", + * "trxn_id": "abctx123", + * "trxn_result_code": "Test a refund with fees", + * "status_id": "7", + * "payment_processor_id": "" + * } + * } * * @param array $params * * @throws \CiviCRM_API3_Exception */ protected function updateContributionRefund($params) { - $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'); + $this->checkRequiredParams('updateContributionRefund', ['contribution_id', 'total_amount'], $params); - civicrm_api3('Contribution', 'create', $params); + $financialTrxn = civicrm_api3('Payment', 'create', $params); - $this->updatePaymentTrxnID($params['id'], $params['payment_trxn_id']); + // order_reference field was introduced in 5.20 but support was not available to save it via Payment.Create + if (version_compare(\CRM_Utils_System::version(), '5.20', '<')) { + // Order reference field not available so we do nothing with it. + } + else { + // @fixme We are on 5.20 or above, so have order_reference field available. But it's not yet updated by Payment.Create API + if (!empty($params['order_reference'])) { + civicrm_api3('FinancialTrxn', 'create', [ + 'id' => $financialTrxn['id'], + 'trxn_id' => $params['trxn_id'], + 'order_reference' => $params['order_reference'] ?? '', + ]); + } + } } /** @@ -245,10 +287,11 @@ trait CRM_Core_Payment_MJWIPNTrait { * Update the payment record so the trxn_id matches the actual transaction from the payment processor as we may have multiple transactions for a single payment (eg. failures, then success). * @param int $contributionID * @param string $trxnID + * @param string $orderReference * * @throws \CiviCRM_API3_Exception */ - private function updatePaymentTrxnID($contributionID, $trxnID) { + private function updatePaymentTrxnID($contributionID, $trxnID, $orderReference = '') { // @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. @@ -264,6 +307,7 @@ trait CRM_Core_Payment_MJWIPNTrait { civicrm_api3('FinancialTrxn', 'create', [ 'id' => $paymentID, 'trxn_id' => $trxnID, + 'order_reference' => $orderReference, ]); } diff --git a/CRM/Mjwshared/Check.php b/CRM/Mjwshared/Check.php new file mode 100644 index 0000000000000000000000000000000000000000..cbbaebc6642fa2e23c5bc2c014ec68f2c9d75aba --- /dev/null +++ b/CRM/Mjwshared/Check.php @@ -0,0 +1,37 @@ +<?php +/* + +--------------------------------------------------------------------+ + | Copyright CiviCRM LLC. All rights reserved. | + | | + | This work is published under the GNU AGPLv3 license with some | + | permitted exceptions and without any warranty. For full license | + | and copyright information, see https://civicrm.org/licensing | + +--------------------------------------------------------------------+ + */ + +use CRM_Mjwshared_ExtensionUtil as E; + +/** + * Class CRM_Mjwshared_Check + */ +class CRM_Mjwshared_Check { + + public static function checkRequirements(&$messages) { + $extensions = civicrm_api3('Extension', 'get', [ + 'full_name' => 'uk.co.nfpservice.onlineworldpay', + ]); + + if (empty($extensions['id']) || ($extensions['values'][$extensions['id']]['status'] === 'installed')) { + $messages[] = new CRM_Utils_Check_Message( + 'mjwshared_incompatible', + E::ts('You have the uk.co.nfpservice.onlineworldpay extension installed. + There are multiple versions of this extension on various sites and the source code has not been released. + It is known to be cause issues with other payment processors and should be disabled'), + E::ts('Incompatible Extension: uk.co.nfpservice.onlineworldpay'), + \Psr\Log\LogLevel::WARNING, + 'fa-money' + ); + } + } + +} diff --git a/CRM/Mjwshared/Utils.php b/CRM/Mjwshared/Utils.php index 0c0311bbb1ca5afa88352af38cde68f4529da0cb..fa9e374b9e96a0aa1cb947c2c77b4a555606a745 100644 --- a/CRM/Mjwshared/Utils.php +++ b/CRM/Mjwshared/Utils.php @@ -1,12 +1,21 @@ <?php -/** - * https://civicrm.org/licensing +/* + +--------------------------------------------------------------------+ + | Copyright CiviCRM LLC. All rights reserved. | + | | + | This work is published under the GNU AGPLv3 license with some | + | permitted exceptions and without any warranty. For full license | + | and copyright information, see https://civicrm.org/licensing | + +--------------------------------------------------------------------+ */ /** * Class CRM_Mjwshared_Utils */ class CRM_Mjwshared_Utils { + /********************** + * MJW_Utils: 20190822 + *********************/ /** * Return the field ID for $fieldName custom field @@ -38,4 +47,3 @@ class CRM_Mjwshared_Utils { } } - diff --git a/README.md b/README.md index 55cb98b22243afa6ce586ee0d0802c0ed6b1af03..f9678b16e93dc5b3571e23a3f7f25f0e22cfa4db 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,8 @@ The extension is licensed under [AGPL-3.0](LICENSE.txt). ## Requirements -* PHP v7.x+ -* CiviCRM 5.13+ +* PHP v7.1+ +* CiviCRM 5.19+ ## Installation diff --git a/api/v3/Contribution/Getbalance.php b/api/v3/Contribution/Getbalance.php index 01d60e08db4d1297a644863c48c57ef79aef72c5..c2cd0a522a20f9c5aa1156425cecae41191f29aa 100644 --- a/api/v3/Contribution/Getbalance.php +++ b/api/v3/Contribution/Getbalance.php @@ -11,8 +11,8 @@ function civicrm_api3_contribution_getbalance($params) { $result['id'] = $params['id']; $result['total'] = (float) CRM_Price_BAO_LineItem::getLineTotal($params['id']); - $result['balance'] = CRM_Contribute_BAO_Contribution::getContributionBalance($params['id']); - $result['paid'] = $result['total'] - $result['balance']; + $result['paid'] = (float) CRM_Core_BAO_FinancialTrxn::getTotalPayments($params['id'], TRUE) ?: 0; + $result['balance'] = $result['total'] - $result['paid']; $result['currency'] = civicrm_api3('Contribution', 'getvalue', [ 'return' => 'currency', 'id' => $params['id'], diff --git a/docs/release/release_notes.md b/docs/release/release_notes.md index f8c3db13404a5aa3031b6f23d249113863a368d9..31267bcacd8534b3bba2b56609ada8247a0ea128 100644 --- a/docs/release/release_notes.md +++ b/docs/release/release_notes.md @@ -8,6 +8,12 @@ Where: * minor: Breaking change in some circumstances, or a new feature. Read carefully and make sure you understand the impact of the change. * incremental: A "safe" change / improvement. Should *always* be safe to upgrade. +## Release 0.6 + +* Improve updateContributionRefund() function to handle new `order_reference` field and use `Payment.create` API. +* Simply calls in Contribution.getbalance to improve performance. +* Add check to warn if nfp worldpay extension is installed as it breaks things! + ## Release 0.5.1 * Fix getBillingEmail() to work in more circumstances and add tests @@ -19,7 +25,7 @@ Where: ## Release 0.4.6 * Fix missing return array on getTokenParameter. - + ## Release 0.4.5 * Remove setTokenParameter, modify getTokenParameter as we're now using pre_approval_parameters in Stripe 6.2 @@ -39,12 +45,12 @@ Where: ## Release 0.4.1 -* Fix 'is not boolean' error on IPNs. `getIsTestMode()` was returning TRUE/FALSE but the API requires 1/0. +* Fix 'is not boolean' error on IPNs. `getIsTestMode()` was returning TRUE/FALSE but the API requires 1/0. ## Release 0.4 * Fix issue with non-default currency on form when you can choose from more than one payment processor on the form. -* Add `getTokenParameter()`/`setTokenParameter()` functions to MJWTrait which should be used when setting parameters +* Add `getTokenParameter()`/`setTokenParameter()` functions to MJWTrait which should be used when setting parameters via javascript (eg. Stripe `paymentIntentID`) which are required when the payment is actually processed (via `doPayment()`). ## Release 0.3 diff --git a/info.xml b/info.xml index 041ce910dc19a09b01397a2fc83bd9efc008721d..a4a8acd5ae3c10ed65201adbf09437070e5fdce4 100644 --- a/info.xml +++ b/info.xml @@ -13,9 +13,9 @@ <url desc="Support">https://www.mjwconsult.co.uk</url> <url desc="Licensing">http://www.gnu.org/licenses/agpl-3.0.html</url> </urls> - <releaseDate>2019-11-01</releaseDate> - <version>0.5.1</version> - <develStage>stable</develStage> + <releaseDate>2019-12-22</releaseDate> + <version>0.6.beta2</version> + <develStage>beta</develStage> <compatibility> <ver>5.13</ver> </compatibility> diff --git a/mjwshared.php b/mjwshared.php index bc739cc01a1af2cbd096f75d38b06682ac9e3cf1..352fb5668fd33133ef097ed9e9b64d84b829b139 100644 --- a/mjwshared.php +++ b/mjwshared.php @@ -148,3 +148,12 @@ function mjwshared_civicrm_buildForm($formName, &$form) { ]); } } + +/** + * Implements hook_civicrm_check(). + * + * @throws \CiviCRM_API3_Exception + */ +function mjwshared_civicrm_check(&$messages) { + CRM_Mjwshared_Check::checkRequirements($messages); +}