CRM-15387 - Add legacy support for honor_contact_id in contribution create api, fix...
authorDave Greenberg <dave@civicrm.org>
Mon, 29 Sep 2014 23:40:41 +0000 (16:40 -0700)
committerDave Greenberg <dave@civicrm.org>
Mon, 29 Sep 2014 23:40:41 +0000 (16:40 -0700)
----------------------------------------
* CRM-15387:
  https://issues.civicrm.org/jira/browse/CRM-15387

api/v3/Contribution.php
api/v3/examples/Contribution/ContributionCreateWithHonoreeContact.php [new file with mode: 0644]
api/v3/examples/Contribution/ContributionCreateWithSoftCreditDefaults.php [new file with mode: 0644]
tests/phpunit/api/v3/ContributionTest.php

index a873073d85f434d040aa29970300fd545f6c8081..17403cff09c932a4b5aabf0883294b8d414833b6 100644 (file)
@@ -54,13 +54,6 @@ function civicrm_api3_contribution_create(&$params) {
   _civicrm_api3_custom_format_params($params, $values, 'Contribution');
   $params = array_merge($params, $values);
 
-  //legacy soft credit handling - recommended approach is chaining
-  if(!empty($params['soft_credit_to'])){
-    $params['soft_credit'] = array(array(
-      'contact_id' => $params['soft_credit_to'],
-      'amount' => $params['total_amount']));
-  }
-
   if (!empty($params['id']) && !empty($params['contribution_status_id'])) {
     $error = array();
     //throw error for invalid status change such as setting completed back to pending
@@ -71,6 +64,9 @@ function civicrm_api3_contribution_create(&$params) {
       throw new API_Exception($error['contribution_status_id']);
     }
   }
+
+  _civicrm_api3_contribution_create_legacy_support_45($params);
+
   return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Contribution');
 }
 
@@ -109,6 +105,13 @@ function _civicrm_api3_contribution_create_spec(&$params) {
     'description' => 'ID of Contact to be Soft credited to',
     'FKClassName' => 'CRM_Contact_DAO_Contact',
   );
+  $params['honor_contact_id'] = array(
+    'name' => 'honor_contact_id',
+    'title' => 'Honoree contact ID',
+    'type' => 1,
+    'description' => 'ID of honoree contact',
+    'FKClassName' => 'CRM_Contact_DAO_Contact',
+  );
   // note this is a recommended option but not adding as a default to avoid
   // creating unnecessary changes for the dev
   $params['skipRecentView'] = array(
@@ -131,6 +134,28 @@ function _civicrm_api3_contribution_create_spec(&$params) {
   );
 }
 
