diff --git a/api/v3/Mjwpayment.php b/api/v3/Mjwpayment.php index 81ac13fda62e20fb27a6ec972f9004ee75265f91..f9dff9415c19204dc74fd140f1b7578439cb4338 100644 --- a/api/v3/Mjwpayment.php +++ b/api/v3/Mjwpayment.php @@ -26,18 +26,46 @@ * Array of parameters determined by getfields. */ function _civicrm_api3_mjwpayment_get_contribution_spec(&$params) { - $params['contribution_test'] = [ - 'api.default' => 0, - 'type' => CRM_Utils_Type::T_BOOLEAN, - 'title' => 'Get Test Contributions?', - 'api.aliases' => ['is_test'], + $params = [ + 'contribution_test' => [ + 'api.default' => 0, + 'type' => CRM_Utils_Type::T_BOOLEAN, + 'title' => 'Get Test Contributions?', + 'api.aliases' => ['is_test'], + ], + 'trxn_id' => [ + 'name' => 'trxn_id', + 'type' => CRM_Utils_Type::T_STRING, + 'title' => ts('Transaction ID'), + 'description' => ts('Transaction id supplied by external processor. This may not be unique.'), + 'maxlength' => 255, + 'size' => 10, + 'where' => 'civicrm_financial_trxn.trxn_id', + 'table_name' => 'civicrm_financial_trxn', + 'entity' => 'FinancialTrxn', + 'bao' => 'CRM_Financial_DAO_FinancialTrxn', + 'localizable' => 0, + 'html' => [ + 'type' => 'Text', + ], + ], + 'order_reference' => [ + 'name' => 'order_reference', + 'type' => CRM_Utils_Type::T_STRING, + 'title' => 'Order Reference', + 'description' => 'Payment Processor external order reference', + 'maxlength' => 255, + 'size' => 25, + 'where' => 'civicrm_financial_trxn.order_reference', + 'table_name' => 'civicrm_financial_trxn', + 'entity' => 'FinancialTrxn', + 'bao' => 'CRM_Financial_DAO_FinancialTrxn', + 'localizable' => 0, + 'html' => [ + 'type' => 'Text', + ], + ], ]; - - $params['financial_type_id']['api.aliases'] = ['contribution_type_id']; - $params['payment_instrument_id']['api.aliases'] = ['contribution_payment_instrument', 'payment_instrument']; - $params['contact_id'] = $params['contribution_contact_id'] ?? NULL; - $params['contact_id']['api.aliases'] = ['contribution_contact_id']; - unset($params['contribution_contact_id']); } /** @@ -50,79 +78,38 @@ function _civicrm_api3_mjwpayment_get_contribution_spec(&$params) { * Array of contributions, if error an array with an error id and error message */ function civicrm_api3_mjwpayment_get_contribution($params) { - $foundContributions = civicrm_api3('Contribution', 'get', $params)['values']; - $contributions = []; + $payments = civicrm_api3('Mjwpayment', 'get_payment', $params); - // If we have a trxn_id check payments for that transaction ID and also return any contributions associated with those payments - // An additional array property "payment_trxn_id" will be available containing all found trxn_ids (eg. if you did ['LIKE' => 'test124%']) - if (!empty($params['trxn_id'])) { - $payments = civicrm_api3('Payment', 'get', $params); - if (!empty($payments['count'])) { - foreach ($payments['values'] as $paymentID => $paymentValues) { - if (empty($contributions[$paymentValues['contribution_id']])) { - // Get the details of each additional contribution found via a payment - $contributions[$paymentValues['contribution_id']] = CRM_Contribute_BAO_Contribution::getValuesWithMappings(['id' => $paymentValues['contribution_id']]); - } - $contributions[$paymentValues['contribution_id']]['payment_trxn_id'][] = $paymentValues['trxn_id']; - } - } - } - - foreach ($contributions as $id => $contribution) { - $softContribution = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($id, TRUE); - $contributions[$id] = array_merge($contributions[$id], $softContribution); - // format soft credit for backward compatibility - _civicrm_api3_mjwpayment_format_soft_credit($contributions[$id]); - _civicrm_api3_mjwpayment_contribution_add_supported_fields($contributions[$id]); + if ($payments['count'] > 0) { + // We found at least one payment for the params we were given. + // We may have more than one payment (eg. A payment + a refund payment) + // Return the contribution of the FIRST payment (all found payments SHOULD reference the same contribution) + $contributionID = reset($payments['values'])['contribution_id']; + $contribution = civicrm_api3('Contribution', 'getsingle', [ + 'id' => $contributionID, + 'contribution_test' => $params['contribution_test'], + ]); + $contribution['payments'] = $payments['values']; } - foreach($foundContributions as $id => $detail) { - if (isset($contributions[$id])) { - $foundContributions[$id] = $contributions[$id]; + else { + $contributionParams = [ + 'options' => ['limit' => 1, 'sort' => 'id DESC'], + 'contribution_test' => $params['contribution_test'], + ]; + if (isset($params['order_reference'])) { + $contributionParams['trxn_id'][] = $params['order_reference']; } - } - return civicrm_api3_create_success($contributions, $params, 'Contribution', 'get'); -} - -/** - * This function is used to format the soft credit for backward compatibility. - * - * As of v4.4 we support multiple soft credit, so now contribution returns array with 'soft_credit' as key - * but we still return first soft credit as a part of contribution array - * - * @param $contribution - */ -function _civicrm_api3_mjwpayment_format_soft_credit(&$contribution) { - if (!empty($contribution['soft_credit'])) { - $contribution['soft_credit_to'] = $contribution['soft_credit'][1]['contact_id']; - $contribution['soft_credit_id'] = $contribution['soft_credit'][1]['soft_credit_id']; - } -} - -/** - * Support for supported output variables. - * - * @param $contribution - */ -function _civicrm_api3_mjwpayment_contribution_add_supported_fields(&$contribution) { - // These are output fields that are supported in our test contract. - // Arguably we should also do the same with 'campaign_id' & - // 'source' - which are also fields being rendered with unique names. - // That seems more consistent with other api where we output the actual field names. - $outputAliases = [ - 'contribution_check_number' => 'check_number', - 'contribution_address_id' => 'address_id', - 'payment_instrument_id' => 'instrument_id', - 'contribution_cancel_date' => 'cancel_date', - ]; - foreach ($outputAliases as $returnName => $copyTo) { - if (array_key_exists($returnName, $contribution)) { - $contribution[$copyTo] = $contribution[$returnName]; + if (isset($params['trxn_id'])) { + $contributionParams['trxn_id'][] = $params['trxn_id']; } + if (isset($contributionParams['trxn_id'])) { + $contributionParams['trxn_id'] = ['IN' => $contributionParams['trxn_id']]; + } + $contribution = civicrm_api3('Contribution', 'getsingle', $contributionParams); } - + return civicrm_api3_create_success([$contribution['id'] => $contribution], $params, 'Mjwpayment', 'get_contribution'); } - /** * Adjust Metadata for Get action. *