From bdf636a34ef8f5236dc28aad691b6a0a372744f9 Mon Sep 17 00:00:00 2001 From: "Matthew Wire (MJW Consulting)" Date: Mon, 14 Oct 2019 10:47:54 +0100 Subject: [PATCH] Extract deprecated Contribution.transact API into it's own file --- api/v3/Contribution.php | 49 -------------------- api/v3/Contribution/Transact.php | 79 ++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 49 deletions(-) create mode 100644 api/v3/Contribution/Transact.php diff --git a/api/v3/Contribution.php b/api/v3/Contribution.php index f190f3eb32..35425bd528 100644 --- a/api/v3/Contribution.php +++ b/api/v3/Contribution.php @@ -396,55 +396,6 @@ function _civicrm_api3_contribute_format_params($params, &$values) { return []; } -/** - * Adjust Metadata for Transact action. - * - * The metadata is used for setting defaults, documentation & validation. - * - * @param array $params - * Array of parameters determined by getfields. - */ -function _civicrm_api3_contribution_transact_spec(&$params) { - $fields = civicrm_api3('Contribution', 'getfields', ['action' => 'create']); - $params = array_merge($params, $fields['values']); - $params['receive_date']['api.default'] = 'now'; -} - -/** - * Process a transaction and record it against the contact. - * - * @param array $params - * Input parameters. - * - * @return array - * contribution of created or updated record (or a civicrm error) - */ -function civicrm_api3_contribution_transact($params) { - // Set some params specific to payment processing - // @todo - fix this function - none of the results checked by civicrm_error would ever be an array with - // 'is_error' set - // also trxn_id is not saved. - // but since there is no test it's not desirable to jump in & make the obvious changes. - $params['payment_processor_mode'] = empty($params['is_test']) ? 'live' : 'test'; - $params['amount'] = $params['total_amount']; - if (!isset($params['net_amount'])) { - $params['net_amount'] = $params['amount']; - } - if (!isset($params['invoiceID']) && isset($params['invoice_id'])) { - $params['invoiceID'] = $params['invoice_id']; - } - - // Some payment processors expect a unique invoice_id - generate one if not supplied - $params['invoice_id'] = CRM_Utils_Array::value('invoice_id', $params, md5(uniqid(rand(), TRUE))); - - $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($params['payment_processor'], $params['payment_processor_mode']); - $paymentProcessor['object']->doPayment($params); - - $params['payment_instrument_id'] = $paymentProcessor['object']->getPaymentInstrumentID(); - - return civicrm_api('Contribution', 'create', $params); -} - /** * Send a contribution confirmation (receipt or invoice). * diff --git a/api/v3/Contribution/Transact.php b/api/v3/Contribution/Transact.php new file mode 100644 index 0000000000..d97167e7f2 --- /dev/null +++ b/api/v3/Contribution/Transact.php @@ -0,0 +1,79 @@ + 'create']); + $params = array_merge($params, $fields['values']); + $params['receive_date']['api.default'] = 'now'; +} + +/** + * Process a transaction and record it against the contact. + * + * @param array $params + * Input parameters. + * + * @return array + * contribution of created or updated record (or a civicrm error) + */ +function civicrm_api3_contribution_transact($params) { + // Set some params specific to payment processing + // @todo - fix this function - none of the results checked by civicrm_error would ever be an array with + // 'is_error' set + // also trxn_id is not saved. + // but since there is no test it's not desirable to jump in & make the obvious changes. + $params['payment_processor_mode'] = empty($params['is_test']) ? 'live' : 'test'; + $params['amount'] = $params['total_amount']; + if (!isset($params['net_amount'])) { + $params['net_amount'] = $params['amount']; + } + if (!isset($params['invoiceID']) && isset($params['invoice_id'])) { + $params['invoiceID'] = $params['invoice_id']; + } + + // Some payment processors expect a unique invoice_id - generate one if not supplied + $params['invoice_id'] = CRM_Utils_Array::value('invoice_id', $params, md5(uniqid(rand(), TRUE))); + + $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($params['payment_processor'], $params['payment_processor_mode']); + $paymentProcessor['object']->doPayment($params); + + $params['payment_instrument_id'] = $paymentProcessor['object']->getPaymentInstrumentID(); + + return civicrm_api('Contribution', 'create', $params); +} -- 2.25.1