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(): void
{
33 $this->contactID
= $this->individualCreate();
34 $event = $this->eventCreate();
35 $this->_eventId
= $event['id'];
41 * @throws \CRM_Core_Exception
43 public function tearDown(): void
{
44 $this->eventDelete($this->_eventId
);
45 $this->quickCleanUpFinancialEntities();
50 * Check that all tests that have created payments have created them with the right financial entities.
52 * Ideally this would be on CiviUnitTestCase but many classes would still fail. Also, it might
53 * be good if it only ran on tests that created at least one contribution.
55 * @throws \CRM_Core_Exception
57 protected function assertPostConditions(): void
{
58 $this->validateAllPayments();
59 $this->validateAllContributions();
63 * Helper function to record participant with paid contribution.
65 * @param int $feeTotal
66 * @param int $actualPaidAmt
67 * @param array $participantParams
68 * @param array $contributionParams
71 * @throws \CRM_Core_Exception
73 protected function addParticipantWithPayment($feeTotal, $actualPaidAmt, $participantParams = [], $contributionParams = []) {
74 $priceSetId = $this->eventPriceSetCreate($feeTotal);
75 CRM_Price_BAO_PriceSet
::addTo('civicrm_event', $this->_eventId
, $priceSetId);
76 // -- processing priceSet using the BAO
78 $priceSet = CRM_Price_BAO_PriceSet
::getSetDetail($priceSetId, TRUE, FALSE);
79 $priceSet = $priceSet[$priceSetId] ??
NULL;
80 $feeBlock = $priceSet['fields'] ??
NULL;
81 $params['price_2'] = $feeTotal;
82 $tempParams = $params;
84 CRM_Price_BAO_PriceSet
::processAmount($feeBlock,
87 foreach ($lineItems as $lineItemID => $lineItem) {
88 $lineItems[$lineItemID]['entity_table'] = 'civicrm_participant';
91 $participantParams = array_merge(
96 'event_id' => $this->_eventId
,
97 'register_date' => date('Y-m-d') . " 00:00:00",
100 'source' => 'Event_' . $this->_eventId
,
101 'contact_id' => $this->contactID
,
102 'note' => 'Note added for Event_' . $this->_eventId
,
103 'fee_level' => '\ 1Price_Field - 55\ 1',
108 // create participant contribution with partial payment
109 $contributionParams = array_merge(
111 'total_amount' => $feeTotal,
112 'source' => 'Fall Fundraiser Dinner: Offline registration',
114 'receipt_date' => 'today',
115 'contact_id' => $this->contactID
,
116 'financial_type_id' => 4,
117 'payment_instrument_id' => 4,
118 'contribution_status_id' => 'Pending',
119 'receive_date' => 'today',
120 'api.Payment.create' => ['total_amount' => $actualPaidAmt],
121 'line_items' => [['line_item' => $lineItems, 'params' => $participantParams]],
126 $contribution = $this->callAPISuccess('Order', 'create', $contributionParams);
127 $participant = $this->callAPISuccessGetSingle('participant', []);
128 $this->callAPISuccessGetSingle('ParticipantPayment', ['contribution_id' => $contribution['id'], 'participant_id' => $participant['id']]);
131 'participant' => $participant,
132 'contribution' => $this->callAPISuccessGetSingle('Contribution', ['id' => $contribution['id']]),
133 'lineItem' => $lineItems,
134 'params' => $tempParams,
135 'feeBlock' => $feeBlock,
136 'priceSetId' => $priceSetId,
141 * See https://lab.civicrm.org/dev/core/issues/153
143 * @throws \CiviCRM_API3_Exception
144 * @throws \CRM_Core_Exception
146 public function testPaymentWithCustomPaymentInstrument() {
150 // Create undetermined Payment Instrument
151 $paymentInstrumentID = $this->createPaymentInstrument(['label' => 'Undetermined'], 'Accounts Receivable');
153 // record pending payment for an event
154 $result = $this->addParticipantWithPayment(
156 ['is_pay_later' => 1],
158 'total_amount' => 100,
159 'payment_instrument_id' => $paymentInstrumentID,
160 'contribution_status_id' => CRM_Core_PseudoConstant
::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'),
163 $contributionID = $result['contribution']['id'];
165 // check payment info
166 $paymentInfo = CRM_Contribute_BAO_Contribution
::getPaymentInfo($result['participant']['id'], 'event');
167 $this->assertEquals($feeAmt, $this->callAPISuccessGetValue('Contribution', ['return' => 'total_amount', 'id' => $contributionID]), 'Total amount recorded is not correct');
168 $this->assertEquals($amtPaid, round($paymentInfo['paid']), 'Amount paid is not correct');
169 $this->assertEquals($feeAmt, CRM_Contribute_BAO_Contribution
::getContributionBalance($contributionID), 'Balance is not correct');
170 $this->assertEquals('Pending Label**', $paymentInfo['contribution_status'], 'Contribution status is not correct');
172 // make additional payment via 'Record Payment' form
173 $form = new CRM_Contribute_Form_AdditionalPayment();
175 'contact_id' => $result['contribution']['contact_id'],
176 'contribution_id' => $contributionID,
177 'total_amount' => 100,
179 'trxn_date' => '2017-04-11 13:05:11',
180 'payment_processor_id' => 0,
181 'payment_instrument_id' => CRM_Core_PseudoConstant
::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check'),
182 'check_number' => '#123',
184 $form->cid
= $result['contribution']['contact_id'];
185 $form->testSubmit($submitParams);
187 // check payment info again and see if the payment is completed
188 $contribution = $this->callAPISuccessGetSingle('Contribution', ['id' => $contributionID]);
189 $this->assertEquals($feeAmt, $contribution['total_amount'], 'Total amount recorded is not proper');
190 $this->assertEquals($feeAmt, CRM_Core_BAO_FinancialTrxn
::getTotalPayments($contributionID), 'Amount paid is not correct');
191 $this->assertEquals(CRM_Contribute_BAO_Contribution
::getContributionBalance($contributionID), 0, 'Balance amount is not proper');
192 $this->assertEquals('Completed', $contribution['contribution_status'], 'Contribution status is not correct');
194 $this->callAPISuccess('OptionValue', 'delete', ['id' => $paymentInstrumentID]);
198 * @see https://issues.civicrm.org/jira/browse/CRM-13964
200 * @throws \CRM_Core_Exception
202 public function testAddPartialPayment() {
204 $amtPaid = (float) 60;
205 $balance = (float) 40;
206 $result = $this->addParticipantWithPayment($feeAmt, $amtPaid);
207 $amountPaid = CRM_Core_BAO_FinancialTrxn
::getTotalPayments($result['contribution']['id']);
208 $contributionBalance = CRM_Contribute_BAO_Contribution
::getContributionBalance($result['contribution']['id']);
210 $this->assertEquals($feeAmt, $this->callAPISuccess('Contribution', 'getvalue', ['return' => 'total_amount', 'id' => $result['contribution']['id']]), 'Total amount recorded is not correct');
211 $this->assertEquals($amtPaid, $amountPaid, 'Amount paid is not correct');
212 $this->assertEquals($balance, $contributionBalance, 'Balance is not correct');
214 $this->assertEquals('Partially paid', $result['contribution']['contribution_status']);
215 $this->assertEquals('Partially paid', $result['participant']['participant_status']);
219 * Test owed/refund info is listed on view payments.
221 * @throws \CiviCRM_API3_Exception
222 * @throws \CRM_Core_Exception
224 public function testTransactionInfo() {
227 $result = $this->addParticipantWithPayment($feeAmt, $amtPaid);
228 $contributionID = $result['contribution']['id'];
230 $this->callAPISuccess('Payment', 'create', [
231 'contribution_id' => $contributionID,
232 'total_amount' => 20,
233 'payment_instrument_id' => 3,
234 'participant_id' => $result['participant']['id'],
237 //Change selection to a lower amount.
238 $params['price_2'] = 50;
239 CRM_Price_BAO_LineItem
::changeFeeSelections($params, $result['participant']['id'], 'participant', $contributionID, $result['feeBlock'], $result['lineItem']);
241 $this->callAPISuccess('Payment', 'create', [
242 'total_amount' => -50,
243 'contribution_id' => $contributionID,
244 'participant_id' => $result['participant']['id'],
245 'payment_instrument_id' => 3,
247 $paymentInfo = CRM_Contribute_BAO_Contribution
::getPaymentInfo($result['participant']['id'], 'event', TRUE);
248 $transaction = $paymentInfo['transaction'];
250 //Assert all transaction(owed and refund) are listed on view payments.
251 $this->assertEquals(count($transaction), 3, 'Transaction Details is not proper');
252 $this->assertEquals($transaction[0]['total_amount'], 80.00);
253 $this->assertEquals($transaction[0]['status'], 'Completed');
255 $this->assertEquals($transaction[1]['total_amount'], 20.00);
256 $this->assertEquals($transaction[1]['status'], 'Completed');
258 $this->assertEquals($transaction[2]['total_amount'], -50.00);
259 $this->assertEquals($transaction[2]['status'], 'Refunded Label**');