3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * Trait for setting up orders for tests.
17 trait CRMTraits_Financial_OrderTrait
{
19 use \Civi\Test\Api3TestTrait
;
22 * Create a pending membership from a recurring order.
24 * @throws \CRM_Core_Exception
26 public function createRepeatMembershipOrder() {
27 $this->createExtraneousContribution();
28 $this->ids
['contact'][0] = $this->individualCreate();
29 $this->ids
['membership_type'][0] = $this->membershipTypeCreate();
31 $contributionRecur = $this->callAPISuccess('ContributionRecur', 'create', array_merge([
32 'contact_id' => $this->_contactID
,
36 'frequency_unit' => 'Month',
37 'frequency_interval' => 1,
38 'invoice_id' => $this->_invoiceID
,
39 'contribution_status_id' => 2,
40 'payment_processor_id' => $this->_paymentProcessorID
,
41 // processor provided ID - use contact ID as proxy.
42 'processor_id' => $this->_contactID
,
45 $orderID = $this->callAPISuccess('Order', 'create', [
46 'total_amount' => '200',
47 'financial_type_id' => 'Donation',
48 'contact_id' => $this->_contactID
,
49 'contribution_page_id' => $this->_contributionPageID
,
50 'payment_processor_id' => $this->_paymentProcessorID
,
52 'receive_date' => '2019-07-25 07:34:23',
53 'skipCleanMoney' => TRUE,
54 'contribution_recur_id' => $contributionRecur['id'],
58 'contact_id' => $this->ids
['contact'][0],
59 'membership_type_id' => $this->ids
['membership_type'][0],
60 'contribution_recur_id' => $contributionRecur['id'],
61 'source' => 'Payment',
63 'line_item' => $this->getMembershipLineItem(),
68 $this->ids
['ContributionRecur'][0] = $contributionRecur['id'];
69 $this->ids
['Contribution'][0] = $orderID;
73 * Create an order with a contribution AND a membership line item.
75 * @throws \CRM_Core_Exception
77 protected function createContributionAndMembershipOrder(): void
{
78 $this->ids
['membership_type'][0] = $this->membershipTypeCreate();
79 $orderID = $this->callAPISuccess('Order', 'create', [
80 'financial_type_id' => 'Donation',
81 'contact_id' => $this->_contactID
,
83 'payment_instrument_id' => 'Check',
84 'receive_date' => date('Y-m-d'),
88 'contact_id' => $this->_contactID
,
89 'source' => 'Payment',
93 'label' => 'Contribution Amount',
97 'financial_type_id' => CRM_Core_PseudoConstant
::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Donation'),
98 'entity_table' => 'civicrm_contribution',
99 'price_field_id' => $this->callAPISuccessGetValue('price_field', [
101 'label' => 'Contribution Amount',
102 'options' => ['limit' => 1, 'sort' => 'id DESC'],
104 'price_field_value_id' => NULL,
110 'contact_id' => $this->_contactID
,
111 'membership_type_id' => 'General',
112 'source' => 'Payment',
113 // This is necessary because Membership_BAO otherwise ignores the
114 // pending status. I do have a fix but it's held up behind other pending-review PRs
115 // so this should be temporary until we get the membership PRs flowing.
116 'skipStatusCal' => TRUE,
118 'line_item' => $this->getMembershipLineItem(),
123 $this->ids
['Contribution'][0] = $orderID;
127 * Create an order with more than one membership.
129 * @throws \CRM_Core_Exception
131 protected function createMultipleMembershipOrder() {
132 $this->createExtraneousContribution();
133 $this->ids
['contact'][0] = $this->individualCreate();
134 $this->ids
['contact'][1] = $this->individualCreate();
135 $this->ids
['membership_type'][0] = $this->membershipTypeCreate();
136 $this->ids
['membership_type'][1] = $this->membershipTypeCreate(['name' => 'Type 2']);
137 $priceFieldID = $this->callAPISuccessGetValue('price_field', [
139 'label' => 'Membership Amount',
140 'options' => ['limit' => 1, 'sort' => 'id DESC'],
142 $generalPriceFieldValueID = $this->callAPISuccessGetValue('price_field_value', [
144 'label' => 'General',
145 'options' => ['limit' => 1, 'sort' => 'id DESC'],
148 $orderID = $this->callAPISuccess('Order', 'create', [
149 'total_amount' => 400,
150 'financial_type_id' => 'Member Dues',
151 'contact_id' => $this->_contactID
,
153 'payment_instrument_id' => 'Check',
154 'receive_date' => '2019-07-25 07:34:23',
158 'contact_id' => $this->ids
['contact'][0],
159 'membership_type_id' => $this->ids
['membership_type'][0],
160 'source' => 'Payment',
164 'label' => 'General',
168 'financial_type_id' => 1,
169 'entity_table' => 'civicrm_membership',
170 'price_field_id' => $priceFieldID,
171 'price_field_value_id' => $generalPriceFieldValueID,
177 'contact_id' => $this->ids
['contact'][1],
178 'membership_type_id' => $this->ids
['membership_type'][0],
179 'source' => 'Payment',
183 'label' => 'General',
187 'financial_type_id' => 1,
188 'entity_table' => 'civicrm_membership',
189 'price_field_id' => $priceFieldID,
190 'price_field_value_id' => $generalPriceFieldValueID,
197 $this->ids
['Contribution'][0] = $orderID;
201 * Create an order for an event.
203 * @param array $orderParams
205 * @throws \CRM_Core_Exception
207 protected function createEventOrder($orderParams = []) {
208 $this->ids
['Contribution'][0] = $this->callAPISuccess('Order', 'create', array_merge($this->getParticipantOrderParams(), $orderParams))['id'];
209 $this->ids
['Participant'][0] = $this->callAPISuccessGetValue('ParticipantPayment', ['return' => 'participant_id', 'contribution_id' => $this->ids
['Contribution'][0]]);
213 * Create an extraneous contribution to throw off any 'number one bugs'.
215 * Ie this means our real data starts from 2 & we won't hit 'pretend passes'
216 * just because the number 1 is used for multiple entities.
218 protected function createExtraneousContribution() {
219 $this->contributionCreate([
220 'contact_id' => $this->individualCreate(),
222 'financial_type_id' => 1,
223 'invoice_id' => 'abcd',
225 'receive_date' => '2019-07-25 07:34:23',
231 * @throws \CRM_Core_Exception
233 protected function getMembershipLineItem(): array {
236 'label' => 'General',
240 'financial_type_id' => 1,
241 'entity_table' => 'civicrm_membership',
242 'price_field_id' => $this->callAPISuccess('price_field', 'getvalue', [
244 'label' => 'Membership Amount',
245 'options' => ['limit' => 1, 'sort' => 'id DESC'],
247 'price_field_value_id' => $this->callAPISuccess('price_field_value', 'getvalue', [
249 'label' => 'General',
250 'options' => ['limit' => 1, 'sort' => 'id DESC'],