From 16c0ec8d454d4da90a9e69f18d74ec75c91e95c3 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sun, 11 Aug 2013 20:22:56 -0700 Subject: [PATCH] CRM-13086 Api improvements to support webform_civicrm 4 ---------------------------------------- * CRM-13086: Support payments via Drupal Webforms http://issues.civicrm.org/jira/browse/CRM-13086 --- CRM/Case/BAO/Case.php | 20 +++++++++++++ CRM/Contribute/BAO/Contribution.php | 27 +++++++++++++++++ api/v3/Case.php | 4 +++ api/v3/Contribution.php | 45 ++++++++++++----------------- api/v3/ContributionPage.php | 1 + 5 files changed, 71 insertions(+), 26 deletions(-) diff --git a/CRM/Case/BAO/Case.php b/CRM/Case/BAO/Case.php index 53be2ddb26..96eb28eb6b 100644 --- a/CRM/Case/BAO/Case.php +++ b/CRM/Case/BAO/Case.php @@ -3216,5 +3216,25 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')'; return $clients; } + + /** + * Get options for a given case field. + * @see CRM_Core_DAO::buildOptions + * + * @param String $fieldName + * @param String $context: @see CRM_Core_DAO::buildOptionsContext + * @param Array $props: whatever is known about this dao object + */ + public static function buildOptions($fieldName, $context = NULL, $props = array()) { + $className = __CLASS__; + $params = array(); + switch ($fieldName) { + // This field is not part of this object but the api supports it + case 'medium_id': + $className = 'CRM_Activity_BAO_Activity'; + break; + } + return CRM_Core_PseudoConstant::get($className, $fieldName, $params, $context); + } } diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index e09fcf5083..e0a3d8262a 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -2855,4 +2855,31 @@ WHERE contribution_id = %1 "; self::deleteContribution($contribution->id); } } + + /** + * Get options for a given contribution field. + * @see CRM_Core_DAO::buildOptions + * + * @param String $fieldName + * @param String $context: @see CRM_Core_DAO::buildOptionsContext + * @param Array $props: whatever is known about this dao object + */ + public static function buildOptions($fieldName, $context = NULL, $props = array()) { + $className = __CLASS__; + $params = array(); + switch ($fieldName) { + // This field is not part of this object but the api supports it + case 'payment_processor': + $className = 'CRM_Contribute_BAO_ContributionPage'; + // Filter results by contribution page + if (!empty($props['contribution_page_id'])) { + $page = civicrm_api('contribution_page', 'getsingle', array('version' => 3, 'id' => ($props['contribution_page_id']))); + $types = (array) CRM_Utils_Array::value('payment_processor', $page, 0); + $params['condition'] = 'id IN (' . implode(',', $types) . ')'; + } + } + return CRM_Core_PseudoConstant::get($className, $fieldName, $params, $context); + } } + + diff --git a/api/v3/Case.php b/api/v3/Case.php index 856b335a4e..ef9490fae0 100644 --- a/api/v3/Case.php +++ b/api/v3/Case.php @@ -146,6 +146,10 @@ function _civicrm_api3_case_create_spec(&$params) { $params['contact_id']['title'] = 'Case Client'; $params['contact_id']['api.required'] = 1; $params['status_id']['api.default'] = 1; + $params['medium_id'] = array( + 'name' => 'medium_id', + 'title' => 'Activity Medium', + ); } /** diff --git a/api/v3/Contribution.php b/api/v3/Contribution.php index 8bbf12399b..84e5998bca 100644 --- a/api/v3/Contribution.php +++ b/api/v3/Contribution.php @@ -253,6 +253,18 @@ function _civicrm_api3_contribute_format_params($params, &$values, $create = FAL return array(); } +/** + * Adjust Metadata for Transact action + * + * The metadata is used for setting defaults, documentation & validation + * @param array $params array or parameters determined by getfields + */ +function _civicrm_api3_contribution_transact_spec(&$params) { + // This function calls create, so should inherit create spec + _civicrm_api3_contribution_create_spec($params); + $params['receive_date']['api.default'] = 'now'; +} + /** * Process a transaction and record it against the contact. * @@ -264,40 +276,22 @@ function _civicrm_api3_contribute_format_params($params, &$values, $create = FAL * */ function civicrm_api3_contribution_transact($params) { - $required = array('amount'); - foreach ($required as $key) { - if (!isset($params[$key])) { - return civicrm_api3_create_error("Missing parameter $key: civicrm_contribute_transact() requires a parameter '$key'."); - } - } - - // allow people to omit some values for convenience - // 'payment_processor_id' => NULL /* we could retrieve the default processor here, but only if it's missing to avoid an extra lookup */ - $defaults = array( - 'payment_processor_mode' => 'live', - ); - $params = array_merge($defaults, $params); - - // clean up / adjust some values which - if (!isset($params['total_amount'])) { - $params['total_amount'] = $params['amount']; - } + // Set some params specific to payment processing + $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['receive_date'])) { - $params['receive_date'] = date('Y-m-d'); - } if (!isset($params['invoiceID']) && isset($params['invoice_id'])) { $params['invoiceID'] = $params['invoice_id']; } - $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($params['payment_processor_id'], $params['payment_processor_mode']); + $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($params['payment_processor'], $params['payment_processor_mode']); if (civicrm_error($paymentProcessor)) { return $paymentProcessor; } - $payment = &CRM_Core_Payment::singleton($params['payment_processor_mode'], $paymentProcessor); + $payment = CRM_Core_Payment::singleton($params['payment_processor_mode'], $paymentProcessor); if (civicrm_error($payment)) { return $payment; } @@ -309,7 +303,7 @@ function civicrm_api3_contribution_transact($params) { // but actually, $payment->doDirectPayment() doesn't return a // CRM_Core_Error by itself - if (get_class($transaction) == 'CRM_Core_Error') { + if (is_object($transaction) && get_class($transaction) == 'CRM_Core_Error') { $errs = $transaction->getErrors(); if (!empty($errs)) { $last_error = array_shift($errs); @@ -317,8 +311,7 @@ function civicrm_api3_contribution_transact($params) { } } - $contribution = civicrm_api('contribution', 'create', $params); - return $contribution['values']; + return civicrm_api('contribution', 'create', $params); } /** * Send a contribution confirmation (receipt or invoice) diff --git a/api/v3/ContributionPage.php b/api/v3/ContributionPage.php index d41f05c8e1..a6981a517f 100644 --- a/api/v3/ContributionPage.php +++ b/api/v3/ContributionPage.php @@ -58,6 +58,7 @@ function civicrm_api3_contribution_page_create($params) { */ function _civicrm_api3_contribution_page_create_spec(&$params) { $params['financial_type_id']['api.required'] = 1; + $params['payment_processor']['api.aliases'] = array('payment_processor_id'); } /** -- 2.25.1