Merge pull request #3196 from JoeMurray/master
[civicrm-core.git] / api / v3 / examples / Contribution / CreateWithNestedLineItems.php
index fb113f584c6b3c66572f4fe169fb58864ccc6bc1..04e5644e780c739d27f8026f3c4fd4a1695dc4a0 100644 (file)
@@ -1,10 +1,10 @@
 <?php
-
-/*
- Create Contribution with Nested Line Items
+/**
+ * Test Generated example of using contribution create API
+ * Create Contribution with Nested Line Items *
  */
 function contribution_create_example(){
-$params = array( 
+$params = array(
   'contact_id' => 1,
   'receive_date' => '20120511',
   'total_amount' => '100',
@@ -17,16 +17,15 @@ $params = array(
   'invoice_id' => 67890,
   'source' => 'SSF',
   'contribution_status_id' => 1,
-  'version' => 3,
   'skipLineItem' => 1,
-  'api.line_item.create' => array( 
-      '0' => array( 
+  'api.line_item.create' => array(
+      '0' => array(
           'price_field_id' => 1,
           'qty' => 2,
           'line_total' => '20',
           'unit_price' => '10',
         ),
-      '1' => array( 
+      '1' => array(
           'price_field_id' => 1,
           'qty' => 1,
           'line_total' => '80',
@@ -35,23 +34,32 @@ $params = array(
     ),
 );
 
-  $result = civicrm_api( 'contribution','create',$params );
+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;
+return $result;
 }
 
-/*
+/**
  * Function returns array of result expected from previous function
  */
 function contribution_create_expectedresult(){
 
-  $expectedResult = array( 
+  $expectedResult = array(
   'is_error' => 0,
   'version' => 3,
   'count' => 1,
   'id' => 1,
-  'values' => array( 
-      '1' => array( 
+  'values' => array(
+      '1' => array(
           'id' => '1',
           'contact_id' => '1',
           'financial_type_id' => '1',
@@ -78,28 +86,17 @@ function contribution_create_expectedresult(){
           'contribution_status_id' => '1',
           'honor_type_id' => '',
           'address_id' => '',
-          'check_number' => 'null',
+          'check_number' => '',
           'campaign_id' => '',
           'contribution_type_id' => '1',
-          'api.line_item.create' => array( 
-              '0' => array( 
+          'api.line_item.create' => array(
+              '0' => array(
                   'is_error' => 0,
-                  'undefined_fields' => array( 
-                      '0' => 'label',
-                      '1' => 'entity_id',
-                      '2' => 'entity_table',
-                      '3' => 'contribution_id',
-                      '4' => 'api.has_parent',
-                      '5' => 'price_field_id',
-                      '6' => 'qty',
-                      '7' => 'line_total',
-                      '8' => 'unit_price',
-                    ),
                   'version' => 3,
                   'count' => 1,
                   'id' => 1,
-                  'values' => array( 
-                      '0' => array( 
+                  'values' => array(
+                      '0' => array(
                           'id' => '1',
                           'entity_table' => 'civicrm_contribution',
                           'entity_id' => '1',
@@ -115,24 +112,13 @@ function contribution_create_expectedresult(){
                         ),
                     ),
                 ),
-              '1' => array( 
+              '1' => array(
                   'is_error' => 0,
-                  'undefined_fields' => array( 
-                      '0' => 'label',
-                      '1' => 'entity_id',
-                      '2' => 'entity_table',
-                      '3' => 'contribution_id',
-                      '4' => 'api.has_parent',
-                      '5' => 'price_field_id',
-                      '6' => 'qty',
-                      '7' => 'line_total',
-                      '8' => 'unit_price',
-                    ),
                   'version' => 3,
                   'count' => 1,
                   'id' => 2,
-                  'values' => array( 
-                      '0' => array( 
+                  'values' => array(
+                      '0' => array(
                           'id' => '2',
                           'entity_table' => 'civicrm_contribution',
                           'entity_id' => '1',
@@ -153,7 +139,7 @@ function contribution_create_expectedresult(){
     ),
 );
 
-  return $expectedResult  ;
+  return $expectedResult;
 }
 
 
@@ -161,20 +147,20 @@ function contribution_create_expectedresult(){
 * This example has been generated from the API test suite. The test that created it is called
 *
 * testCreateContributionChainedLineItems and can be found in
-* http://svn.civicrm.org/civicrm/trunk/tests/phpunit/CiviTest/api/v3/ContributionTest.php
+* https://github.com/civicrm/civicrm-core/blob/master/tests/phpunit/api/v3/ContributionTest.php
 *
 * You can see the outcome of the API tests at
-* http://tests.dev.civicrm.org/trunk/results-api_v3
+* https://test.civicrm.org/job/CiviCRM-master-git/
 *
 * To Learn about the API read
-* http://book.civicrm.org/developer/current/techniques/api/
+* http://wiki.civicrm.org/confluence/display/CRMDOC/Using+the+API
 *
-* and review the wiki at
-* http://wiki.civicrm.org/confluence/display/CRMDOC/CiviCRM+Public+APIs
+* 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
-*/
\ No newline at end of file
+*/