Merge pull request #5049 from totten/master-mailing-multiling
[civicrm-core.git] / api / v3 / examples / Contribution / CreateWithNestedLineItems.php
1 <?php
2 /**
3 * Test Generated example of using contribution create API.
4 *
5 * Create Contribution with Nested Line Items
6 *
7 * @return array
8 * API result array
9 */
10 function contribution_create_example() {
11 $params = array(
12 'contact_id' => 10,
13 'receive_date' => '20120511',
14 'total_amount' => '100',
15 'financial_type_id' => 1,
16 'payment_instrument_id' => 1,
17 'non_deductible_amount' => '10',
18 'fee_amount' => '50',
19 'net_amount' => '90',
20 'trxn_id' => 12345,
21 'invoice_id' => 67890,
22 'source' => 'SSF',
23 'contribution_status_id' => 1,
24 'skipLineItem' => 1,
25 'api.line_item.create' => array(
26 '0' => array(
27 'price_field_id' => 1,
28 'qty' => 2,
29 'line_total' => '20',
30 'unit_price' => '10',
31 ),
32 '1' => array(
33 'price_field_id' => 1,
34 'qty' => 1,
35 'line_total' => '80',
36 'unit_price' => '80',
37 ),
38 ),
39 );
40
41 try{
42 $result = civicrm_api3('contribution', 'create', $params);
43 }
44 catch (CiviCRM_API3_Exception $e) {
45 // Handle error here.
46 $errorMessage = $e->getMessage();
47 $errorCode = $e->getErrorCode();
48 $errorData = $e->getExtraParams();
49 return array(
50 'error' => $errorMessage,
51 'error_code' => $errorCode,
52 'error_data' => $errorData,
53 );
54 }
55
56 return $result;
57 }
58
59 /**
60 * Function returns array of result expected from previous function.
61 *
62 * @return array
63 * API result array
64 */
65 function contribution_create_expectedresult() {
66
67 $expectedResult = array(
68 'is_error' => 0,
69 'version' => 3,
70 'count' => 1,
71 'id' => 1,
72 'values' => array(
73 '1' => array(
74 'id' => '1',
75 'contact_id' => '10',
76 'financial_type_id' => '1',
77 'contribution_page_id' => '',
78 'payment_instrument_id' => '1',
79 'receive_date' => '20120511000000',
80 'non_deductible_amount' => '10',
81 'total_amount' => '100',
82 'fee_amount' => '50',
83 'net_amount' => '90',
84 'trxn_id' => '12345',
85 'invoice_id' => '67890',
86 'currency' => 'USD',
87 'cancel_date' => '',
88 'cancel_reason' => '',
89 'receipt_date' => '',
90 'thankyou_date' => '',
91 'source' => 'SSF',
92 'amount_level' => '',
93 'contribution_recur_id' => '',
94 'is_test' => '',
95 'is_pay_later' => '',
96 'contribution_status_id' => '1',
97 'address_id' => '',
98 'check_number' => '',
99 'campaign_id' => '',
100 'creditnote_id' => '',
101 'tax_amount' => 0,
102 'contribution_type_id' => '1',
103 'api.line_item.create' => array(
104 '0' => array(
105 'is_error' => 0,
106 'version' => 3,
107 'count' => 1,
108 'id' => 1,
109 'values' => array(
110 '0' => array(
111 'id' => '1',
112 'entity_table' => 'civicrm_contribution',
113 'entity_id' => '1',
114 'contribution_id' => '1',
115 'price_field_id' => '1',
116 'label' => 'line item',
117 'qty' => '2',
118 'unit_price' => '10',
119 'line_total' => '20',
120 'participant_count' => '',
121 'price_field_value_id' => '',
122 'financial_type_id' => '',
123 'deductible_amount' => '',
124 'tax_amount' => '',
125 ),
126 ),
127 ),
128 '1' => array(
129 'is_error' => 0,
130 'version' => 3,
131 'count' => 1,
132 'id' => 2,
133 'values' => array(
134 '0' => array(
135 'id' => '2',
136 'entity_table' => 'civicrm_contribution',
137 'entity_id' => '1',
138 'contribution_id' => '1',
139 'price_field_id' => '1',
140 'label' => 'line item',
141 'qty' => '1',
142 'unit_price' => '80',
143 'line_total' => '80',
144 'participant_count' => '',
145 'price_field_value_id' => '',
146 'financial_type_id' => '',
147 'deductible_amount' => '',
148 'tax_amount' => '',
149 ),
150 ),
151 ),
152 ),
153 ),
154 ),
155 );
156
157 return $expectedResult;
158 }
159
160 /**
161 * This example has been generated from the API test suite.
162 * The test that created it is called
163 * testCreateContributionChainedLineItems
164 * and can be found in
165 * https://github.com/civicrm/civicrm-core/blob/master/tests/phpunit/api/v3/ContributionTest.php
166 *
167 * You can see the outcome of the API tests at
168 * https://test.civicrm.org/job/CiviCRM-master-git/
169 *
170 * To Learn about the API read
171 * http://wiki.civicrm.org/confluence/display/CRMDOC/Using+the+API
172 *
173 * Browse the api on your own site with the api explorer
174 * http://MYSITE.ORG/path/to/civicrm/api
175 *
176 * Read more about testing here
177 * http://wiki.civicrm.org/confluence/display/CRM/Testing
178 *
179 * API Standards documentation:
180 * http://wiki.civicrm.org/confluence/display/CRM/API+Architecture+Standards
181 */