Merge pull request #19017 from eileenmcnaughton/remove_recur
[civicrm-core.git] / tests / phpunit / CRMTraits / Financial / OrderTrait.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 use Civi\Test\Api3TestTrait;
13
14 /**
15 * Trait OrderTrait
16 *
17 * Trait for setting up orders for tests.
18 */
19 trait CRMTraits_Financial_OrderTrait {
20
21 use Api3TestTrait;
22
23 /**
24 * Create a pending membership from a recurring order.
25 *
26 * @throws \CRM_Core_Exception
27 */
28 public function createRepeatMembershipOrder(): void {
29 $this->createExtraneousContribution();
30 $this->ids['contact'][0] = $this->individualCreate();
31 $this->ids['membership_type'][0] = $this->membershipTypeCreate();
32
33 $contributionRecur = $this->callAPISuccess('ContributionRecur', 'create', array_merge([
34 'contact_id' => $this->_contactID,
35 'amount' => 1000,
36 'sequential' => 1,
37 'installments' => 5,
38 'frequency_unit' => 'Month',
39 'frequency_interval' => 1,
40 'invoice_id' => $this->_invoiceID,
41 'contribution_status_id' => 2,
42 'payment_processor_id' => $this->_paymentProcessorID,
43 // processor provided ID - use contact ID as proxy.
44 'processor_id' => $this->_contactID,
45 ]));
46
47 $orderID = $this->callAPISuccess('Order', 'create', [
48 'total_amount' => '200',
49 'financial_type_id' => 'Donation',
50 'source' => 'Online Contribution: form payment',
51 'contact_id' => $this->_contactID,
52 'contribution_page_id' => $this->_contributionPageID,
53 'payment_processor_id' => $this->_paymentProcessorID,
54 'is_test' => 0,
55 'receive_date' => '2019-07-25 07:34:23',
56 'skipCleanMoney' => TRUE,
57 'contribution_recur_id' => $contributionRecur['id'],
58 'line_items' => [
59 [
60 'params' => [
61 'contact_id' => $this->ids['contact'][0],
62 'membership_type_id' => $this->ids['membership_type'][0],
63 'contribution_recur_id' => $contributionRecur['id'],
64 'source' => 'Payment',
65 ],
66 'line_item' => $this->getMembershipLineItem(),
67 ],
68 ],
69 ])['id'];
70
71 $this->ids['ContributionRecur'][0] = $contributionRecur['id'];
72 $this->ids['Contribution'][0] = $orderID;
73 }
74
75 /**
76 * Create an order with a contribution AND a membership line item.
77 *
78 * @throws \CRM_Core_Exception
79 */
80 protected function createContributionAndMembershipOrder(): void {
81 $this->ids['membership_type'][0] = $this->membershipTypeCreate();
82 $orderID = $this->callAPISuccess('Order', 'create', [
83 'financial_type_id' => 'Donation',
84 'contact_id' => $this->_contactID,
85 'is_test' => 0,
86 'payment_instrument_id' => 'Check',
87 'receive_date' => date('Y-m-d'),
88 'line_items' => [
89 [
90 'params' => [
91 'contact_id' => $this->_contactID,
92 'source' => 'Payment',
93 ],
94 'line_item' => [
95 [
96 'label' => 'Contribution Amount',
97 'qty' => 1,
98 'unit_price' => 100,
99 'line_total' => 100,
100 'financial_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Donation'),
101 'entity_table' => 'civicrm_contribution',
102 'price_field_id' => $this->callAPISuccessGetValue('price_field', [
103 'return' => 'id',
104 'label' => 'Contribution Amount',
105 'options' => ['limit' => 1, 'sort' => 'id DESC'],
106 ]),
107 'price_field_value_id' => NULL,
108 ],
109 ],
110 ],
111 [
112 'params' => [
113 'contact_id' => $this->_contactID,
114 'membership_type_id' => 'General',
115 'source' => 'Payment',
116 // This is necessary because Membership_BAO otherwise ignores the
117 // pending status. I do have a fix but it's held up behind other pending-review PRs
118 // so this should be temporary until we get the membership PRs flowing.
119 'skipStatusCal' => TRUE,
120 ],
121 'line_item' => $this->getMembershipLineItem(),
122 ],
123 ],
124 ])['id'];
125
126 $this->ids['Contribution'][0] = $orderID;
127 }
128
129 /**
130 * Create an order with more than one membership.
131 *
132 * @throws \CRM_Core_Exception
133 */
134 protected function createMultipleMembershipOrder() {
135 $this->createExtraneousContribution();
136 $this->ids['contact'][0] = $this->individualCreate();
137 $this->ids['contact'][1] = $this->individualCreate();
138 $this->ids['membership_type'][0] = $this->membershipTypeCreate();
139 $this->ids['membership_type'][1] = $this->membershipTypeCreate(['name' => 'Type 2']);
140 $priceFieldID = $this->callAPISuccessGetValue('price_field', [
141 'return' => 'id',
142 'label' => 'Membership Amount',
143 'options' => ['limit' => 1, 'sort' => 'id DESC'],
144 ]);
145 $generalPriceFieldValueID = $this->callAPISuccessGetValue('price_field_value', [
146 'return' => 'id',
147 'label' => 'General',
148 'options' => ['limit' => 1, 'sort' => 'id DESC'],
149 ]);
150
151 $orderID = $this->callAPISuccess('Order', 'create', [
152 'total_amount' => 400,
153 'financial_type_id' => 'Member Dues',
154 'contact_id' => $this->_contactID,
155 'is_test' => 0,
156 'payment_instrument_id' => 'Check',
157 'receive_date' => '2019-07-25 07:34:23',
158 'line_items' => [
159 [
160 'params' => [
161 'contact_id' => $this->ids['contact'][0],
162 'membership_type_id' => $this->ids['membership_type'][0],
163 'source' => 'Payment',
164 ],
165 'line_item' => [
166 [
167 'label' => 'General',
168 'qty' => 1,
169 'unit_price' => 200,
170 'line_total' => 200,
171 'financial_type_id' => 1,
172 'entity_table' => 'civicrm_membership',
173 'price_field_id' => $priceFieldID,
174 'price_field_value_id' => $generalPriceFieldValueID,
175 ],
176 ],
177 ],
178 [
179 'params' => [
180 'contact_id' => $this->ids['contact'][1],
181 'membership_type_id' => $this->ids['membership_type'][0],
182 'source' => 'Payment',
183 ],
184 'line_item' => [
185 [
186 'label' => 'General',
187 'qty' => 1,
188 'unit_price' => 200,
189 'line_total' => 200,
190 'financial_type_id' => 1,
191 'entity_table' => 'civicrm_membership',
192 'price_field_id' => $priceFieldID,
193 'price_field_value_id' => $generalPriceFieldValueID,
194 ],
195 ],
196 ],
197 ],
198 ])['id'];
199
200 $this->ids['Contribution'][0] = $orderID;
201 }
202
203 /**
204 * Create an order for an event.
205 *
206 * @param array $orderParams
207 *
208 * @throws \CRM_Core_Exception
209 */
210 protected function createEventOrder($orderParams = []) {
211 $this->ids['Contribution'][0] = $this->callAPISuccess('Order', 'create', array_merge($this->getParticipantOrderParams(), $orderParams))['id'];
212 $this->ids['Participant'][0] = $this->callAPISuccessGetValue('ParticipantPayment', ['return' => 'participant_id', 'contribution_id' => $this->ids['Contribution'][0]]);
213 }
214
215 /**
216 * Create an extraneous contribution to throw off any 'number one bugs'.
217 *
218 * Ie this means our real data starts from 2 & we won't hit 'pretend passes'
219 * just because the number 1 is used for multiple entities.
220 */
221 protected function createExtraneousContribution() {
222 $this->contributionCreate([
223 'contact_id' => $this->individualCreate(),
224 'is_test' => 1,
225 'financial_type_id' => 1,
226 'invoice_id' => 'abcd',
227 'trxn_id' => 345,
228 'receive_date' => '2019-07-25 07:34:23',
229 ]);
230 }
231
232 /**
233 * @return array[]
234 * @throws \CRM_Core_Exception
235 */
236 protected function getMembershipLineItem(): array {
237 return [
238 [
239 'label' => 'General',
240 'qty' => 1,
241 'unit_price' => 200,
242 'line_total' => 200,
243 'financial_type_id' => 1,
244 'entity_table' => 'civicrm_membership',
245 'price_field_id' => $this->callAPISuccess('price_field', 'getvalue', [
246 'return' => 'id',
247 'label' => 'Membership Amount',
248 'options' => ['limit' => 1, 'sort' => 'id DESC'],
249 ]),
250 'price_field_value_id' => $this->callAPISuccess('price_field_value', 'getvalue', [
251 'return' => 'id',
252 'label' => 'General',
253 'options' => ['limit' => 1, 'sort' => 'id DESC'],
254 ]),
255 ],
256 ];
257 }
258
259 }