Merge pull request #15433 from aydun/add_contribution_is_template
[civicrm-core.git] / api / v3 / examples / Contribution / CreateWithNestedLineItems.ex.php
1 <?php
2 /**
3 * Test Generated example demonstrating the 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 = [
12 'contact_id' => 13,
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' => [
26 '0' => [
27 'price_field_id' => 1,
28 'qty' => 2,
29 'line_total' => '20',
30 'unit_price' => '10',
31 ],
32 '1' => [
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 [
50 'is_error' => 1,
51 'error_message' => $errorMessage,
52 'error_code' => $errorCode,
53 'error_data' => $errorData,
54 ];
55 }
56
57 return $result;
58 }
59
60 /**
61 * Function returns array of result expected from previous function.
62 *
63 * @return array
64 * API result array
65 */
66 function contribution_create_expectedresult() {
67
68 $expectedResult = [
69 'is_error' => 0,
70 'version' => 3,
71 'count' => 1,
72 'id' => 1,
73 'values' => [
74 '1' => [
75 'id' => '1',
76 'contact_id' => '13',
77 'financial_type_id' => '1',
78 'contribution_page_id' => '',
79 'payment_instrument_id' => '1',
80 'receive_date' => '20120511000000',
81 'non_deductible_amount' => '10',
82 'total_amount' => '100',
83 'fee_amount' => '50',
84 'net_amount' => '90',
85 'trxn_id' => '12345',
86 'invoice_id' => '67890',
87 'invoice_number' => '',
88 'currency' => 'USD',
89 'cancel_date' => '',
90 'cancel_reason' => '',
91 'receipt_date' => '',
92 'thankyou_date' => '',
93 'source' => 'SSF',
94 'amount_level' => '',
95 'contribution_recur_id' => '',
96 'is_test' => '',
97 'is_pay_later' => '',
98 'contribution_status_id' => '1',
99 'address_id' => '',
100 'check_number' => '',
101 'campaign_id' => '',
102 'creditnote_id' => '',
103 'tax_amount' => 0,
104 'revenue_recognition_date' => '',
105 'is_template' => '',
106 'contribution_type_id' => '1',
107 'api.line_item.create' => [
108 '0' => [
109 'is_error' => 0,
110 'version' => 3,
111 'count' => 1,
112 'id' => 1,
113 'values' => [
114 '0' => [
115 'id' => '1',
116 'entity_table' => 'civicrm_contribution',
117 'entity_id' => '1',
118 'contribution_id' => '1',
119 'price_field_id' => '1',
120 'label' => 'line item',
121 'qty' => '2',
122 'unit_price' => '10',
123 'line_total' => '20',
124 'participant_count' => '',
125 'price_field_value_id' => '',
126 'financial_type_id' => '',
127 'non_deductible_amount' => '',
128 'tax_amount' => '',
129 ],
130 ],
131 ],
132 '1' => [
133 'is_error' => 0,
134 'version' => 3,
135 'count' => 1,
136 'id' => 2,
137 'values' => [
138 '0' => [
139 'id' => '2',
140 'entity_table' => 'civicrm_contribution',
141 'entity_id' => '1',
142 'contribution_id' => '1',
143 'price_field_id' => '1',
144 'label' => 'line item',
145 'qty' => '1',
146 'unit_price' => '80',
147 'line_total' => '80',
148 'participant_count' => '',
149 'price_field_value_id' => '',
150 'financial_type_id' => '',
151 'non_deductible_amount' => '',
152 'tax_amount' => '',
153 ],
154 ],
155 ],
156 ],
157 ],
158 ],
159 ];
160
161 return $expectedResult;
162 }
163
164 /*
165 * This example has been generated from the API test suite.
166 * The test that created it is called "testCreateContributionChainedLineItems"
167 * and can be found at:
168 * https://github.com/civicrm/civicrm-core/blob/master/tests/phpunit/api/v3/ContributionTest.php
169 *
170 * You can see the outcome of the API tests at
171 * https://test.civicrm.org/job/CiviCRM-Core-Matrix/
172 *
173 * To Learn about the API read
174 * https://docs.civicrm.org/dev/en/latest/api/
175 *
176 * Browse the API on your own site with the API Explorer. It is in the main
177 * CiviCRM menu, under: Support > Development > API Explorer.
178 *
179 * Read more about testing here
180 * https://docs.civicrm.org/dev/en/latest/testing/
181 *
182 * API Standards documentation:
183 * https://docs.civicrm.org/dev/en/latest/framework/api-architecture/
184 */