diff --git a/api/v3/Mjwpayment.php b/api/v3/Mjwpayment.php index 6eb4dd1a4f181f32b13b048673ab61b91dce39b4..969607b00d186d90db099032d8df52bd9de8baa8 100644 --- a/api/v3/Mjwpayment.php +++ b/api/v3/Mjwpayment.php @@ -119,6 +119,8 @@ function civicrm_api3_mjwpayment_get_contribution($params) { * Adjust Metadata for Get action. * * The metadata is used for setting defaults, documentation & validation. + * @fixme replace with call to _civicrm_api3_payment_get_spec once https://github.com/civicrm/civicrm-core/pull/19449 + * is merged - 5.35? * * @param array $params * Array of parameters determined by getfields. @@ -159,7 +161,6 @@ function _civicrm_api3_mjwpayment_get_payment_spec(&$params) { /** * Retrieve a set of financial transactions which are payments. - * @todo This matches Payment.Get following https://github.com/civicrm/civicrm-core/pull/17071 which will be in CiviCRM 5.26 * * @param array $params * Input parameters. @@ -169,45 +170,11 @@ function _civicrm_api3_mjwpayment_get_payment_spec(&$params) { * @throws \CiviCRM_API3_Exception */ function civicrm_api3_mjwpayment_get_payment($params) { - $params['is_payment'] = TRUE; - $contributionID = $params['entity_id'] ?? NULL; - - // In order to support contribution id we need to do an extra lookup. - if ($contributionID) { - $eftParams = [ - 'entity_id' => $contributionID, - 'entity_table' => 'civicrm_contribution', - 'options' => ['limit' => 0], - 'financial_trxn_id.is_payment' => 1, - ]; - $eft = civicrm_api3('EntityFinancialTrxn', 'get', $eftParams)['values']; - if (empty($eft)) { - return civicrm_api3_create_success([], $params, 'Payment', 'get'); - } - foreach ($eft as $entityFinancialTrxn) { - $params['financial_trxn_id']['IN'][] = $entityFinancialTrxn['financial_trxn_id']; - } - } - - $financialTrxn = civicrm_api3('FinancialTrxn', 'get', array_merge($params, ['sequential' => FALSE]))['values']; - if ($contributionID) { - foreach ($financialTrxn as &$values) { - $values['contribution_id'] = $contributionID; - } - } - elseif (!empty($financialTrxn)) { - $entityFinancialTrxns = civicrm_api3('EntityFinancialTrxn', 'get', ['financial_trxn_id' => ['IN' => array_keys($financialTrxn)], 'entity_table' => 'civicrm_contribution', 'options' => ['limit' => 0]])['values']; - foreach ($entityFinancialTrxns as $entityFinancialTrxn) { - $financialTrxn[$entityFinancialTrxn['financial_trxn_id']]['contribution_id'] = $entityFinancialTrxn['entity_id']; - } - } - - return civicrm_api3_create_success($financialTrxn, $params, 'Mjwpayment', 'get_payment'); + return civicrm_api3_payment_get($params); } /** * Adjust Metadata for Create action. - * @fixme Spec per Payment.create in CiviCRM 5.27 * * The metadata is used for setting defaults, documentation & validation. * @@ -215,174 +182,12 @@ function civicrm_api3_mjwpayment_get_payment($params) { * Array of parameters. */ function _civicrm_api3_mjwpayment_create_payment_spec(&$params) { - $params = [ - 'contribution_id' => [ - 'api.required' => 1, - 'title' => ts('Contribution ID'), - 'type' => CRM_Utils_Type::T_INT, - // We accept order_id as an alias so that we can chain like - // civicrm_api3('Order', 'create', ['blah' => 'blah', 'contribution_status_id' => 'Pending', 'api.Payment.create => ['total_amount' => 5]] - 'api.aliases' => ['order_id'], - ], - 'total_amount' => [ - 'api.required' => 1, - 'title' => ts('Total Payment Amount'), - 'type' => CRM_Utils_Type::T_FLOAT, - ], - 'fee_amount' => [ - 'title' => ts('Fee Amount'), - 'type' => CRM_Utils_Type::T_FLOAT, - ], - 'payment_processor_id' => [ - 'name' => 'payment_processor_id', - 'type' => CRM_Utils_Type::T_INT, - 'title' => ts('Payment Processor'), - 'description' => ts('Payment Processor for this payment'), - 'where' => 'civicrm_financial_trxn.payment_processor_id', - 'table_name' => 'civicrm_financial_trxn', - 'entity' => 'FinancialTrxn', - 'bao' => 'CRM_Financial_DAO_FinancialTrxn', - 'localizable' => 0, - 'FKClassName' => 'CRM_Financial_DAO_PaymentProcessor', - ], - 'id' => [ - 'title' => ts('Payment ID'), - 'type' => CRM_Utils_Type::T_INT, - 'api.aliases' => ['payment_id'], - ], - 'trxn_date' => [ - 'title' => ts('Payment Date'), - 'type' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME, - 'api.default' => 'now', - 'api.required' => TRUE, - ], - 'is_send_contribution_notification' => [ - 'title' => ts('Send out notifications based on contribution status change?'), - 'description' => ts('Most commonly this equates to emails relating to the contribution, event, etcwhen a payment completes a contribution'), - 'type' => CRM_Utils_Type::T_BOOLEAN, - 'api.default' => TRUE, - ], - 'payment_instrument_id' => [ - 'name' => 'payment_instrument_id', - 'type' => CRM_Utils_Type::T_INT, - 'title' => ts('Payment Method'), - 'description' => ts('FK to payment_instrument option group values'), - 'where' => 'civicrm_financial_trxn.payment_instrument_id', - 'table_name' => 'civicrm_financial_trxn', - 'entity' => 'FinancialTrxn', - 'bao' => 'CRM_Financial_DAO_FinancialTrxn', - 'localizable' => 0, - 'html' => [ - 'type' => 'Select', - ], - 'pseudoconstant' => [ - 'optionGroupName' => 'payment_instrument', - 'optionEditPath' => 'civicrm/admin/options/payment_instrument', - ], - ], - 'card_type_id' => [ - 'name' => 'card_type_id', - 'type' => CRM_Utils_Type::T_INT, - 'title' => ts('Card Type ID'), - 'description' => ts('FK to accept_creditcard option group values'), - 'where' => 'civicrm_financial_trxn.card_type_id', - 'table_name' => 'civicrm_financial_trxn', - 'entity' => 'FinancialTrxn', - 'bao' => 'CRM_Financial_DAO_FinancialTrxn', - 'localizable' => 0, - 'html' => [ - 'type' => 'Select', - ], - 'pseudoconstant' => [ - 'optionGroupName' => 'accept_creditcard', - 'optionEditPath' => 'civicrm/admin/options/accept_creditcard', - ], - ], - 'trxn_result_code' => [ - 'name' => 'trxn_result_code', - 'type' => CRM_Utils_Type::T_STRING, - 'title' => ts('Transaction Result Code'), - 'description' => ts('processor result code'), - 'maxlength' => 255, - 'size' => CRM_Utils_Type::HUGE, - 'where' => 'civicrm_financial_trxn.trxn_result_code', - 'table_name' => 'civicrm_financial_trxn', - 'entity' => 'FinancialTrxn', - 'bao' => 'CRM_Financial_DAO_FinancialTrxn', - 'localizable' => 0, - ], - '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', - ], - ], - 'check_number' => [ - 'name' => 'check_number', - 'type' => CRM_Utils_Type::T_STRING, - 'title' => ts('Check Number'), - 'description' => ts('Check number'), - 'maxlength' => 255, - 'size' => 6, - 'where' => 'civicrm_financial_trxn.check_number', - 'table_name' => 'civicrm_financial_trxn', - 'entity' => 'FinancialTrxn', - 'bao' => 'CRM_Financial_DAO_FinancialTrxn', - 'localizable' => 0, - 'html' => [ - 'type' => 'Text', - ], - ], - 'pan_truncation' => [ - 'name' => 'pan_truncation', - 'type' => CRM_Utils_Type::T_STRING, - 'title' => ts('PAN Truncation'), - 'description' => ts('Last 4 digits of credit card'), - 'maxlength' => 4, - 'size' => 4, - 'where' => 'civicrm_financial_trxn.pan_truncation', - 'table_name' => 'civicrm_financial_trxn', - 'entity' => 'FinancialTrxn', - 'bao' => 'CRM_Financial_DAO_FinancialTrxn', - 'localizable' => 0, - 'html' => [ - 'type' => 'Text', - ], - ], - ]; + _civicrm_api3_payment_create_spec($params); } /** * Add a payment for a Contribution. - * @fixme Payment.create didn't support order_reference param until CiviCRM version 5.27 - * (https://github.com/civicrm/civicrm-core/pull/17278) - * This provides compatibility by copying APIv3 Payment.create + FinancialTrxn.create + * @fixme This is a copy of API3 Payment.create including some handling for bugfixes in certain versions. * * @param array $params * Input parameters. @@ -423,14 +228,6 @@ function civicrm_api3_mjwpayment_create_payment($params) { $trxn = CRM_Financial_BAO_Payment::create($params); - 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';