From 691d8993be33213b4612944e513abb1e07b315b7 Mon Sep 17 00:00:00 2001 From: Matthew Wire <mjw@mjwconsult.co.uk> Date: Wed, 25 Mar 2020 13:21:45 +0000 Subject: [PATCH] Update Mjwpayment.get_payment API --- api/v3/Mjwpayment.php | 111 ++++++++++++++++++++++++++---------------- 1 file changed, 68 insertions(+), 43 deletions(-) diff --git a/api/v3/Mjwpayment.php b/api/v3/Mjwpayment.php index 0b1421e..8b29798 100644 --- a/api/v3/Mjwpayment.php +++ b/api/v3/Mjwpayment.php @@ -9,10 +9,11 @@ /** * @todo mjwpayment.get_contribution is a replacement for Contribution.get - * mjwpayment.get_payment is a replacement for Payment.get * which support querying by contribution/payment trxn_id per https://github.com/civicrm/civicrm-core/pull/14748 * - These API functions should be REMOVED once core has the above PR merged and we increment the min version for the extension. * - The change is small, but to re-implement them here we have to copy quite a lot over. + * @todo mjwpayment.get_payment is a replacement for Payment.get: + * https://github.com/civicrm/civicrm-core/pull/16603 */ /** * Adjust Metadata for Get action. @@ -132,22 +133,39 @@ function _civicrm_api3_mjwpayment_contribution_add_supported_fields(&$contributi function _civicrm_api3_mjwpayment_get_payment_spec(&$params) { $params = [ 'contribution_id' => [ - 'title' => 'Contribution ID', + 'title' => ts('Contribution ID'), 'type' => CRM_Utils_Type::T_INT, ], 'entity_table' => [ - 'title' => 'Entity Table', + 'title' => ts('Entity Table'), 'api.default' => 'civicrm_contribution', ], 'entity_id' => [ - 'title' => 'Entity ID', + 'title' => ts('Entity ID'), 'type' => CRM_Utils_Type::T_INT, 'api.aliases' => ['contribution_id'], ], 'trxn_id' => [ - 'title' => 'Transaction ID', + 'title' => ts('Transaction ID'), + 'description' => ts('Transaction id supplied by external processor. This may not be unique.'), 'type' => CRM_Utils_Type::T_STRING, ], + 'trxn_date' => [ + 'title' => ts('Payment Date'), + 'type' => CRM_Utils_Type::T_TIMESTAMP, + ], + 'financial_trxn_id' => [ + 'title' => ts('Payment ID'), + 'description' => ts('The ID of the record in civicrm_financial_trxn'), + 'type' => CRM_Utils_Type::T_INT, + 'api.aliases' => ['payment_id', 'id'], + ], + 'is_payment' => [ + 'title' => ts('Is Payment'), + 'description' => ts('Is this entry a payment (is_payment=1) or a reversal of a payment (is_payment=0)?'), + 'type' => CRM_Utils_Type::T_BOOLEAN, + 'api.default' => TRUE, + ], ]; } @@ -162,49 +180,56 @@ function _civicrm_api3_mjwpayment_get_payment_spec(&$params) { * @throws \CiviCRM_API3_Exception */ function civicrm_api3_mjwpayment_get_payment($params) { - $financialTrxn = []; - $limit = ''; - if (isset($params['options']) && CRM_Utils_Array::value('limit', $params['options'])) { - $limit = CRM_Utils_Array::value('limit', $params['options']); - } - $params['options']['limit'] = 0; - - $ftParams['is_payment'] = 1; - if ($limit) { - $ftParams['options']['limit'] = $limit; - } + $ftParams = []; - if (!empty($params['trxn_id'])) { - $ftParams['trxn_id'] = $params['trxn_id']; - $financialTrxn = civicrm_api3('FinancialTrxn', 'get', $ftParams); - if (!empty($financialTrxn['count'])) { - $financialTrxnIDs = CRM_Utils_Array::collect('id', CRM_Utils_Array::value('values', $financialTrxn)); - $params['financial_trxn_id'] = ['IN' => $financialTrxnIDs]; - $eft = civicrm_api3('EntityFinancialTrxn', 'get', $params); - foreach ($eft['values'] as $eftID => $eftValues) { - $financialTrxn['values'][$eftValues['financial_trxn_id']]['contribution_id'] = $eftValues['entity_id']; + // To get by contribution ID we need to "join" civicrm_financial_trxn.id on civicrm_entity_financial_trxn.financial_trxn_id + // But this is a pseudo API so we can't do that directly (FinancialTrxn can). + if (isset($params['entity_id']) && ($params['entity_table'] === 'civicrm_contribution')) { + $eftParams = [ + 'entity_id' => $params['entity_id'], + 'entity_table' => $params['entity_table'], + 'options' => ['limit' => 0], + ]; + $eft = civicrm_api3('EntityFinancialTrxn', 'get', $eftParams)['values']; + $eftIds = []; + foreach ($eft as $efts) { + if (empty($efts['financial_trxn_id'])) { + continue; } + $eftIds[] = $efts['financial_trxn_id']; + $map[$efts['financial_trxn_id']] = $efts['entity_id']; + } + if (!empty($eftIds)) { + $ftParams = [ + 'id' => ['IN' => $eftIds], + 'is_payment' => $params['is_payment'], + ]; } } - else { - $eft = civicrm_api3('EntityFinancialTrxn', 'get', $params); - if (!empty($eft['values'])) { - $eftIds = []; - foreach ($eft['values'] as $efts) { - if (empty($efts['financial_trxn_id'])) { - continue; - } - $eftIds[] = $efts['financial_trxn_id']; - $map[$efts['financial_trxn_id']] = $efts['entity_id']; - } - if (!empty($eftIds)) { - $ftParams['id'] = ['IN' => $eftIds]; - $financialTrxn = civicrm_api3('FinancialTrxn', 'get', $ftParams); - foreach ($financialTrxn['values'] as &$values) { - $values['contribution_id'] = $map[$values['id']]; - } + + // If we have IDs from EntityFinancialTrxn merge them here. + $ftParams = array_merge($params, $ftParams); + $ftParams['return'] = array_values(CRM_Utils_Array::collect('name', civicrm_api3('FinancialTrxn', 'getfields')['values'])); + $ftParams['return'][] = 'id.entity_id'; + $ftParams['sequential'] = 0; + $financialTrxn = civicrm_api3('FinancialTrxn', 'get', $ftParams)['values']; + if (empty($eftIds) && !empty($financialTrxn)) { + $eftParams = [ + 'financial_trxn_id' => ['IN' => array_keys($financialTrxn)], + 'entity_table' => 'civicrm_contribution', + ]; + $eft = civicrm_api3('EntityFinancialTrxn', 'get', $eftParams)['values']; + foreach ($eft as $efts) { + if (empty($efts['financial_trxn_id'])) { + continue; } + $map[$efts['financial_trxn_id']] = $efts['entity_id']; } } - return civicrm_api3_create_success(CRM_Utils_Array::value('values', $financialTrxn, []), $params, 'Payment', 'get'); + // Map contribution_id from id.entity_id + foreach ($financialTrxn as &$values) { + $values['contribution_id'] = $map[$values['id']] ?? NULL; + } + + return civicrm_api3_create_success($financialTrxn ?? [], $params, 'Mjwpayment', 'get_payment'); } -- GitLab