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 +--------------------------------------------------------------------+
12 use Civi\Test\Api3TestTrait
;
17 * Trait for setting up orders for tests.
19 trait CRMTraits_Financial_OrderTrait
{
24 * Create a pending membership from a recurring order.
26 * @throws \CRM_Core_Exception
28 public function createRepeatMembershipOrder(): void
{
29 $this->createExtraneousContribution();
30 $this->ids
['contact'][0] = $this->individualCreate();
31 $this->ids
['membership_type'][0] = $this->membershipTypeCreate();
33 $contributionRecur = $this->callAPISuccess('ContributionRecur', 'create', array_merge([
34 'contact_id' => $this->_contactID
,
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
,
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
,
55 'receive_date' => '2019-07-25 07:34:23',
56 'skipCleanMoney' => TRUE,
57 'contribution_recur_id' => $contributionRecur['id'],
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',
66 'line_item' => $this->getMembershipLineItem(),
71 $this->ids
['ContributionRecur'][0] = $contributionRecur['id'];
72 $this->ids
['Contribution'][0] = $orderID;
76 * Create an order with a contribution AND a membership line item.
78 * @throws \CRM_Core_Exception
80 protected function createContributionAndMembershipOrder() {
81 $this->ids
['membership_type'][0] = $this->membershipTypeCreate();
82 $orderID = $this->callAPISuccess('Order', 'create', [
83 'financial_type_id' => 'Donation',
84 'contact_id' => $this->_contactID
,
86 'payment_instrument_id' => 'Check',
87 'receive_date' => date('Y-m-d'),
91 'contact_id' => $this->_contactID
,
92 'source' => 'Payment',
96 'label' => 'Contribution Amount',
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', [
104 'label' => 'Contribution Amount',
105 'options' => ['limit' => 1, 'sort' => 'id DESC'],
107 'price_field_value_id' => NULL,
113 'contact_id' => $this->_contactID
,
114 'membership_type_id' => 'General',
115 'source' => 'Payment',
117 'line_item' => $this->getMembershipLineItem(),
122 $this->ids
['Contribution'][0] = $orderID;
126 * Create an order with more than one membership.
128 * @throws \CRM_Core_Exception
130 protected function createMultipleMembershipOrder() {
131 $this->createExtraneousContribution();
132 $this->ids
['contact'][0] = $this->individualCreate();
133 $this->ids
['contact'][1] = $this->individualCreate();
134 $this->ids
['membership_type'][0] = $this->membershipTypeCreate();
135 $this->ids
['membership_type'][1] = $this->membershipTypeCreate(['name' => 'Type 2']);
136 $priceFieldID = $this->callAPISuccessGetValue('price_field', [
138 'label' => 'Membership Amount',
139 'options' => ['limit' => 1, 'sort' => 'id DESC'],
141 $generalPriceFieldValueID = $this->callAPISuccessGetValue('price_field_value', [
143 'label' => 'General',
144 'options' => ['limit' => 1, 'sort' => 'id DESC'],
147 $orderID = $this->callAPISuccess('Order', 'create', [
148 'total_amount' => 400,
149 'financial_type_id' => 'Member Dues',
150 'contact_id' => $this->_contactID
,
152 'payment_instrument_id' => 'Check',
153 'receive_date' => '2019-07-25 07:34:23',
157 'contact_id' => $this->ids
['contact'][0],
158 'membership_type_id' => $this->ids
['membership_type'][0],
159 'source' => 'Payment',
163 'label' => 'General',
167 'financial_type_id' => 1,
168 'entity_table' => 'civicrm_membership',
169 'price_field_id' => $priceFieldID,
170 'price_field_value_id' => $generalPriceFieldValueID,
176 'contact_id' => $this->ids
['contact'][1],
177 'membership_type_id' => $this->ids
['membership_type'][0],
178 'source' => 'Payment',
182 'label' => 'General',
186 'financial_type_id' => 1,
187 'entity_table' => 'civicrm_membership',
188 'price_field_id' => $priceFieldID,
189 'price_field_value_id' => $generalPriceFieldValueID,
196 $this->ids
['Contribution'][0] = $orderID;
200 * Create an order for an event.
202 * @param array $orderParams
204 * @throws \CRM_Core_Exception
206 protected function createEventOrder($orderParams = []) {
207 $this->ids
['Contribution'][0] = $this->callAPISuccess('Order', 'create', array_merge($this->getParticipantOrderParams(), $orderParams))['id'];
208 $this->ids
['Participant'][0] = $this->callAPISuccessGetValue('ParticipantPayment', ['return' => 'participant_id', 'contribution_id' => $this->ids
['Contribution'][0]]);
212 * Create an extraneous contribution to throw off any 'number one bugs'.
214 * Ie this means our real data starts from 2 & we won't hit 'pretend passes'
215 * just because the number 1 is used for multiple entities.
217 protected function createExtraneousContribution() {
218 $this->contributionCreate([
219 'contact_id' => $this->individualCreate(),
221 'financial_type_id' => 1,
222 'invoice_id' => 'abcd',
224 'receive_date' => '2019-07-25 07:34:23',
230 * @throws \CRM_Core_Exception
232 protected function getMembershipLineItem(): array {
235 'label' => 'General',
239 'financial_type_id' => 1,
240 'entity_table' => 'civicrm_membership',
241 'price_field_id' => $this->callAPISuccess('price_field', 'getvalue', [
243 'label' => 'Membership Amount',
244 'options' => ['limit' => 1, 'sort' => 'id DESC'],
246 'price_field_value_id' => $this->callAPISuccess('price_field_value', 'getvalue', [
248 'label' => 'General',
249 'options' => ['limit' => 1, 'sort' => 'id DESC'],