+/**
+* Support for schema changes made in 4.5
+* The main purpose of the API is to provide integrators a level of stability not provided by
+* the core code or schema - this means we have to provide support for api calls (where possible)
+* across schema changes.
+*/
+function _civicrm_api3_contribution_create_legacy_support_45(&$params){
+  //legacy soft credit handling - recommended approach is chaining
+  if(!empty($params['soft_credit_to'])){
+    $params['soft_credit'] = array(array(
+      'contact_id'          => $params['soft_credit_to'],
+      'amount'              => $params['total_amount'],
+      'soft_credit_type_id' => CRM_Core_OptionGroup::getDefaultValue("soft_credit_type")));
+  }
+  if(!empty($params['honor_contact_id'])){
+    $params['soft_credit'] = array(array(
+      'contact_id'          => $params['honor_contact_id'],
+      'amount'              => $params['total_amount'],
+      'soft_credit_type_id' => CRM_Core_OptionGroup::getValue('soft_credit_type', 'in_honor_of', 'name')));
+  }
+}
+
 /**
  * Delete a contribution
  *
diff --git a/api/v3/examples/Contribution/ContributionCreateWithHonoreeContact.php b/api/v3/examples/Contribution/ContributionCreateWithHonoreeContact.php
new file mode 100644 (file)
index 0000000..6484675
--- /dev/null
@@ -0,0 +1,101 @@
+<?php
+/**
+ * Test Generated example of using contribution create API
+ * Demonstrates creating contribution with Soft Credit by passing in honor_contact_id *
+ */
+function contribution_create_example(){
+$params = array(
+  'contact_id' => 3,
+  'receive_date' => '20120511',
+  'total_amount' => '100',
+  'financial_type_id' => 1,
+  'non_deductible_amount' => '10',
+  'fee_amount' => '5',
+  'net_amount' => '95',
+  'source' => 'SSF',
+  'contribution_status_id' => 1,
+  'honor_contact_id' => 4,
+);
+
+try{
+  $result = civicrm_api3('contribution', 'create', $params);
+}
+catch (CiviCRM_API3_Exception $e) {
+  // handle error here
+  $errorMessage = $e->getMessage();
+  $errorCode = $e->getErrorCode();
+  $errorData = $e->getExtraParams();
+  return array('error' => $errorMessage, 'error_code' => $errorCode, 'error_data' => $errorData);
+}
+
+return $result;
+}
+
+/**
+ * Function returns array of result expected from previous function
+ */
+function contribution_create_expectedresult(){
+
+  $expectedResult = array(
+  'is_error' => 0,
+  'version' => 3,
+  'count' => 1,
+  'id' => 1,
+  'values' => array(
+      '1' => array(
+          'id' => '1',
+          'contact_id' => '3',
+          'financial_type_id' => '1',
+          'contribution_page_id' => '',
+          'payment_instrument_id' => '4',
+          'receive_date' => '20120511000000',
+          'non_deductible_amount' => '10',
+          'total_amount' => '100',
+          'fee_amount' => '5',
+          'net_amount' => '95',
+          'trxn_id' => '',
+          'invoice_id' => '',
+          'currency' => 'USD',
+          'cancel_date' => '',
+          'cancel_reason' => '',
+          'receipt_date' => '',
+          'thankyou_date' => '',
+          'source' => 'SSF',
+          'amount_level' => '',
+          'contribution_recur_id' => '',
+          'is_test' => '',
+          'is_pay_later' => '',
+          'contribution_status_id' => '1',
+          'address_id' => '',
+          'check_number' => '',
+          'campaign_id' => '',
+          'contribution_type_id' => '1',
+        ),
+    ),
+);
+
+  return $expectedResult;
+}
+
+
+/*
+* This example has been generated from the API test suite. The test that created it is called
+*
+* testCreateContributionWithHonoreeContact and can be found in
+* https://github.com/civicrm/civicrm-core/blob/master/tests/phpunit/api/v3/ContributionTest.php
+*
+* You can see the outcome of the API tests at
+* https://test.civicrm.org/job/CiviCRM-master-git/
+*
+* To Learn about the API read
+* http://wiki.civicrm.org/confluence/display/CRMDOC/Using+the+API
+*
+* Browse the api on your own site with the api explorer
+* http://MYSITE.ORG/path/to/civicrm/api/explorer
+*
+* Read more about testing here
+* http://wiki.civicrm.org/confluence/display/CRM/Testing
+*
+* API Standards documentation:
+* http://wiki.civicrm.org/confluence/display/CRM/API+Architecture+Standards
+*/
diff --git a/api/v3/examples/Contribution/ContributionCreateWithSoftCreditDefaults.php b/api/v3/examples/Contribution/ContributionCreateWithSoftCreditDefaults.php
new file mode 100644 (file)
index 0000000..ffe3b52
--- /dev/null
@@ -0,0 +1,101 @@
+<?php
+/**
+ * Test Generated example of using contribution create API
+ * Demonstrates creating contribution with Soft Credit defaults for amount and type *
+ */
+function contribution_create_example(){
+$params = array(
+  'contact_id' => 3,
+  'receive_date' => '20120511',
+  'total_amount' => '100',
+  'financial_type_id' => 1,
+  'non_deductible_amount' => '10',
+  'fee_amount' => '5',
+  'net_amount' => '95',
+  'source' => 'SSF',
+  'contribution_status_id' => 1,
+  'soft_credit_to' => 4,
+);
+
+try{
+  $result = civicrm_api3('contribution', 'create', $params);
+}
+catch (CiviCRM_API3_Exception $e) {
+  // handle error here
+  $errorMessage = $e->getMessage();
+  $errorCode = $e->getErrorCode();
+  $errorData = $e->getExtraParams();
+  return array('error' => $errorMessage, 'error_code' => $errorCode, 'error_data' => $errorData);
+}
+
+return $result;
+}
+
+/**
+ * Function returns array of result expected from previous function
+ */
+function contribution_create_expectedresult(){
+
+  $expectedResult = array(
+  'is_error' => 0,
+  'version' => 3,
+  'count' => 1,
+  'id' => 1,
+  'values' => array(
+      '1' => array(
+          'id' => '1',
+          'contact_id' => '3',
+          'financial_type_id' => '1',
+          'contribution_page_id' => '',
+          'payment_instrument_id' => '4',
+          'receive_date' => '20120511000000',
+          'non_deductible_amount' => '10',
+          'total_amount' => '100',
+          'fee_amount' => '5',
+          'net_amount' => '95',
+          'trxn_id' => '',
+          'invoice_id' => '',
+          'currency' => 'USD',
+          'cancel_date' => '',
+          'cancel_reason' => '',
+          'receipt_date' => '',
+          'thankyou_date' => '',
+          'source' => 'SSF',
+          'amount_level' => '',
+          'contribution_recur_id' => '',
+          'is_test' => '',
+          'is_pay_later' => '',
+          'contribution_status_id' => '1',
+          'address_id' => '',
+          'check_number' => '',
+          'campaign_id' => '',
+          'contribution_type_id' => '1',
+        ),
+    ),
+);
+
+  return $expectedResult;
+}
+
+
+/*
+* This example has been generated from the API test suite. The test that created it is called
+*
+* testCreateContributionWithSoftCreditDefaults and can be found in
+* https://github.com/civicrm/civicrm-core/blob/master/tests/phpunit/api/v3/ContributionTest.php
+*
+* You can see the outcome of the API tests at
+* https://test.civicrm.org/job/CiviCRM-master-git/
+*
+* To Learn about the API read
+* http://wiki.civicrm.org/confluence/display/CRMDOC/Using+the+API
+*
+* Browse the api on your own site with the api explorer
+* http://MYSITE.ORG/path/to/civicrm/api/explorer
+*
+* Read more about testing here
+* http://wiki.civicrm.org/confluence/display/CRM/Testing
+*
+* API Standards documentation:
+* http://wiki.civicrm.org/confluence/display/CRM/API+Architecture+Standards
+*/
index 8938845c4454220d3e327c53133406e509131ad0..41d98760782c27c021de8b30ddb8fb5305e6694c 100644 (file)
@@ -642,6 +642,45 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
     $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
   }
 
