| 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 | /** |
| 13 | * Trait OrderTrait |
| 14 | * |
| 15 | * Trait for setting up orders for tests. |
| 16 | */ |
| 17 | trait CRMTraits_Financial_OrderTrait { |
| 18 | |
| 19 | use \Civi\Test\Api3TestTrait; |
| 20 | |
| 21 | /** |
| 22 | * Create a pending membership from a recurring order. |
| 23 | * |
| 24 | * @throws \CRM_Core_Exception |
| 25 | */ |
| 26 | public function createRepeatMembershipOrder() { |
| 27 | $this->createExtraneousContribution(); |
| 28 | $this->ids['contact'][0] = $this->individualCreate(); |
| 29 | $this->ids['membership_type'][0] = $this->membershipTypeCreate(); |
| 30 | |
| 31 | $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge([ |
| 32 | 'contact_id' => $this->_contactID, |
| 33 | 'amount' => 1000, |
| 34 | 'sequential' => 1, |
| 35 | 'installments' => 5, |
| 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, |
| 43 | ])); |
| 44 | |
| 45 | $orderID = $this->callAPISuccess('Order', 'create', [ |
| 46 | 'total_amount' => '200', |
| 47 | 'financial_type_id' => 'Donation', |
| 48 | 'contribution_status_id' => 'Pending', |
| 49 | 'contact_id' => $this->_contactID, |
| 50 | 'contribution_page_id' => $this->_contributionPageID, |
| 51 | 'payment_processor_id' => $this->_paymentProcessorID, |
| 52 | 'is_test' => 0, |
| 53 | 'receive_date' => '2019-07-25 07:34:23', |
| 54 | 'skipCleanMoney' => TRUE, |
| 55 | 'contribution_recur_id' => $contributionRecur['id'], |
| 56 | 'line_items' => [ |
| 57 | [ |
| 58 | 'params' => [ |
| 59 | 'contact_id' => $this->ids['contact'][0], |
| 60 | 'membership_type_id' => $this->ids['membership_type'][0], |
| 61 | 'contribution_recur_id' => $contributionRecur['id'], |
| 62 | 'source' => 'Payment', |
| 63 | ], |
| 64 | 'line_item' => [ |
| 65 | [ |
| 66 | 'label' => 'General', |
| 67 | 'qty' => 1, |
| 68 | 'unit_price' => 200, |
| 69 | 'line_total' => 200, |
| 70 | 'financial_type_id' => 1, |
| 71 | 'entity_table' => 'civicrm_membership', |
| 72 | 'price_field_id' => $this->callAPISuccess('price_field', 'getvalue', [ |
| 73 | 'return' => 'id', |
| 74 | 'label' => 'Membership Amount', |
| 75 | 'options' => ['limit' => 1, 'sort' => 'id DESC'], |
| 76 | ]), |
| 77 | 'price_field_value_id' => $this->callAPISuccess('price_field_value', 'getvalue', [ |
| 78 | 'return' => 'id', |
| 79 | 'label' => 'General', |
| 80 | 'options' => ['limit' => 1, 'sort' => 'id DESC'], |
| 81 | ]), |
| 82 | ], |
| 83 | ], |
| 84 | ], |
| 85 | ], |
| 86 | ])['id']; |
| 87 | |
| 88 | $this->ids['ContributionRecur'][0] = $contributionRecur['id']; |
| 89 | $this->ids['Contribution'][0] = $orderID; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Create an extraneous contribution to throw off any 'number one bugs'. |
| 94 | * |
| 95 | * Ie this means our real data starts from 2 & we won't hit 'pretend passes' |
| 96 | * just because the number 1 is used for multiple entities. |
| 97 | */ |
| 98 | protected function createExtraneousContribution() { |
| 99 | $this->contributionCreate([ |
| 100 | 'contact_id' => $this->_contactID, |
| 101 | 'is_test' => 1, |
| 102 | 'financial_type_id' => 1, |
| 103 | 'invoice_id' => 'abcd', |
| 104 | 'trxn_id' => 345, |
| 105 | 'receive_date' => '2019-07-25 07:34:23', |
| 106 | ]); |
| 107 | } |
| 108 | |
| 109 | } |