Update code comments
[civicrm-core.git] / tests / phpunit / CRM / Contribute / Form / Contribution / ConfirmTest.php
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 * Test APIv3 civicrm_contribute_* functions
14 *
15 * @package CiviCRM_APIv3
16 * @subpackage API_Contribution
17 * @group headless
18 */
19 class CRM_Contribute_Form_Contribution_ConfirmTest extends CiviUnitTestCase {
20
21 /**
22 * Clean up DB.
23 */
24 public function tearDown() {
25 $this->quickCleanUpFinancialEntities();
26 }
27
28 /**
29 * CRM-21200: Test that making online payment for pending contribution doesn't overwite the contribution details
30 */
31 public function testPaynowPayment() {
32 $contactID = $this->individualCreate();
33 $paymentProcessorID = $this->paymentProcessorCreate(['payment_processor_type_id' => 'Dummy']);
34
35 // create a contribution page which is later used to make pay-later contribution
36 $result = $this->callAPISuccess('ContributionPage', 'create', [
37 'title' => 'Test Contribution Page',
38 'financial_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Campaign Contribution'),
39 'currency' => 'USD',
40 'financial_account_id' => 1,
41 'payment_processor' => $paymentProcessorID,
42 'is_active' => 1,
43 'is_allow_other_amount' => 1,
44 'min_amount' => 20,
45 'max_amount' => 2000,
46 ]);
47 $contributionPageID1 = $result['id'];
48 // create pending contribution
49 $contribution = $this->callAPISuccess('Contribution', 'create', [
50 'contact_id' => $contactID,
51 'financial_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Campaign Contribution'),
52 'currency' => 'USD',
53 'total_amount' => 100.00,
54 'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'),
55 'contribution_page_id' => $contributionPageID1,
56 'source' => 'backoffice pending contribution',
57 ]);
58
59 // create a contribution page which is later used to make online payment for pending contribution
60 $result = $this->callAPISuccess('ContributionPage', 'create', [
61 'title' => 'Test Contribution Page',
62 'financial_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Campaign Contribution'),
63 'currency' => 'USD',
64 'financial_account_id' => 1,
65 'payment_processor' => $paymentProcessorID,
66 'is_active' => 1,
67 'is_allow_other_amount' => 1,
68 'min_amount' => 10,
69 'max_amount' => 1000,
70 ]);
71 $form = new CRM_Contribute_Form_Contribution_Confirm();
72 $contributionPageID2 = $result['id'];
73 $form->_id = $contributionPageID2;
74 $form->_values = $result['values'][$contributionPageID2];
75 $form->_paymentProcessor = [
76 'id' => $paymentProcessorID,
77 'billing_mode' => CRM_Core_Payment::BILLING_MODE_FORM,
78 'object' => Civi\Payment\System::singleton()->getById($paymentProcessorID),
79 'is_recur' => FALSE,
80 'payment_instrument_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Credit card'),
81 ];
82 $form->_params = [
83 'qfKey' => 'donotcare',
84 'contribution_id' => $contribution['id'],
85 'credit_card_number' => 4111111111111111,
86 'cvv2' => 234,
87 'credit_card_exp_date' => [
88 'M' => 2,
89 'Y' => 2021,
90 ],
91 'credit_card_type' => 'Visa',
92 'email-5' => 'test@test.com',
93 'total_amount' => 100.00,
94 'payment_processor_id' => $paymentProcessorID,
95 'amount' => 100,
96 'tax_amount' => 0.00,
97 'year' => 2021,
98 'month' => 2,
99 'currencyID' => 'USD',
100 'is_pay_later' => 0,
101 'invoiceID' => '6e443672a9bb2198cc12f076aed70e7a',
102 'is_quick_config' => 1,
103 'description' => $contribution['values'][$contribution['id']]['source'],
104 'skipLineItem' => 0,
105 ];
106
107 $processConfirmResult = CRM_Contribute_BAO_Contribution_Utils::processConfirm($form,
108 $form->_params,
109 $contactID,
110 $form->_values['financial_type_id'],
111 0, FALSE
112 );
113
114 // Make sure that certain parameters are set on return from processConfirm
115 $this->assertEquals($form->_values['financial_type_id'], $processConfirmResult['financial_type_id']);
116
117 // Based on the processed contribution, complete transaction which update the contribution status based on payment result.
118 if (!empty($processConfirmResult['contribution'])) {
119 $this->callAPISuccess('contribution', 'completetransaction', [
120 'id' => $processConfirmResult['contribution']->id,
121 'trxn_date' => date('Y-m-d'),
122 'payment_processor_id' => $paymentProcessorID,
123 ]);
124 }
125
126 $contribution = $this->callAPISuccessGetSingle('Contribution', [
127 'id' => $form->_params['contribution_id'],
128 'return' => [
129 'contribution_page_id',
130 'contribution_status',
131 'contribution_source',
132 ],
133 ]);
134
135 // check that contribution page ID isn't changed
136 $this->assertEquals($contributionPageID1, $contribution['contribution_page_id']);
137 // check that paid later information is present in contribution's source
138 $this->assertRegExp("/Paid later via page ID: $contributionPageID2/", $contribution['contribution_source']);
139 // check that contribution status is changed to 'Completed' from 'Pending'
140 $this->assertEquals('Completed', $contribution['contribution_status']);
141
142 //delete contribution.
143 $this->callAPISuccess('contribution', 'delete', [
144 'id' => $processConfirmResult['contribution']->id,
145 ]);
146
147 //Process on behalf contribution.
148 unset($form->_params['contribution_id']);
149 $form->_contactID = $form->_values['related_contact'] = $form->_params['onbehalf_contact_id'] = $contactID;
150 $form->_params['contact_id'] = $this->organizationCreate();
151 $this->callAPISuccess('Relationship', 'create', [
152 'contact_id_a' => $contactID,
153 'contact_id_b' => $form->_params['contact_id'],
154 'relationship_type_id' => 5,
155 'is_current_employer' => 1,
156 ]);
157 $processConfirmResult = CRM_Contribute_BAO_Contribution_Utils::processConfirm($form,
158 $form->_params,
159 $form->_params['onbehalf_contact_id'],
160 $form->_values['financial_type_id'],
161 0, FALSE
162 );
163 //check if contribution is created on org.
164 $contribution = $this->callAPISuccessGetSingle('Contribution', [
165 'contact_id' => $form->_params['onbehalf_contact_id'],
166 ]);
167
168 $activity = civicrm_api3('Activity', 'get', [
169 'sequential' => 1,
170 'source_record_id' => $contribution['id'],
171 'contact_id' => $form->_params['onbehalf_contact_id'],
172 'activity_type_id' => "Contribution",
173 ]);
174 $this->assertEquals(1, $activity['count']);
175 }
176
177 }