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 * Test APIv3 civicrm_contribute_* functions
15 * @package CiviCRM_APIv3
16 * @subpackage API_Contribution
19 class api_v3_PaymentTest
extends CiviUnitTestCase
{
21 protected $_individualId;
23 protected $_financialTypeId = 1;
28 * @throws \CRM_Core_Exception
30 public function setUp() {
33 $this->_apiversion
= 3;
34 $this->_individualId
= $this->individualCreate();
35 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= [];
39 * Clean up after each test.
43 public function tearDown() {
44 $this->quickCleanUpFinancialEntities();
45 $this->quickCleanup(['civicrm_uf_match']);
46 unset(CRM_Core_Config
::singleton()->userPermissionClass
->permissions
);
51 * Test Get Payment api.
53 * @throws \CRM_Core_Exception
55 public function testGetPayment() {
57 'contact_id' => $this->_individualId
,
58 'receive_date' => '2010-01-20',
59 'total_amount' => 100.00,
60 'financial_type_id' => $this->_financialTypeId
,
62 'contribution_status_id' => 1,
64 $contribution = $this->callAPISuccess('contribution', 'create', $p);
67 'contribution_id' => $contribution['id'],
68 'check_permissions' => TRUE,
70 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['access CiviCRM', 'administer CiviCRM'];
71 $payment = $this->callAPIFailure('payment', 'get', $params, 'API permission check failed for Payment/get call; insufficient permission: require access CiviCRM and access CiviContribute');
73 array_push(CRM_Core_Config
::singleton()->userPermissionClass
->permissions
, 'access CiviContribute');
74 $payment = $this->callAPISuccess('payment', 'get', $params);
76 $payment = $this->callAPIAndDocument('payment', 'get', $params, __FUNCTION__
, __FILE__
);
77 $this->assertEquals(1, $payment['count']);
80 $contribution['id'] => [
81 'total_amount' => 100,
83 'trxn_date' => '2010-01-20 00:00:00',
84 'contribution_id' => $contribution['id'],
88 $this->checkPaymentResult($payment, $expectedResult);
89 $this->callAPISuccess('Contribution', 'Delete', [
90 'id' => $contribution['id'],
92 $this->validateAllPayments();
96 * Retrieve Payment using trxn_id.
98 * @throws \CRM_Core_Exception
100 public function testGetPaymentWithTrxnID() {
101 $this->_individualId2
= $this->individualCreate();
103 'contact_id' => $this->_individualId
,
105 'total_amount' => 10,
107 $contributionID1 = $this->contributionCreate($params1);
110 'contact_id' => $this->_individualId2
,
112 'total_amount' => 20,
114 $contributionID2 = $this->contributionCreate($params2);
116 $paymentParams = ['trxn_id' => 111111];
117 $payment = $this->callAPISuccess('payment', 'get', $paymentParams);
120 'total_amount' => 10,
124 'contribution_id' => $contributionID1,
127 $this->checkPaymentResult($payment, $expectedResult);
129 $paymentParams = ['trxn_id' => 222222];
130 $payment = $this->callAPISuccess('payment', 'get', $paymentParams);
133 'total_amount' => 20,
137 'contribution_id' => $contributionID2,
140 $this->checkPaymentResult($payment, $expectedResult);
141 $this->validateAllPayments();
145 * Test email receipt for partial payment.
147 * @throws \CRM_Core_Exception
149 public function testPaymentEmailReceipt() {
150 $mut = new CiviMailUtils($this);
151 $contribution = $this->createPartiallyPaidParticipantOrder();
152 $event = $this->callAPISuccess('Event', 'get', []);
153 $this->addLocationToEvent($event['id']);
155 'contribution_id' => $contribution['id'],
156 'total_amount' => 50,
157 'check_number' => '345',
158 'trxn_date' => '2018-08-13 17:57:56',
160 $payment = $this->callAPISuccess('payment', 'create', $params);
161 $this->checkPaymentResult($payment, [
163 'from_financial_account_id' => 7,
164 'to_financial_account_id' => 6,
165 'total_amount' => 50,
171 $this->callAPISuccess('Payment', 'sendconfirmation', ['id' => $payment['id']]);
172 $mut->assertSubjects(['Payment Receipt - Annual CiviCRM meet - Mr. Anthony Anderson II']);
174 'From: "FIXME" <info@EXAMPLE.ORG>',
176 'Total Fee: $ 300.00',
177 'This Payment Amount: $ 50.00',
178 //150 was paid in the 1st payment.
179 'Balance Owed: $ 100.00',
180 'Event Information and Location',
183 'Transaction Date: August 13th, 2018 5:57 PM',
188 $mut->clearMessages();
189 $this->validateAllPayments();
193 * Test email receipt for partial payment.
195 * @throws \CRM_Core_Exception
197 public function testPaymentEmailReceiptFullyPaid() {
198 $mut = new CiviMailUtils($this);
199 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['access CiviContribute', 'edit contributions', 'access CiviCRM'];
200 $contribution = $this->createPartiallyPaidParticipantOrder();
203 'contribution_id' => $contribution['id'],
204 'total_amount' => 150,
206 $payment = $this->callAPISuccess('payment', 'create', $params);
208 // Here we set the email to an invalid email & use check_permissions, domain email should be used.
209 $email = $this->callAPISuccess('Email', 'create', ['contact_id' => 1, 'email' => 'bob@example.com']);
210 $this->callAPISuccess('Payment', 'sendconfirmation', ['id' => $payment['id'], 'from' => $email['id'], 'check_permissions' => 1]);
211 $mut->assertSubjects(['Payment Receipt - Annual CiviCRM meet - Mr. Anthony Anderson II', 'Registration Confirmation - Annual CiviCRM meet - Mr. Anthony Anderson II']);
213 'From: "FIXME" <info@EXAMPLE.ORG>',
215 'Below you will find a receipt for this payment.',
216 'Total Fee: $ 300.00',
217 'This Payment Amount: $ 150.00',
218 'Balance Owed: $ 0.00',
219 'Thank you for completing this payment.',
222 $mut->clearMessages();
223 $this->validateAllPayments();
227 * Test email receipt for partial payment.
229 * @dataProvider getThousandSeparators
231 * @param string $thousandSeparator
233 * @throws \CRM_Core_Exception
235 public function testRefundEmailReceipt($thousandSeparator) {
236 $this->setCurrencySeparators($thousandSeparator);
237 $decimalSeparator = ($thousandSeparator === ',' ?
'.' : ',');
238 $mut = new CiviMailUtils($this);
239 $contribution = $this->createPartiallyPaidParticipantOrder();
240 $this->callAPISuccess('payment', 'create', [
241 'contribution_id' => $contribution['id'],
242 'total_amount' => 50,
243 'check_number' => '345',
244 'trxn_date' => '2018-08-13 17:57:56',
247 $payment = $this->callAPISuccess('payment', 'create', [
248 'contribution_id' => $contribution['id'],
249 'total_amount' => -30,
250 'trxn_date' => '2018-11-13 12:01:56',
251 'sequential' => TRUE,
255 'from_financial_account_id' => 7,
256 'to_financial_account_id' => 6,
257 'total_amount' => -30,
258 'status_id' => CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_FinancialTrxn', 'status_id', 'Refunded'),
261 foreach ($expected as $key => $value) {
262 $this->assertEquals($expected[$key], $payment[$key], 'mismatch on key ' . $key);
265 $this->callAPISuccess('Payment', 'sendconfirmation', ['id' => $payment['id']]);
266 $mut->assertSubjects(['Refund Notification - Annual CiviCRM meet - Mr. Anthony Anderson II']);
269 'A refund has been issued based on changes in your registration selections.',
270 'Total Fee: $ 300' . $decimalSeparator . '00',
271 'Refund Amount: $ -30' . $decimalSeparator . '00',
272 'Event Information and Location',
274 'Transaction Date: November 13th, 2018 12:01 PM',
275 'Total Paid: $ 170' . $decimalSeparator . '00',
278 $mut->clearMessages();
279 $this->validateAllPayments();
283 * Test adding a payment to a pending multi-line order.
285 * @throws \CRM_Core_Exception
287 public function testCreatePaymentPendingOrderNoLineItems() {
288 $order = $this->createPendingParticipantOrder();
289 $this->callAPISuccess('Payment', 'create', [
290 'order_id' => $order['id'],
291 'total_amount' => 50,
293 $this->validateAllPayments();
297 * Test that Payment.create does not fail if the line items are missing.
299 * In the original spec it was anticipated that financial items would not be created
300 * for pending contributions in some circumstances. We've backed away from this and
301 * I mostly could not find a way to do it through the UI. But I did seem to once &
302 * I want to be sure that if they ARE missing no fatal occurs so this tests
303 * that in an artificial way.
305 * @throws \CRM_Core_Exception
307 public function testAddPaymentMissingFinancialItems() {
308 $contribution = $this->callAPISuccess('Contribution', 'create', [
309 'total_amount' => 50,
310 'financial_type_id' => 'Donation',
311 'contact_id' => $this->individualCreate(),
312 'contribution_status_id' => 'Pending',
314 CRM_Core_DAO
::executeQuery('DELETE FROM civicrm_financial_item');
315 $this->callAPISuccess('Payment', 'create', ['contribution_id' => $contribution['id'], 'payment_instrument_id' => 'Check', 'total_amount' => 5]);
316 $this->validateAllPayments();
320 * Add participant with contribution
324 * @throws \CRM_Core_Exception
326 protected function createPendingParticipantOrder() {
327 return $this->callAPISuccess('Order', 'create', $this->getParticipantOrderParams());
331 * Test create payment api with no line item in params
333 * @throws \CRM_Core_Exception
335 public function testCreatePaymentNoLineItems() {
336 $contribution = $this->createPartiallyPaidParticipantOrder();
338 //Create partial payment
340 'contribution_id' => $contribution['id'],
341 'total_amount' => 50,
343 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__
, __FILE__
);
344 $this->checkPaymentIsValid($payment['id'], $contribution['id']);
347 'entity_table' => 'civicrm_financial_item',
348 'financial_trxn_id' => $payment['id'],
350 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
351 $amounts = [33.33, 16.67];
352 foreach ($eft['values'] as $value) {
353 $this->assertEquals($value['amount'], array_pop($amounts));
356 // Now create payment to complete total amount of contribution
358 'contribution_id' => $contribution['id'],
359 'total_amount' => 100,
361 $payment = $this->callAPISuccess('payment', 'create', $params);
364 'from_financial_account_id' => 7,
365 'to_financial_account_id' => 6,
366 'total_amount' => 100,
371 $this->checkPaymentResult($payment, $expectedResult);
373 'entity_table' => 'civicrm_financial_item',
374 'financial_trxn_id' => $payment['id'],
376 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
377 $amounts = [66.67, 33.33];
378 foreach ($eft['values'] as $value) {
379 $this->assertEquals($value['amount'], array_pop($amounts));
381 // Check contribution for completed status
382 $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id']]);
384 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
385 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 300.00);
386 $paymentParticipant = [
387 'contribution_id' => $contribution['id'],
389 $participantPayment = $this->callAPISuccess('ParticipantPayment', 'getsingle', $paymentParticipant);
390 $participant = $this->callAPISuccess('participant', 'get', ['id' => $participantPayment['participant_id']]);
391 $this->assertEquals($participant['values'][$participant['id']]['participant_status'], 'Registered');
392 $this->callAPISuccess('Contribution', 'Delete', [
393 'id' => $contribution['id'],
395 $this->validateAllPayments();
399 * Function to assert db values
401 * @throws \CRM_Core_Exception
403 public function checkPaymentResult($payment, $expectedResult) {
404 $refreshedPayment = $this->callAPISuccessGetSingle('Payment', ['financial_trxn_id' => $payment['id']]);
405 foreach ($expectedResult[$payment['id']] as $key => $value) {
406 $this->assertEquals($refreshedPayment[$key], $value, 'mismatch on ' . $key); $this->assertEquals($refreshedPayment[$key], $value, 'mismatch on ' . $key);
411 * Test create payment api with line item in params
413 * @throws \CRM_Core_Exception
415 public function testCreatePaymentLineItems() {
416 $contribution = $this->createPartiallyPaidParticipantOrder();
417 $lineItems = $this->callAPISuccess('LineItem', 'get', ['contribution_id' => $contribution['id']])['values'];
419 // Create partial payment by passing line item array is params.
421 'contribution_id' => $contribution['id'],
422 'total_amount' => 50,
425 foreach ($lineItems as $id => $ignore) {
426 $params['line_item'][] = [$id => array_pop($amounts)];
428 $payment = $this->callAPIAndDocument('Payment', 'create', $params, __FUNCTION__
, __FILE__
, 'Payment with line item', 'CreatePaymentWithLineItems');
429 $this->checkPaymentIsValid($payment['id'], $contribution['id']);
432 'entity_table' => 'civicrm_financial_item',
433 'financial_trxn_id' => $payment['id'],
434 'return' => ['entity_id.entity_id', 'amount'],
436 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params)['values'];
437 $this->assertCount(2, $eft);
439 foreach ($eft as $value) {
440 $this->assertEquals($value['amount'], array_pop($amounts));
443 // Now create payment to complete total amount of contribution
445 'contribution_id' => $contribution['id'],
446 'total_amount' => 100,
449 foreach ($lineItems as $id => $ignore) {
450 $params['line_item'][] = [$id => array_pop($amounts)];
452 $payment = $this->callAPISuccess('Payment', 'create', $params);
455 'from_financial_account_id' => 7,
456 'to_financial_account_id' => 6,
457 'total_amount' => 100,
462 $this->checkPaymentResult($payment, $expectedResult);
464 'entity_table' => 'civicrm_financial_item',
465 'financial_trxn_id' => $payment['id'],
467 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params)['values'];
468 $this->assertCount(2, $eft);
470 foreach ($eft as $value) {
471 $this->assertEquals($value['amount'], array_pop($amounts));
473 // Check contribution for completed status
474 $contribution = $this->callAPISuccess('Contribution', 'get', ['id' => $contribution['id']]);
476 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
477 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 300.00);
478 $paymentParticipant = [
479 'contribution_id' => $contribution['id'],
481 $participantPayment = $this->callAPISuccess('ParticipantPayment', 'getsingle', $paymentParticipant);
482 $participant = $this->callAPISuccess('participant', 'get', ['id' => $participantPayment['participant_id']]);
483 $this->assertEquals($participant['values'][$participant['id']]['participant_status'], 'Registered');
484 $this->validateAllPayments();
488 * Test negative payment using create API.
490 public function testRefundPayment() {
491 $result = $this->callAPISuccess('Contribution', 'create', [
492 'financial_type_id' => "Donation",
493 'total_amount' => 100,
494 'contact_id' => $this->_individualId
,
496 $contributionID = $result['id'];
498 //Refund a part of the main amount.
499 $this->callAPISuccess('Payment', 'create', [
500 'contribution_id' => $contributionID,
501 'total_amount' => -10,
504 $contribution = $this->callAPISuccessGetSingle('Contribution', [
505 'return' => ["contribution_status_id"],
506 'id' => $contributionID,
508 //Still we've a status of Completed after refunding a partial amount.
509 $this->assertEquals($contribution['contribution_status'], 'Completed');
511 //Refund the complete amount.
512 $this->callAPISuccess('Payment', 'create', [
513 'contribution_id' => $contributionID,
514 'total_amount' => -90,
516 $contribution = $this->callAPISuccessGetSingle('Contribution', [
517 'return' => ["contribution_status_id"],
518 'id' => $contributionID,
520 //Assert if main contribution status is updated to "Refunded".
521 $this->assertEquals($contribution['contribution_status'], 'Refunded Label**');
525 * Test cancel payment api
527 * @throws \CRM_Core_Exception
529 public function testCancelPayment() {
530 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['administer CiviCRM', 'access CiviContribute'];
531 $contribution = $this->createPartiallyPaidParticipantOrder();
534 'contribution_id' => $contribution['id'],
537 $payment = $this->callAPISuccess('payment', 'get', $params);
538 $this->assertEquals(1, $payment['count']);
541 'id' => $payment['id'],
542 'check_permissions' => TRUE,
544 $payment = $this->callAPIFailure('payment', 'cancel', $cancelParams, 'API permission check failed for Payment/cancel call; insufficient permission: require access CiviCRM and access CiviContribute and edit contributions');
546 array_push(CRM_Core_Config
::singleton()->userPermissionClass
->permissions
, 'access CiviCRM', 'edit contributions');
548 $this->callAPIAndDocument('payment', 'cancel', $cancelParams, __FUNCTION__
, __FILE__
);
550 $payment = $this->callAPISuccess('payment', 'get', $params);
551 $this->assertEquals(2, $payment['count']);
552 $amounts = [-150.00, 150.00];
553 foreach ($payment['values'] as $value) {
554 $this->assertEquals($value['total_amount'], array_pop($amounts), 'Mismatch total amount');
557 $this->callAPISuccess('Contribution', 'Delete', [
558 'id' => $contribution['id'],
560 $this->validateAllPayments();
564 * Test delete payment api
566 * @throws \CRM_Core_Exception
568 public function testDeletePayment() {
569 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['administer CiviCRM', 'access CiviContribute'];
570 $contribution = $this->createPartiallyPaidParticipantOrder();
573 'contribution_id' => $contribution['id'],
576 $payment = $this->callAPISuccess('payment', 'get', $params);
577 $this->assertEquals(1, $payment['count']);
580 'id' => $payment['id'],
581 'check_permissions' => TRUE,
583 $payment = $this->callAPIFailure('payment', 'delete', $deleteParams, 'API permission check failed for Payment/delete call; insufficient permission: require access CiviCRM and access CiviContribute and delete in CiviContribute');
585 array_push(CRM_Core_Config
::singleton()->userPermissionClass
->permissions
, 'access CiviCRM', 'delete in CiviContribute');
586 $this->callAPIAndDocument('payment', 'delete', $deleteParams, __FUNCTION__
, __FILE__
);
588 $payment = $this->callAPISuccess('payment', 'get', $params);
589 $this->assertEquals(0, $payment['count']);
591 $this->callAPISuccess('Contribution', 'Delete', [
592 'id' => $contribution['id'],
597 * Test update payment api.
599 * 1) create a contribution for $300 with a partial payment of $150
600 * - this results in 2 financial transactions. The accounts receivable transaction is linked
601 * via entity_financial_trxns to the 2 line items. The $150 payment is not linked to the line items
602 * so the line items are fully allocated even though they are only half paid.
604 * 2) add a payment of $50 -
605 * This payment transaction IS linked to the line items so $350 of the $300 in line items is allocated
608 * 3) update that payment to be $100
609 * This results in a negative and a positive payment ($50 & $100) - the negative payment results in
610 * financial_items but the positive payment does not.
612 * The final result is we have
613 * - 1 partly paid contribution of $300
614 * - payment financial_trxns totalling $250
615 * - 1 Accounts receivable financial_trxn totalling $300
616 * - 2 financial items totalling $300 linked to the Accounts receivable financial_trxn
617 * - 6 entries in the civicrm_entity_financial_trxn linked to line items - totalling $450.
618 * - 5 entries in the civicrm_entity_financial_trxn linked to contributions - totalling $550.
620 * @throws \CRM_Core_Exception
622 public function testUpdatePayment() {
623 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['administer CiviCRM', 'access CiviContribute', 'edit contributions'];
624 $contribution = $this->createPartiallyPaidParticipantOrder();
626 //Create partial payment by passing line item array is params
628 'contribution_id' => $contribution['id'],
629 'total_amount' => 50,
632 $payment = $this->callAPISuccess('payment', 'create', $params);
635 'from_financial_account_id' => 7,
636 'to_financial_account_id' => 6,
637 'total_amount' => 50,
642 $this->checkPaymentResult($payment, $expectedResult);
645 'entity_table' => 'civicrm_financial_item',
646 'financial_trxn_id' => $payment['id'],
648 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
649 $amounts = [33.33, 16.67];
650 foreach ($eft['values'] as $value) {
651 $this->assertEquals($value['amount'], array_pop($amounts));
654 // update the amount for payment
656 'contribution_id' => $contribution['id'],
657 'total_amount' => 100,
658 'id' => $payment['id'],
659 'check_permissions' => TRUE,
661 // @todo - move this permissions test to it's own test - it just confuses here.
662 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['administer CiviCRM', 'access CiviContribute'];
663 $this->callAPIFailure('payment', 'create', $params, 'API permission check failed for Payment/create call; insufficient permission: require access CiviCRM and access CiviContribute and edit contributions');
665 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['administer CiviCRM', 'access CiviContribute', 'access CiviCRM', 'edit contributions'];
666 $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__
, __FILE__
, 'Update Payment', 'UpdatePayment');
668 $this->validateAllPayments();
669 // Check for proportional cancelled payment against lineitems.
671 'entity_table' => 'civicrm_financial_item',
672 'financial_trxn_id' => $payment['id'] - 1,
675 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $minParams)['values'];
676 $this->assertCount(2, $eft);
677 $amounts = [-33.33, -16.67];
679 foreach ($eft as $value) {
680 $this->assertEquals($value['amount'], array_pop($amounts));
683 // Check for proportional updated payment against lineitems.
685 'entity_table' => 'civicrm_financial_item',
686 'financial_trxn_id' => $payment['id'],
688 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params)['values'];
689 $amounts = [66.67, 33.33];
690 foreach ($eft as $value) {
691 $this->assertEquals($value['amount'], array_pop($amounts));
693 $items = $this->callAPISuccess('FinancialItem', 'get', [])['values'];
694 $this->assertCount(2, $items);
696 foreach ($items as $item) {
697 $this->assertEquals('civicrm_line_item', $item['entity_table']);
698 $itemSum +
= $item['amount'];
700 $this->assertEquals(300, $itemSum);
703 'contribution_id' => $contribution['id'],
705 $payment = $this->callAPISuccess('payment', 'get', $params);
706 $amounts = [100.00, -50.00, 50.00, 150.00];
707 foreach ($payment['values'] as $value) {
708 $amount = array_pop($amounts);
709 $this->assertEquals($value['total_amount'], $amount, 'Mismatch total amount');
711 // Check entity financial trxn created properly
713 'entity_id' => $contribution['id'],
714 'entity_table' => 'civicrm_contribution',
715 'financial_trxn_id' => $value['id'],
717 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
718 $this->assertEquals($eft['values'][$eft['id']]['amount'], $amount);
721 $this->callAPISuccess('Contribution', 'Delete', [
722 'id' => $contribution['id'],
724 $this->validateAllPayments();
728 * Test that a contribution can be overpaid with the payment api.
730 * @throws \CRM_Core_Exception
732 public function testCreatePaymentOverPay() {
733 $contributionID = $this->contributionCreate(['contact_id' => $this->individualCreate()]);
734 $payment = $this->callAPISuccess('Payment', 'create', ['total_amount' => 5, 'order_id' => $contributionID]);
735 $contribution = $this->callAPISuccessGetSingle('Contribution', ['id' => $contributionID]);
736 $this->assertEquals('Completed', $contribution['contribution_status']);
737 $this->callAPISuccessGetCount('EntityFinancialTrxn', ['financial_trxn_id' => $payment['id'], 'entity_table' => 'civicrm_financial_item'], 0);
738 $this->validateAllPayments();
739 $this->validateAllContributions();
743 * Test create payment api for paylater contribution
745 * @throws \CRM_Core_Exception
747 public function testCreatePaymentPayLater() {
748 $this->createLoggedInUser();
749 $processorID = $this->paymentProcessorCreate();
750 $contributionParams = [
751 'total_amount' => 100,
753 'contact_id' => $this->_individualId
,
754 'financial_type_id' => 1,
755 'contribution_status_id' => 2,
758 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
759 //add payment for pay later transaction
761 'contribution_id' => $contribution['id'],
762 'total_amount' => 100,
763 'card_type_id' => 'Visa',
764 'pan_truncation' => '1234',
765 'trxn_result_code' => 'Startling success',
766 'payment_instrument_id' => $processorID,
769 $payment = $this->callAPISuccess('Payment', 'create', $params);
772 'from_financial_account_id' => 7,
773 'to_financial_account_id' => 6,
774 'total_amount' => 100,
778 'pan_truncation' => '1234',
779 'trxn_result_code' => 'Startling success',
781 'payment_instrument_id' => 1,
784 $this->checkPaymentResult($payment, $expectedResult);
785 // Check entity financial trxn created properly
787 'entity_id' => $contribution['id'],
788 'entity_table' => 'civicrm_contribution',
789 'financial_trxn_id' => $payment['id'],
791 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
792 $this->assertEquals($eft['values'][$eft['id']]['amount'], 100);
794 'entity_table' => 'civicrm_financial_item',
795 'financial_trxn_id' => $payment['id'],
797 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
798 $this->assertEquals($eft['values'][$eft['id']]['amount'], 100);
799 // Check contribution for completed status
800 $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id']]);
801 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
802 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
803 $this->callAPISuccess('Contribution', 'Delete', [
804 'id' => $contribution['id'],
806 $this->validateAllPayments();
810 * Test net amount is set when fee amount is passed in.
812 * @throws \CRM_Core_Exception
814 public function testNetAmount() {
815 $order = $this->createPendingParticipantOrder();
816 $payment = $this->callAPISuccess('Payment', 'create', ['order_id' => $order['id'], 'total_amount' => 10, 'fee_amount' => .25]);
817 $this->assertEquals('9.75', $this->callAPISuccessGetValue('Payment', ['id' => $payment['id'], 'return' => 'net_amount']));
821 * Test create payment api for pay later contribution with partial payment.
823 * https://lab.civicrm.org/dev/financial/issues/69
824 * @throws \CRM_Core_Exception
826 public function testCreatePaymentIncompletePaymentPartialPayment() {
827 $contributionParams = [
828 'total_amount' => 100,
830 'contact_id' => $this->_individualId
,
831 'financial_type_id' => 1,
832 'contribution_status_id' => 2,
834 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
835 $this->callAPISuccess('Payment', 'create', [
836 'contribution_id' => $contribution['id'],
837 'total_amount' => 50,
838 'payment_instrument_id' => 'Cash',
840 $payments = $this->callAPISuccess('Payment', 'get', ['contribution_id' => $contribution['id']])['values'];
841 $this->assertCount(1, $payments);
842 $this->validateAllPayments();
846 * Test create payment api for pay later contribution with partial payment.
848 * @throws \CRM_Core_Exception
850 public function testCreatePaymentPayLaterPartialPayment() {
851 $this->createLoggedInUser();
852 $contributionParams = [
853 'total_amount' => 100,
855 'contact_id' => $this->_individualId
,
856 'financial_type_id' => 1,
857 'contribution_status_id' => 2,
860 $contribution = $this->callAPISuccess('Order', 'create', $contributionParams);
861 //Create partial payment
863 'contribution_id' => $contribution['id'],
864 'total_amount' => 60,
866 $payment = $this->callAPISuccess('Payment', 'create', $params);
869 'total_amount' => 60,
874 $this->checkPaymentResult($payment, $expectedResult);
875 // Check entity financial trxn created properly
877 'entity_id' => $contribution['id'],
878 'entity_table' => 'civicrm_contribution',
879 'financial_trxn_id' => $payment['id'],
881 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
882 $this->assertEquals($eft['values'][$eft['id']]['amount'], 60);
884 'entity_table' => 'civicrm_financial_item',
885 'financial_trxn_id' => $payment['id'],
887 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
888 $this->assertEquals($eft['values'][$eft['id']]['amount'], 60);
889 $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id']]);
890 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Partially paid');
891 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
892 //Create full payment
894 'contribution_id' => $contribution['id'],
895 'total_amount' => 40,
897 // Rename the 'completed' status label first to check that we are not using the labels!
898 $this->callAPISuccess('OptionValue', 'get', ['name' => 'Completed', 'option_group_id' => 'contribution_status', 'api.OptionValue.create' => ['label' => 'Unicorn']]);
899 $payment = $this->callAPISuccess('Payment', 'create', $params);
902 'from_financial_account_id' => 7,
903 'to_financial_account_id' => 6,
904 'total_amount' => 40,
909 $this->checkPaymentResult($payment, $expectedResult);
910 // Check entity financial trxn created properly
912 'entity_id' => $contribution['id'],
913 'entity_table' => 'civicrm_contribution',
914 'financial_trxn_id' => $payment['id'],
916 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
917 $this->assertEquals($eft['values'][$eft['id']]['amount'], 40);
919 'entity_table' => 'civicrm_financial_item',
920 'financial_trxn_id' => $payment['id'],
922 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
923 $this->assertEquals($eft['values'][$eft['id']]['amount'], 40);
924 // Check contribution for completed status
925 $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id']]);
926 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Unicorn');
927 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
928 $this->callAPISuccess('Contribution', 'Delete', [
929 'id' => $contribution['id'],
931 $this->callAPISuccess('OptionValue', 'get', ['name' => 'Completed', 'option_group_id' => 'contribution_status', 'api.OptionValue.create' => ['label' => 'Completed']]);
932 $this->callAPISuccessGetCount('Activity', ['target_contact_id' => $this->_individualId
, 'activity_type_id' => 'Payment'], 2);
933 $this->validateAllPayments();
937 * Test that Payment.create uses the to_account of the payment processor.
939 * @throws \CiviCRM_API3_Exception
940 * @throws \CRM_Core_Exception
942 public function testPaymentWithProcessorWithOddFinancialAccount() {
943 $processor = $this->dummyProcessorCreate(['financial_account_id' => 'Deposit Bank Account', 'payment_instrument_id' => 'Cash']);
944 $processor2 = $this->dummyProcessorCreate(['financial_account_id' => 'Payment Processor Account', 'name' => 'p2', 'payment_instrument_id' => 'EFT']);
945 $contributionParams = [
946 'total_amount' => 100,
948 'contact_id' => $this->_individualId
,
949 'financial_type_id' => 1,
950 'contribution_status_id' => 'Pending',
952 $order = $this->callAPISuccess('Order', 'create', $contributionParams);
953 $this->callAPISuccess('Payment', 'create', ['payment_processor_id' => $processor->getID(), 'total_amount' => 6, 'contribution_id' => $order['id']]);
954 $this->callAPISuccess('Payment', 'create', ['payment_processor_id' => $processor2->getID(), 'total_amount' => 15, 'contribution_id' => $order['id']]);
955 $payments = $this->callAPISuccess('Payment', 'get', ['sequential' => 1, 'contribution_id' => $order['id']])['values'];
956 $this->assertEquals('Deposit Bank Account', CRM_Core_PseudoConstant
::getName('CRM_Core_BAO_FinancialTrxn', 'to_financial_account_id', $payments[0]['to_financial_account_id']));
957 $this->assertEquals('Payment Processor Account', CRM_Core_PseudoConstant
::getName('CRM_Core_BAO_FinancialTrxn', 'to_financial_account_id', $payments[1]['to_financial_account_id']));
958 $this->assertEquals('Accounts Receivable', CRM_Core_PseudoConstant
::getName('CRM_Core_BAO_FinancialTrxn', 'from_financial_account_id', $payments[0]['from_financial_account_id']));
959 $this->assertEquals('Accounts Receivable', CRM_Core_PseudoConstant
::getName('CRM_Core_BAO_FinancialTrxn', 'from_financial_account_id', $payments[1]['from_financial_account_id']));
960 $this->assertEquals('Cash', CRM_Core_PseudoConstant
::getName('CRM_Core_BAO_FinancialTrxn', 'payment_instrument_id', $payments[0]['payment_instrument_id']));
961 $this->assertEquals('EFT', CRM_Core_PseudoConstant
::getName('CRM_Core_BAO_FinancialTrxn', 'payment_instrument_id', $payments[1]['payment_instrument_id']));
962 // $order = $this->callAPISuccessGetSingle('Order', ['id' => $processor->getID()]);
963 // $this->assertEquals('Cash', CRM_Core_PseudoConstant::getName('CRM_Core_BAO_FinancialTrxn', 'payment_instrument_id', $order['payment_instrument_id']));
967 * Add a location to our event.
969 * @param int $eventID
971 * @throws \CRM_Core_Exception
973 protected function addLocationToEvent($eventID) {
975 'name' => 'event place',
976 'street_address' => 'streety street',
977 'location_type_id' => 1,
980 // api requires contact_id - perhaps incorrectly but use add to get past that.
981 $address = CRM_Core_BAO_Address
::add($addressParams);
983 $location = $this->callAPISuccess('LocBlock', 'create', ['address_id' => $address->id
]);
984 $this->callAPISuccess('Event', 'create', [
986 'loc_block_id' => $location['id'],
987 'is_show_location' => TRUE,
989 $this->validateAllPayments();
993 * Check the created payment is valid.
995 * This is probably over-testing really since we are repetitively checking a basic function...
997 * @param int $paymentID
998 * @param int $contributionID
1001 * @throws \CRM_Core_Exception
1003 protected function checkPaymentIsValid($paymentID, $contributionID, $amount = 50) {
1004 $payment = $this->callAPISuccess('Payment', 'getsingle', ['financial_trxn_id' => $paymentID]);
1005 $this->assertEquals(7, $payment['from_financial_account_id']);
1006 $this->assertEquals(6, $payment['to_financial_account_id']);
1007 $this->assertEquals(1, $payment['status_id']);
1008 $this->assertEquals(1, $payment['is_payment']);
1009 $this->assertEquals($amount, $payment['total_amount']);
1011 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', [
1012 'entity_id' => $contributionID,
1013 'entity_table' => 'civicrm_contribution',
1014 'financial_trxn_id' => $payment['id'],
1017 $this->assertEquals($eft['values'][$eft['id']]['amount'], $amount);
1018 $this->validateAllPayments();