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 +--------------------------------------------------------------------+
13 * Class CRM_Event_BAO_AdditionalPaymentTest
17 class CRM_Event_BAO_AdditionalPaymentTest
extends CiviUnitTestCase
{
29 * @throws \CRM_Core_Exception
31 public function setUp() {
33 $this->contactID
= $this->individualCreate();
34 $event = $this->eventCreate();
35 $this->_eventId
= $event['id'];
41 * @throws \CRM_Core_Exception
43 public function tearDown() {
44 $this->eventDelete($this->_eventId
);
45 $this->quickCleanUpFinancialEntities();
49 * Check that all tests that have created payments have created them with the right financial entities.
51 * Ideally this would be on CiviUnitTestCase but many classes would still fail. Also, it might
52 * be good if it only ran on tests that created at least one contribution.
54 * @throws \CRM_Core_Exception
56 protected function assertPostConditions() {
57 $this->validateAllPayments();
58 $this->validateAllContributions();
62 * Helper function to record participant with paid contribution.
64 * @param int $feeTotal
65 * @param int $actualPaidAmt
66 * @param array $participantParams
67 * @param array $contributionParams
70 * @throws \CRM_Core_Exception
72 protected function addParticipantWithPayment($feeTotal, $actualPaidAmt, $participantParams = [], $contributionParams = []) {
73 $priceSetId = $this->eventPriceSetCreate($feeTotal);
74 CRM_Price_BAO_PriceSet
::addTo('civicrm_event', $this->_eventId
, $priceSetId);
75 // -- processing priceSet using the BAO
77 $priceSet = CRM_Price_BAO_PriceSet
::getSetDetail($priceSetId, TRUE, FALSE);
78 $priceSet = $priceSet[$priceSetId] ??
NULL;
79 $feeBlock = $priceSet['fields'] ??
NULL;
80 $params['price_2'] = $feeTotal;
81 $tempParams = $params;
83 CRM_Price_BAO_PriceSet
::processAmount($feeBlock,
86 foreach ($lineItems as $lineItemID => $lineItem) {
87 $lineItems[$lineItemID]['entity_table'] = 'civicrm_participant';
90 $participantParams = array_merge(
95 'event_id' => $this->_eventId
,
96 'register_date' => date('Y-m-d') . " 00:00:00",
99 'source' => 'Event_' . $this->_eventId
,
100 'contact_id' => $this->contactID
,
101 'note' => 'Note added for Event_' . $this->_eventId
,
102 'fee_level' => '\ 1Price_Field - 55\ 1',
107 // create participant contribution with partial payment
108 $contributionParams = array_merge(
110 'total_amount' => $feeTotal,
111 'source' => 'Fall Fundraiser Dinner: Offline registration',
113 'receipt_date' => 'today',
114 'contact_id' => $this->contactID
,
115 'financial_type_id' => 4,
116 'payment_instrument_id' => 4,
117 'contribution_status_id' => 'Pending',
118 'receive_date' => 'today',
119 'api.Payment.create' => ['total_amount' => $actualPaidAmt],
120 'line_items' => [['line_item' => $lineItems, 'params' => $participantParams]],
125 $contribution = $this->callAPISuccess('Order', 'create', $contributionParams);
126 $participant = $this->callAPISuccessGetSingle('participant', []);
127 $this->callAPISuccessGetSingle('ParticipantPayment', ['contribution_id' => $contribution['id'], 'participant_id' => $participant['id']]);
130 'participant' => $participant,
131 'contribution' => $this->callAPISuccessGetSingle('Contribution', ['id' => $contribution['id']]),
132 'lineItem' => $lineItems,
133 'params' => $tempParams,
134 'feeBlock' => $feeBlock,
135 'priceSetId' => $priceSetId,
140 * See https://lab.civicrm.org/dev/core/issues/153
142 * @throws \CiviCRM_API3_Exception
143 * @throws \CRM_Core_Exception
145 public function testPaymentWithCustomPaymentInstrument() {
149 // Create undetermined Payment Instrument
150 $paymentInstrumentID = $this->createPaymentInstrument(['label' => 'Undetermined'], 'Accounts Receivable');
152 // record pending payment for an event
153 $result = $this->addParticipantWithPayment(
155 ['is_pay_later' => 1],
157 'total_amount' => 100,
158 'payment_instrument_id' => $paymentInstrumentID,
159 'contribution_status_id' => CRM_Core_PseudoConstant
::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'),
162 $contributionID = $result['contribution']['id'];
164 // check payment info
165 $paymentInfo = CRM_Contribute_BAO_Contribution
::getPaymentInfo($result['participant']['id'], 'event');
166 $this->assertEquals($feeAmt, $this->callAPISuccessGetValue('Contribution', ['return' => 'total_amount', 'id' => $contributionID]), 'Total amount recorded is not correct');
167 $this->assertEquals($amtPaid, round($paymentInfo['paid']), 'Amount paid is not correct');
168 $this->assertEquals($feeAmt, CRM_Contribute_BAO_Contribution
::getContributionBalance($contributionID), 'Balance is not correct');
169 $this->assertEquals('Pending Label**', $paymentInfo['contribution_status'], 'Contribution status is not correct');
171 // make additional payment via 'Record Payment' form
172 $form = new CRM_Contribute_Form_AdditionalPayment();
174 'contact_id' => $result['contribution']['contact_id'],
175 'contribution_id' => $contributionID,
176 'total_amount' => 100,
178 'trxn_date' => '2017-04-11 13:05:11',
179 'payment_processor_id' => 0,
180 'payment_instrument_id' => CRM_Core_PseudoConstant
::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check'),
181 'check_number' => '#123',
183 $form->cid
= $result['contribution']['contact_id'];
184 $form->testSubmit($submitParams);
186 // check payment info again and see if the payment is completed
187 $contribution = $this->callAPISuccessGetSingle('Contribution', ['id' => $contributionID]);
188 $this->assertEquals($feeAmt, $contribution['total_amount'], 'Total amount recorded is not proper');
189 $this->assertEquals($feeAmt, CRM_Core_BAO_FinancialTrxn
::getTotalPayments($contributionID), 'Amount paid is not correct');
190 $this->assertEquals(CRM_Contribute_BAO_Contribution
::getContributionBalance($contributionID), 0, 'Balance amount is not proper');
191 $this->assertEquals('Completed', $contribution['contribution_status'], 'Contribution status is not correct');
193 $this->callAPISuccess('OptionValue', 'delete', ['id' => $paymentInstrumentID]);
197 * @see https://issues.civicrm.org/jira/browse/CRM-13964
199 * @throws \CRM_Core_Exception
201 public function testAddPartialPayment() {
203 $amtPaid = (float) 60;
204 $balance = (float) 40;
205 $result = $this->addParticipantWithPayment($feeAmt, $amtPaid);
206 $amountPaid = CRM_Core_BAO_FinancialTrxn
::getTotalPayments($result['contribution']['id']);
207 $contributionBalance = CRM_Contribute_BAO_Contribution
::getContributionBalance($result['contribution']['id']);
209 $this->assertEquals($feeAmt, $this->callAPISuccess('Contribution', 'getvalue', ['return' => 'total_amount', 'id' => $result['contribution']['id']]), 'Total amount recorded is not correct');
210 $this->assertEquals($amtPaid, $amountPaid, 'Amount paid is not correct');
211 $this->assertEquals($balance, $contributionBalance, 'Balance is not correct');
213 $this->assertEquals('Partially paid', $result['contribution']['contribution_status']);
214 $this->assertEquals('Partially paid', $result['participant']['participant_status']);
218 * Test owed/refund info is listed on view payments.
220 * @throws \CiviCRM_API3_Exception
221 * @throws \CRM_Core_Exception
223 public function testTransactionInfo() {
226 $result = $this->addParticipantWithPayment($feeAmt, $amtPaid);
227 $contributionID = $result['contribution']['id'];
229 $this->callAPISuccess('Payment', 'create', [
230 'contribution_id' => $contributionID,
231 'total_amount' => 20,
232 'payment_instrument_id' => 3,
233 'participant_id' => $result['participant']['id'],
236 //Change selection to a lower amount.
237 $params['price_2'] = 50;
238 CRM_Price_BAO_LineItem
::changeFeeSelections($params, $result['participant']['id'], 'participant', $contributionID, $result['feeBlock'], $result['lineItem']);
240 $this->callAPISuccess('Payment', 'create', [
241 'total_amount' => -50,
242 'contribution_id' => $contributionID,
243 'participant_id' => $result['participant']['id'],
244 'payment_instrument_id' => 3,
246 $paymentInfo = CRM_Contribute_BAO_Contribution
::getPaymentInfo($result['participant']['id'], 'event', TRUE);
247 $transaction = $paymentInfo['transaction'];
249 //Assert all transaction(owed and refund) are listed on view payments.
250 $this->assertEquals(count($transaction), 3, 'Transaction Details is not proper');
251 $this->assertEquals($transaction[0]['total_amount'], 80.00);
252 $this->assertEquals($transaction[0]['status'], 'Completed');
254 $this->assertEquals($transaction[1]['total_amount'], 20.00);
255 $this->assertEquals($transaction[1]['status'], 'Completed');
257 $this->assertEquals($transaction[2]['total_amount'], -50.00);
258 $this->assertEquals($transaction[2]['status'], 'Refunded Label**');