+  function testCreateContributionWithSoftCreditDefaults() {
+    $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type";
+    $subfile     = "ContributionCreateWithSoftCreditDefaults";
+    $contact2    = $this->callAPISuccess('Contact', 'create', array('display_name' => 'superman', 'contact_type' => 'Individual'));
+    $params = $this->_params + array(
+      'soft_credit_to' => $contact2['id'],
+    );
+    $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
+    $result = $this->callAPISuccess('contribution','get', array('return'=> 'soft_credit', 'sequential' => 1));
+
+    $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
+    // Default soft credit amount = contribution.total_amount
+    $this->assertEquals($this->_params['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
+    $this->assertEquals(CRM_Core_OptionGroup::getDefaultValue("soft_credit_type"), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
+
+    $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
+    $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
+  }
+
+  function testCreateContributionWithHonoreeContact() {
+    $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id";
+    $subfile     = "ContributionCreateWithHonoreeContact";
+    $contact2    = $this->callAPISuccess('Contact', 'create', array('display_name' => 'superman', 'contact_type' => 'Individual'));
+    $params = $this->_params + array(
+      'honor_contact_id' => $contact2['id'],
+    );
+    $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
+    $result = $this->callAPISuccess('contribution','get', array('return'=> 'soft_credit', 'sequential' => 1));
+
+    $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
+    // Default soft credit amount = contribution.total_amount
+    // Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type
+    $this->assertEquals($this->_params['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
+    $this->assertEquals(CRM_Core_OptionGroup::getValue('soft_credit_type', 'in_honor_of', 'name'), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
+
+    $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
+    $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
+  }
+
   /**
    *  Test  using example code
    */