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(): 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
,
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',
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,
121 'line_item' => $this->getMembershipLineItem(),
126 $this->ids
['Contribution'][0] = $orderID;
130 * Create an order with more than one membership.
132 * @throws \CRM_Core_Exception
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', [
142 'label' => 'Membership Amount',
143 'options' => ['limit' => 1, 'sort' => 'id DESC'],
145 $generalPriceFieldValueID = $this->callAPISuccessGetValue('price_field_value', [
147 'label' => 'General',
148 'options' => ['limit' => 1, 'sort' => 'id DESC'],
151 $orderID = $this->callAPISuccess('Order', 'create', [
152 'total_amount' => 400,
153 'financial_type_id' => 'Member Dues',
154 'contact_id' => $this->_contactID
,
156 'payment_instrument_id' => 'Check',
157 'receive_date' => '2019-07-25 07:34:23',
161 'contact_id' => $this->ids
['contact'][0],
162 'membership_type_id' => $this->ids
['membership_type'][0],
163 'source' => 'Payment',
167 'label' => 'General',
171 'financial_type_id' => 1,
172 'entity_table' => 'civicrm_membership',
173 'price_field_id' => $priceFieldID,
174 'price_field_value_id' => $generalPriceFieldValueID,
180 'contact_id' => $this->ids
['contact'][1],
181 'membership_type_id' => $this->ids
['membership_type'][0],
182 'source' => 'Payment',
186 'label' => 'General',
190 'financial_type_id' => 1,
191 'entity_table' => 'civicrm_membership',
192 'price_field_id' => $priceFieldID,
193 'price_field_value_id' => $generalPriceFieldValueID,
200 $this->ids
['Contribution'][0] = $orderID;
204 * Create an order for an event.
206 * @param array $orderParams
208 * @throws \CRM_Core_Exception
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]]);
216 * Create an extraneous contribution to throw off any 'number one bugs'.
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.
221 protected function createExtraneousContribution() {
222 $this->contributionCreate([
223 'contact_id' => $this->individualCreate(),
225 'financial_type_id' => 1,
226 'invoice_id' => 'abcd',
228 'receive_date' => '2019-07-25 07:34:23',
234 * @throws \CRM_Core_Exception
236 protected function getMembershipLineItem(): array {
239 'label' => 'General',
243 'financial_type_id' => 1,
244 'entity_table' => 'civicrm_membership',
245 'price_field_id' => $this->callAPISuccess('price_field', 'getvalue', [
247 'label' => 'Membership Amount',
248 'options' => ['limit' => 1, 'sort' => 'id DESC'],
250 'price_field_value_id' => $this->callAPISuccess('price_field_value', 'getvalue', [
252 'label' => 'General',
253 'options' => ['limit' => 1, 'sort' => 'id DESC'],