Merge pull request #15638 from eileenmcnaughton/vangelis
[civicrm-core.git] / tests / phpunit / api / v3 / ContributionTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
2fe49090 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035 27
6a488035
TO
28/**
29 * Test APIv3 civicrm_contribute_* functions
30 *
6c6e6187
TO
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contribution
acb109b7 33 * @group headless
6a488035 34 */
6a488035
TO
35class api_v3_ContributionTest extends CiviUnitTestCase {
36
ed7e2e99 37 use CRMTraits_Profile_ProfileTrait;
38
6a488035
TO
39 protected $_individualId;
40 protected $_contribution;
4ab7d517 41 protected $_financialTypeId = 1;
6a488035
TO
42 protected $_apiversion;
43 protected $_entity = 'Contribution';
44 public $debug = 0;
45 protected $_params;
9099cab3
CW
46 protected $_ids = [];
47 protected $_pageParams = [];
1eade77d 48 /**
49 * Payment processor ID (dummy processor).
50 *
51 * @var int
52 */
53 protected $paymentProcessorID;
b7c9bc4c 54
1e52837d
EM
55 /**
56 * Parameters to create payment processor.
57 *
58 * @var array
59 */
9099cab3 60 protected $_processorParams = [];
1e52837d
EM
61
62 /**
63 * ID of created event.
64 *
65 * @var int
66 */
67 protected $_eventID;
68
ec7e3954
E
69 /**
70 * @var CiviMailUtils
71 */
72 protected $mut;
73
22f80e87
EM
74 /**
75 * Setup function.
76 */
00be9182 77 public function setUp() {
6a488035
TO
78 parent::setUp();
79
80 $this->_apiversion = 3;
81 $this->_individualId = $this->individualCreate();
9099cab3 82 $this->_params = [
6a488035
TO
83 'contact_id' => $this->_individualId,
84 'receive_date' => '20120511',
85 'total_amount' => 100.00,
5896d037 86 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
87 'non_deductible_amount' => 10.00,
88 'fee_amount' => 5.00,
89 'net_amount' => 95.00,
90 'source' => 'SSF',
91 'contribution_status_id' => 1,
9099cab3
CW
92 ];
93 $this->_processorParams = [
6a488035
TO
94 'domain_id' => 1,
95 'name' => 'Dummy',
97502bac 96 'payment_processor_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Financial_BAO_PaymentProcessor', 'payment_processor_type_id', 'Dummy'),
6a488035
TO
97 'financial_account_id' => 12,
98 'is_active' => 1,
99 'user_name' => '',
100 'url_site' => 'http://dummy.com',
101 'url_recur' => 'http://dummy.com',
102 'billing_mode' => 1,
9099cab3 103 ];
1eade77d 104 $this->paymentProcessorID = $this->processorCreate();
9099cab3 105 $this->_pageParams = [
6a488035
TO
106 'title' => 'Test Contribution Page',
107 'financial_type_id' => 1,
108 'currency' => 'USD',
109 'financial_account_id' => 1,
1eade77d 110 'payment_processor' => $this->paymentProcessorID,
6a488035
TO
111 'is_active' => 1,
112 'is_allow_other_amount' => 1,
113 'min_amount' => 10,
114 'max_amount' => 1000,
9099cab3 115 ];
6a488035
TO
116 }
117
22f80e87
EM
118 /**
119 * Clean up after each test.
f736929b 120 *
121 * @throws \CRM_Core_Exception
22f80e87 122 */
00be9182 123 public function tearDown() {
39d632fd 124 $this->quickCleanUpFinancialEntities();
9099cab3
CW
125 $this->quickCleanup(['civicrm_uf_match']);
126 $financialAccounts = $this->callAPISuccess('FinancialAccount', 'get', []);
83644f47 127 foreach ($financialAccounts['values'] as $financialAccount) {
f736929b 128 if ($financialAccount['name'] === 'Test Tax financial account ' || $financialAccount['name'] === 'Test taxable financial Type') {
9099cab3 129 $entityFinancialTypes = $this->callAPISuccess('EntityFinancialAccount', 'get', [
83644f47 130 'financial_account_id' => $financialAccount['id'],
9099cab3 131 ]);
83644f47 132 foreach ($entityFinancialTypes['values'] as $entityFinancialType) {
9099cab3 133 $this->callAPISuccess('EntityFinancialAccount', 'delete', ['id' => $entityFinancialType['id']]);
83644f47 134 }
9099cab3 135 $this->callAPISuccess('FinancialAccount', 'delete', ['id' => $financialAccount['id']]);
83644f47 136 }
137 }
f736929b 138 $this->restoreUFGroupOne();
6a488035
TO
139 }
140
22f80e87
EM
141 /**
142 * Test Get.
143 */
00be9182 144 public function testGetContribution() {
802c1c41 145 $contributionSettings = $this->enableTaxAndInvoicing();
b07b172b 146 $invoice_prefix = CRM_Contribute_BAO_Contribution::checkContributeSettings('invoice_prefix', TRUE);
9099cab3 147 $p = [
6a488035
TO
148 'contact_id' => $this->_individualId,
149 'receive_date' => '2010-01-20',
150 'total_amount' => 100.00,
4ab7d517 151 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
152 'non_deductible_amount' => 10.00,
153 'fee_amount' => 5.00,
154 'net_amount' => 95.00,
155 'trxn_id' => 23456,
156 'invoice_id' => 78910,
157 'source' => 'SSF',
158 'contribution_status_id' => 1,
9099cab3 159 ];
2f45e1c2 160 $this->_contribution = $this->callAPISuccess('contribution', 'create', $p);
6a488035 161
9099cab3 162 $params = [
6a488035 163 'contribution_id' => $this->_contribution['id'],
9099cab3 164 ];
e0e3c51b 165
3af96592 166 $contributions = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__, __FILE__);
4ab7d517 167 $financialParams['id'] = $this->_financialTypeId;
6c6e6187 168 $default = NULL;
858d0bf8 169 CRM_Financial_BAO_FinancialType::retrieve($financialParams, $default);
2f45e1c2 170
3af96592 171 $this->assertEquals(1, $contributions['count']);
172 $contribution = $contributions['values'][$contributions['id']];
173 $this->assertEquals($contribution['contact_id'], $this->_individualId);
22f80e87
EM
174 // Note there was an assertion converting financial_type_id to 'Donation' which wasn't working.
175 // Passing back a string rather than an id seems like an error/cruft.
176 // If it is to be introduced we should discuss.
3af96592 177 $this->assertEquals($contribution['financial_type_id'], 1);
178 $this->assertEquals($contribution['total_amount'], 100.00);
179 $this->assertEquals($contribution['non_deductible_amount'], 10.00);
180 $this->assertEquals($contribution['fee_amount'], 5.00);
181 $this->assertEquals($contribution['net_amount'], 95.00);
182 $this->assertEquals($contribution['trxn_id'], 23456);
183 $this->assertEquals($contribution['invoice_id'], 78910);
802c1c41 184 $this->assertEquals($contribution['invoice_number'], $invoice_prefix . $contributions['id']);
3af96592 185 $this->assertEquals($contribution['contribution_source'], 'SSF');
186 $this->assertEquals($contribution['contribution_status'], 'Completed');
22f80e87 187 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
6a488035
TO
188 $p['trxn_id'] = '3847';
189 $p['invoice_id'] = '3847';
190
2f45e1c2 191 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
192
22f80e87 193 // Now we have 2 - test getcount.
9099cab3 194 $contribution = $this->callAPISuccess('contribution', 'getcount', []);
6a488035 195 $this->assertEquals(2, $contribution);
22f80e87 196 // Test id only format.
9099cab3 197 $contribution = $this->callAPISuccess('contribution', 'get', [
4ab7d517 198 'id' => $this->_contribution['id'],
199 'format.only_id' => 1,
9099cab3 200 ]);
22f80e87
EM
201 $this->assertEquals($this->_contribution['id'], $contribution, print_r($contribution, TRUE));
202 // Test id only format.
9099cab3 203 $contribution = $this->callAPISuccess('contribution', 'get', [
4ab7d517 204 'id' => $contribution2['id'],
205 'format.only_id' => 1,
9099cab3 206 ]);
4ab7d517 207 $this->assertEquals($contribution2['id'], $contribution);
22f80e87 208 // Test id as field.
9099cab3 209 $contribution = $this->callAPISuccess('contribution', 'get', [
4ab7d517 210 'id' => $this->_contribution['id'],
9099cab3 211 ]);
2bfae985 212 $this->assertEquals(1, $contribution['count']);
4ab7d517 213
22f80e87 214 // Test get by contact id works.
9099cab3 215 $contribution = $this->callAPISuccess('contribution', 'get', ['contact_id' => $this->_individualId]);
6a488035 216
2bfae985 217 $this->assertEquals(2, $contribution['count']);
9099cab3 218 $this->callAPISuccess('Contribution', 'Delete', [
6a488035 219 'id' => $this->_contribution['id'],
9099cab3
CW
220 ]);
221 $this->callAPISuccess('Contribution', 'Delete', [
6a488035 222 'id' => $contribution2['id'],
9099cab3 223 ]);
6a488035
TO
224 }
225
71d5a412 226 /**
227 * Test that test contributions can be retrieved.
228 */
229 public function testGetTestContribution() {
9099cab3
CW
230 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, ['is_test' => 1]));
231 $this->callAPISuccessGetSingle('Contribution', ['is_test' => 1]);
71d5a412 232 }
233
7d96cec3
SL
234 /**
235 * Test Creating a check contribution with original check_number field
236 */
237 public function testCreateCheckContribution() {
238 $params = $this->_params;
239 $params['contribution_check_number'] = 'bouncer';
240 $params['payment_instrument_id'] = 'Check';
241 $params['cancel_date'] = 'yesterday';
242 $params['receipt_date'] = 'yesterday';
243 $params['thankyou_date'] = 'yesterday';
244 $params['revenue_recognition_date'] = 'yesterday';
245 $params['amount_level'] = 'Unreasonable';
246 $params['cancel_reason'] = 'You lose sucker';
247 $params['creditnote_id'] = 'sudo rm -rf';
248 $params['tax_amount'] = '1';
249 $address = $this->callAPISuccess('Address', 'create', [
250 'street_address' => 'Knockturn Alley',
251 'contact_id' => $this->_individualId,
252 'location_type_id' => 'Home',
253 ]);
254 $params['address_id'] = $address['id'];
255 $contributionPage = $this->contributionPageCreate();
256 $params['contribution_page_id'] = $contributionPage['id'];
257 $params['campaign_id'] = $this->campaignCreate();
258 $contributionID = $this->contributionCreate($params);
259 $getResult = $this->callAPISuccess('Contribution', 'get', ['id' => $contributionID]);
260 $this->assertEquals('bouncer', $getResult['values'][$contributionID]['check_number']);
261 $entityFinancialTrxn = $this->callAPISuccess('EntityFinancialTrxn', 'get', ['entity_id' => $contributionID, 'entity_table' => 'civicrm_contribution']);
262 foreach ($entityFinancialTrxn['values'] as $eft) {
263 $financialTrxn = $this->callAPISuccess('FinancialTrxn', 'get', ['id' => $eft['financial_trxn_id']]);
264 $this->assertEquals('bouncer', $financialTrxn['values'][$financialTrxn['id']]['check_number']);
265 }
266 }
267
59f9e5a6 268 /**
269 * Test the 'return' param works for all fields.
270 */
271 public function testGetContributionReturnFunctionality() {
272 $params = $this->_params;
7d96cec3 273 $params['contribution_check_number'] = 'bouncer';
59f9e5a6 274 $params['payment_instrument_id'] = 'Check';
275 $params['cancel_date'] = 'yesterday';
276 $params['receipt_date'] = 'yesterday';
277 $params['thankyou_date'] = 'yesterday';
278 $params['revenue_recognition_date'] = 'yesterday';
279 $params['amount_level'] = 'Unreasonable';
280 $params['cancel_reason'] = 'You lose sucker';
281 $params['creditnote_id'] = 'sudo rm -rf';
282 $params['tax_amount'] = '1';
9099cab3 283 $address = $this->callAPISuccess('Address', 'create', [
59f9e5a6 284 'street_address' => 'Knockturn Alley',
285 'contact_id' => $this->_individualId,
286 'location_type_id' => 'Home',
9099cab3 287 ]);
59f9e5a6 288 $params['address_id'] = $address['id'];
289 $contributionPage = $this->contributionPageCreate();
290 $params['contribution_page_id'] = $contributionPage['id'];
9099cab3 291 $contributionRecur = $this->callAPISuccess('ContributionRecur', 'create', [
59f9e5a6 292 'contact_id' => $this->_individualId,
293 'frequency_interval' => 1,
294 'amount' => 5,
9099cab3 295 ]);
59f9e5a6 296 $params['contribution_recur_id'] = $contributionRecur['id'];
297
298 $params['campaign_id'] = $this->campaignCreate();
299
300 $contributionID = $this->contributionCreate($params);
9cad3ff4
CW
301
302 // update contribution with invoice number
9099cab3 303 $params = array_merge($params, [
9cad3ff4
CW
304 'id' => $contributionID,
305 'invoice_number' => CRM_Utils_Array::value('invoice_prefix', Civi::settings()->get('contribution_invoice_settings')) . "" . $contributionID,
360a0925 306 'trxn_id' => 12345,
307 'invoice_id' => 6789,
9099cab3 308 ]);
9cad3ff4
CW
309 $contributionID = $this->contributionCreate($params);
310
9099cab3 311 $contribution = $this->callAPISuccessGetSingle('Contribution', ['id' => $contributionID]);
59f9e5a6 312 $this->assertEquals('bouncer', $contribution['check_number']);
313 $this->assertEquals('bouncer', $contribution['contribution_check_number']);
314
315 $fields = CRM_Contribute_BAO_Contribution::fields();
3123273f 316 // Re-add these 2 to the fields to check. They were locked in but the metadata changed so we
317 // need to specify them.
318 $fields['address_id'] = $fields['contribution_address_id'];
319 $fields['check_number'] = $fields['contribution_check_number'];
320
9099cab3 321 $fieldsLockedIn = [
59f9e5a6 322 'contribution_id', 'contribution_contact_id', 'financial_type_id', 'contribution_page_id',
323 'payment_instrument_id', 'receive_date', 'non_deductible_amount', 'total_amount',
6e7cc0f5 324 'fee_amount', 'net_amount', 'trxn_id', 'invoice_id', 'currency', 'contribution_cancel_date', 'cancel_reason',
59f9e5a6 325 'receipt_date', 'thankyou_date', 'contribution_source', 'amount_level', 'contribution_recur_id',
326 'is_test', 'is_pay_later', 'contribution_status_id', 'address_id', 'check_number', 'contribution_campaign_id',
327 'creditnote_id', 'tax_amount', 'revenue_recognition_date', 'decoy',
9099cab3 328 ];
59f9e5a6 329 $missingFields = array_diff($fieldsLockedIn, array_keys($fields));
330 // If any of the locked in fields disappear from the $fields array we need to make sure it is still
331 // covered as the test contract now guarantees them in the return array.
9099cab3 332 $this->assertEquals($missingFields, [29 => 'decoy'], 'A field which was covered by the test contract has changed.');
59f9e5a6 333 foreach ($fields as $fieldName => $fieldSpec) {
9099cab3 334 $contribution = $this->callAPISuccessGetSingle('Contribution', ['id' => $contributionID, 'return' => $fieldName]);
59f9e5a6 335 $returnField = $fieldName;
336 if ($returnField == 'contribution_contact_id') {
337 $returnField = 'contact_id';
338 }
339 $this->assertTrue((!empty($contribution[$returnField]) || $contribution[$returnField] === "0"), $returnField);
340 }
7d96cec3
SL
341 $entityFinancialTrxn = $this->callAPISuccess('EntityFinancialTrxn', 'get', ['entity_id' => $contributionID, 'entity_table' => 'civicrm_contribution']);
342 foreach ($entityFinancialTrxn['values'] as $eft) {
343 $financialTrxn = $this->callAPISuccess('FinancialTrxn', 'get', ['id' => $eft['financial_trxn_id']]);
344 $this->assertEquals('bouncer', $financialTrxn['values'][$financialTrxn['id']]['check_number']);
345 }
59f9e5a6 346 }
347
927898c5 348 /**
349 * Test cancel reason works as a filter.
350 */
351 public function testFilterCancelReason() {
352 $params = $this->_params;
353 $params['cancel_date'] = 'yesterday';
354 $params['cancel_reason'] = 'You lose sucker';
355 $this->callAPISuccess('Contribution', 'create', $params);
356 $params = $this->_params;
357 $params['cancel_date'] = 'yesterday';
358 $params['cancel_reason'] = 'You are a winner';
359 $this->callAPISuccess('Contribution', 'create', $params);
360 $this->callAPISuccessGetCount('Contribution', ['cancel_reason' => 'You are a winner'], 1);
361 }
362
6c6e6187 363 /**
22f80e87 364 * We need to ensure previous tested behaviour still works as part of the api contract.
6c6e6187 365 */
00be9182 366 public function testGetContributionLegacyBehaviour() {
9099cab3 367 $p = [
6a488035
TO
368 'contact_id' => $this->_individualId,
369 'receive_date' => '2010-01-20',
370 'total_amount' => 100.00,
4ab7d517 371 'contribution_type_id' => $this->_financialTypeId,
6a488035
TO
372 'non_deductible_amount' => 10.00,
373 'fee_amount' => 5.00,
374 'net_amount' => 95.00,
375 'trxn_id' => 23456,
376 'invoice_id' => 78910,
377 'source' => 'SSF',
378 'contribution_status_id' => 1,
9099cab3 379 ];
71d5a412 380 $this->_contribution = $this->callAPISuccess('Contribution', 'create', $p);
6a488035 381
9099cab3 382 $params = [
6a488035 383 'contribution_id' => $this->_contribution['id'],
9099cab3 384 ];
5be22f39 385 $contribution = $this->callAPISuccess('contribution', 'get', $params);
4ab7d517 386 $financialParams['id'] = $this->_financialTypeId;
6c6e6187 387 $default = NULL;
858d0bf8 388 CRM_Financial_BAO_FinancialType::retrieve($financialParams, $default);
2f45e1c2 389
6c6e6187 390 $this->assertEquals(1, $contribution['count']);
2bfae985 391 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
4ab7d517 392 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->_financialTypeId);
393 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_type_id'], $this->_financialTypeId);
2bfae985
EM
394 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
395 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
396 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
397 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
398 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
399 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
400 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
401 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
22f80e87
EM
402
403 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
6a488035
TO
404 $p['trxn_id'] = '3847';
405 $p['invoice_id'] = '3847';
406
2f45e1c2 407 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
6a488035 408
6a488035 409 // now we have 2 - test getcount
9099cab3 410 $contribution = $this->callAPISuccess('contribution', 'getcount', []);
6a488035
TO
411 $this->assertEquals(2, $contribution);
412 //test id only format
9099cab3 413 $contribution = $this->callAPISuccess('contribution', 'get', [
4ab7d517 414 'id' => $this->_contribution['id'],
415 'format.only_id' => 1,
9099cab3 416 ]);
22f80e87 417 $this->assertEquals($this->_contribution['id'], $contribution, print_r($contribution, TRUE));
6a488035 418 //test id only format
9099cab3 419 $contribution = $this->callAPISuccess('contribution', 'get', [
4ab7d517 420 'id' => $contribution2['id'],
421 'format.only_id' => 1,
9099cab3 422 ]);
6a488035 423 $this->assertEquals($contribution2['id'], $contribution);
9099cab3 424 $contribution = $this->callAPISuccess('contribution', 'get', [
6a488035 425 'id' => $this->_contribution['id'],
9099cab3 426 ]);
6a488035 427 //test id as field
2bfae985 428 $this->assertEquals(1, $contribution['count']);
6a488035
TO
429 // $this->assertEquals($this->_contribution['id'], $contribution['id'] ) ;
430 //test get by contact id works
9099cab3 431 $contribution = $this->callAPISuccess('contribution', 'get', ['contact_id' => $this->_individualId]);
6a488035 432
2bfae985 433 $this->assertEquals(2, $contribution['count']);
9099cab3 434 $this->callAPISuccess('Contribution', 'Delete', [
5896d037 435 'id' => $this->_contribution['id'],
9099cab3
CW
436 ]);
437 $this->callAPISuccess('Contribution', 'Delete', [
5896d037 438 'id' => $contribution2['id'],
9099cab3 439 ]);
6a488035 440 }
5896d037 441
a1a2a83d
TO
442 /**
443 * Create an contribution_id=FALSE and financial_type_id=Donation.
444 */
00be9182 445 public function testCreateEmptyContributionIDUseDonation() {
9099cab3 446 $params = [
6a488035
TO
447 'contribution_id' => FALSE,
448 'contact_id' => 1,
449 'total_amount' => 1,
6c6e6187 450 'check_permissions' => FALSE,
6a488035 451 'financial_type_id' => 'Donation',
9099cab3 452 ];
858d0bf8 453 $this->callAPISuccess('contribution', 'create', $params);
6c6e6187 454 }
6a488035 455
6a488035 456 /**
1e52837d
EM
457 * Check with complete array + custom field.
458 *
6a488035
TO
459 * Note that the test is written on purpose without any
460 * variables specific to participant so it can be replicated into other entities
461 * and / or moved to the automated test suite
462 */
00be9182 463 public function testCreateWithCustom() {
6a488035
TO
464 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
465
466 $params = $this->_params;
467 $params['custom_' . $ids['custom_field_id']] = "custom string";
468
6c6e6187 469 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
6a488035 470 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
9099cab3 471 $check = $this->callAPISuccess($this->_entity, 'get', [
4ab7d517 472 'return.custom_' . $ids['custom_field_id'] => 1,
473 'id' => $result['id'],
9099cab3 474 ]);
6a488035
TO
475 $this->customFieldDelete($ids['custom_field_id']);
476 $this->customGroupDelete($ids['custom_group_id']);
22f80e87 477 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']]);
6a488035
TO
478 }
479
480 /**
fd786d03
EM
481 * Check with complete array + custom field.
482 *
6a488035
TO
483 * Note that the test is written on purpose without any
484 * variables specific to participant so it can be replicated into other entities
485 * and / or moved to the automated test suite
486 */
00be9182 487 public function testCreateGetFieldsWithCustom() {
5896d037 488 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
6a488035 489 $idsContact = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTest.php');
9099cab3 490 $result = $this->callAPISuccess('Contribution', 'getfields', []);
6a488035
TO
491 $this->assertArrayHasKey('custom_' . $ids['custom_field_id'], $result['values']);
492 $this->assertArrayNotHasKey('custom_' . $idsContact['custom_field_id'], $result['values']);
493 $this->customFieldDelete($ids['custom_field_id']);
494 $this->customGroupDelete($ids['custom_group_id']);
495 $this->customFieldDelete($idsContact['custom_field_id']);
496 $this->customGroupDelete($idsContact['custom_group_id']);
497 }
498
00be9182 499 public function testCreateContributionNoLineItems() {
6a488035 500
9099cab3 501 $params = [
6a488035
TO
502 'contact_id' => $this->_individualId,
503 'receive_date' => '20120511',
504 'total_amount' => 100.00,
5896d037 505 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
506 'payment_instrument_id' => 1,
507 'non_deductible_amount' => 10.00,
508 'fee_amount' => 50.00,
509 'net_amount' => 90.00,
510 'trxn_id' => 12345,
511 'invoice_id' => 67890,
512 'source' => 'SSF',
513 'contribution_status_id' => 1,
6a488035 514 'skipLineItem' => 1,
9099cab3 515 ];
6a488035 516
2f45e1c2 517 $contribution = $this->callAPISuccess('contribution', 'create', $params);
9099cab3 518 $lineItems = $this->callAPISuccess('line_item', 'get', [
6a488035
TO
519 'entity_id' => $contribution['id'],
520 'entity_table' => 'civicrm_contribution',
521 'sequential' => 1,
9099cab3 522 ]);
6a488035
TO
523 $this->assertEquals(0, $lineItems['count']);
524 }
5896d037 525
a1a2a83d 526 /**
eceb18cc 527 * Test checks that passing in line items suppresses the create mechanism.
6a488035 528 */
00be9182 529 public function testCreateContributionChainedLineItems() {
9099cab3 530 $params = [
6a488035
TO
531 'contact_id' => $this->_individualId,
532 'receive_date' => '20120511',
533 'total_amount' => 100.00,
4ab7d517 534 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
535 'payment_instrument_id' => 1,
536 'non_deductible_amount' => 10.00,
537 'fee_amount' => 50.00,
538 'net_amount' => 90.00,
539 'trxn_id' => 12345,
540 'invoice_id' => 67890,
541 'source' => 'SSF',
542 'contribution_status_id' => 1,
6a488035 543 'skipLineItem' => 1,
9099cab3
CW
544 'api.line_item.create' => [
545 [
6a488035
TO
546 'price_field_id' => 1,
547 'qty' => 2,
548 'line_total' => '20',
549 'unit_price' => '10',
9099cab3
CW
550 ],
551 [
6a488035
TO
552 'price_field_id' => 1,
553 'qty' => 1,
554 'line_total' => '80',
555 'unit_price' => '80',
9099cab3
CW
556 ],
557 ],
558 ];
6a488035 559
5c49fee0 560 $description = "Create Contribution with Nested Line Items.";
6a488035 561 $subfile = "CreateWithNestedLineItems";
6c6e6187 562 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
2f45e1c2 563
9099cab3 564 $lineItems = $this->callAPISuccess('line_item', 'get', [
6a488035 565 'entity_id' => $contribution['id'],
4ede4532 566 'contribution_id' => $contribution['id'],
6a488035
TO
567 'entity_table' => 'civicrm_contribution',
568 'sequential' => 1,
9099cab3 569 ]);
6a488035
TO
570 $this->assertEquals(2, $lineItems['count']);
571 }
572
00be9182 573 public function testCreateContributionOffline() {
9099cab3 574 $params = [
6a488035
TO
575 'contact_id' => $this->_individualId,
576 'receive_date' => '20120511',
577 'total_amount' => 100.00,
578 'financial_type_id' => 1,
579 'trxn_id' => 12345,
580 'invoice_id' => 67890,
581 'source' => 'SSF',
582 'contribution_status_id' => 1,
9099cab3 583 ];
6a488035 584
5be22f39 585 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2bfae985
EM
586 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
587 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
5896d037 588 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
2bfae985
EM
589 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
590 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
591 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
592 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
9099cab3 593 $lineItems = $this->callAPISuccess('line_item', 'get', [
6a488035 594 'entity_id' => $contribution['id'],
4ede4532 595 'contribution_id' => $contribution['id'],
6a488035
TO
596 'entity_table' => 'civicrm_contribution',
597 'sequential' => 1,
9099cab3 598 ]);
6a488035
TO
599 $this->assertEquals(1, $lineItems['count']);
600 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
4ede4532 601 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
6a488035
TO
602 $this->_checkFinancialRecords($contribution, 'offline');
603 $this->contributionGetnCheck($params, $contribution['id']);
604 }
5896d037 605
f70a6752 606 /**
28de42d1 607 * Test create with valid payment instrument.
6a488035 608 */
00be9182 609 public function testCreateContributionWithPaymentInstrument() {
9099cab3 610 $params = $this->_params + ['payment_instrument' => 'EFT'];
53191813 611 $contribution = $this->callAPISuccess('contribution', 'create', $params);
9099cab3 612 $contribution = $this->callAPISuccess('contribution', 'get', [
53191813 613 'sequential' => 1,
21dfd5f5 614 'id' => $contribution['id'],
9099cab3 615 ]);
6a488035 616 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
53191813
CW
617 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
618
9099cab3 619 $this->callAPISuccess('contribution', 'create', [
92915c55
TO
620 'id' => $contribution['id'],
621 'payment_instrument' => 'Credit Card',
9099cab3
CW
622 ]);
623 $contribution = $this->callAPISuccess('contribution', 'get', [
53191813 624 'sequential' => 1,
21dfd5f5 625 'id' => $contribution['id'],
9099cab3 626 ]);
53191813
CW
627 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
628 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
6a488035
TO
629 }
630
00be9182 631 public function testGetContributionByPaymentInstrument() {
9099cab3
CW
632 $params = $this->_params + ['payment_instrument' => 'EFT'];
633 $params2 = $this->_params + ['payment_instrument' => 'Cash'];
6c6e6187
TO
634 $this->callAPISuccess('contribution', 'create', $params);
635 $this->callAPISuccess('contribution', 'create', $params2);
9099cab3 636 $contribution = $this->callAPISuccess('contribution', 'get', [
92915c55 637 'sequential' => 1,
7d543448 638 'contribution_payment_instrument' => 'Cash',
9099cab3 639 ]);
6a488035 640 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
ff977830
EM
641 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
642 $this->assertEquals(1, $contribution['count']);
9099cab3 643 $contribution = $this->callAPISuccess('contribution', 'get', ['sequential' => 1, 'payment_instrument' => 'Cash']);
6a488035 644 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
868e247d 645 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
6a488035 646 $this->assertEquals(1, $contribution['count']);
9099cab3 647 $contribution = $this->callAPISuccess('contribution', 'get', [
92915c55
TO
648 'sequential' => 1,
649 'payment_instrument_id' => 5,
9099cab3 650 ]);
6a488035 651 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
6c6e6187
TO
652 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
653 $this->assertEquals(1, $contribution['count']);
9099cab3 654 $contribution = $this->callAPISuccess('contribution', 'get', [
92915c55
TO
655 'sequential' => 1,
656 'payment_instrument' => 'EFT',
9099cab3 657 ]);
6a488035 658 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
53191813
CW
659 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
660 $this->assertEquals(1, $contribution['count']);
9099cab3 661 $contribution = $this->callAPISuccess('contribution', 'create', [
92915c55
TO
662 'id' => $contribution['id'],
663 'payment_instrument' => 'Credit Card',
9099cab3
CW
664 ]);
665 $contribution = $this->callAPISuccess('contribution', 'get', ['sequential' => 1, 'id' => $contribution['id']]);
6a488035 666 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
6c6e6187
TO
667 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
668 $this->assertEquals(1, $contribution['count']);
6a488035
TO
669 }
670
b5a37491
EM
671 /**
672 * CRM-16227 introduces invoice_id as a parameter.
673 */
674 public function testGetContributionByInvoice() {
9099cab3
CW
675 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, ['invoice_id' => 'curly']));
676 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params), ['invoice_id' => 'churlish']);
677 $this->callAPISuccessGetCount('Contribution', [], 2);
678 $this->callAPISuccessGetSingle('Contribution', ['invoice_id' => 'curly']);
b5a37491
EM
679 // The following don't work. They are the format we are trying to introduce but although the form uses this format
680 // CRM_Contact_BAO_Query::convertFormValues puts them into the other format & the where only supports that.
681 // ideally the where clause would support this format (as it does on contact_BAO_Query) and those lines would
682 // come out of convertFormValues
683 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('LIKE' => '%ish%')));
684 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('NOT IN' => array('curly'))));
685 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('LIKE' => '%ly%')), 2);
686 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('IN' => array('curly', 'churlish'))),
687 // 2);
688 }
689
2b3e31ac 690 /**
691 * Check the credit note retrieval is case insensitive.
692 */
693 public function testGetCreditNoteCaseInsensitive() {
9099cab3
CW
694 $this->contributionCreate(['contact_id' => $this->_individualId]);
695 $this->contributionCreate(['creditnote_id' => 'cN1234', 'contact_id' => $this->_individualId, 'invoice_id' => rand(), 'trxn_id' => rand()]);
696 $contribution = $this->callAPISuccess('Contribution', 'getsingle', ['creditnote_id' => 'CN1234']);
2b3e31ac 697 $this->assertEquals($contribution['creditnote_id'], 'cN1234');
698 }
699
e58a3abb 700 /**
701 * Test retrieval by total_amount works.
702 *
703 * @throws Exception
704 */
705 public function testGetContributionByTotalAmount() {
9099cab3
CW
706 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, ['total_amount' => '5']));
707 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, ['total_amount' => '10']));
708 $this->callAPISuccessGetCount('Contribution', ['total_amount' => 10], 1);
709 $this->callAPISuccessGetCount('Contribution', ['total_amount' => ['>' => 6]], 1);
710 $this->callAPISuccessGetCount('Contribution', ['total_amount' => ['>' => 0]], 2);
711 $this->callAPISuccessGetCount('Contribution', ['total_amount' => ['>' => -5]], 2);
712 $this->callAPISuccessGetCount('Contribution', ['total_amount' => ['<' => 0]], 0);
713 $this->callAPISuccessGetCount('Contribution', [], 2);
e58a3abb 714 }
715
c490a46a 716 /**
eceb18cc 717 * Create test with unique field name on source.
c490a46a 718 */
00be9182 719 public function testCreateContributionSource() {
6a488035 720
9099cab3 721 $params = [
6a488035
TO
722 'contact_id' => $this->_individualId,
723 'receive_date' => date('Ymd'),
724 'total_amount' => 100.00,
4ab7d517 725 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
726 'payment_instrument_id' => 1,
727 'non_deductible_amount' => 10.00,
728 'fee_amount' => 50.00,
729 'net_amount' => 90.00,
730 'trxn_id' => 12345,
731 'invoice_id' => 67890,
732 'contribution_source' => 'SSF',
733 'contribution_status_id' => 1,
9099cab3 734 ];
6a488035 735
4ab7d517 736 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2bfae985
EM
737 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
738 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
6a488035 739 }
d5d148ab 740
d424ffde 741 /**
eceb18cc 742 * Create test with unique field name on source.
4750ab01 743 *
744 * @param string $thousandSeparator
745 * punctuation used to refer to thousands.
746 *
747 * @dataProvider getThousandSeparators
d424ffde 748 */
4750ab01 749 public function testCreateDefaultNow($thousandSeparator) {
750 $this->setCurrencySeparators($thousandSeparator);
d5d148ab 751 $params = $this->_params;
4750ab01 752 unset($params['receive_date'], $params['net_amount']);
753
754 $params['total_amount'] = $this->formatMoneyInput(5000.77);
755 $params['fee_amount'] = $this->formatMoneyInput(.77);
25ac4ffa 756 $params['skipCleanMoney'] = FALSE;
d5d148ab
EM
757
758 $contribution = $this->callAPISuccess('contribution', 'create', $params);
9099cab3 759 $contribution = $this->callAPISuccessGetSingle('contribution', ['id' => $contribution['id']]);
d5d148ab 760 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date'])));
4750ab01 761 $this->assertEquals(5000.77, $contribution['total_amount'], 'failed to handle ' . $this->formatMoneyInput(5000.77));
762 $this->assertEquals(.77, $contribution['fee_amount']);
763 $this->assertEquals(5000, $contribution['net_amount']);
d5d148ab
EM
764 }
765
325033b9 766 /**
767 * Create test with unique field name on source.
768 */
769 public function testCreateContributionNullOutThankyouDate() {
770
771 $params = $this->_params;
772 $params['thankyou_date'] = 'yesterday';
773
774 $contribution = $this->callAPISuccess('contribution', 'create', $params);
9099cab3 775 $contribution = $this->callAPISuccessGetSingle('contribution', ['id' => $contribution['id']]);
325033b9 776 $this->assertEquals(date('Y-m-d', strtotime('yesterday')), date('Y-m-d', strtotime($contribution['thankyou_date'])));
777
778 $params['thankyou_date'] = 'null';
779 $contribution = $this->callAPISuccess('contribution', 'create', $params);
780 $contribution = $this->assertTrue(empty($contribution['thankyou_date']));
781 }
782
c490a46a 783 /**
55d2c6f1 784 * Create test with unique field name on source.
c490a46a 785 */
55d2c6f1 786 public function testCreateContributionSourceInvalidContact() {
6a488035 787
9099cab3 788 $params = [
6a488035
TO
789 'contact_id' => 999,
790 'receive_date' => date('Ymd'),
791 'total_amount' => 100.00,
4ab7d517 792 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
793 'payment_instrument_id' => 1,
794 'non_deductible_amount' => 10.00,
795 'fee_amount' => 50.00,
796 'net_amount' => 90.00,
797 'trxn_id' => 12345,
798 'invoice_id' => 67890,
799 'contribution_source' => 'SSF',
800 'contribution_status_id' => 1,
9099cab3 801 ];
6a488035 802
858d0bf8 803 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
6a488035
TO
804 }
805
00be9182 806 public function testCreateContributionSourceInvalidContContact() {
6a488035 807
9099cab3 808 $params = [
6a488035
TO
809 'contribution_contact_id' => 999,
810 'receive_date' => date('Ymd'),
811 'total_amount' => 100.00,
4ab7d517 812 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
813 'payment_instrument_id' => 1,
814 'non_deductible_amount' => 10.00,
815 'fee_amount' => 50.00,
816 'net_amount' => 90.00,
817 'trxn_id' => 12345,
818 'invoice_id' => 67890,
819 'contribution_source' => 'SSF',
820 'contribution_status_id' => 1,
9099cab3 821 ];
6a488035 822
33139905 823 $this->callAPIFailure('contribution', 'create', $params);
6a488035
TO
824 }
825
858d0bf8 826 /**
442cf836 827 * Test note created correctly.
858d0bf8 828 */
00be9182 829 public function testCreateContributionWithNote() {
5c49fee0 830 $description = "Demonstrates creating contribution with Note Entity.";
5896d037 831 $subfile = "ContributionCreateWithNote";
9099cab3 832 $params = [
6a488035
TO
833 'contact_id' => $this->_individualId,
834 'receive_date' => '2012-01-01',
835 'total_amount' => 100.00,
4ab7d517 836 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
837 'payment_instrument_id' => 1,
838 'non_deductible_amount' => 10.00,
839 'fee_amount' => 50.00,
840 'net_amount' => 90.00,
841 'trxn_id' => 12345,
842 'invoice_id' => 67890,
843 'source' => 'SSF',
844 'contribution_status_id' => 1,
6a488035 845 'note' => 'my contribution note',
9099cab3 846 ];
6a488035 847
4ab7d517 848 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
9099cab3 849 $result = $this->callAPISuccess('note', 'get', [
92915c55
TO
850 'entity_table' => 'civicrm_contribution',
851 'entity_id' => $contribution['id'],
852 'sequential' => 1,
9099cab3 853 ]);
6a488035 854 $this->assertEquals('my contribution note', $result['values'][0]['note']);
9099cab3 855 $this->callAPISuccess('contribution', 'delete', ['id' => $contribution['id']]);
6a488035
TO
856 }
857
00be9182 858 public function testCreateContributionWithNoteUniqueNameAliases() {
9099cab3 859 $params = [
6a488035
TO
860 'contact_id' => $this->_individualId,
861 'receive_date' => '2012-01-01',
862 'total_amount' => 100.00,
4ab7d517 863 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
864 'payment_instrument_id' => 1,
865 'non_deductible_amount' => 10.00,
866 'fee_amount' => 50.00,
867 'net_amount' => 90.00,
868 'trxn_id' => 12345,
869 'invoice_id' => 67890,
870 'source' => 'SSF',
871 'contribution_status_id' => 1,
6a488035 872 'contribution_note' => 'my contribution note',
9099cab3 873 ];
6a488035 874
4ab7d517 875 $contribution = $this->callAPISuccess('contribution', 'create', $params);
9099cab3 876 $result = $this->callAPISuccess('note', 'get', [
92915c55
TO
877 'entity_table' => 'civicrm_contribution',
878 'entity_id' => $contribution['id'],
879 'sequential' => 1,
9099cab3 880 ]);
6a488035 881 $this->assertEquals('my contribution note', $result['values'][0]['note']);
9099cab3 882 $this->callAPISuccess('contribution', 'delete', ['id' => $contribution['id']]);
6a488035 883 }
c490a46a
CW
884
885 /**
55d2c6f1 886 * This is the test for creating soft credits.
c490a46a 887 */
55d2c6f1 888 public function testCreateContributionWithSoftCredit() {
5c49fee0 889 $description = "Demonstrates creating contribution with SoftCredit.";
5896d037 890 $subfile = "ContributionCreateWithSoftCredit";
9099cab3 891 $contact2 = $this->callAPISuccess('Contact', 'create', [
92915c55
TO
892 'display_name' => 'superman',
893 'contact_type' => 'Individual',
9099cab3
CW
894 ]);
895 $softParams = [
bcc03b98 896 'contact_id' => $contact2['id'],
897 'amount' => 50,
21dfd5f5 898 'soft_credit_type_id' => 3,
9099cab3 899 ];
6a488035 900
9099cab3 901 $params = $this->_params + ['soft_credit' => [1 => $softParams]];
4ab7d517 902 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
9099cab3 903 $result = $this->callAPISuccess('contribution', 'get', ['return' => 'soft_credit', 'sequential' => 1]);
a1c68fd2 904
55d2c6f1
EM
905 $this->assertEquals($softParams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
906 $this->assertEquals($softParams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
907 $this->assertEquals($softParams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']);
6a516bd6 908
9099cab3
CW
909 $this->callAPISuccess('contribution', 'delete', ['id' => $contribution['id']]);
910 $this->callAPISuccess('contact', 'delete', ['id' => $contact2['id']]);
6a516bd6
DG
911 }
912
00be9182 913 public function testCreateContributionWithSoftCreditDefaults() {
5c49fee0 914 $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type.";
5896d037 915 $subfile = "ContributionCreateWithSoftCreditDefaults";
9099cab3 916 $contact2 = $this->callAPISuccess('Contact', 'create', [
92915c55
TO
917 'display_name' => 'superman',
918 'contact_type' => 'Individual',
9099cab3
CW
919 ]);
920 $params = $this->_params + [
442cf836 921 'soft_credit_to' => $contact2['id'],
9099cab3 922 ];
6a516bd6 923 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
9099cab3 924 $result = $this->callAPISuccess('contribution', 'get', ['return' => 'soft_credit', 'sequential' => 1]);
6a516bd6
DG
925
926 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
927 // Default soft credit amount = contribution.total_amount
928 $this->assertEquals($this->_params['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
929 $this->assertEquals(CRM_Core_OptionGroup::getDefaultValue("soft_credit_type"), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
930
9099cab3
CW
931 $this->callAPISuccess('contribution', 'delete', ['id' => $contribution['id']]);
932 $this->callAPISuccess('contact', 'delete', ['id' => $contact2['id']]);
6a516bd6
DG
933 }
934
6c266de6 935 /**
936 * Test creating contribution with Soft Credit by passing in honor_contact_id.
937 */
00be9182 938 public function testCreateContributionWithHonoreeContact() {
5c49fee0 939 $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id.";
5896d037 940 $subfile = "ContributionCreateWithHonoreeContact";
9099cab3 941 $contact2 = $this->callAPISuccess('Contact', 'create', [
92915c55
TO
942 'display_name' => 'superman',
943 'contact_type' => 'Individual',
9099cab3
CW
944 ]);
945 $params = $this->_params + [
442cf836 946 'honor_contact_id' => $contact2['id'],
9099cab3 947 ];
6a516bd6 948 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
9099cab3 949 $result = $this->callAPISuccess('contribution', 'get', ['return' => 'soft_credit', 'sequential' => 1]);
6a516bd6
DG
950
951 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
952 // Default soft credit amount = contribution.total_amount
953 // Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type
954 $this->assertEquals($this->_params['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
6c266de6 955 $softCreditValueTypeID = $result['values'][0]['soft_credit'][1]['soft_credit_type'];
956 $this->assertEquals('in_honor_of', CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', $softCreditValueTypeID));
6a488035 957
9099cab3
CW
958 $this->callAPISuccess('contribution', 'delete', ['id' => $contribution['id']]);
959 $this->callAPISuccess('contact', 'delete', ['id' => $contact2['id']]);
6a488035
TO
960 }
961
962 /**
92c99a4a 963 * Test using example code.
6a488035 964 */
00be9182 965 public function testContributionCreateExample() {
6a488035 966 //make sure at least on page exists since there is a truncate in tear down
8be629ac 967 $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
be44cfcb 968 require_once 'api/v3/examples/Contribution/Create.ex.php';
5896d037 969 $result = contribution_create_example();
006d6361 970 $id = $result['id'];
6a488035 971 $expectedResult = contribution_create_expectedresult();
8e342a79 972 $this->checkArrayEquals($expectedResult, $result);
006d6361 973 $this->contributionDelete($id);
6a488035
TO
974 }
975
a1a2a83d 976 /**
f55c5fa8 977 * Function tests that additional financial records are created when fee amount is recorded.
6a488035 978 */
00be9182 979 public function testCreateContributionWithFee() {
9099cab3 980 $params = [
6a488035
TO
981 'contact_id' => $this->_individualId,
982 'receive_date' => '20120511',
983 'total_amount' => 100.00,
984 'fee_amount' => 50,
985 'financial_type_id' => 1,
986 'trxn_id' => 12345,
987 'invoice_id' => 67890,
988 'source' => 'SSF',
989 'contribution_status_id' => 1,
9099cab3 990 ];
6a488035 991
5be22f39 992 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2bfae985
EM
993 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
994 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
e0e3c51b
EM
995 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 50.00);
996 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 50.00);
5896d037 997 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
2bfae985
EM
998 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
999 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
1000 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
1001 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
e0e3c51b 1002
9099cab3 1003 $lineItems = $this->callAPISuccess('line_item', 'get', [
4ab7d517 1004
6a488035
TO
1005 'entity_id' => $contribution['id'],
1006 'entity_table' => 'civicrm_contribution',
1007 'sequential' => 1,
9099cab3 1008 ]);
6a488035
TO
1009 $this->assertEquals(1, $lineItems['count']);
1010 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
4ede4532 1011 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
9099cab3 1012 $lineItems = $this->callAPISuccess('line_item', 'get', [
4ab7d517 1013
5896d037
TO
1014 'entity_id' => $contribution['id'],
1015 'contribution_id' => $contribution['id'],
1016 'entity_table' => 'civicrm_contribution',
1017 'sequential' => 1,
9099cab3 1018 ]);
6a488035
TO
1019 $this->assertEquals(1, $lineItems['count']);
1020 $this->_checkFinancialRecords($contribution, 'feeAmount');
1021 }
1022
f70a6752 1023 /**
442cf836 1024 * Function tests that additional financial records are created when online contribution is created.
6a488035 1025 */
00be9182 1026 public function testCreateContributionOnline() {
858d0bf8 1027 CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
5896d037 1028 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
fc928539 1029 $this->assertAPISuccess($contributionPage);
9099cab3 1030 $params = [
6a488035
TO
1031 'contact_id' => $this->_individualId,
1032 'receive_date' => '20120511',
1033 'total_amount' => 100.00,
1034 'financial_type_id' => 1,
1035 'contribution_page_id' => $contributionPage['id'],
16f3bd02 1036 'payment_processor' => $this->paymentProcessorID,
6a488035
TO
1037 'trxn_id' => 12345,
1038 'invoice_id' => 67890,
1039 'source' => 'SSF',
1040 'contribution_status_id' => 1,
4ab7d517 1041
9099cab3 1042 ];
6a488035 1043
5be22f39 1044 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2bfae985
EM
1045 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
1046 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
5896d037 1047 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
2bfae985
EM
1048 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
1049 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
1050 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
1051 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
9099cab3 1052 $contribution['payment_instrument_id'] = $this->callAPISuccessGetValue('PaymentProcessor', [
16f3bd02 1053 'id' => $this->paymentProcessorID,
1054 'return' => 'payment_instrument_id',
9099cab3 1055 ]);
6a488035
TO
1056 $this->_checkFinancialRecords($contribution, 'online');
1057 }
1058
f70a6752 1059 /**
442cf836
EM
1060 * Check handling of financial type.
1061 *
100fef9d 1062 * In the interests of removing financial type / contribution type checks from
f70a6752 1063 * legacy format function lets test that the api is doing this for us
1064 */
00be9182 1065 public function testCreateInvalidFinancialType() {
f70a6752 1066 $params = $this->_params;
1067 $params['financial_type_id'] = 99999;
858d0bf8 1068 $this->callAPIFailure($this->_entity, 'create', $params, "'99999' is not a valid option for field financial_type_id");
f70a6752 1069 }
1070
4302618d 1071 /**
442cf836
EM
1072 * Check handling of financial type.
1073 *
100fef9d 1074 * In the interests of removing financial type / contribution type checks from
4302618d 1075 * legacy format function lets test that the api is doing this for us
1076 */
00be9182 1077 public function testValidNamedFinancialType() {
4302618d 1078 $params = $this->_params;
1079 $params['financial_type_id'] = 'Donation';
858d0bf8 1080 $this->callAPISuccess($this->_entity, 'create', $params);
4302618d 1081 }
1082
f70a6752 1083 /**
92c99a4a
EM
1084 * Tests that additional financial records are created.
1085 *
1086 * Checks when online contribution with pay later option is created
6a488035 1087 */
00be9182 1088 public function testCreateContributionPayLaterOnline() {
858d0bf8 1089 CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
6a488035 1090 $this->_pageParams['is_pay_later'] = 1;
5896d037 1091 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
fc928539 1092 $this->assertAPISuccess($contributionPage);
9099cab3 1093 $params = [
6a488035
TO
1094 'contact_id' => $this->_individualId,
1095 'receive_date' => '20120511',
1096 'total_amount' => 100.00,
1097 'financial_type_id' => 1,
1098 'contribution_page_id' => $contributionPage['id'],
1099 'trxn_id' => 12345,
1100 'is_pay_later' => 1,
1101 'invoice_id' => 67890,
1102 'source' => 'SSF',
1103 'contribution_status_id' => 2,
4ab7d517 1104
9099cab3 1105 ];
6a488035 1106
4ab7d517 1107 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
2bfae985
EM
1108 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
1109 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
5896d037 1110 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
2bfae985
EM
1111 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
1112 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
1113 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
1114 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
6a488035
TO
1115 $this->_checkFinancialRecords($contribution, 'payLater');
1116 }
1117
a1a2a83d 1118 /**
1e52837d 1119 * Function tests that additional financial records are created for online contribution with pending option.
6a488035 1120 */
00be9182 1121 public function testCreateContributionPendingOnline() {
8a40179e 1122 CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
5896d037 1123 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
fc928539 1124 $this->assertAPISuccess($contributionPage);
9099cab3 1125 $params = [
6a488035
TO
1126 'contact_id' => $this->_individualId,
1127 'receive_date' => '20120511',
1128 'total_amount' => 100.00,
1129 'financial_type_id' => 1,
1130 'contribution_page_id' => $contributionPage['id'],
1131 'trxn_id' => 12345,
1132 'invoice_id' => 67890,
1133 'source' => 'SSF',
1134 'contribution_status_id' => 2,
9099cab3 1135 ];
6a488035 1136
5be22f39 1137 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2bfae985
EM
1138 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
1139 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
5896d037 1140 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
2bfae985
EM
1141 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
1142 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
1143 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
1144 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
6a488035
TO
1145 $this->_checkFinancialRecords($contribution, 'pending');
1146 }
1147
e748bf60 1148 /**
92c99a4a 1149 * Test that BAO defaults work.
e748bf60 1150 */
00be9182 1151 public function testCreateBAODefaults() {
e748bf60
EM
1152 unset($this->_params['contribution_source_id'], $this->_params['payment_instrument_id']);
1153 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
9099cab3 1154 $contribution = $this->callAPISuccess('contribution', 'getsingle', [
92915c55
TO
1155 'id' => $contribution['id'],
1156 'api.contribution.delete' => 1,
9099cab3 1157 ]);
e748bf60 1158 $this->assertEquals(1, $contribution['contribution_status_id']);
12879069 1159 $this->assertEquals('Check', $contribution['payment_instrument']);
8744e9ce 1160 $this->callAPISuccessGetCount('Contribution', ['id' => $contribution['id']], 0);
1161 }
1162
1163 /**
1164 * Test that getsingle can be chained with delete.
1165 */
1166 public function testDeleteChainedGetSingle() {
1167 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
9099cab3 1168 $contribution = $this->callAPISuccess('contribution', 'getsingle', [
8744e9ce 1169 'id' => $contribution['id'],
1170 'api.contribution.delete' => 1,
9099cab3 1171 ]);
8744e9ce 1172 $this->callAPISuccessGetCount('Contribution', ['id' => $contribution['id']], 0);
e748bf60
EM
1173 }
1174
a1a2a83d 1175 /**
1e52837d 1176 * Function tests that line items, financial records are updated when contribution amount is changed.
6a488035 1177 */
00be9182 1178 public function testCreateUpdateContributionChangeTotal() {
4ab7d517 1179 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
9099cab3 1180 $lineItems = $this->callAPISuccess('line_item', 'getvalue', [
4ab7d517 1181
6a488035
TO
1182 'entity_id' => $contribution['id'],
1183 'entity_table' => 'civicrm_contribution',
1184 'sequential' => 1,
1185 'return' => 'line_total',
9099cab3 1186 ]);
6a488035
TO
1187 $this->assertEquals('100.00', $lineItems);
1188 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
1189 // Financial trxn SUM = 100 + 5 (fee)
1190 $this->assertEquals('105.00', $trxnAmount);
9099cab3 1191 $newParams = [
4ab7d517 1192
6a488035 1193 'id' => $contribution['id'],
21dfd5f5 1194 'total_amount' => '125',
9099cab3 1195 ];
694769da 1196 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
4ab7d517 1197
9099cab3 1198 $lineItems = $this->callAPISuccess('line_item', 'getvalue', [
6a488035 1199
5896d037
TO
1200 'entity_id' => $contribution['id'],
1201 'entity_table' => 'civicrm_contribution',
1202 'sequential' => 1,
1203 'return' => 'line_total',
9099cab3 1204 ]);
6a488035
TO
1205
1206 $this->assertEquals('125.00', $lineItems);
1207 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
28de42d1
EM
1208
1209 // Financial trxn SUM = 125 + 5 (fee).
6a488035 1210 $this->assertEquals('130.00', $trxnAmount);
28de42d1 1211 $this->assertEquals('125.00', $this->_getFinancialItemAmount($contribution['id']));
6a488035
TO
1212 }
1213
a1a2a83d 1214 /**
1e52837d 1215 * Function tests that line items, financial records are updated when pay later contribution is received.
6a488035 1216 */
00be9182 1217 public function testCreateUpdateContributionPayLater() {
9099cab3 1218 $contribParams = [
6a488035
TO
1219 'contact_id' => $this->_individualId,
1220 'receive_date' => '2012-01-01',
1221 'total_amount' => 100.00,
4ab7d517 1222 'financial_type_id' => $this->_financialTypeId,
6a488035 1223 'payment_instrument_id' => 1,
8f39a111 1224 'contribution_status_id' => 2,
1225 'is_pay_later' => 1,
4ab7d517 1226
9099cab3 1227 ];
4ab7d517 1228 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
6a488035 1229
9099cab3 1230 $newParams = array_merge($contribParams, [
39b959db
SL
1231 'id' => $contribution['id'],
1232 'contribution_status_id' => 1,
9099cab3 1233 ]);
694769da 1234 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
6a488035 1235 $contribution = $contribution['values'][$contribution['id']];
6c6e6187 1236 $this->assertEquals($contribution['contribution_status_id'], '1');
6a488035
TO
1237 $this->_checkFinancialItem($contribution['id'], 'paylater');
1238 $this->_checkFinancialTrxn($contribution, 'payLater');
1239 }
1240
a1a2a83d 1241 /**
eceb18cc 1242 * Function tests that financial records are updated when Payment Instrument is changed.
6a488035 1243 */
00be9182 1244 public function testCreateUpdateContributionPaymentInstrument() {
6a488035 1245 $instrumentId = $this->_addPaymentInstrument();
9099cab3 1246 $contribParams = [
6a488035
TO
1247 'contact_id' => $this->_individualId,
1248 'total_amount' => 100.00,
4ab7d517 1249 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1250 'payment_instrument_id' => 4,
1251 'contribution_status_id' => 1,
4ab7d517 1252
9099cab3 1253 ];
4ab7d517 1254 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
6a488035 1255
9099cab3 1256 $newParams = array_merge($contribParams, [
39b959db
SL
1257 'id' => $contribution['id'],
1258 'payment_instrument_id' => $instrumentId,
9099cab3 1259 ]);
694769da 1260 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
fc928539 1261 $this->assertAPISuccess($contribution);
b0e806fa 1262 $this->checkFinancialTrxnPaymentInstrumentChange($contribution['id'], 4, $instrumentId);
02a9c0a4 1263
1264 // cleanup - delete created payment instrument
1265 $this->_deletedAddedPaymentInstrument();
6a488035
TO
1266 }
1267
122250ec
SL
1268 /**
1269 * Function tests that financial records are updated when Payment Instrument is changed when amount is negative.
1270 */
1271 public function testCreateUpdateNegativeContributionPaymentInstrument() {
1272 $instrumentId = $this->_addPaymentInstrument();
9099cab3 1273 $contribParams = [
122250ec
SL
1274 'contact_id' => $this->_individualId,
1275 'total_amount' => -100.00,
1276 'financial_type_id' => $this->_financialTypeId,
1277 'payment_instrument_id' => 4,
1278 'contribution_status_id' => 1,
1279
9099cab3 1280 ];
122250ec
SL
1281 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1282
9099cab3 1283 $newParams = array_merge($contribParams, [
39b959db
SL
1284 'id' => $contribution['id'],
1285 'payment_instrument_id' => $instrumentId,
9099cab3 1286 ]);
122250ec
SL
1287 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1288 $this->assertAPISuccess($contribution);
b0e806fa 1289 $this->checkFinancialTrxnPaymentInstrumentChange($contribution['id'], 4, $instrumentId, -100);
02a9c0a4 1290
1291 // cleanup - delete created payment instrument
1292 $this->_deletedAddedPaymentInstrument();
122250ec
SL
1293 }
1294
a1a2a83d 1295 /**
eceb18cc 1296 * Function tests that financial records are added when Contribution is Refunded.
05a42c4a 1297 *
1298 * @throws \CRM_Core_Exception
6a488035 1299 */
00be9182 1300 public function testCreateUpdateContributionRefund() {
9099cab3 1301 $contributionParams = [
6a488035
TO
1302 'contact_id' => $this->_individualId,
1303 'receive_date' => '2012-01-01',
1304 'total_amount' => 100.00,
4ab7d517 1305 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1306 'payment_instrument_id' => 4,
1307 'contribution_status_id' => 1,
797d4c52 1308 'trxn_id' => 'original_payment',
9099cab3 1309 ];
797d4c52 1310 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
9099cab3 1311 $newParams = array_merge($contributionParams, [
39b959db
SL
1312 'id' => $contribution['id'],
1313 'contribution_status_id' => 'Refunded',
1314 'cancel_date' => '2015-01-01 09:00',
1315 'refund_trxn_id' => 'the refund',
9099cab3 1316 ]);
797d4c52 1317
1318 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1319 $this->_checkFinancialTrxn($contribution, 'refund');
1320 $this->_checkFinancialItem($contribution['id'], 'refund');
9099cab3 1321 $this->assertEquals('original_payment', $this->callAPISuccessGetValue('Contribution', [
797d4c52 1322 'id' => $contribution['id'],
1323 'return' => 'trxn_id',
9099cab3 1324 ]));
797d4c52 1325 }
4ab7d517 1326
52da5b1e 1327 /**
1328 * Refund a contribution for a financial type with a contra account.
1329 *
1330 * CRM-17951 the contra account is a financial account with a relationship to a
1331 * financial type. It is not always configured but should be reflected
1332 * in the financial_trxn & financial_item table if it is.
f0d18278 1333 *
1334 * @throws \CRM_Core_Exception
52da5b1e 1335 */
1336 public function testCreateUpdateChargebackContributionDefaultAccount() {
1337 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
9099cab3 1338 $this->callAPISuccess('Contribution', 'create', [
52da5b1e 1339 'id' => $contribution['id'],
1340 'contribution_status_id' => 'Chargeback',
9099cab3
CW
1341 ]);
1342 $this->callAPISuccessGetSingle('Contribution', ['contribution_status_id' => 'Chargeback']);
52da5b1e 1343
9099cab3 1344 $lineItems = $this->callAPISuccessGetSingle('LineItem', [
52da5b1e 1345 'contribution_id' => $contribution['id'],
9099cab3
CW
1346 'api.FinancialItem.getsingle' => ['amount' => ['<' => 0]],
1347 ]);
52da5b1e 1348 $this->assertEquals(1, $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
9099cab3 1349 $this->callAPISuccessGetSingle('FinancialTrxn', [
52da5b1e 1350 'total_amount' => -100,
1351 'status_id' => 'Chargeback',
1352 'to_financial_account_id' => 6,
9099cab3 1353 ]);
52da5b1e 1354 }
1355
1356 /**
1357 * Refund a contribution for a financial type with a contra account.
1358 *
1359 * CRM-17951 the contra account is a financial account with a relationship to a
1360 * financial type. It is not always configured but should be reflected
1361 * in the financial_trxn & financial_item table if it is.
05a42c4a 1362 *
1363 * @throws \CRM_Core_Exception
52da5b1e 1364 */
1365 public function testCreateUpdateChargebackContributionCustomAccount() {
9099cab3 1366 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', [
52da5b1e 1367 'name' => 'Chargeback Account',
1368 'is_active' => TRUE,
9099cab3 1369 ]);
52da5b1e 1370
9099cab3 1371 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', [
52da5b1e 1372 'entity_id' => $this->_financialTypeId,
1373 'entity_table' => 'civicrm_financial_type',
1374 'account_relationship' => 'Chargeback Account is',
1375 'financial_account_id' => 'Chargeback Account',
9099cab3 1376 ]);
52da5b1e 1377
1378 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
9099cab3 1379 $this->callAPISuccess('Contribution', 'create', [
52da5b1e 1380 'id' => $contribution['id'],
1381 'contribution_status_id' => 'Chargeback',
9099cab3
CW
1382 ]);
1383 $this->callAPISuccessGetSingle('Contribution', ['contribution_status_id' => 'Chargeback']);
52da5b1e 1384
9099cab3 1385 $lineItems = $this->callAPISuccessGetSingle('LineItem', [
52da5b1e 1386 'contribution_id' => $contribution['id'],
9099cab3
CW
1387 'api.FinancialItem.getsingle' => ['amount' => ['<' => 0]],
1388 ]);
52da5b1e 1389 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1390
9099cab3
CW
1391 $this->callAPISuccess('Contribution', 'delete', ['id' => $contribution['id']]);
1392 $this->callAPISuccess('EntityFinancialAccount', 'delete', ['id' => $entityFinancialAccount['id']]);
1393 $this->callAPISuccess('FinancialAccount', 'delete', ['id' => $financialAccount['id']]);
52da5b1e 1394 }
1395
bf2cf926 1396 /**
1397 * Refund a contribution for a financial type with a contra account.
1398 *
1399 * CRM-17951 the contra account is a financial account with a relationship to a
1400 * financial type. It is not always configured but should be reflected
1401 * in the financial_trxn & financial_item table if it is.
1402 */
1403 public function testCreateUpdateRefundContributionConfiguredContraAccount() {
9099cab3 1404 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', [
bf2cf926 1405 'name' => 'Refund Account',
1406 'is_active' => TRUE,
9099cab3 1407 ]);
bf2cf926 1408
9099cab3 1409 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', [
bf2cf926 1410 'entity_id' => $this->_financialTypeId,
1411 'entity_table' => 'civicrm_financial_type',
1412 'account_relationship' => 'Credit/Contra Revenue Account is',
1413 'financial_account_id' => 'Refund Account',
9099cab3 1414 ]);
bf2cf926 1415
1416 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
9099cab3 1417 $this->callAPISuccess('Contribution', 'create', [
bf2cf926 1418 'id' => $contribution['id'],
1419 'contribution_status_id' => 'Refunded',
9099cab3 1420 ]);
bf2cf926 1421
9099cab3 1422 $lineItems = $this->callAPISuccessGetSingle('LineItem', [
bf2cf926 1423 'contribution_id' => $contribution['id'],
9099cab3
CW
1424 'api.FinancialItem.getsingle' => ['amount' => ['<' => 0]],
1425 ]);
bf2cf926 1426 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1427
9099cab3
CW
1428 $this->callAPISuccess('Contribution', 'delete', ['id' => $contribution['id']]);
1429 $this->callAPISuccess('EntityFinancialAccount', 'delete', ['id' => $entityFinancialAccount['id']]);
1430 $this->callAPISuccess('FinancialAccount', 'delete', ['id' => $financialAccount['id']]);
bf2cf926 1431 }
1432
797d4c52 1433 /**
1434 * Function tests that trxn_id is set when passed in.
1435 *
1436 * Here we ensure that the civicrm_financial_trxn.trxn_id & the civicrm_contribution.trxn_id are set
1437 * when trxn_id is passed in.
1438 */
1439 public function testCreateUpdateContributionRefundTrxnIDPassedIn() {
9099cab3 1440 $contributionParams = [
797d4c52 1441 'contact_id' => $this->_individualId,
1442 'receive_date' => '2012-01-01',
1443 'total_amount' => 100.00,
1444 'financial_type_id' => $this->_financialTypeId,
1445 'payment_instrument_id' => 4,
1446 'contribution_status_id' => 1,
1447 'trxn_id' => 'original_payment',
9099cab3 1448 ];
797d4c52 1449 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
9099cab3 1450 $newParams = array_merge($contributionParams, [
39b959db
SL
1451 'id' => $contribution['id'],
1452 'contribution_status_id' => 'Refunded',
1453 'cancel_date' => '2015-01-01 09:00',
1454 'trxn_id' => 'the refund',
9099cab3 1455 ]);
797d4c52 1456
1457 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1458 $this->_checkFinancialTrxn($contribution, 'refund');
1459 $this->_checkFinancialItem($contribution['id'], 'refund');
9099cab3 1460 $this->assertEquals('the refund', $this->callAPISuccessGetValue('Contribution', [
797d4c52 1461 'id' => $contribution['id'],
1462 'return' => 'trxn_id',
9099cab3 1463 ]));
797d4c52 1464 }
1465
1466 /**
1467 * Function tests that trxn_id is set when passed in.
1468 *
1469 * Here we ensure that the civicrm_contribution.trxn_id is set
1470 * when trxn_id is passed in but if refund_trxn_id is different then that
1471 * is kept for the refund transaction.
1472 */
1473 public function testCreateUpdateContributionRefundRefundAndTrxnIDPassedIn() {
9099cab3 1474 $contributionParams = [
797d4c52 1475 'contact_id' => $this->_individualId,
1476 'receive_date' => '2012-01-01',
1477 'total_amount' => 100.00,
1478 'financial_type_id' => $this->_financialTypeId,
1479 'payment_instrument_id' => 4,
1480 'contribution_status_id' => 1,
1481 'trxn_id' => 'original_payment',
9099cab3 1482 ];
797d4c52 1483 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
9099cab3 1484 $newParams = array_merge($contributionParams, [
39b959db
SL
1485 'id' => $contribution['id'],
1486 'contribution_status_id' => 'Refunded',
1487 'cancel_date' => '2015-01-01 09:00',
1488 'trxn_id' => 'cont id',
1489 'refund_trxn_id' => 'the refund',
9099cab3 1490 ]);
6a488035 1491
694769da 1492 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
6a488035
TO
1493 $this->_checkFinancialTrxn($contribution, 'refund');
1494 $this->_checkFinancialItem($contribution['id'], 'refund');
9099cab3 1495 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', [
797d4c52 1496 'id' => $contribution['id'],
1497 'return' => 'trxn_id',
9099cab3 1498 ]));
797d4c52 1499 }
1500
1501 /**
1502 * Function tests that refund_trxn_id is set when passed in empty.
1503 *
1504 * Here we ensure that the civicrm_contribution.trxn_id is set
1505 * when trxn_id is passed in but if refund_trxn_id isset but empty then that
1506 * is kept for the refund transaction.
1507 */
1508 public function testCreateUpdateContributionRefundRefundNullTrxnIDPassedIn() {
9099cab3 1509 $contributionParams = [
797d4c52 1510 'contact_id' => $this->_individualId,
1511 'receive_date' => '2012-01-01',
1512 'total_amount' => 100.00,
1513 'financial_type_id' => $this->_financialTypeId,
1514 'payment_instrument_id' => 4,
1515 'contribution_status_id' => 1,
1516 'trxn_id' => 'original_payment',
9099cab3 1517 ];
797d4c52 1518 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
9099cab3 1519 $newParams = array_merge($contributionParams, [
39b959db
SL
1520 'id' => $contribution['id'],
1521 'contribution_status_id' => 'Refunded',
1522 'cancel_date' => '2015-01-01 09:00',
1523 'trxn_id' => 'cont id',
1524 'refund_trxn_id' => '',
9099cab3 1525 ]);
797d4c52 1526
1527 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
9099cab3 1528 $this->_checkFinancialTrxn($contribution, 'refund', NULL, ['trxn_id' => NULL]);
797d4c52 1529 $this->_checkFinancialItem($contribution['id'], 'refund');
9099cab3 1530 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', [
797d4c52 1531 'id' => $contribution['id'],
1532 'return' => 'trxn_id',
9099cab3 1533 ]));
8f39a111 1534 }
c71ae314 1535
a1a2a83d 1536 /**
eceb18cc 1537 * Function tests invalid contribution status change.
c71ae314 1538 */
00be9182 1539 public function testCreateUpdateContributionInValidStatusChange() {
9099cab3 1540 $contribParams = [
c71ae314
PN
1541 'contact_id' => 1,
1542 'receive_date' => '2012-01-01',
1543 'total_amount' => 100.00,
1544 'financial_type_id' => 1,
1545 'payment_instrument_id' => 1,
1546 'contribution_status_id' => 1,
9099cab3 1547 ];
4ab7d517 1548 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
9099cab3 1549 $newParams = array_merge($contribParams, [
39b959db
SL
1550 'id' => $contribution['id'],
1551 'contribution_status_id' => 2,
9099cab3 1552 ]);
6c6e6187 1553 $this->callAPIFailure('contribution', 'create', $newParams, ts('Cannot change contribution status from Completed to Pending.'));
c71ae314 1554
6a488035
TO
1555 }
1556
a1a2a83d 1557 /**
eceb18cc 1558 * Function tests that financial records are added when Pending Contribution is Canceled.
6a488035 1559 */
00be9182 1560 public function testCreateUpdateContributionCancelPending() {
f0d18278 1561 // Enable & disable invoicing just to standardise the credit note id setting.
1562 // Longer term we want to separate that setting from 'taxAndInvoicing'.
1563 // and / or remove from core.
1564 $this->enableTaxAndInvoicing();
9099cab3 1565 $contribParams = [
6a488035
TO
1566 'contact_id' => $this->_individualId,
1567 'receive_date' => '2012-01-01',
1568 'total_amount' => 100.00,
4ab7d517 1569 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1570 'payment_instrument_id' => 1,
1571 'contribution_status_id' => 2,
c71ae314 1572 'is_pay_later' => 1,
4ab7d517 1573
9099cab3 1574 ];
4ab7d517 1575 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
9099cab3 1576 $newParams = array_merge($contribParams, [
39b959db
SL
1577 'id' => $contribution['id'],
1578 'contribution_status_id' => 3,
1579 'cancel_date' => '2012-02-02 09:00',
9099cab3 1580 ]);
0a8160e8 1581 //Check if trxn_date is same as cancel_date.
9099cab3 1582 $checkTrxnDate = [
0a8160e8 1583 'trxn_date' => '2012-02-02 09:00:00',
9099cab3 1584 ];
694769da 1585 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
0a8160e8 1586 $this->_checkFinancialTrxn($contribution, 'cancelPending', NULL, $checkTrxnDate);
6a488035 1587 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
f0d18278 1588 $this->assertEquals('CN_1', $contribution['values'][$contribution['id']]['creditnote_id']);
1589 $this->disableTaxAndInvoicing();
6a488035
TO
1590 }
1591
a1a2a83d 1592 /**
eceb18cc 1593 * Function tests that financial records are added when Financial Type is Changed.
592a64a0 1594 *
1595 * @throws \CRM_Core_Exception
6a488035 1596 */
00be9182 1597 public function testCreateUpdateContributionChangeFinancialType() {
9099cab3 1598 $contribParams = [
6a488035
TO
1599 'contact_id' => $this->_individualId,
1600 'receive_date' => '2012-01-01',
1601 'total_amount' => 100.00,
1602 'financial_type_id' => 1,
1603 'payment_instrument_id' => 1,
1604 'contribution_status_id' => 1,
4ab7d517 1605
9099cab3 1606 ];
4ab7d517 1607 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
9099cab3 1608 $newParams = array_merge($contribParams, [
39b959db
SL
1609 'id' => $contribution['id'],
1610 'financial_type_id' => 3,
9099cab3 1611 ]);
694769da 1612 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
6a488035
TO
1613 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1614 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1615 }
1616
694769da 1617 /**
1e52837d 1618 * Test that update does not change status id CRM-15105.
694769da 1619 */
00be9182 1620 public function testCreateUpdateWithoutChangingPendingStatus() {
9099cab3
CW
1621 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params, ['contribution_status_id' => 2]));
1622 $this->callAPISuccess('contribution', 'create', ['id' => $contribution['id'], 'source' => 'new source']);
1623 $contribution = $this->callAPISuccess('contribution', 'getsingle', [
92915c55
TO
1624 'id' => $contribution['id'],
1625 'api.contribution.delete' => 1,
9099cab3 1626 ]);
694769da
VU
1627 $this->assertEquals(2, $contribution['contribution_status_id']);
1628 }
a1a2a83d
TO
1629
1630 /**
28de42d1
EM
1631 * Test Updating a Contribution.
1632 *
a1a2a83d
TO
1633 * CHANGE: we require the API to do an incremental update
1634 */
00be9182 1635 public function testCreateUpdateContribution() {
9099cab3 1636 $contributionID = $this->contributionCreate([
78ab0ca4 1637 'contact_id' => $this->_individualId,
1638 'trxn_id' => 212355,
1639 'financial_type_id' => $this->_financialTypeId,
1640 'invoice_id' => 'old_invoice',
9099cab3
CW
1641 ]);
1642 $old_params = [
6a488035 1643 'contribution_id' => $contributionID,
9099cab3 1644 ];
4ab7d517 1645 $original = $this->callAPISuccess('contribution', 'get', $old_params);
2bfae985 1646 $this->assertEquals($original['id'], $contributionID);
6a488035
TO
1647 //set up list of old params, verify
1648
1649 //This should not be required on update:
1650 $old_contact_id = $original['values'][$contributionID]['contact_id'];
7d543448 1651 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
6a488035
TO
1652 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1653 $old_source = $original['values'][$contributionID]['contribution_source'];
1654
6a488035
TO
1655 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1656 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1657
1658 //check against values in CiviUnitTestCase::createContribution()
2bfae985
EM
1659 $this->assertEquals($old_contact_id, $this->_individualId);
1660 $this->assertEquals($old_fee_amount, 5.00);
1661 $this->assertEquals($old_source, 'SSF');
1662 $this->assertEquals($old_trxn_id, 212355);
78ab0ca4 1663 $this->assertEquals($old_invoice_id, 'old_invoice');
9099cab3 1664 $params = [
6a488035
TO
1665 'id' => $contributionID,
1666 'contact_id' => $this->_individualId,
09201732
K
1667 'total_amount' => 105.00,
1668 'fee_amount' => 7.00,
4ab7d517 1669 'financial_type_id' => $this->_financialTypeId,
09201732 1670 'non_deductible_amount' => 22.00,
6a488035 1671 'contribution_status_id' => 1,
ef32adff 1672 'note' => 'Donating for Noble Cause',
9099cab3 1673 ];
6a488035 1674
4ab7d517 1675 $contribution = $this->callAPISuccess('contribution', 'create', $params);
6a488035 1676
9099cab3 1677 $new_params = [
6a488035 1678 'contribution_id' => $contribution['id'],
9099cab3 1679 ];
ef32adff 1680 $contribution = $this->callAPISuccessGetSingle('contribution', $new_params);
1681
1682 $this->assertEquals($contribution['contact_id'], $this->_individualId);
09201732 1683 $this->assertEquals($contribution['total_amount'], 105.00);
ef32adff 1684 $this->assertEquals($contribution['financial_type_id'], $this->_financialTypeId);
1685 $this->assertEquals($contribution['financial_type'], 'Donation');
1686 $this->assertEquals($contribution['instrument_id'], $old_payment_instrument);
09201732
K
1687 $this->assertEquals($contribution['non_deductible_amount'], 22.00);
1688 $this->assertEquals($contribution['fee_amount'], 7.00);
ef32adff 1689 $this->assertEquals($contribution['trxn_id'], $old_trxn_id);
1690 $this->assertEquals($contribution['invoice_id'], $old_invoice_id);
1691 $this->assertEquals($contribution['contribution_source'], $old_source);
1692 $this->assertEquals($contribution['contribution_status'], 'Completed');
09201732
K
1693
1694 $this->assertEquals($contribution['net_amount'], $contribution['total_amount'] - $contribution['fee_amount']);
1695
9099cab3 1696 $params = [
6a488035 1697 'contribution_id' => $contributionID,
9099cab3 1698 ];
4ab7d517 1699 $result = $this->callAPISuccess('contribution', 'delete', $params);
22f80e87 1700 $this->assertAPISuccess($result);
6a488035
TO
1701 }
1702
a3c2cbe6 1703 /**
1704 * Check that net_amount is updated when a contribution is updated.
1705 *
1706 * Update fee amount AND total amount, just fee amount, just total amount
1707 * and neither to check that net_amount is keep updated.
1708 */
1709 public function testUpdateContributionNetAmountVariants() {
1710 $contributionID = $this->contributionCreate(['contact_id' => $this->individualCreate()]);
1711
1712 $this->callAPISuccess('Contribution', 'create', [
1713 'id' => $contributionID,
1714 'total_amount' => 90,
1715 'fee_amount' => 6,
1716 ]);
1717 $contribution = $this->callAPISuccessGetSingle('Contribution', [
1718 'id' => $contributionID,
1719 'return' => ['net_amount', 'fee_amount', 'total_amount'],
1720 ]);
1721 $this->assertEquals(6, $contribution['fee_amount']);
1722 $this->assertEquals(90, $contribution['total_amount']);
1723 $this->assertEquals(84, $contribution['net_amount']);
1724
1725 $this->callAPISuccess('Contribution', 'create', [
1726 'id' => $contributionID,
1727 'fee_amount' => 3,
1728 ]);
1729 $contribution = $this->callAPISuccessGetSingle('Contribution', [
1730 'id' => $contributionID,
1731 'return' => ['net_amount', 'fee_amount', 'total_amount'],
1732 ]);
1733 $this->assertEquals(3, $contribution['fee_amount']);
1734 $this->assertEquals(90, $contribution['total_amount']);
1735 $this->assertEquals(87, $contribution['net_amount']);
1736
1737 $this->callAPISuccess('Contribution', 'create', [
1738 'id' => $contributionID,
1739 'total_amount' => 200,
1740 ]);
1741 $contribution = $this->callAPISuccessGetSingle('Contribution', [
1742 'id' => $contributionID,
1743 'return' => ['net_amount', 'fee_amount', 'total_amount'],
1744 ]);
1745 $this->assertEquals(3, $contribution['fee_amount']);
1746 $this->assertEquals(200, $contribution['total_amount']);
1747 $this->assertEquals(197, $contribution['net_amount']);
1748
1749 $this->callAPISuccess('Contribution', 'create', [
1750 'id' => $contributionID,
39b959db 1751 'payment_instrument' => 'Cash',
a3c2cbe6 1752 ]);
1753 $contribution = $this->callAPISuccessGetSingle('Contribution', [
1754 'id' => $contributionID,
1755 'return' => ['net_amount', 'fee_amount', 'total_amount'],
1756 ]);
1757 $this->assertEquals(3, $contribution['fee_amount']);
1758 $this->assertEquals(200, $contribution['total_amount']);
1759 $this->assertEquals(197, $contribution['net_amount']);
1760 }
1761
a1a2a83d
TO
1762 /**
1763 * Attempt (but fail) to delete a contribution without parameters.
1764 */
00be9182 1765 public function testDeleteEmptyParamsContribution() {
9099cab3 1766 $params = [];
858d0bf8 1767 $this->callAPIFailure('contribution', 'delete', $params);
6a488035
TO
1768 }
1769
00be9182 1770 public function testDeleteParamsNotArrayContribution() {
6a488035 1771 $params = 'contribution_id= 1';
d0e1eff2 1772 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
6a488035
TO
1773 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1774 }
1775
00be9182 1776 public function testDeleteWrongParamContribution() {
9099cab3 1777 $params = [
6a488035 1778 'contribution_source' => 'SSF',
9099cab3 1779 ];
858d0bf8 1780 $this->callAPIFailure('contribution', 'delete', $params);
6a488035
TO
1781 }
1782
00be9182 1783 public function testDeleteContribution() {
9099cab3 1784 $contributionID = $this->contributionCreate([
78ab0ca4 1785 'contact_id' => $this->_individualId,
1786 'financial_type_id' => $this->_financialTypeId,
9099cab3
CW
1787 ]);
1788 $params = [
6a488035 1789 'id' => $contributionID,
9099cab3 1790 ];
4ab7d517 1791 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__, __FILE__);
6a488035
TO
1792 }
1793
1794 /**
d177a2a6 1795 * Test civicrm_contribution_search with empty params.
1e52837d 1796 *
d177a2a6 1797 * All available contributions expected.
6a488035 1798 */
00be9182 1799 public function testSearchEmptyParams() {
9099cab3 1800 $params = [];
6a488035 1801
9099cab3 1802 $p = [
6a488035
TO
1803 'contact_id' => $this->_individualId,
1804 'receive_date' => date('Ymd'),
1805 'total_amount' => 100.00,
4ab7d517 1806 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1807 'non_deductible_amount' => 10.00,
1808 'fee_amount' => 5.00,
1809 'net_amount' => 95.00,
1810 'trxn_id' => 23456,
1811 'invoice_id' => 78910,
1812 'source' => 'SSF',
1813 'contribution_status_id' => 1,
9099cab3 1814 ];
4ab7d517 1815 $contribution = $this->callAPISuccess('contribution', 'create', $p);
6a488035 1816
4ab7d517 1817 $result = $this->callAPISuccess('contribution', 'get', $params);
6a488035
TO
1818 // We're taking the first element.
1819 $res = $result['values'][$contribution['id']];
1820
2bfae985
EM
1821 $this->assertEquals($p['contact_id'], $res['contact_id']);
1822 $this->assertEquals($p['total_amount'], $res['total_amount']);
5896d037 1823 $this->assertEquals($p['financial_type_id'], $res['financial_type_id']);
2bfae985
EM
1824 $this->assertEquals($p['net_amount'], $res['net_amount']);
1825 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1826 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1827 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1828 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1829 $this->assertEquals($p['source'], $res['contribution_source']);
6a488035 1830 // contribution_status_id = 1 => Completed
2bfae985 1831 $this->assertEquals('Completed', $res['contribution_status']);
6a488035
TO
1832
1833 $this->contributionDelete($contribution['id']);
1834 }
1835
1836 /**
d177a2a6 1837 * Test civicrm_contribution_search. Success expected.
6a488035 1838 */
00be9182 1839 public function testSearch() {
9099cab3 1840 $p1 = [
6a488035
TO
1841 'contact_id' => $this->_individualId,
1842 'receive_date' => date('Ymd'),
1843 'total_amount' => 100.00,
4ab7d517 1844 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1845 'non_deductible_amount' => 10.00,
1846 'contribution_status_id' => 1,
9099cab3 1847 ];
4ab7d517 1848 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
6a488035 1849
9099cab3 1850 $p2 = [
6a488035
TO
1851 'contact_id' => $this->_individualId,
1852 'receive_date' => date('Ymd'),
1853 'total_amount' => 200.00,
4ab7d517 1854 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1855 'non_deductible_amount' => 20.00,
1856 'trxn_id' => 5454565,
1857 'invoice_id' => 1212124,
1858 'fee_amount' => 50.00,
1859 'net_amount' => 60.00,
1860 'contribution_status_id' => 2,
9099cab3 1861 ];
2f45e1c2 1862 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
6a488035 1863
9099cab3 1864 $params = [
6a488035 1865 'contribution_id' => $contribution2['id'],
9099cab3 1866 ];
2f45e1c2 1867 $result = $this->callAPISuccess('contribution', 'get', $params);
6a488035
TO
1868 $res = $result['values'][$contribution2['id']];
1869
2bfae985
EM
1870 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1871 $this->assertEquals($p2['total_amount'], $res['total_amount']);
5896d037 1872 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id']);
2bfae985
EM
1873 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1874 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1875 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1876 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1877 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
91786f44 1878 $this->assertEquals(CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'), $res['contribution_status_id']);
6a488035
TO
1879
1880 $this->contributionDelete($contribution1['id']);
1881 $this->contributionDelete($contribution2['id']);
1882 }
2f45e1c2 1883
0efa8efe 1884 /**
eceb18cc 1885 * Test completing a transaction via the API.
0efa8efe 1886 *
1887 * Note that we are creating a logged in user because email goes out from
1888 * that person
1889 */
00be9182 1890 public function testCompleteTransaction() {
5896d037 1891 $mut = new CiviMailUtils($this, TRUE);
ec7e3954 1892 $this->swapMessageTemplateForTestTemplate();
0efa8efe 1893 $this->createLoggedInUser();
9099cab3 1894 $params = array_merge($this->_params, ['contribution_status_id' => 2]);
6c6e6187 1895 $contribution = $this->callAPISuccess('contribution', 'create', $params);
9099cab3 1896 $this->callAPISuccess('contribution', 'completetransaction', [
0efa8efe 1897 'id' => $contribution['id'],
9099cab3
CW
1898 ]);
1899 $contribution = $this->callAPISuccess('contribution', 'getsingle', ['id' => $contribution['id']]);
d5580ed4 1900 $this->assertEquals('SSF', $contribution['contribution_source']);
cc7b912f 1901 $this->assertEquals('Completed', $contribution['contribution_status']);
1902 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
9099cab3 1903 $mut->checkMailLog([
ec7e3954
E
1904 'email:::anthony_anderson@civicrm.org',
1905 'is_monetary:::1',
1906 'amount:::100.00',
1907 'currency:::USD',
1908 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])),
76e8d9c4 1909 "receipt_date:::\n",
8a40179e 1910 'contributeMode:::notify',
1911 'title:::Contribution',
1912 'displayName:::Mr. Anthony Anderson II',
1913 'contributionStatus:::Completed',
9099cab3 1914 ]);
8a40179e 1915 $mut->stop();
1916 $this->revertTemplateToReservedTemplate();
1917 }
1918
1919 /**
1920 * Test completing a transaction via the API with a non-USD transaction.
1921 */
1922 public function testCompleteTransactionEuro() {
1923 $mut = new CiviMailUtils($this, TRUE);
1924 $this->swapMessageTemplateForTestTemplate();
1925 $this->createLoggedInUser();
9099cab3 1926 $params = array_merge($this->_params, ['contribution_status_id' => 2, 'currency' => 'EUR']);
8a40179e 1927 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1928
9099cab3 1929 $this->callAPISuccess('contribution', 'completetransaction', [
8a40179e 1930 'id' => $contribution['id'],
9099cab3 1931 ]);
8a40179e 1932
9099cab3 1933 $contribution = $this->callAPISuccess('contribution', 'getsingle', ['id' => $contribution['id']]);
8a40179e 1934 $this->assertEquals('SSF', $contribution['contribution_source']);
1935 $this->assertEquals('Completed', $contribution['contribution_status']);
1936 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
1937
1938 $entityFinancialTransactions = $this->getFinancialTransactionsForContribution($contribution['id']);
1939 $entityFinancialTransaction = reset($entityFinancialTransactions);
9099cab3 1940 $financialTrxn = $this->callAPISuccessGetSingle('FinancialTrxn', ['id' => $entityFinancialTransaction['financial_trxn_id']]);
8a40179e 1941 $this->assertEquals('EUR', $financialTrxn['currency']);
1942
9099cab3 1943 $mut->checkMailLog([
8a40179e 1944 'email:::anthony_anderson@civicrm.org',
1945 'is_monetary:::1',
1946 'amount:::100.00',
1947 'currency:::EUR',
1948 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])),
1949 "receipt_date:::\n",
ec7e3954
E
1950 'contributeMode:::notify',
1951 'title:::Contribution',
1952 'displayName:::Mr. Anthony Anderson II',
3b28799d 1953 'contributionStatus:::Completed',
9099cab3 1954 ]);
46fa5206 1955 $mut->stop();
ec7e3954 1956 $this->revertTemplateToReservedTemplate();
46fa5206
EM
1957 }
1958
e05d2e11 1959 /**
1960 * Test to ensure mail is sent on chosing pay later
1961 */
1962 public function testpayLater() {
1963 $mut = new CiviMailUtils($this, TRUE);
1964 $this->swapMessageTemplateForTestTemplate();
1965 $this->createLoggedInUser();
1966
1967 // create contribution page first
9099cab3 1968 $contributionPageParams = [
e05d2e11 1969 'title' => 'Help Support CiviCRM!',
1970 'financial_type_id' => 1,
1971 'is_monetary' => TRUE,
1972 'is_pay_later' => 1,
1973 'is_quick_config' => TRUE,
1974 'pay_later_text' => 'I will send payment by check',
1975 'pay_later_receipt' => 'This is a pay later reciept',
1976 'is_allow_other_amount' => 1,
1977 'min_amount' => 10.00,
1978 'max_amount' => 10000.00,
1979 'goal_amount' => 100000.00,
1980 'is_email_receipt' => 1,
1981 'is_active' => 1,
1982 'amount_block_is_active' => 1,
1983 'currency' => 'USD',
1984 'is_billing_required' => 0,
9099cab3 1985 ];
e05d2e11 1986 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', $contributionPageParams);
1987
1988 // submit form values
9099cab3
CW
1989 $priceSet = $this->callAPISuccess('price_set', 'getsingle', ['name' => 'default_contribution_amount']);
1990 $params = [
e05d2e11 1991 'id' => $contributionPageResult['id'],
1992 'contact_id' => $this->_individualId,
1993 'email-5' => 'anthony_anderson@civicrm.org',
1994 'payment_processor_id' => 0,
1995 'amount' => 100.00,
1996 'tax_amount' => '',
1997 'currencyID' => 'USD',
1998 'is_pay_later' => 1,
1999 'invoiceID' => 'f28e1ddc86f8c4a0ff5bcf46393e4bc8',
2000 'is_quick_config' => 1,
2001 'description' => 'Online Contribution: Help Support CiviCRM!',
2002 'price_set_id' => $priceSet['id'],
9099cab3 2003 ];
e05d2e11 2004 $this->callAPISuccess('contribution_page', 'submit', $params);
2005
9099cab3 2006 $mut->checkMailLog([
e05d2e11 2007 'is_pay_later:::1',
2008 'email:::anthony_anderson@civicrm.org',
2009 'pay_later_receipt:::' . $contributionPageParams['pay_later_receipt'],
2010 'displayName:::Mr. Anthony Anderson II',
2011 'contributionPageId:::' . $contributionPageResult['id'],
2012 'title:::' . $contributionPageParams['title'],
8beee0e8 2013 'amount:::' . $params['amount'],
9099cab3 2014 ]);
e05d2e11 2015 $mut->stop();
2016 $this->revertTemplateToReservedTemplate();
2017 }
2018
2a0df9d9 2019 /**
2020 * Test to check whether contact billing address is used when no contribution address
2021 */
2022 public function testBillingAddress() {
2023 $mut = new CiviMailUtils($this, TRUE);
2024 $this->swapMessageTemplateForTestTemplate();
2025 $this->createLoggedInUser();
2026
2027 //Scenario 1: When Contact don't have any address
9099cab3 2028 $params = array_merge($this->_params, ['contribution_status_id' => 2]);
2a0df9d9 2029 $contribution = $this->callAPISuccess('contribution', 'create', $params);
9099cab3 2030 $this->callAPISuccess('contribution', 'completetransaction', [
2a0df9d9 2031 'id' => $contribution['id'],
9099cab3
CW
2032 ]);
2033 $mut->checkMailLog([
2a0df9d9 2034 'address:::',
9099cab3 2035 ]);
2a0df9d9 2036
2037 // Scenario 2: Contribution using address
9099cab3 2038 $address = $this->callAPISuccess('address', 'create', [
2a0df9d9 2039 'street_address' => 'contribution billing st',
2040 'location_type_id' => 2,
2041 'contact_id' => $this->_params['contact_id'],
9099cab3
CW
2042 ]);
2043 $params = array_merge($this->_params, [
76e8d9c4 2044 'contribution_status_id' => 2,
2a0df9d9 2045 'address_id' => $address['id'],
9099cab3 2046 ]
2a0df9d9 2047 );
2048 $contribution = $this->callAPISuccess('contribution', 'create', $params);
9099cab3 2049 $this->callAPISuccess('contribution', 'completetransaction', [
2a0df9d9 2050 'id' => $contribution['id'],
9099cab3
CW
2051 ]);
2052 $mut->checkMailLog([
2a0df9d9 2053 'address:::contribution billing st',
9099cab3 2054 ]);
2a0df9d9 2055
2056 // Scenario 3: Contribution wtth no address but contact has a billing address
9099cab3 2057 $this->callAPISuccess('address', 'create', [
2a0df9d9 2058 'id' => $address['id'],
2059 'street_address' => 'is billing st',
2060 'contact_id' => $this->_params['contact_id'],
9099cab3
CW
2061 ]);
2062 $params = array_merge($this->_params, ['contribution_status_id' => 2]);
2a0df9d9 2063 $contribution = $this->callAPISuccess('contribution', 'create', $params);
9099cab3 2064 $this->callAPISuccess('contribution', 'completetransaction', [
2a0df9d9 2065 'id' => $contribution['id'],
9099cab3
CW
2066 ]);
2067 $mut->checkMailLog([
2a0df9d9 2068 'address:::is billing st',
9099cab3 2069 ]);
2a0df9d9 2070
2071 $mut->stop();
2072 $this->revertTemplateToReservedTemplate();
2073 }
2074
080a561b 2075 /**
2076 * Test completing a transaction via the API.
2077 *
2078 * Note that we are creating a logged in user because email goes out from
2079 * that person
2080 */
2081 public function testCompleteTransactionFeeAmount() {
2082 $this->createLoggedInUser();
9099cab3 2083 $params = array_merge($this->_params, ['contribution_status_id' => 2]);
080a561b 2084 $contribution = $this->callAPISuccess('contribution', 'create', $params);
9099cab3 2085 $this->callAPISuccess('contribution', 'completetransaction', [
080a561b 2086 'id' => $contribution['id'],
2087 'fee_amount' => '.56',
2088 'trxn_id' => '7778888',
9099cab3
CW
2089 ]);
2090 $contribution = $this->callAPISuccess('contribution', 'getsingle', ['id' => $contribution['id'], 'sequential' => 1]);
080a561b 2091 $this->assertEquals('Completed', $contribution['contribution_status']);
2092 $this->assertEquals('7778888', $contribution['trxn_id']);
2093 $this->assertEquals('.56', $contribution['fee_amount']);
2094 $this->assertEquals('99.44', $contribution['net_amount']);
2095 }
2096
effb4d85 2097 /**
83644f47 2098 * CRM-19126 Add test to verify when complete transaction is called tax amount is not changed.
2099 *
2100 * @param string $thousandSeparator
2101 * punctuation used to refer to thousands.
2102 *
2103 * @dataProvider getThousandSeparators
effb4d85 2104 */
83644f47 2105 public function testCheckTaxAmount($thousandSeparator) {
2106 $this->setCurrencySeparators($thousandSeparator);
effb4d85 2107 $contact = $this->createLoggedInUser();
9099cab3 2108 $financialType = $this->callAPISuccess('financial_type', 'create', [
effb4d85
SL
2109 'name' => 'Test taxable financial Type',
2110 'is_reserved' => 0,
2111 'is_active' => 1,
9099cab3
CW
2112 ]);
2113 $financialAccount = $this->callAPISuccess('financial_account', 'create', [
39b959db
SL
2114 'name' => 'Test Tax financial account ',
2115 'contact_id' => $contact,
2116 'financial_account_type_id' => 2,
2117 'is_tax' => 1,
2118 'tax_rate' => 5.00,
2119 'is_reserved' => 0,
2120 'is_active' => 1,
2121 'is_default' => 0,
9099cab3 2122 ]);
effb4d85
SL
2123 $financialTypeId = $financialType['id'];
2124 $financialAccountId = $financialAccount['id'];
9099cab3 2125 $financialAccountParams = [
effb4d85
SL
2126 'entity_table' => 'civicrm_financial_type',
2127 'entity_id' => $financialTypeId,
2128 'account_relationship' => 10,
2129 'financial_account_id' => $financialAccountId,
9099cab3 2130 ];
a76b8bd8 2131 CRM_Financial_BAO_FinancialTypeAccount::add($financialAccountParams);
f527e012 2132
9099cab3 2133 $params = array_merge($this->_params, ['contribution_status_id' => 2, 'financial_type_id' => $financialTypeId]);
effb4d85 2134 $contribution = $this->callAPISuccess('contribution', 'create', $params);
9099cab3
CW
2135 $contribution1 = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id'], 'return' => 'tax_amount', 'sequential' => 1]);
2136 $this->callAPISuccess('contribution', 'completetransaction', [
39b959db
SL
2137 'id' => $contribution['id'],
2138 'trxn_id' => '777788888',
2139 'fee_amount' => '6.00',
9099cab3
CW
2140 ]);
2141 $contribution2 = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id'], 'return' => ['tax_amount', 'fee_amount', 'net_amount'], 'sequential' => 1]);
effb4d85 2142 $this->assertEquals($contribution1['values'][0]['tax_amount'], $contribution2['values'][0]['tax_amount']);
99a4cd32 2143 $this->assertEquals('6.00', $contribution2['values'][0]['fee_amount']);
f836984d 2144 $this->assertEquals('99.00', $contribution2['values'][0]['net_amount']);
effb4d85
SL
2145 }
2146
d97c96dc 2147 /**
0e6ccb2e 2148 * Test repeat contribution successfully creates line item.
d97c96dc 2149 */
1e52837d 2150 public function testRepeatTransaction() {
9099cab3
CW
2151 $originalContribution = $this->setUpRepeatTransaction($recurParams = [], 'single');
2152 $this->callAPISuccess('contribution', 'repeattransaction', [
d97c96dc
EM
2153 'original_contribution_id' => $originalContribution['id'],
2154 'contribution_status_id' => 'Completed',
2155 'trxn_id' => uniqid(),
9099cab3
CW
2156 ]);
2157 $lineItemParams = [
d97c96dc
EM
2158 'entity_id' => $originalContribution['id'],
2159 'sequential' => 1,
9099cab3 2160 'return' => [
d97c96dc
EM
2161 'entity_table',
2162 'qty',
2163 'unit_price',
2164 'line_total',
2165 'label',
2166 'financial_type_id',
2167 'deductible_amount',
2168 'price_field_value_id',
2169 'price_field_id',
9099cab3
CW
2170 ],
2171 ];
2172 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
d97c96dc 2173 'entity_id' => $originalContribution['id'],
9099cab3
CW
2174 ]));
2175 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
d97c96dc 2176 'entity_id' => $originalContribution['id'] + 1,
9099cab3 2177 ]));
d97c96dc
EM
2178 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
2179 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2180 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
9099cab3 2181 $this->_checkFinancialRecords([
f69a9ac3 2182 'id' => $originalContribution['id'] + 1,
9099cab3 2183 'payment_instrument_id' => $this->callAPISuccessGetValue('PaymentProcessor', [
f69a9ac3 2184 'id' => $originalContribution['payment_processor_id'],
2185 'return' => 'payment_instrument_id',
9099cab3
CW
2186 ]),
2187 ], 'online');
d97c96dc
EM
2188 $this->quickCleanUpFinancialEntities();
2189 }
2190
893a550c 2191 /**
0e6ccb2e
K
2192 * Test repeat contribution successfully creates line items (plural).
2193 */
2194 public function testRepeatTransactionLineItems() {
7150b1c8 2195 // CRM-19309
9099cab3
CW
2196 $originalContribution = $this->setUpRepeatTransaction($recurParams = [], 'multiple');
2197 $this->callAPISuccess('contribution', 'repeattransaction', [
0e6ccb2e
K
2198 'original_contribution_id' => $originalContribution['id'],
2199 'contribution_status_id' => 'Completed',
2200 'trxn_id' => uniqid(),
9099cab3 2201 ]);
0e6ccb2e 2202
9099cab3 2203 $lineItemParams = [
0e6ccb2e
K
2204 'entity_id' => $originalContribution['id'],
2205 'sequential' => 1,
9099cab3 2206 'return' => [
0e6ccb2e
K
2207 'entity_table',
2208 'qty',
2209 'unit_price',
2210 'line_total',
2211 'label',
2212 'financial_type_id',
2213 'deductible_amount',
2214 'price_field_value_id',
2215 'price_field_id',
9099cab3
CW
2216 ],
2217 ];
2218 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
0e6ccb2e 2219 'entity_id' => $originalContribution['id'],
9099cab3
CW
2220 ]));
2221 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
0e6ccb2e 2222 'entity_id' => $originalContribution['id'] + 1,
9099cab3 2223 ]));
0e6ccb2e
K
2224
2225 // unset id and entity_id for all of them to be able to compare the lineItems:
2226 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
2227 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2228 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
2229
2230 unset($lineItem1['values'][1]['id'], $lineItem1['values'][1]['entity_id']);
2231 unset($lineItem2['values'][1]['id'], $lineItem2['values'][1]['entity_id']);
2232 $this->assertEquals($lineItem1['values'][1], $lineItem2['values'][1]);
2233
7150b1c8
K
2234 // CRM-19309 so in future we also want to:
2235 // check that financial_line_items have been created for entity_id 3 and 4;
0e6ccb2e 2236
9099cab3 2237 $this->callAPISuccessGetCount('FinancialItem', ['description' => 'Sales Tax', 'amount' => 0], 0);
0e6ccb2e
K
2238 $this->quickCleanUpFinancialEntities();
2239 }
2240
2241 /**
2242 * Test repeat contribution successfully creates is_test transaction.
893a550c 2243 */
2244 public function testRepeatTransactionIsTest() {
2245 $this->_params['is_test'] = 1;
9099cab3 2246 $originalContribution = $this->setUpRepeatTransaction(['is_test' => 1], 'single');
893a550c 2247
9099cab3 2248 $this->callAPISuccess('contribution', 'repeattransaction', [
893a550c 2249 'original_contribution_id' => $originalContribution['id'],
2250 'contribution_status_id' => 'Completed',
2251 'trxn_id' => uniqid(),
9099cab3
CW
2252 ]);
2253 $this->callAPISuccessGetCount('Contribution', ['contribution_test' => 1], 2);
893a550c 2254 }
2255
d5580ed4 2256 /**
2257 * Test repeat contribution passed in status.
2258 */
2259 public function testRepeatTransactionPassedInStatus() {
9099cab3 2260 $originalContribution = $this->setUpRepeatTransaction($recurParams = [], 'single');
d5580ed4 2261
9099cab3 2262 $this->callAPISuccess('contribution', 'repeattransaction', [
d5580ed4 2263 'original_contribution_id' => $originalContribution['id'],
2264 'contribution_status_id' => 'Pending',
2265 'trxn_id' => uniqid(),
9099cab3
CW
2266 ]);
2267 $this->callAPISuccessGetCount('Contribution', ['contribution_status_id' => 2], 1);
d5580ed4 2268 }
2269
1eade77d 2270 /**
2271 * Test repeat contribution accepts recur_id instead of original_contribution_id.
2272 */
2273 public function testRepeatTransactionAcceptRecurID() {
9099cab3 2274 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', [
1eade77d 2275 'contact_id' => $this->_individualId,
2276 'installments' => '12',
2277 'frequency_interval' => '1',
2278 'amount' => '100',
2279 'contribution_status_id' => 1,
2280 'start_date' => '2012-01-01 00:00:00',
2281 'currency' => 'USD',
2282 'frequency_unit' => 'month',
2283 'payment_processor_id' => $this->paymentProcessorID,
9099cab3 2284 ]);
1eade77d 2285 $this->callAPISuccess('contribution', 'create', array_merge(
2286 $this->_params,
9099cab3 2287 ['contribution_recur_id' => $contributionRecur['id']])
1eade77d 2288 );
2289
9099cab3 2290 $this->callAPISuccess('contribution', 'repeattransaction', [
1eade77d 2291 'contribution_recur_id' => $contributionRecur['id'],
2292 'contribution_status_id' => 'Completed',
2293 'trxn_id' => uniqid(),
9099cab3 2294 ]);
1eade77d 2295
2296 $this->quickCleanUpFinancialEntities();
2297 }
2298
28124368
PH
2299 /**
2300 * CRM-19873 Test repattransaction if contribution_recur_id is a test.
2301 */
2302 public function testRepeatTransactionTestRecurId() {
9099cab3 2303 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', [
28124368
PH
2304 'contact_id' => $this->_individualId,
2305 'frequency_interval' => '1',
2306 'amount' => '1.00',
2307 'contribution_status_id' => 1,
2308 'start_date' => '2017-01-01 00:00:00',
2309 'currency' => 'USD',
2310 'frequency_unit' => 'month',
2311 'payment_processor_id' => $this->paymentProcessorID,
2312 'is_test' => 1,
9099cab3 2313 ]);
28124368
PH
2314 $this->callAPISuccess('contribution', 'create', array_merge(
2315 $this->_params,
9099cab3 2316 [
28124368
PH
2317 'contribution_recur_id' => $contributionRecur['id'],
2318 'is_test' => 1,
9099cab3 2319 ])
28124368
PH
2320 );
2321
9099cab3 2322 $repeatedContribution = $this->callAPISuccess('contribution', 'repeattransaction', [
28124368
PH
2323 'contribution_recur_id' => $contributionRecur['id'],
2324 'contribution_status_id' => 'Completed',
2325 'trxn_id' => uniqid(),
9099cab3 2326 ]);
28124368
PH
2327
2328 $this->assertEquals($contributionRecur['values'][1]['is_test'], $repeatedContribution['values'][2]['is_test']);
2329 $this->quickCleanUpFinancialEntities();
2330 }
37f29fcf 2331
d2334242 2332 /**
37f29fcf 2333 * CRM-19945 Tests that Contribute.repeattransaction renews a membership when contribution status=Completed
7c3f3d59 2334 *
d2334242 2335 */
37f29fcf 2336 public function testRepeatTransactionMembershipRenewCompletedContribution() {
d2334242
PH
2337 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
2338
9099cab3 2339 $this->callAPISuccess('contribution', 'create', [
7c3f3d59 2340 'contact_id' => $originalContribution['values'][1]['contact_id'],
2341 'financial_type_id' => $originalContribution['values'][1]['financial_type_id'],
2342 'total_amount' => $originalContribution['values'][1]['total_amount'],
2343 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2344 'contribution_status_id' => "Failed",
9099cab3 2345 ]);
d2334242 2346
9099cab3 2347 $this->callAPISuccess('membership', 'create', [
39b959db
SL
2348 'id' => $membership['id'],
2349 'end_date' => 'yesterday',
2350 'status_id' => 'Expired',
9099cab3 2351 ]);
d2334242 2352
9099cab3 2353 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', [
39b959db
SL
2354 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2355 'contribution_status_id' => 'Completed',
2356 'trxn_id' => 'bobsled',
9099cab3 2357 ]);
d2334242 2358
9099cab3 2359 $membershipStatusId = $this->callAPISuccess('membership', 'getvalue', [
d2334242
PH
2360 'id' => $membership['id'],
2361 'return' => 'status_id',
9099cab3 2362 ]);
d2334242 2363
9099cab3 2364 $membership = $this->callAPISuccess('membership', 'get', [
37f29fcf 2365 'id' => $membership['id'],
9099cab3 2366 ]);
37f29fcf
MW
2367
2368 $this->assertEquals('New', CRM_Core_PseudoConstant::getName('CRM_Member_BAO_Membership', 'status_id', $membershipStatusId));
2369
9099cab3 2370 $lineItem = $this->callAPISuccessGetSingle('LineItem', ['contribution_id' => $contribution['id']]);
37f29fcf 2371 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
9099cab3 2372 $this->callAPISuccessGetCount('MembershipPayment', ['membership_id' => $membership['id']]);
37f29fcf
MW
2373 $this->quickCleanUpFinancialEntities();
2374 $this->contactDelete($originalContribution['values'][1]['contact_id']);
2375 }
2376
2377 /**
2378 * CRM-19945 Tests that Contribute.repeattransaction DOES NOT renew a membership when contribution status=Failed
2379 *
2380 * @dataProvider contributionStatusProvider
2381 */
2382 public function testRepeatTransactionMembershipRenewContributionNotCompleted($contributionStatus) {
2383 // Completed status should renew so we don't test that here
2384 // In Progress status is only for recurring contributions so we don't test that here
2385 if (in_array($contributionStatus['name'], ['Completed', 'In Progress'])) {
2386 return;
2387 }
2388 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
2389
9099cab3 2390 $this->callAPISuccess('contribution', 'create', [
37f29fcf
MW
2391 'contact_id' => $originalContribution['values'][1]['contact_id'],
2392 'financial_type_id' => $originalContribution['values'][1]['financial_type_id'],
2393 'total_amount' => $originalContribution['values'][1]['total_amount'],
2394 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2395 'contribution_status_id' => "Completed",
9099cab3 2396 ]);
37f29fcf 2397
9099cab3 2398 $this->callAPISuccess('membership', 'create', [
37f29fcf
MW
2399 'id' => $membership['id'],
2400 'end_date' => 'yesterday',
2401 'status_id' => 'Expired',
9099cab3 2402 ]);
37f29fcf 2403
9099cab3 2404 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', [
37f29fcf
MW
2405 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2406 'contribution_status_id' => $contributionStatus['name'],
2407 'trxn_id' => 'bobsled',
9099cab3 2408 ]);
37f29fcf 2409
9099cab3 2410 $updatedMembership = $this->callAPISuccess('membership', 'getsingle', [
37f29fcf 2411 'id' => $membership['id'],
9099cab3 2412 ]);
37f29fcf
MW
2413
2414 $dateTime = new DateTime('yesterday');
2415 $this->assertEquals($dateTime->format('Y-m-d'), $updatedMembership['end_date']);
2416 $this->assertEquals(CRM_Core_PseudoConstant::getKey('CRM_Member_BAO_Membership', 'status_id', 'Expired'), $updatedMembership['status_id']);
7c3f3d59 2417
9099cab3 2418 $lineItem = $this->callAPISuccessGetSingle('LineItem', ['contribution_id' => $contribution['id']]);
7c3f3d59 2419 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
9099cab3 2420 $this->callAPISuccessGetCount('MembershipPayment', ['membership_id' => $membership['id']]);
d2334242
PH
2421 $this->quickCleanUpFinancialEntities();
2422 $this->contactDelete($originalContribution['values'][1]['contact_id']);
2423 }
28124368 2424
37f29fcf
MW
2425 /**
2426 * Dataprovider provides contribution status as [optionvalue=>contribution_status_name]
2427 * FIXME: buildOptions seems to die in CRM_Core_Config::_construct when in test mode.
2428 *
2429 * @return array
2430 * @throws \CiviCRM_API3_Exception
2431 */
2432 public function contributionStatusProvider() {
2433 $contributionStatuses = civicrm_api3('OptionValue', 'get', [
2434 'return' => ["id", "name"],
2435 'option_group_id' => "contribution_status",
2436 ]);
2437 foreach ($contributionStatuses['values'] as $statusName) {
2438 $statuses[] = [$statusName];
2439 }
2440 return $statuses;
2441 }
2442
c03f1689
EM
2443 /**
2444 * CRM-16397 test appropriate action if total amount has changed for single line items.
2445 */
2446 public function testRepeatTransactionAlteredAmount() {
2447 $paymentProcessorID = $this->paymentProcessorCreate();
9099cab3 2448 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', [
c03f1689
EM
2449 'contact_id' => $this->_individualId,
2450 'installments' => '12',
2451 'frequency_interval' => '1',
2452 'amount' => '500',
2453 'contribution_status_id' => 1,
2454 'start_date' => '2012-01-01 00:00:00',
2455 'currency' => 'USD',
2456 'frequency_unit' => 'month',
2457 'payment_processor_id' => $paymentProcessorID,
9099cab3 2458 ]);
c03f1689
EM
2459 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2460 $this->_params,
9099cab3 2461 [
c03f1689 2462 'contribution_recur_id' => $contributionRecur['id'],
9099cab3 2463 ])
c03f1689
EM
2464 );
2465
9099cab3 2466 $this->callAPISuccess('contribution', 'repeattransaction', [
c03f1689
EM
2467 'original_contribution_id' => $originalContribution['id'],
2468 'contribution_status_id' => 'Completed',
2469 'trxn_id' => uniqid(),
2470 'total_amount' => '400',
2471 'fee_amount' => 50,
9099cab3 2472 ]);
0e6ccb2e 2473
9099cab3 2474 $lineItemParams = [
c03f1689
EM
2475 'entity_id' => $originalContribution['id'],
2476 'sequential' => 1,
9099cab3 2477 'return' => [
c03f1689
EM
2478 'entity_table',
2479 'qty',
2480 'unit_price',
2481 'line_total',
2482 'label',
2483 'financial_type_id',
2484 'deductible_amount',
2485 'price_field_value_id',
2486 'price_field_id',
9099cab3
CW
2487 ],
2488 ];
2489 $this->callAPISuccessGetSingle('contribution', [
c03f1689 2490 'total_amount' => 400,
c03f1689
EM
2491 'fee_amount' => 50,
2492 'net_amount' => 350,
9099cab3
CW
2493 ]);
2494 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
c03f1689 2495 'entity_id' => $originalContribution['id'],
9099cab3 2496 ]));
c03f1689 2497 $expectedLineItem = array_merge(
9099cab3 2498 $lineItem1['values'][0], [
c03f1689
EM
2499 'line_total' => '400.00',
2500 'unit_price' => '400.00',
9099cab3 2501 ]
542d9e2c 2502 );
c03f1689 2503
9099cab3 2504 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
c03f1689 2505 'entity_id' => $originalContribution['id'] + 1,
9099cab3 2506 ]));
0e6ccb2e 2507
c03f1689
EM
2508 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2509 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2510 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
c02c17df 2511 }
c03f1689 2512
3c49d90c 2513 /**
2514 * CRM-17718 test appropriate action if financial type has changed for single line items.
2515 */
2516 public function testRepeatTransactionPassedInFinancialType() {
2517 $originalContribution = $this->setUpRecurringContribution();
2518
9099cab3 2519 $this->callAPISuccess('contribution', 'repeattransaction', [
3c49d90c 2520 'original_contribution_id' => $originalContribution['id'],
2521 'contribution_status_id' => 'Completed',
2522 'trxn_id' => uniqid(),
2523 'financial_type_id' => 2,
9099cab3
CW
2524 ]);
2525 $lineItemParams = [
3c49d90c 2526 'entity_id' => $originalContribution['id'],
2527 'sequential' => 1,
9099cab3 2528 'return' => [
3c49d90c 2529 'entity_table',
2530 'qty',
2531 'unit_price',
2532 'line_total',
2533 'label',
2534 'financial_type_id',
2535 'deductible_amount',
2536 'price_field_value_id',
2537 'price_field_id',
9099cab3
CW
2538 ],
2539 ];
3c49d90c 2540
9099cab3 2541 $this->callAPISuccessGetSingle('contribution', [
3c49d90c 2542 'total_amount' => 100,
2543 'financial_type_id' => 2,
9099cab3
CW
2544 ]);
2545 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
3c49d90c 2546 'entity_id' => $originalContribution['id'],
9099cab3 2547 ]));
3c49d90c 2548 $expectedLineItem = array_merge(
9099cab3 2549 $lineItem1['values'][0], [
3c49d90c 2550 'line_total' => '100.00',
2551 'unit_price' => '100.00',
2552 'financial_type_id' => 2,
257ab382 2553 'contribution_type_id' => 2,
9099cab3 2554 ]
3c49d90c 2555 );
9099cab3 2556 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
3c49d90c 2557 'entity_id' => $originalContribution['id'] + 1,
9099cab3 2558 ]));
3c49d90c 2559 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2560 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2561 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2562 }
2563
7f4ef731 2564 /**
2565 * CRM-17718 test appropriate action if financial type has changed for single line items.
2566 */
2567 public function testRepeatTransactionUpdatedFinancialType() {
9099cab3 2568 $originalContribution = $this->setUpRecurringContribution([], ['financial_type_id' => 2]);
7f4ef731 2569
9099cab3 2570 $this->callAPISuccess('contribution', 'repeattransaction', [
7f4ef731 2571 'contribution_recur_id' => $originalContribution['id'],
2572 'contribution_status_id' => 'Completed',
2573 'trxn_id' => uniqid(),
9099cab3
CW
2574 ]);
2575 $lineItemParams = [
7f4ef731 2576 'entity_id' => $originalContribution['id'],
2577 'sequential' => 1,
9099cab3 2578 'return' => [
7f4ef731 2579 'entity_table',
2580 'qty',
2581 'unit_price',
2582 'line_total',
2583 'label',
2584 'financial_type_id',
2585 'deductible_amount',
2586 'price_field_value_id',
2587 'price_field_id',
9099cab3
CW
2588 ],
2589 ];
7f4ef731 2590
9099cab3 2591 $this->callAPISuccessGetSingle('contribution', [
7f4ef731 2592 'total_amount' => 100,
2593 'financial_type_id' => 2,
9099cab3
CW
2594 ]);
2595 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
7f4ef731 2596 'entity_id' => $originalContribution['id'],
9099cab3 2597 ]));
7f4ef731 2598 $expectedLineItem = array_merge(
9099cab3 2599 $lineItem1['values'][0], [
7f4ef731 2600 'line_total' => '100.00',
2601 'unit_price' => '100.00',
2602 'financial_type_id' => 2,
257ab382 2603 'contribution_type_id' => 2,
9099cab3 2604 ]
7f4ef731 2605 );
2606
9099cab3 2607 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
7f4ef731 2608 'entity_id' => $originalContribution['id'] + 1,
9099cab3 2609 ]));
7f4ef731 2610 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2611 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2612 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2613 }
2614
c02c17df 2615 /**
1eade77d 2616 * CRM-16397 test appropriate action if campaign has been passed in.
c02c17df 2617 */
2618 public function testRepeatTransactionPassedInCampaign() {
2619 $paymentProcessorID = $this->paymentProcessorCreate();
2620 $campaignID = $this->campaignCreate();
2621 $campaignID2 = $this->campaignCreate();
9099cab3 2622 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', [
c02c17df 2623 'contact_id' => $this->_individualId,
2624 'installments' => '12',
2625 'frequency_interval' => '1',
2626 'amount' => '100',
2627 'contribution_status_id' => 1,
2628 'start_date' => '2012-01-01 00:00:00',
2629 'currency' => 'USD',
2630 'frequency_unit' => 'month',
2631 'payment_processor_id' => $paymentProcessorID,
9099cab3 2632 ]);
c02c17df 2633 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2634 $this->_params,
9099cab3 2635 [
c02c17df 2636 'contribution_recur_id' => $contributionRecur['id'],
2637 'campaign_id' => $campaignID,
9099cab3 2638 ])
c02c17df 2639 );
2640
9099cab3 2641 $this->callAPISuccess('contribution', 'repeattransaction', [
c02c17df 2642 'original_contribution_id' => $originalContribution['id'],
2643 'contribution_status_id' => 'Completed',
2644 'trxn_id' => uniqid(),
2645 'campaign_id' => $campaignID2,
9099cab3 2646 ]);
c02c17df 2647
9099cab3 2648 $this->callAPISuccessGetSingle('contribution', [
c02c17df 2649 'total_amount' => 100,
2650 'campaign_id' => $campaignID2,
9099cab3 2651 ]);
c02c17df 2652 }
2653
2654 /**
2655 * CRM-17718 campaign stored on contribution recur gets priority.
2656 *
2657 * This reflects the fact we permit people to update them.
2658 */
2659 public function testRepeatTransactionUpdatedCampaign() {
2660 $paymentProcessorID = $this->paymentProcessorCreate();
2661 $campaignID = $this->campaignCreate();
2662 $campaignID2 = $this->campaignCreate();
9099cab3 2663 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', [
c02c17df 2664 'contact_id' => $this->_individualId,
2665 'installments' => '12',
2666 'frequency_interval' => '1',
2667 'amount' => '100',
2668 'contribution_status_id' => 1,
2669 'start_date' => '2012-01-01 00:00:00',
2670 'currency' => 'USD',
2671 'frequency_unit' => 'month',
2672 'payment_processor_id' => $paymentProcessorID,
2673 'campaign_id' => $campaignID,
9099cab3 2674 ]);
c02c17df 2675 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2676 $this->_params,
9099cab3 2677 [
c02c17df 2678 'contribution_recur_id' => $contributionRecur['id'],
2679 'campaign_id' => $campaignID2,
9099cab3 2680 ])
c02c17df 2681 );
2682
9099cab3 2683 $this->callAPISuccess('contribution', 'repeattransaction', [
c02c17df 2684 'original_contribution_id' => $originalContribution['id'],
2685 'contribution_status_id' => 'Completed',
2686 'trxn_id' => uniqid(),
9099cab3 2687 ]);
c02c17df 2688
9099cab3 2689 $this->callAPISuccessGetSingle('contribution', [
c02c17df 2690 'total_amount' => 100,
2691 'campaign_id' => $campaignID,
9099cab3 2692 ]);
c03f1689
EM
2693 }
2694
2b0de476
E
2695 /**
2696 * CRM-20685 Repeattransaction produces incorrect Financial Type ID (in specific circumstance) - if number of lineItems = 1.
2697 *
2698 * This case happens when the line item & contribution do not have the same type in his initiating transaction.
2699 */
2700 public function testRepeatTransactionUpdatedFinancialTypeAndNotEquals() {
9099cab3 2701 $originalContribution = $this->setUpRecurringContribution([], ['financial_type_id' => 2]);
2b0de476 2702 // This will made the trick to get the not equals behaviour.
9099cab3
CW
2703 $this->callAPISuccess('line_item', 'create', ['id' => 1, 'financial_type_id' => 4]);
2704 $this->callAPISuccess('contribution', 'repeattransaction', [
2b0de476
E
2705 'contribution_recur_id' => $originalContribution['id'],
2706 'contribution_status_id' => 'Completed',
2707 'trxn_id' => uniqid(),
9099cab3
CW
2708 ]);
2709 $lineItemParams = [
2b0de476
E
2710 'entity_id' => $originalContribution['id'],
2711 'sequential' => 1,
9099cab3 2712 'return' => [
2b0de476
E
2713 'entity_table',
2714 'qty',
2715 'unit_price',
2716 'line_total',
2717 'label',
2718 'financial_type_id',
2719 'deductible_amount',
2720 'price_field_value_id',
2721 'price_field_id',
9099cab3
CW
2722 ],
2723 ];
2724 $this->callAPISuccessGetSingle('contribution', [
2b0de476
E
2725 'total_amount' => 100,
2726 'financial_type_id' => 2,
9099cab3
CW
2727 ]);
2728 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
2b0de476 2729 'entity_id' => $originalContribution['id'],
9099cab3 2730 ]));
2b0de476 2731 $expectedLineItem = array_merge(
9099cab3 2732 $lineItem1['values'][0], [
2b0de476
E
2733 'line_total' => '100.00',
2734 'unit_price' => '100.00',
2735 'financial_type_id' => 4,
2736 'contribution_type_id' => 4,
9099cab3 2737 ]
2b0de476
E
2738 );
2739
9099cab3 2740 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
2b0de476 2741 'entity_id' => $originalContribution['id'] + 1,
9099cab3
CW
2742 ]));
2743 $this->callAPISuccess('line_item', 'create', ['id' => 1, 'financial_type_id' => 1]);
2b0de476
E
2744 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2745 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2746 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2747 }
2748
2936c3b5
EM
2749 /**
2750 * Test completing a transaction does not 'mess' with net amount (CRM-15960).
2751 */
2752 public function testCompleteTransactionNetAmountOK() {
2753 $this->createLoggedInUser();
9099cab3 2754 $params = array_merge($this->_params, ['contribution_status_id' => 2]);
2936c3b5
EM
2755 unset($params['net_amount']);
2756 $contribution = $this->callAPISuccess('contribution', 'create', $params);
9099cab3 2757 $this->callAPISuccess('contribution', 'completetransaction', [
2936c3b5 2758 'id' => $contribution['id'],
9099cab3
CW
2759 ]);
2760 $contribution = $this->callAPISuccess('contribution', 'getsingle', ['id' => $contribution['id']]);
2936c3b5
EM
2761 $this->assertEquals('Completed', $contribution['contribution_status']);
2762 $this->assertTrue(($contribution['total_amount'] - $contribution['net_amount']) == $contribution['fee_amount']);
2763 }
2764
46fa5206 2765 /**
1e52837d 2766 * CRM-14151 - Test completing a transaction via the API.
46fa5206 2767 */
00be9182 2768 public function testCompleteTransactionWithReceiptDateSet() {
76e8d9c4 2769 $this->swapMessageTemplateForTestTemplate();
5896d037 2770 $mut = new CiviMailUtils($this, TRUE);
46fa5206 2771 $this->createLoggedInUser();
9099cab3 2772 $params = array_merge($this->_params, ['contribution_status_id' => 2, 'receipt_date' => 'now']);
6c6e6187 2773 $contribution = $this->callAPISuccess('contribution', 'create', $params);
9099cab3
CW
2774 $this->callAPISuccess('contribution', 'completetransaction', ['id' => $contribution['id'], 'trxn_date' => date('Y-m-d')]);
2775 $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id'], 'sequential' => 1]);
46fa5206 2776 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
7104593e 2777 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['values'][0]['receive_date'])));
9099cab3 2778 $mut->checkMailLog([
46fa5206 2779 'Receipt - Contribution',
76e8d9c4 2780 'receipt_date:::' . date('Ymd'),
9099cab3 2781 ]);
0efa8efe 2782 $mut->stop();
76e8d9c4 2783 $this->revertTemplateToReservedTemplate();
0efa8efe 2784 }
2785
e2ca457d
KG
2786 /**
2787 * CRM-1960 - Test to ensure that completetransaction respects the is_email_receipt setting
2788 */
2789 public function testCompleteTransactionWithEmailReceiptInput() {
d891a273 2790 $contributionPage = $this->createReceiptableContributionPage();
2791
e2ca457d 2792 $this->_params['contribution_page_id'] = $contributionPage['id'];
9099cab3 2793 $params = array_merge($this->_params, ['contribution_status_id' => 2]);
e2ca457d
KG
2794 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2795 // Complete the transaction overriding is_email_receipt to = FALSE
9099cab3 2796 $this->callAPISuccess('contribution', 'completetransaction', [
e2ca457d
KG
2797 'id' => $contribution['id'],
2798 'trxn_date' => date('2011-04-09'),
2799 'trxn_id' => 'kazam',
2800 'is_email_receipt' => 0,
9099cab3 2801 ]);
e2ca457d 2802 // Check if a receipt was issued
9099cab3 2803 $receipt_date = $this->callAPISuccess('Contribution', 'getvalue', ['id' => $contribution['id'], 'return' => 'receipt_date']);
e2ca457d
KG
2804 $this->assertEquals('', $receipt_date);
2805 }
3b8c739e 2806
d891a273 2807 /**
2808 * Test that $is_recur is assigned to the receipt.
2809 */
2810 public function testCompleteTransactionForRecurring() {
2811
2812 $this->swapMessageTemplateForTestTemplate();
2813 $recurring = $this->setUpRecurringContribution();
9099cab3 2814 $contributionPage = $this->createReceiptableContributionPage(['is_recur' => TRUE, 'recur_frequency_unit' => 'month', 'recur_interval' => 1]);
d891a273 2815
2816 $this->_params['contribution_page_id'] = $contributionPage['id'];
2817 $this->_params['contribution_recur_id'] = $recurring['id'];
2818
2819 $contribution = $this->setUpForCompleteTransaction();
2820
9099cab3 2821 $this->callAPISuccess('contribution', 'completetransaction', [
d891a273 2822 'id' => $contribution['id'],
2823 'trxn_date' => date('2011-04-09'),
2824 'trxn_id' => 'kazam',
2825 'is_email_receipt' => 1,
9099cab3 2826 ]);
d891a273 2827
9099cab3 2828 $this->mut->checkMailLog([
d891a273 2829 'is_recur:::1',
0131a2ce 2830 'cancelSubscriptionUrl:::' . CIVICRM_UF_BASEURL,
9099cab3 2831 ]);
d891a273 2832 $this->mut->stop();
2833 $this->revertTemplateToReservedTemplate();
2834 }
39b959db 2835
55df1211
AS
2836 /**
2837 * CRM-19710 - Test to ensure that completetransaction respects the input for is_email_receipt setting.
2838 *
2839 * If passed in it will override the default from contribution page.
2840 */
2841 public function testCompleteTransactionWithEmailReceiptInputTrue() {
2842 $mut = new CiviMailUtils($this, TRUE);
2843 $this->createLoggedInUser();
2844 // Create a Contribution Page with is_email_receipt = FALSE
9099cab3 2845 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', [
55df1211
AS
2846 'receipt_from_name' => 'Mickey Mouse',
2847 'receipt_from_email' => 'mickey@mouse.com',
2848 'title' => "Test Contribution Page",
2849 'financial_type_id' => 1,
2850 'currency' => 'CAD',
2851 'is_monetary' => TRUE,
2852 'is_email_receipt' => 0,
9099cab3 2853 ]);
55df1211 2854 $this->_params['contribution_page_id'] = $contributionPage['id'];
9099cab3 2855 $params = array_merge($this->_params, ['contribution_status_id' => 2, 'receipt_date' => 'now']);
55df1211
AS
2856 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2857 // Complete the transaction overriding is_email_receipt to = TRUE
9099cab3 2858 $this->callAPISuccess('contribution', 'completetransaction', [
55df1211
AS
2859 'id' => $contribution['id'],
2860 'is_email_receipt' => 1,
9099cab3
CW
2861 ]);
2862 $mut->checkMailLog([
e2481819 2863 'Contribution Information',
9099cab3 2864 ]);
55df1211
AS
2865 $mut->stop();
2866 }
d891a273 2867
b80f2ad1
E
2868 /**
2869 * Complete the transaction using the template with all the possible.
2870 */
2871 public function testCompleteTransactionWithTestTemplate() {
2872 $this->swapMessageTemplateForTestTemplate();
ec7e3954 2873 $contribution = $this->setUpForCompleteTransaction();
9099cab3 2874 $this->callAPISuccess('contribution', 'completetransaction', [
b80f2ad1
E
2875 'id' => $contribution['id'],
2876 'trxn_date' => date('2011-04-09'),
2877 'trxn_id' => 'kazam',
9099cab3
CW
2878 ]);
2879 $receive_date = $this->callAPISuccess('Contribution', 'getvalue', ['id' => $contribution['id'], 'return' => 'receive_date']);
2880 $this->mut->checkMailLog([
b80f2ad1
E
2881 'email:::anthony_anderson@civicrm.org',
2882 'is_monetary:::1',
2883 'amount:::100.00',
2884 'currency:::USD',
2885 'receive_date:::' . date('Ymd', strtotime($receive_date)),
2886 'receipt_date:::' . date('Ymd'),
2887 'contributeMode:::notify',
2888 'title:::Contribution',
2889 'displayName:::Mr. Anthony Anderson II',
2890 'trxn_id:::kazam',
ec7e3954 2891 'contactID:::' . $this->_params['contact_id'],
b80f2ad1
E
2892 'contributionID:::' . $contribution['id'],
2893 'financialTypeId:::1',
2894 'financialTypeName:::Donation',
9099cab3 2895 ]);
ec7e3954 2896 $this->mut->stop();
b80f2ad1
E
2897 $this->revertTemplateToReservedTemplate();
2898 }
2899
ec7e3954
E
2900 /**
2901 * Complete the transaction using the template with all the possible.
2902 */
2903 public function testCompleteTransactionContributionPageFromAddress() {
9099cab3 2904 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', [
ec7e3954
E
2905 'receipt_from_name' => 'Mickey Mouse',
2906 'receipt_from_email' => 'mickey@mouse.com',
2907 'title' => "Test Contribution Page",
2908 'financial_type_id' => 1,
2909 'currency' => 'NZD',
2910 'goal_amount' => 50,
2911 'is_pay_later' => 1,
2912 'is_monetary' => TRUE,
2913 'is_email_receipt' => TRUE,
9099cab3 2914 ]);
ec7e3954
E
2915 $this->_params['contribution_page_id'] = $contributionPage['id'];
2916 $contribution = $this->setUpForCompleteTransaction();
9099cab3
CW
2917 $this->callAPISuccess('contribution', 'completetransaction', ['id' => $contribution['id']]);
2918 $this->mut->checkMailLog([
ec7e3954
E
2919 'mickey@mouse.com',
2920 'Mickey Mouse <',
9099cab3 2921 ]);
ec7e3954
E
2922 $this->mut->stop();
2923 }
2924
91259407 2925 /**
2926 * Test completing first transaction in a recurring series.
2927 *
2928 * The status should be set to 'in progress' and the next scheduled payment date calculated.
050e11d5 2929 *
2930 * @dataProvider getScheduledDateData
2931 *
2932 * @param array $dataSet
2933 *
2934 * @throws \Exception
91259407 2935 */
050e11d5 2936 public function testCompleteTransactionSetStatusToInProgress($dataSet) {
91259407 2937 $paymentProcessorID = $this->paymentProcessorCreate();
9099cab3 2938 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge([
91259407 2939 'contact_id' => $this->_individualId,
050e11d5 2940 'installments' => '2',
91259407 2941 'frequency_interval' => '1',
2942 'amount' => '500',
2943 'contribution_status_id' => 'Pending',
2944 'start_date' => '2012-01-01 00:00:00',
2945 'currency' => 'USD',
2946 'frequency_unit' => 'month',
2947 'payment_processor_id' => $paymentProcessorID,
9099cab3 2948 ], $dataSet['data']));
91259407 2949 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(
2950 $this->_params,
9099cab3 2951 [
91259407 2952 'contribution_recur_id' => $contributionRecur['id'],
2953 'contribution_status_id' => 'Pending',
050e11d5 2954 'receive_date' => $dataSet['receive_date'],
9099cab3 2955 ])
91259407 2956 );
9099cab3 2957 $this->callAPISuccess('Contribution', 'completetransaction', [
050e11d5 2958 'id' => $contribution,
2959 'receive_date' => $dataSet['receive_date'],
9099cab3
CW
2960 ]);
2961 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', [
91259407 2962 'id' => $contributionRecur['id'],
9099cab3
CW
2963 'return' => ['next_sched_contribution_date', 'contribution_status_id'],
2964 ]);
91259407 2965 $this->assertEquals(5, $contributionRecur['contribution_status_id']);
050e11d5 2966 $this->assertEquals($dataSet['expected'], $contributionRecur['next_sched_contribution_date']);
2967 $this->callAPISuccess('Contribution', 'create', array_merge(
2968 $this->_params,
9099cab3 2969 [
050e11d5 2970 'contribution_recur_id' => $contributionRecur['id'],
2971 'contribution_status_id' => 'Completed',
9099cab3 2972 ]
050e11d5 2973 ));
9099cab3 2974 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', [
050e11d5 2975 'id' => $contributionRecur['id'],
9099cab3
CW
2976 'return' => ['contribution_status_id'],
2977 ]);
050e11d5 2978 $this->assertEquals(1, $contributionRecur['contribution_status_id']);
2979 }
2980
2981 /**
2982 * Get dates for testing.
2983 *
2984 * @return array
2985 */
2986 public function getScheduledDateData() {
9099cab3
CW
2987 $result = [];
2988 $result[]['2016-08-31-1-month'] = [
2989 'data' => [
050e11d5 2990 'start_date' => '2016-08-31',
2991 'frequency_interval' => 1,
2992 'frequency_unit' => 'month',
9099cab3 2993 ],
050e11d5 2994 'receive_date' => '2016-08-31',
2995 'expected' => '2016-10-01 00:00:00',
9099cab3
CW
2996 ];
2997 $result[]['2012-01-01-1-month'] = [
2998 'data' => [
050e11d5 2999 'start_date' => '2012-01-01',
3000 'frequency_interval' => 1,
3001 'frequency_unit' => 'month',
9099cab3 3002 ],
050e11d5 3003 'receive_date' => '2012-01-01',
3004 'expected' => '2012-02-01 00:00:00',
9099cab3
CW
3005 ];
3006 $result[]['2012-01-01-1-month'] = [
3007 'data' => [
050e11d5 3008 'start_date' => '2012-01-01',
3009 'frequency_interval' => 1,
3010 'frequency_unit' => 'month',
9099cab3 3011 ],
050e11d5 3012 'receive_date' => '2012-02-29',
3013 'expected' => '2012-03-29 00:00:00',
9099cab3
CW
3014 ];
3015 $result['receive_date_includes_time']['2012-01-01-1-month'] = [
3016 'data' => [
46f459f2 3017 'start_date' => '2012-01-01',
3018 'frequency_interval' => 1,
3019 'frequency_unit' => 'month',
3020 'next_sched_contribution_date' => '2012-02-29',
9099cab3 3021 ],
46f459f2 3022 'receive_date' => '2012-02-29 16:00:00',
3023 'expected' => '2012-03-29 00:00:00',
9099cab3 3024 ];
050e11d5 3025 return $result;
91259407 3026 }
3027
294cc627 3028 /**
3029 * Test completing a pledge with the completeTransaction api..
3030 *
3031 * Note that we are creating a logged in user because email goes out from
3032 * that person.
3033 */
3034 public function testCompleteTransactionUpdatePledgePayment() {
9f9fa558 3035 $this->swapMessageTemplateForTestTemplate();
294cc627 3036 $mut = new CiviMailUtils($this, TRUE);
3037 $mut->clearMessages();
3038 $this->createLoggedInUser();
3039 $contributionID = $this->createPendingPledgeContribution();
9099cab3 3040 $this->callAPISuccess('contribution', 'completetransaction', [
294cc627 3041 'id' => $contributionID,
3042 'trxn_date' => '1 Feb 2013',
9099cab3
CW
3043 ]);
3044 $pledge = $this->callAPISuccessGetSingle('Pledge', [
294cc627 3045 'id' => $this->_ids['pledge'],
9099cab3 3046 ]);
294cc627 3047 $this->assertEquals('Completed', $pledge['pledge_status']);
3048
9099cab3 3049 $status = $this->callAPISuccessGetValue('PledgePayment', [
294cc627 3050 'pledge_id' => $this->_ids['pledge'],
3051 'return' => 'status_id',
9099cab3 3052 ]);
294cc627 3053 $this->assertEquals(1, $status);
9099cab3 3054 $mut->checkMailLog([
9f9fa558
EM
3055 'amount:::500.00',
3056 'receive_date:::20130201000000',
76e8d9c4 3057 "receipt_date:::\n",
9099cab3 3058 ]);
294cc627 3059 $mut->stop();
9f9fa558 3060 $this->revertTemplateToReservedTemplate();
294cc627 3061 }
3062
0efa8efe 3063 /**
eceb18cc 3064 * Test completing a transaction with an event via the API.
0efa8efe 3065 *
3066 * Note that we are creating a logged in user because email goes out from
3067 * that person
ed7e2e99 3068 *
3069 * @throws \CRM_Core_Exception
0efa8efe 3070 */
00be9182 3071 public function testCompleteTransactionWithParticipantRecord() {
5896d037 3072 $mut = new CiviMailUtils($this, TRUE);
0efa8efe 3073 $mut->clearMessages();
71acd4bf 3074 $this->_individualId = $this->createLoggedInUser();
0efa8efe 3075 $contributionID = $this->createPendingParticipantContribution();
ed7e2e99 3076 $this->createJoinedProfile(['entity_id' => $this->_ids['event']['test'], 'entity_table' => 'civicrm_event']);
f736929b 3077 $this->createJoinedProfile(['entity_id' => $this->_ids['event']['test'], 'entity_table' => 'civicrm_event', 'weight' => 2], ['name' => 'post_1', 'title' => 'title_post_2', 'frontend_title' => 'public 2']);
3078 $this->createJoinedProfile(['entity_id' => $this->_ids['event']['test'], 'entity_table' => 'civicrm_event', 'weight' => 3], ['name' => 'post_2', 'title' => 'title_post_3', 'frontend_title' => 'public 3']);
3079 $this->eliminateUFGroupOne();
ed7e2e99 3080
9099cab3 3081 $this->callAPISuccess('contribution', 'completetransaction', [
39b959db 3082 'id' => $contributionID,
9099cab3 3083 ]
0efa8efe 3084 );
9099cab3 3085 $participantStatus = $this->callAPISuccessGetValue('participant', [
92915c55
TO
3086 'id' => $this->_ids['participant'],
3087 'return' => 'participant_status_id',
9099cab3 3088 ]);
0efa8efe 3089 $this->assertEquals(1, $participantStatus);
71acd4bf
JP
3090
3091 //Assert only three activities are created.
ed7e2e99 3092 $activities = $this->callAPISuccess('Activity', 'get', [
3093 'contact_id' => $this->_individualId,
3094 ])['values'];
3095
f736929b 3096 $this->assertCount(3, $activities);
71acd4bf 3097 $activityNames = array_count_values(CRM_Utils_Array::collect('activity_name', $activities));
b56b628c 3098 // record two activities before and after completing payment for Event registration
71acd4bf 3099 $this->assertEquals(2, $activityNames['Event Registration']);
b56b628c 3100 // update the original 'Contribution' activity created after completing payment
71acd4bf
JP
3101 $this->assertEquals(1, $activityNames['Contribution']);
3102
9099cab3 3103 $mut->checkMailLog([
0efa8efe 3104 'Annual CiviCRM meet',
3105 'Event',
12ff7379 3106 'This is a confirmation that your registration has been received and your status has been updated to Registered.',
ed7e2e99 3107 'First Name: Logged In',
3108 'Public title',
f736929b 3109 'public 2',
3110 'public 3',
3111 ], ['Back end title', 'title_post_2', 'title_post_3']);
66d3f9be
EM
3112 $mut->stop();
3113 }
3114
3115 /**
eceb18cc 3116 * Test membership is renewed when transaction completed.
66d3f9be 3117 */
00be9182 3118 public function testCompleteTransactionMembershipPriceSet() {
66d3f9be 3119 $this->createPriceSetWithPage('membership');
9099cab3 3120 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', [
4ff927bc 3121 'name' => 'Grace',
39b959db 3122 'return' => 'id',
9099cab3 3123 ]);
66d3f9be 3124 $this->setUpPendingContribution($this->_ids['price_field_value'][0]);
9099cab3
CW
3125 $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $this->_ids['membership']]);
3126 $logs = $this->callAPISuccess('MembershipLog', 'get', [
4ff927bc 3127 'membership_id' => $this->_ids['membership'],
9099cab3 3128 ]);
4ff927bc 3129 $this->assertEquals(1, $logs['count']);
3130 $this->assertEquals($stateOfGrace, $membership['status_id']);
9099cab3
CW
3131 $this->callAPISuccess('contribution', 'completetransaction', ['id' => $this->_ids['contribution']]);
3132 $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $this->_ids['membership']]);
66d3f9be 3133 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
9099cab3 3134 $this->callAPISuccessGetSingle('LineItem', [
4ff927bc 3135 'entity_id' => $this->_ids['membership'],
3136 'entity_table' => 'civicrm_membership',
9099cab3
CW
3137 ]);
3138 $logs = $this->callAPISuccess('MembershipLog', 'get', [
4ff927bc 3139 'membership_id' => $this->_ids['membership'],
9099cab3 3140 ]);
4ff927bc 3141 $this->assertEquals(2, $logs['count']);
3142 $this->assertNotEquals($stateOfGrace, $logs['values'][2]['status_id']);
98f0683a
JP
3143 //Assert only three activities are created.
3144 $activities = CRM_Activity_BAO_Activity::getContactActivity($this->_ids['contact']);
3145 $this->assertEquals(3, count($activities));
3146 $activityNames = array_flip(CRM_Utils_Array::collect('activity_name', $activities));
3147 $this->assertArrayHasKey('Contribution', $activityNames);
3148 $this->assertArrayHasKey('Membership Signup', $activityNames);
3149 $this->assertArrayHasKey('Change Membership Status', $activityNames);
66d3f9be
EM
3150 $this->cleanUpAfterPriceSets();
3151 }
3152
0f07bb06 3153 /**
3154 * Test if renewal activity is create after changing Pending contribution to Completed via offline
3155 */
3156 public function testPendingToCompleteContribution() {
c77986c0 3157 $this->createPriceSetWithPage('membership');
0f07bb06 3158 $this->setUpPendingContribution($this->_ids['price_field_value'][0]);
9099cab3 3159 $this->callAPISuccess('membership', 'getsingle', ['id' => $this->_ids['membership']]);
66a1e31f 3160 // Case 1: Assert that Membership Signup Activity is created on Pending to Completed Contribution via backoffice
9099cab3 3161 $activity = $this->callAPISuccess('Activity', 'get', [
b6d493f3
MD
3162 'activity_type_id' => 'Membership Signup',
3163 'source_record_id' => $this->_ids['membership'],
3164 'status_id' => 'Scheduled',
9099cab3 3165 ]);
b6d493f3 3166 $this->assertEquals(1, $activity['count']);
0f07bb06 3167
3168 // change pending contribution to completed
3169 $form = new CRM_Contribute_Form_Contribution();
c77986c0 3170
9099cab3 3171 $form->_params = [
0f07bb06 3172 'id' => $this->_ids['contribution'],
3173 'total_amount' => 20,
3174 'net_amount' => 20,
3175 'fee_amount' => 0,
3176 'financial_type_id' => 1,
0f07bb06 3177 'contact_id' => $this->_individualId,
3178 'contribution_status_id' => 1,
3179 'billing_middle_name' => '',
3180 'billing_last_name' => 'Adams',
3181 'billing_street_address-5' => '790L Lincoln St S',
3182 'billing_city-5' => 'Maryknoll',
3183 'billing_state_province_id-5' => 1031,
3184 'billing_postal_code-5' => 10545,
3185 'billing_country_id-5' => 1228,
3186 'frequency_interval' => 1,
3187 'frequency_unit' => 'month',
3188 'installments' => '',
3189 'hidden_AdditionalDetail' => 1,
3190 'hidden_Premium' => 1,
3191 'from_email_address' => '"civi45" <civi45@civicrm.com>',
3192 'receipt_date' => '',
3193 'receipt_date_time' => '',
3194 'payment_processor_id' => $this->paymentProcessorID,
3195 'currency' => 'USD',
3196 'contribution_page_id' => $this->_ids['contribution_page'],
3197 'contribution_mode' => 'membership',
3198 'source' => 'Membership Signup and Renewal',
9099cab3 3199 ];
c77986c0 3200
3201 $form->testSubmit($form->_params, CRM_Core_Action::UPDATE);
3202
66a1e31f 3203 // Case 2: After successful payment for Pending backoffice there are three activities created
b6d493f3 3204 // 2.a Update status of existing Scheduled Membership Signup (created in step 1) to Completed
9099cab3 3205 $activity = $this->callAPISuccess('Activity', 'get', [
767d3e2e 3206 'activity_type_id' => 'Membership Signup',
b6d493f3
MD
3207 'source_record_id' => $this->_ids['membership'],
3208 'status_id' => 'Completed',
9099cab3 3209 ]);
767d3e2e 3210 $this->assertEquals(1, $activity['count']);
b6d493f3 3211 // 2.b Contribution activity created to record successful payment
9099cab3 3212 $activity = $this->callAPISuccess('Activity', 'get', [
b6d493f3 3213 'activity_type_id' => 'Contribution',
767d3e2e 3214 'source_record_id' => $this->_ids['contribution'],
b6d493f3 3215 'status_id' => 'Completed',
9099cab3 3216 ]);
b6d493f3 3217 $this->assertEquals(1, $activity['count']);
fde55343 3218
b6d493f3 3219 // 2.c 'Change membership type' activity created to record Membership status change from Grace to Current
9099cab3 3220 $activity = $this->callAPISuccess('Activity', 'get', [
b6d493f3
MD
3221 'activity_type_id' => 'Change Membership Status',
3222 'source_record_id' => $this->_ids['membership'],
3223 'status_id' => 'Completed',
9099cab3 3224 ]);
d2460a89 3225 $this->assertEquals(1, $activity['count']);
b6d493f3 3226 $this->assertEquals('Status changed from Grace to Current', $activity['values'][$activity['id']]['subject']);
fde55343
JP
3227
3228 //Create another pending contribution for renewal
9099cab3 3229 $contribution = $this->callAPISuccess('contribution', 'create', [
fde55343
JP
3230 'domain_id' => 1,
3231 'contact_id' => $this->_ids['contact'],
3232 'receive_date' => date('Ymd'),
3233 'total_amount' => 20.00,
3234 'financial_type_id' => 1,
3235 'payment_instrument_id' => 'Credit Card',
3236 'non_deductible_amount' => 10.00,
3237 'trxn_id' => 'rdhfi88',
3238 'invoice_id' => 'dofhiewuyr',
3239 'source' => 'SSF',
3240 'contribution_status_id' => 2,
3241 'contribution_page_id' => $this->_ids['contribution_page'],
c77986c0 3242 // We can't rely on contribution api to link line items correctly to membership
3243 'skipLineItem' => TRUE,
9099cab3
CW
3244 'api.membership_payment.create' => ['membership_id' => $this->_ids['membership']],
3245 ]);
fde55343 3246
9099cab3 3247 $this->callAPISuccess('line_item', 'create', [
fde55343
JP
3248 'entity_id' => $contribution['id'],
3249 'entity_table' => 'civicrm_contribution',
3250 'contribution_id' => $contribution['id'],
3251 'price_field_id' => $this->_ids['price_field'][0],
3252 'qty' => 1,
3253 'unit_price' => 20,
3254 'line_total' => 20,
3255 'financial_type_id' => 1,
c77986c0 3256 'price_field_value_id' => $this->_ids['price_field_value']['cont'],
9099cab3
CW
3257 ]);
3258 $this->callAPISuccess('line_item', 'create', [
c77986c0 3259 'entity_id' => $this->_ids['membership'],
3260 'entity_table' => 'civicrm_membership',
3261 'contribution_id' => $contribution['id'],
3262 'price_field_id' => $this->_ids['price_field'][0],
3263 'qty' => 1,
3264 'unit_price' => 20,
3265 'line_total' => 20,
3266 'financial_type_id' => 1,
fde55343 3267 'price_field_value_id' => $this->_ids['price_field_value'][0],
c77986c0 3268 'membership_type_id' => $this->_ids['membership_type'],
9099cab3 3269 ]);
fde55343
JP
3270
3271 //Update it to Failed.
3272 $form->_params['id'] = $contribution['id'];
3273 $form->_params['contribution_status_id'] = 4;
c77986c0 3274
3275 $form->testSubmit($form->_params, CRM_Core_Action::UPDATE);
fde55343 3276 //Existing membership should not get updated to expired.
9099cab3 3277 $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $this->_ids['membership']]);
fde55343 3278 $this->assertNotEquals($membership['status_id'], 4);
0f07bb06 3279 }
3280
66d3f9be 3281 /**
eceb18cc 3282 * Test membership is renewed when transaction completed.
66d3f9be 3283 */
00be9182 3284 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
66d3f9be
EM
3285 $this->createPriceSetWithPage('membership');
3286 $this->setUpPendingContribution($this->_ids['price_field_value'][1]);
9099cab3
CW
3287 $this->callAPISuccess('contribution', 'completetransaction', ['id' => $this->_ids['contribution']]);
3288 $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $this->_ids['membership']]);
66d3f9be
EM
3289 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
3290 $this->cleanUpAfterPriceSets();
3291 }
3292
00be9182 3293 public function cleanUpAfterPriceSets() {
1cf3c2b1 3294 $this->quickCleanUpFinancialEntities();
66d3f9be 3295 $this->contactDelete($this->_ids['contact']);
66d3f9be
EM
3296 }
3297
66d3f9be 3298 /**
eceb18cc 3299 * Set up a pending transaction with a specific price field id.
1e52837d 3300 *
100fef9d 3301 * @param int $priceFieldValueID
39b959db 3302 * @param array $contriParams
66d3f9be 3303 */
9099cab3 3304 public function setUpPendingContribution($priceFieldValueID, $contriParams = []) {
66d3f9be 3305 $contactID = $this->individualCreate();
9099cab3 3306 $membership = $this->callAPISuccess('membership', 'create', [
66d3f9be
EM
3307 'contact_id' => $contactID,
3308 'membership_type_id' => $this->_ids['membership_type'],
3309 'start_date' => 'yesterday - 1 year',
3310 'end_date' => 'yesterday',
4ff927bc 3311 'join_date' => 'yesterday - 1 year',
9099cab3
CW
3312 ]);
3313 $contribution = $this->callAPISuccess('contribution', 'create', array_merge([
66d3f9be
EM
3314 'domain_id' => 1,
3315 'contact_id' => $contactID,
3316 'receive_date' => date('Ymd'),
0f07bb06 3317 'total_amount' => 20.00,
66d3f9be
EM
3318 'financial_type_id' => 1,
3319 'payment_instrument_id' => 'Credit Card',
3320 'non_deductible_amount' => 10.00,
98f0683a
JP
3321 'trxn_id' => 'jdhfi' . rand(1, 100),
3322 'invoice_id' => 'djfhiew' . rand(5, 100),
66d3f9be
EM
3323 'source' => 'SSF',
3324 'contribution_status_id' => 2,
3325 'contribution_page_id' => $this->_ids['contribution_page'],
9099cab3
CW
3326 'api.membership_payment.create' => ['membership_id' => $membership['id']],
3327 ], $contriParams));
66d3f9be 3328
9099cab3 3329 $this->callAPISuccess('line_item', 'create', [
66d3f9be
EM
3330 'entity_id' => $contribution['id'],
3331 'entity_table' => 'civicrm_contribution',
1274acef 3332 'contribution_id' => $contribution['id'],
66d3f9be
EM
3333 'price_field_id' => $this->_ids['price_field'][0],
3334 'qty' => 1,
3335 'unit_price' => 20,
3336 'line_total' => 20,
3337 'financial_type_id' => 1,
3338 'price_field_value_id' => $priceFieldValueID,
9099cab3 3339 ]);
66d3f9be
EM
3340 $this->_ids['contact'] = $contactID;
3341 $this->_ids['contribution'] = $contribution['id'];
3342 $this->_ids['membership'] = $membership['id'];
0efa8efe 3343 }
3344
2f45e1c2 3345 /**
eceb18cc 3346 * Test sending a mail via the API.
450dfb41 3347 *
3348 * @throws \CRM_Core_Exception
6a488035 3349 */
00be9182 3350 public function testSendMail() {
5896d037 3351 $mut = new CiviMailUtils($this, TRUE);
6c6e6187 3352 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
9099cab3 3353 $this->callAPISuccess('contribution', 'sendconfirmation', [
39b959db
SL
3354 'id' => $contribution['id'],
3355 'receipt_from_email' => 'api@civicrm.org',
9099cab3
CW
3356 ]);
3357 $mut->checkMailLog([
39b959db
SL
3358 '$ 100.00',
3359 'Contribution Information',
9099cab3 3360 ], [
39b959db 3361 'Event',
9099cab3 3362 ]);
128d44a1 3363
3364 $this->checkCreditCardDetails($mut, $contribution['id']);
6a488035
TO
3365 $mut->stop();
3366 }
3367
dbacb875
AS
3368 /**
3369 * Test sending a mail via the API.
3370 * This simulates webform_civicrm using pay later contribution page
3371 */
3372 public function testSendconfirmationPayLater() {
3373 $mut = new CiviMailUtils($this, TRUE);
3374
3375 // Create contribution page
9099cab3 3376 $pageParams = [
dbacb875
AS
3377 'title' => 'Webform Contributions',
3378 'financial_type_id' => 1,
3379 'contribution_type_id' => 1,
3380 'is_confirm_enabled' => 1,
3381 'is_pay_later' => 1,
3382 'pay_later_text' => 'I will send payment by cheque',
3383 'pay_later_receipt' => 'Send your cheque payable to "CiviCRM LLC" to the office',
9099cab3 3384 ];
dbacb875
AS
3385 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $pageParams);
3386
3387 // Create pay later contribution
9099cab3 3388 $contribParams = [
dbacb875
AS
3389 'contact_id' => $this->_individualId,
3390 'financial_type_id' => 1,
3391 'is_pay_later' => 1,
3392 'contribution_status_id' => 2,
3393 'contribution_page_id' => $contributionPage['id'],
3394 'total_amount' => '10.00',
9099cab3 3395 ];
dbacb875
AS
3396 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
3397
3398 // Create line item
9099cab3 3399 $lineItemParams = [
dbacb875
AS
3400 'contribution_id' => $contribution['id'],
3401 'entity_id' => $contribution['id'],
3402 'entity_table' => 'civicrm_contribution',
3403 'label' => 'My lineitem label',
3404 'qty' => 1,
3405 'unit_price' => "10.00",
3406 'line_total' => "10.00",
9099cab3 3407 ];
dbacb875
AS
3408 $lineItem = $this->callAPISuccess('lineItem', 'create', $lineItemParams);
3409
3410 // Create email
3411 try {
9099cab3 3412 civicrm_api3('contribution', 'sendconfirmation', [
39b959db
SL
3413 'id' => $contribution['id'],
3414 'receipt_from_email' => 'api@civicrm.org',
9099cab3 3415 ]);
717fdb8a
AS
3416 }
3417 catch (Exception $e) {
dbacb875
AS
3418 // Need to figure out how to stop this some other day
3419 // We don't care about the Payment Processor because this is Pay Later
3420 // The point of this test is to check we get the pay_later version of the mail
3421 if ($e->getMessage() != "Undefined variable: CRM16923AnUnreliableMethodHasBeenUserToDeterminePaymentProcessorFromContributionPage") {
3422 throw $e;
3423 }
3424 }
3425
3426 // Retrieve mail & check it has the pay_later_receipt info
3427 $mut->getMostRecentEmail('raw');
9099cab3 3428 $mut->checkMailLog([
39b959db
SL
3429 (string) $contribParams['total_amount'],
3430 $pageParams['pay_later_receipt'],
9099cab3 3431 ], [
39b959db 3432 'Event',
9099cab3 3433 ]);
dbacb875
AS
3434 $mut->stop();
3435 }
3436
128d44a1 3437 /**
3438 * Check credit card details in sent mail via API
3439 *
450dfb41 3440 * @param CiviMailUtils $mut
128d44a1 3441 * @param int $contributionID Contribution ID
3442 *
450dfb41 3443 * @throws \CRM_Core_Exception
128d44a1 3444 */
3445 public function checkCreditCardDetails($mut, $contributionID) {
450dfb41 3446 $this->callAPISuccess('contribution', 'create', $this->_params);
9099cab3 3447 $this->callAPISuccess('contribution', 'sendconfirmation', [
39b959db
SL
3448 'id' => $contributionID,
3449 'receipt_from_email' => 'api@civicrm.org',
3450 'payment_processor_id' => $this->paymentProcessorID,
9099cab3
CW
3451 ]);
3452 $mut->checkMailLog([
39b959db
SL
3453 // credit card header
3454 'Credit Card Information',
3455 // billing header
3456 'Billing Name and Address',
3457 // billing name
3458 'anthony_anderson@civicrm.org',
9099cab3 3459 ], [
39b959db 3460 'Event',
9099cab3 3461 ]);
128d44a1 3462 }
3463
2f45e1c2 3464 /**
eceb18cc 3465 * Test sending a mail via the API.
6a488035 3466 */
00be9182 3467 public function testSendMailEvent() {
5896d037 3468 $mut = new CiviMailUtils($this, TRUE);
6c6e6187 3469 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
9099cab3 3470 $event = $this->eventCreate([
6a488035
TO
3471 'is_email_confirm' => 1,
3472 'confirm_from_email' => 'test@civicrm.org',
9099cab3 3473 ]);
6a488035 3474 $this->_eventID = $event['id'];
9099cab3 3475 $participantParams = [
6a488035
TO
3476 'contact_id' => $this->_individualId,
3477 'event_id' => $this->_eventID,
3478 'status_id' => 1,
3479 'role_id' => 1,
3480 // to ensure it matches later on
3481 'register_date' => '2007-07-21 00:00:00',
3482 'source' => 'Online Event Registration: API Testing',
4ab7d517 3483
9099cab3 3484 ];
2f45e1c2 3485 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
9099cab3 3486 $this->callAPISuccess('participant_payment', 'create', [
6a488035
TO
3487 'participant_id' => $participant['id'],
3488 'contribution_id' => $contribution['id'],
9099cab3
CW
3489 ]);
3490 $this->callAPISuccess('contribution', 'sendconfirmation', [
39b959db
SL
3491 'id' => $contribution['id'],
3492 'receipt_from_email' => 'api@civicrm.org',
9099cab3 3493 ]);
6a488035 3494
9099cab3 3495 $mut->checkMailLog([
39b959db
SL
3496 'Annual CiviCRM meet',
3497 'Event',
3498 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
9099cab3 3499 ], []);
6a488035
TO
3500 $mut->stop();
3501 }
3502
4302618d 3503 /**
1e52837d
EM
3504 * This function does a GET & compares the result against the $params.
3505 *
3506 * Use as a double check on Creates.
3507 *
3508 * @param array $params
3509 * @param int $id
67f947ac 3510 * @param bool $delete
6c6e6187 3511 */
1e52837d 3512 public function contributionGetnCheck($params, $id, $delete = TRUE) {
6a488035 3513
9099cab3 3514 $contribution = $this->callAPISuccess('Contribution', 'Get', [
6a488035 3515 'id' => $id,
9099cab3 3516 ]);
6a488035
TO
3517
3518 if ($delete) {
9099cab3 3519 $this->callAPISuccess('contribution', 'delete', ['id' => $id]);
6a488035 3520 }
2bfae985 3521 $this->assertAPISuccess($contribution, 0);
6a488035
TO
3522 $values = $contribution['values'][$contribution['id']];
3523 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
3524 // this is not returned in id format
3525 unset($params['payment_instrument_id']);
3526 $params['contribution_source'] = $params['source'];
3527 unset($params['source']);
3528 foreach ($params as $key => $value) {
22f80e87 3529 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
6a488035
TO
3530 }
3531 }
3532
294cc627 3533 /**
3534 * Create a pending contribution & linked pending pledge record.
3535 */
3536 public function createPendingPledgeContribution() {
3537
9099cab3 3538 $pledgeID = $this->pledgeCreate(['contact_id' => $this->_individualId, 'installments' => 1, 'amount' => 500]);
294cc627 3539 $this->_ids['pledge'] = $pledgeID;
9099cab3 3540 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params, [
294cc627 3541 'contribution_status_id' => 'Pending',
39b959db 3542 'total_amount' => 500,
9099cab3
CW
3543 ]));
3544 $paymentID = $this->callAPISuccessGetValue('PledgePayment', [
3545 'options' => ['limit' => 1],
294cc627 3546 'return' => 'id',
9099cab3
CW
3547 ]);
3548 $this->callAPISuccess('PledgePayment', 'create', [
294cc627 3549 'id' => $paymentID,
3550 'contribution_id' =>
3551 $contribution['id'],
3552 'status_id' => 'Pending',
3553 'scheduled_amount' => 500,
9099cab3 3554 ]);
294cc627 3555
3556 return $contribution['id'];
3557 }
3558
0efa8efe 3559 /**
1e52837d 3560 * Create a pending contribution & linked pending participant record (along with an event).
0efa8efe 3561 */
5896d037 3562 public function createPendingParticipantContribution() {
ed7e2e99 3563 $this->_ids['event']['test'] = $this->eventCreate(['is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'])['id'];
3564 $participantID = $this->participantCreate(['event_id' => $this->_ids['event']['test'], 'status_id' => 6, 'contact_id' => $this->_individualId]);
5896d037 3565 $this->_ids['participant'] = $participantID;
9099cab3 3566 $params = array_merge($this->_params, ['contact_id' => $this->_individualId, 'contribution_status_id' => 2, 'financial_type_id' => 'Event Fee']);
6c6e6187 3567 $contribution = $this->callAPISuccess('contribution', 'create', $params);
9099cab3 3568 $this->callAPISuccess('participant_payment', 'create', [
92915c55
TO
3569 'contribution_id' => $contribution['id'],
3570 'participant_id' => $participantID,
9099cab3
CW
3571 ]);
3572 $this->callAPISuccess('line_item', 'get', [
0efa8efe 3573 'entity_id' => $contribution['id'],
3574 'entity_table' => 'civicrm_contribution',
9099cab3 3575 'api.line_item.create' => [
0efa8efe 3576 'entity_id' => $participantID,
3577 'entity_table' => 'civicrm_participant',
9099cab3
CW
3578 ],
3579 ]);
0efa8efe 3580 return $contribution['id'];
3581 }
3582
4cbe18b8 3583 /**
1e52837d
EM
3584 * Get financial transaction amount.
3585 *
100fef9d 3586 * @param int $contId
4cbe18b8
EM
3587 *
3588 * @return null|string
f4d89200 3589 */
2da40d21 3590 public function _getFinancialTrxnAmount($contId) {
6c6e6187 3591 $query = "SELECT
6a488035
TO
3592 SUM( ft.total_amount ) AS total
3593 FROM civicrm_financial_trxn AS ft
3594 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
3595 WHERE ceft.entity_table = 'civicrm_contribution'
3596 AND ceft.entity_id = {$contId}";
3597
6c6e6187
TO
3598 $result = CRM_Core_DAO::singleValueQuery($query);
3599 return $result;
3600 }
6a488035 3601
4cbe18b8 3602 /**
100fef9d 3603 * @param int $contId
4cbe18b8
EM
3604 *
3605 * @return null|string
f4d89200 3606 */
2da40d21 3607 public function _getFinancialItemAmount($contId) {
6c6e6187
TO
3608 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
3609 $query = "SELECT
6a488035
TO
3610 SUM(amount)
3611 FROM civicrm_financial_item
3612 WHERE entity_table = 'civicrm_line_item'
3613 AND entity_id = {$lineItem}";
6c6e6187
TO
3614 $result = CRM_Core_DAO::singleValueQuery($query);
3615 return $result;
3616 }
6a488035 3617
4cbe18b8 3618 /**
100fef9d 3619 * @param int $contId
4cbe18b8
EM
3620 * @param $context
3621 */
00be9182 3622 public function _checkFinancialItem($contId, $context) {
6c6e6187 3623 if ($context != 'paylater') {
9099cab3 3624 $params = [
5896d037
TO
3625 'entity_id' => $contId,
3626 'entity_table' => 'civicrm_contribution',
9099cab3 3627 ];
6c6e6187 3628 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($params, TRUE));
9099cab3 3629 $entityParams = [
6a488035
TO
3630 'financial_trxn_id' => $trxn['financial_trxn_id'],
3631 'entity_table' => 'civicrm_financial_item',
9099cab3 3632 ];
6c6e6187 3633 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
9099cab3 3634 $params = [
6a488035 3635 'id' => $entityTrxn['entity_id'],
9099cab3 3636 ];
6c6e6187
TO
3637 }
3638 if ($context == 'paylater') {
3639 $lineItems = CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution');
3640 foreach ($lineItems as $key => $item) {
9099cab3 3641 $params = [
5896d037
TO
3642 'entity_id' => $key,
3643 'entity_table' => 'civicrm_line_item',
9099cab3
CW
3644 ];
3645 $compareParams = ['status_id' => 1];
6c6e6187
TO
3646 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3647 }
3648 }
3649 elseif ($context == 'refund') {
9099cab3 3650 $compareParams = [
5896d037
TO
3651 'status_id' => 1,
3652 'financial_account_id' => 1,
3653 'amount' => -100,
9099cab3 3654 ];
6c6e6187
TO
3655 }
3656 elseif ($context == 'cancelPending') {
9099cab3 3657 $compareParams = [
5896d037
TO
3658 'status_id' => 3,
3659 'financial_account_id' => 1,
3660 'amount' => -100,
9099cab3 3661 ];
6c6e6187
TO
3662 }
3663 elseif ($context == 'changeFinancial') {
3664 $lineKey = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
9099cab3 3665 $params = [
5896d037
TO
3666 'entity_id' => $lineKey,
3667 'amount' => -100,
9099cab3
CW
3668 ];
3669 $compareParams = [
5896d037 3670 'financial_account_id' => 1,
9099cab3 3671 ];
6c6e6187 3672 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
9099cab3 3673 $params = [
5896d037
TO
3674 'financial_account_id' => 3,
3675 'entity_id' => $lineKey,
9099cab3
CW
3676 ];
3677 $compareParams = [
5896d037 3678 'amount' => 100,
9099cab3 3679 ];
6c6e6187
TO
3680 }
3681 if ($context != 'paylater') {
3682 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3683 }
3684 }
6a488035 3685
b0e806fa 3686 /**
3687 * Check correct financial transaction entries were created for the change in payment instrument.
3688 *
3689 * @param int $contributionID
3690 * @param int $originalInstrumentID
3691 * @param int $newInstrumentID
39b959db 3692 * @param int $amount
b0e806fa 3693 */
3694 public function checkFinancialTrxnPaymentInstrumentChange($contributionID, $originalInstrumentID, $newInstrumentID, $amount = 100) {
3695
3696 $entityFinancialTrxns = $this->getFinancialTransactionsForContribution($contributionID);
3697
9099cab3 3698 $originalTrxnParams = [
b0e806fa 3699 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($originalInstrumentID),
3700 'payment_instrument_id' => $originalInstrumentID,
3701 'amount' => $amount,
3702 'status_id' => 1,
9099cab3 3703 ];
b0e806fa 3704
9099cab3 3705 $reversalTrxnParams = [
b0e806fa 3706 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($originalInstrumentID),
3707 'payment_instrument_id' => $originalInstrumentID,
3708 'amount' => -$amount,
3709 'status_id' => 1,
9099cab3 3710 ];
b0e806fa 3711
9099cab3 3712 $newTrxnParams = [
b0e806fa 3713 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($newInstrumentID),
3714 'payment_instrument_id' => $newInstrumentID,
3715 'amount' => $amount,
3716 'status_id' => 1,
9099cab3 3717 ];
b0e806fa 3718
9099cab3 3719 foreach ([$originalTrxnParams, $reversalTrxnParams, $newTrxnParams] as $index => $transaction) {
b0e806fa 3720 $entityFinancialTrxn = $entityFinancialTrxns[$index];
3721 $this->assertEquals($entityFinancialTrxn['amount'], $transaction['amount']);
3722
9099cab3 3723 $financialTrxn = $this->callAPISuccessGetSingle('FinancialTrxn', [
b0e806fa 3724 'id' => $entityFinancialTrxn['financial_trxn_id'],
9099cab3 3725 ]);
b0e806fa 3726 $this->assertEquals($transaction['status_id'], $financialTrxn['status_id']);
3727 $this->assertEquals($transaction['amount'], $financialTrxn['total_amount']);
3728 $this->assertEquals($transaction['amount'], $financialTrxn['net_amount']);
3729 $this->assertEquals(0, $financialTrxn['fee_amount']);
3730 $this->assertEquals($transaction['payment_instrument_id'], $financialTrxn['payment_instrument_id']);
3731 $this->assertEquals($transaction['to_financial_account_id'], $financialTrxn['to_financial_account_id']);
3732
3733 // Generic checks.
3734 $this->assertEquals(1, $financialTrxn['is_payment']);
3735 $this->assertEquals('USD', $financialTrxn['currency']);
3736 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($financialTrxn['trxn_date'])));
3737 }
3738 }
3739
4cbe18b8 3740 /**
52da5b1e 3741 * Check financial transaction.
3742 *
3743 * @todo break this down into sensible functions - most calls to it only use a few lines out of the big if.
3744 *
4ff927bc 3745 * @param array $contribution
3746 * @param string $context
100fef9d 3747 * @param int $instrumentId
52da5b1e 3748 * @param array $extraParams
4cbe18b8 3749 */
9099cab3 3750 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL, $extraParams = []) {
b0e806fa 3751 $financialTrxns = $this->getFinancialTransactionsForContribution($contribution['id']);
3752 $trxn = array_pop($financialTrxns);
3753
9099cab3 3754 $params = [
5896d037 3755 'id' => $trxn['financial_trxn_id'],
9099cab3 3756 ];
6c6e6187 3757 if ($context == 'payLater') {
9099cab3 3758 $compareParams = [
5896d037 3759 'status_id' => 1,
876b8ab0 3760 'from_financial_account_id' => CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($contribution['financial_type_id'], 'Accounts Receivable Account is'),
9099cab3 3761 ];
6c6e6187
TO
3762 }
3763 elseif ($context == 'refund') {
9099cab3 3764 $compareParams = [
5896d037
TO
3765 'to_financial_account_id' => 6,
3766 'total_amount' => -100,
3767 'status_id' => 7,
b7990bb6 3768 'trxn_date' => '2015-01-01 09:00:00',
797d4c52 3769 'trxn_id' => 'the refund',
9099cab3 3770 ];
6c6e6187
TO
3771 }
3772 elseif ($context == 'cancelPending') {
9099cab3 3773 $compareParams = [
ed4d0aea 3774 'to_financial_account_id' => 7,
5896d037
TO
3775 'total_amount' => -100,
3776 'status_id' => 3,
9099cab3 3777 ];
6c6e6187
TO
3778 }
3779 elseif ($context == 'changeFinancial' || $context == 'paymentInstrument') {
b0e806fa 3780 // @todo checkFinancialTrxnPaymentInstrumentChange instead for paymentInstrument.
3781 // It does the same thing with greater readability.
3782 // @todo remove handling for
3783
9099cab3 3784 $entityParams = [
5896d037
TO
3785 'entity_id' => $contribution['id'],
3786 'entity_table' => 'civicrm_contribution',
3787 'amount' => -100,
9099cab3 3788 ];
6c6e6187 3789 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
9099cab3 3790 $trxnParams1 = [
6a488035 3791 'id' => $trxn['financial_trxn_id'],
9099cab3 3792 ];
122250ec 3793 if (empty($extraParams)) {
9099cab3 3794 $compareParams = [
122250ec
SL
3795 'total_amount' => -100,
3796 'status_id' => 1,
9099cab3 3797 ];
122250ec
SL
3798 }
3799 else {
9099cab3 3800 $compareParams = [
122250ec
SL
3801 'total_amount' => 100,
3802 'status_id' => 1,
9099cab3 3803 ];
122250ec 3804 }
6c6e6187
TO
3805 if ($context == 'paymentInstrument') {
3806 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($instrumentId);
3807 $compareParams['payment_instrument_id'] = $instrumentId;
3808 }
3809 else {
3810 $compareParams['to_financial_account_id'] = 12;
3811 }
5ca657dd 3812 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, array_merge($compareParams, $extraParams));
3813 $compareParams['total_amount'] = 100;
6c6e6187
TO
3814 }
3815
797d4c52 3816 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, array_merge($compareParams, $extraParams));
6c6e6187 3817 }
6a488035 3818
4cbe18b8
EM
3819 /**
3820 * @return mixed
3821 */
5896d037 3822 public function _addPaymentInstrument() {
6c6e6187 3823 $gId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
9099cab3 3824 $optionParams = [
5896d037
TO
3825 'option_group_id' => $gId,
3826 'label' => 'Test Card',
3827 'name' => 'Test Card',
3828 'value' => '6',
3829 'weight' => '6',
3830 'is_active' => 1,
9099cab3 3831 ];
6c6e6187
TO
3832 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
3833 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
9099cab3 3834 $financialParams = [
5896d037
TO
3835 'entity_table' => 'civicrm_option_value',
3836 'entity_id' => $optionValue['id'],
3837 'account_relationship' => $relationTypeId,
3838 'financial_account_id' => 7,
9099cab3 3839 ];
db62d3a5 3840 CRM_Financial_BAO_FinancialTypeAccount::add($financialParams);
6c6e6187
TO
3841 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
3842 return $optionValue['values'][$optionValue['id']]['value'];
3843 }
6a488035 3844
02a9c0a4 3845 public function _deletedAddedPaymentInstrument() {
9099cab3 3846 $result = $this->callAPISuccess('OptionValue', 'get', [
02a9c0a4 3847 'option_group_id' => 'payment_instrument',
3848 'name' => 'Test Card',
3849 'value' => '6',
3850 'is_active' => 1,
9099cab3 3851 ]);
02a9c0a4 3852 if ($id = CRM_Utils_Array::value('id', $result)) {
9099cab3 3853 $this->callAPISuccess('OptionValue', 'delete', ['id' => $id]);
02a9c0a4 3854 }
3855 }
3856
3c49d90c 3857 /**
3858 * Set up the basic recurring contribution for tests.
3859 *
3860 * @param array $generalParams
3861 * Parameters that can be merged into the recurring AND the contribution.
7f4ef731 3862 *
3863 * @param array $recurParams
3864 * Parameters to merge into the recur only.
3c49d90c 3865 *
3866 * @return array|int
3867 */
9099cab3
CW
3868 protected function setUpRecurringContribution($generalParams = [], $recurParams = []) {
3869 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge([
3c49d90c 3870 'contact_id' => $this->_individualId,
3871 'installments' => '12',
3872 'frequency_interval' => '1',
3873 'amount' => '100',
3874 'contribution_status_id' => 1,
3875 'start_date' => '2012-01-01 00:00:00',
3876 'currency' => 'USD',
3877 'frequency_unit' => 'month',
3878 'payment_processor_id' => $this->paymentProcessorID,
9099cab3 3879 ], $generalParams, $recurParams));
3c49d90c 3880 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3881 $this->_params,
9099cab3 3882 [
3c49d90c 3883 'contribution_recur_id' => $contributionRecur['id'],
9099cab3 3884 ], $generalParams)
3c49d90c 3885 );
3886 return $originalContribution;
3887 }
3888
d2334242
PH
3889 /**
3890 * Set up a basic auto-renew membership for tests.
3891 *
3892 * @param array $generalParams
3893 * Parameters that can be merged into the recurring AND the contribution.
3894 *
3895 * @param array $recurParams
3896 * Parameters to merge into the recur only.
3897 *
3898 * @return array|int
3899 */
9099cab3
CW
3900 protected function setUpAutoRenewMembership($generalParams = [], $recurParams = []) {
3901 $newContact = $this->callAPISuccess('Contact', 'create', [
39b959db
SL
3902 'contact_type' => 'Individual',
3903 'sort_name' => 'McTesterson, Testy',
3904 'display_name' => 'Testy McTesterson',
3905 'preferred_language' => 'en_US',
3906 'preferred_mail_format' => 'Both',
3907 'first_name' => 'Testy',
3908 'last_name' => 'McTesterson',
3909 'contact_is_deleted' => '0',
3910 'email_id' => '4',
3911 'email' => 'tmctesterson@example.com',
3912 'on_hold' => '0',
9099cab3
CW
3913 ]);
3914 $membershipType = $this->callAPISuccess('MembershipType', 'create', [
d2334242
PH
3915 'domain_id' => "Default Domain Name",
3916 'member_of_contact_id' => 1,
3917 'financial_type_id' => "Member Dues",
3918 'duration_unit' => "month",
3919 'duration_interval' => 1,
5b1b8db2 3920 'period_type' => 'rolling',
d2334242
PH
3921 'name' => "Standard Member",
3922 'minimum_fee' => 100,
9099cab3
CW
3923 ]);
3924 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge([
76e80087 3925 'contact_id' => $newContact['id'],
d2334242
PH
3926 'installments' => '12',
3927 'frequency_interval' => '1',
3928 'amount' => '100',
3929 'contribution_status_id' => 1,
3930 'start_date' => '2012-01-01 00:00:00',
3931 'currency' => 'USD',
3932 'frequency_unit' => 'month',
3933 'payment_processor_id' => $this->paymentProcessorID,
9099cab3 3934 ], $generalParams, $recurParams));
7c3f3d59 3935
9099cab3 3936 $membership = $this->callAPISuccess('membership', 'create', [
7c3f3d59 3937 'contact_id' => $newContact['id'],
3938 'contribution_recur_id' => $contributionRecur['id'],
3939 'financial_type_id' => "Member Dues",
3940 'membership_type_id' => $membershipType['id'],
3941 'num_terms' => 1,
3942 'skipLineItem' => TRUE,
9099cab3 3943 ]);
7c3f3d59 3944
3945 CRM_Price_BAO_LineItem::getLineItemArray($this->_params, NULL, 'membership', $membershipType['id']);
d2334242
PH
3946 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3947 $this->_params,
9099cab3 3948 [
d2334242
PH
3949 'contact_id' => $newContact['id'],
3950 'contribution_recur_id' => $contributionRecur['id'],
3951 'financial_type_id' => "Member Dues",
3952 'contribution_status_id' => 1,
3953 'invoice_id' => uniqid(),
9099cab3 3954 ], $generalParams)
d2334242 3955 );
9099cab3 3956 $lineItem = $this->callAPISuccess('LineItem', 'getsingle', []);
7c3f3d59 3957 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
9099cab3
CW
3958 $membership = $this->callAPISuccess('Membership', 'getsingle', ['id' => $lineItem['entity_id']]);
3959 $this->callAPISuccess('LineItem', 'getsingle', []);
3960 $this->callAPISuccessGetCount('MembershipPayment', ['membership_id' => $membership['id']], 1);
d2334242 3961
9099cab3 3962 return [$originalContribution, $membership];
d2334242 3963 }
39b959db 3964
893a550c 3965 /**
3966 * Set up a repeat transaction.
3967 *
3968 * @param array $recurParams
39b959db
SL
3969 * @param mixed $flag
3970 * @param array $contributionParams
893a550c 3971 * @return array
3972 */
9099cab3 3973 protected function setUpRepeatTransaction($recurParams = [], $flag, $contributionParams = []) {
893a550c 3974 $paymentProcessorID = $this->paymentProcessorCreate();
9099cab3 3975 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge([
893a550c 3976 'contact_id' => $this->_individualId,
3977 'installments' => '12',
3978 'frequency_interval' => '1',
3979 'amount' => '500',
3980 'contribution_status_id' => 1,
3981 'start_date' => '2012-01-01 00:00:00',
3982 'currency' => 'USD',
3983 'frequency_unit' => 'month',
3984 'payment_processor_id' => $paymentProcessorID,
9099cab3 3985 ], $recurParams));
0e6ccb2e 3986
7150b1c8 3987 $originalContribution = '';
0e6ccb2e 3988 if ($flag == 'multiple') {
7150b1c8 3989 // CRM-19309 create a contribution + also add in line_items (plural):
19893cf2 3990 $params = array_merge($this->_params, $contributionParams);
0e6ccb2e 3991 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
19893cf2 3992 $params,
9099cab3 3993 [
0e6ccb2e
K
3994 'contribution_recur_id' => $contributionRecur['id'],
3995 'skipLineItem' => 1,
9099cab3
CW
3996 'api.line_item.create' => [
3997 [
0e6ccb2e
K
3998 'price_field_id' => 1,
3999 'qty' => 2,
4000 'line_total' => '20',
4001 'unit_price' => '10',
4002 'financial_type_id' => 1,
9099cab3
CW
4003 ],
4004 [
0e6ccb2e
K
4005 'price_field_id' => 1,
4006 'qty' => 1,
4007 'line_total' => '80',
4008 'unit_price' => '80',
4009 'financial_type_id' => 2,
9099cab3
CW
4010 ],
4011 ],
4012 ]
0e6ccb2e
K
4013 )
4014 );
4015 }
4016 elseif ($flag == 'single') {
9099cab3 4017 $params = array_merge($this->_params, ['contribution_recur_id' => $contributionRecur['id']]);
19893cf2
SL
4018 $params = array_merge($params, $contributionParams);
4019 $originalContribution = $this->callAPISuccess('contribution', 'create', $params);
0e6ccb2e 4020 }
f69a9ac3 4021 $originalContribution['payment_processor_id'] = $paymentProcessorID;
893a550c 4022 return $originalContribution;
4023 }
4024
ec7e3954
E
4025 /**
4026 * Common set up routine.
4027 *
4028 * @return array
4029 */
4030 protected function setUpForCompleteTransaction() {
4031 $this->mut = new CiviMailUtils($this, TRUE);
4032 $this->createLoggedInUser();
9099cab3 4033 $params = array_merge($this->_params, ['contribution_status_id' => 2, 'receipt_date' => 'now']);
ec7e3954
E
4034 $contribution = $this->callAPISuccess('contribution', 'create', $params);
4035 return $contribution;
4036 }
4037
9c01d961
SL
4038 /**
4039 * Test repeat contribution uses the Payment Processor' payment_instrument setting.
4040 */
4041 public function testRepeatTransactionWithNonCreditCardDefault() {
9099cab3 4042 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', [
9c01d961
SL
4043 'contact_id' => $this->_individualId,
4044 'installments' => '12',
4045 'frequency_interval' => '1',
4046 'amount' => '100',
4047 'contribution_status_id' => 1,
4048 'start_date' => '2012-01-01 00:00:00',
4049 'currency' => 'USD',
4050 'frequency_unit' => 'month',
4051 'payment_processor_id' => $this->paymentProcessorID,
9099cab3 4052 ]);
9c01d961
SL
4053 $contribution1 = $this->callAPISuccess('contribution', 'create', array_merge(
4054 $this->_params,
9099cab3 4055 ['contribution_recur_id' => $contributionRecur['id'], 'payment_instrument_id' => 2])
9c01d961
SL
4056 );
4057 $paymentInstruments = CRM_Contribute_PseudoConstant::paymentInstrument('name');
9099cab3 4058 $contribution2 = $this->callAPISuccess('contribution', 'repeattransaction', [
9c01d961
SL
4059 'contribution_status_id' => 'Completed',
4060 'trxn_id' => uniqid(),
4061 'original_contribution_id' => $contribution1,
9099cab3 4062 ]);
9c01d961
SL
4063 $this->assertEquals(array_search('Debit Card', $paymentInstruments), $contribution2['values'][$contribution2['id']]['payment_instrument_id']);
4064 $this->quickCleanUpFinancialEntities();
4065 }
4066
ee63135d 4067 /**
4068 * CRM-20008 Tests repeattransaction creates pending membership.
4069 */
37f29fcf 4070 public function testRepeatTransactionMembershipCreatePendingContribution() {
ee63135d 4071 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
9099cab3 4072 $this->callAPISuccess('membership', 'create', [
ee63135d 4073 'id' => $membership['id'],
4074 'end_date' => 'yesterday',
4075 'status_id' => 'Expired',
9099cab3
CW
4076 ]);
4077 $repeatedContribution = $this->callAPISuccess('contribution', 'repeattransaction', [
ee63135d 4078 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
4079 'contribution_status_id' => 'Pending',
4080 'trxn_id' => uniqid(),
9099cab3
CW
4081 ]);
4082 $membershipStatusId = $this->callAPISuccess('membership', 'getvalue', [
ee63135d 4083 'id' => $membership['id'],
4084 'return' => 'status_id',
9099cab3 4085 ]);
ee63135d 4086
4087 // Let's see if the membership payments got created while we're at it.
9099cab3 4088 $membershipPayments = $this->callAPISuccess('MembershipPayment', 'get', [
37f29fcf 4089 'membership_id' => $membership['id'],
9099cab3 4090 ]);
ee63135d 4091 $this->assertEquals(2, $membershipPayments['count']);
4092
4093 $this->assertEquals('Expired', CRM_Core_PseudoConstant::getLabel('CRM_Member_BAO_Membership', 'status_id', $membershipStatusId));
9099cab3
CW
4094 $this->callAPISuccess('Contribution', 'completetransaction', ['id' => $repeatedContribution['id']]);
4095 $membership = $this->callAPISuccessGetSingle('membership', [
ee63135d 4096 'id' => $membership['id'],
4097 'return' => 'status_id, end_date',
9099cab3 4098 ]);
37f29fcf 4099 $this->assertEquals('New', CRM_Core_PseudoConstant::getName('CRM_Member_BAO_Membership', 'status_id', $membership['status_id']));
ee63135d 4100 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 month')), $membership['end_date']);
4101
4102 $this->quickCleanUpFinancialEntities();
4103 $this->contactDelete($originalContribution['values'][1]['contact_id']);
4104 }
4105
cefed6df
SL
4106 /**
4107 * Test sending a mail via the API.
4108 */
4109 public function testSendMailWithAPISetFromDetails() {
4110 $mut = new CiviMailUtils($this, TRUE);
4111 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
9099cab3 4112 $this->callAPISuccess('contribution', 'sendconfirmation', [
cefed6df
SL
4113 'id' => $contribution['id'],
4114 'receipt_from_email' => 'api@civicrm.org',
4115 'receipt_from_name' => 'CiviCRM LLC',
9099cab3
CW
4116 ]);
4117 $mut->checkMailLog([
39b959db
SL
4118 'From: CiviCRM LLC <api@civicrm.org>',
4119 'Contribution Information',
9099cab3 4120 ], [
39b959db 4121 'Event',
9099cab3 4122 ]);
cefed6df
SL
4123 $mut->stop();
4124 }
4125
4126 /**
4127 * Test sending a mail via the API.
4128 */
4129 public function testSendMailWithNoFromSetFallToDomain() {
4130 $this->createLoggedInUser();
4131 $mut = new CiviMailUtils($this, TRUE);
4132 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
9099cab3 4133 $this->callAPISuccess('contribution', 'sendconfirmation', [
cefed6df 4134 'id' => $contribution['id'],
9099cab3
CW
4135 ]);
4136 $domain = $this->callAPISuccess('domain', 'getsingle', ['id' => 1]);
4137 $mut->checkMailLog([
39b959db
SL
4138 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>',
4139 'Contribution Information',
9099cab3 4140 ], [
39b959db 4141 'Event',
9099cab3 4142 ]);
cefed6df
SL
4143 $mut->stop();
4144 }
4145
4146 /**
4147 * Test sending a mail via the API.
4148 */
4149 public function testSendMailWithRepeatTransactionAPIFalltoDomain() {
4150 $this->createLoggedInUser();
4151 $mut = new CiviMailUtils($this, TRUE);
9099cab3
CW
4152 $contribution = $this->setUpRepeatTransaction([], 'single');
4153 $this->callAPISuccess('contribution', 'repeattransaction', [
cefed6df
SL
4154 'contribution_status_id' => 'Completed',
4155 'trxn_id' => uniqid(),
4156 'original_contribution_id' => $contribution,
9099cab3
CW
4157 ]);
4158 $domain = $this->callAPISuccess('domain', 'getsingle', ['id' => 1]);
4159 $mut->checkMailLog([
39b959db
SL
4160 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>',
4161 'Contribution Information',
9099cab3 4162 ], [
39b959db 4163 'Event',
9099cab3 4164 ]
cefed6df
SL
4165 );
4166 $mut->stop();
4167 }
4168
4169 /**
4170 * Test sending a mail via the API.
4171 */
4172 public function testSendMailWithRepeatTransactionAPIFalltoContributionPage() {
4173 $mut = new CiviMailUtils($this, TRUE);
9099cab3 4174 $contributionPage = $this->contributionPageCreate(['receipt_from_name' => 'CiviCRM LLC', 'receipt_from_email' => 'contributionpage@civicrm.org', 'is_email_receipt' => 1]);
cefed6df 4175 $paymentProcessorID = $this->paymentProcessorCreate();
9099cab3 4176 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', [
cefed6df
SL
4177 'contact_id' => $this->_individualId,
4178 'installments' => '12',
4179 'frequency_interval' => '1',
4180 'amount' => '500',
4181 'contribution_status_id' => 1,
4182 'start_date' => '2012-01-01 00:00:00',
4183 'currency' => 'USD',
4184 'frequency_unit' => 'month',
4185 'payment_processor_id' => $paymentProcessorID,
9099cab3 4186 ]);
cefed6df
SL
4187 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
4188 $this->_params,
9099cab3 4189 [
cefed6df 4190 'contribution_recur_id' => $contributionRecur['id'],
39b959db 4191 'contribution_page_id' => $contributionPage['id'],
9099cab3 4192 ])
cefed6df 4193 );
9099cab3 4194 $this->callAPISuccess('contribution', 'repeattransaction', [
cefed6df
SL
4195 'contribution_status_id' => 'Completed',
4196 'trxn_id' => uniqid(),
4197 'original_contribution_id' => $originalContribution,
9099cab3 4198 ]
cefed6df 4199 );
9099cab3 4200 $mut->checkMailLog([
39b959db
SL
4201 'From: CiviCRM LLC <contributionpage@civicrm.org>',
4202 'Contribution Information',
9099cab3 4203 ], [
39b959db 4204 'Event',
9099cab3 4205 ]);
cefed6df
SL
4206 $mut->stop();
4207 }
4208
4fb4e64f
SL
4209 /**
4210 * Test sending a mail via the API.
4211 */
4212 public function testSendMailWithRepeatTransactionAPIFalltoSystemFromNoDefaultFrom() {
4213 $mut = new CiviMailUtils($this, TRUE);
9099cab3
CW
4214 $originalContribution = $contribution = $this->setUpRepeatTransaction([], 'single');
4215 $fromEmail = $this->CallAPISuccess('optionValue', 'get', ['is_default' => 1, 'option_group_id' => 'from_email_address', 'sequential' => 1]);
4fb4e64f 4216 foreach ($fromEmail['values'] as $from) {
9099cab3 4217 $this->callAPISuccess('optionValue', 'create', ['is_default' => 0, 'id' => $from['id']]);
4fb4e64f 4218 }
9099cab3
CW
4219 $domain = $this->callAPISuccess('domain', 'getsingle', ['id' => CRM_Core_Config::domainID()]);
4220 $this->callAPISuccess('contribution', 'repeattransaction', [
4fb4e64f
SL
4221 'contribution_status_id' => 'Completed',
4222 'trxn_id' => uniqid(),
4223 'original_contribution_id' => $originalContribution,
9099cab3
CW
4224 ]);
4225 $mut->checkMailLog([
39b959db
SL
4226 'From: ' . $domain['name'] . ' <' . $domain['domain_email'] . '>',
4227 'Contribution Information',
9099cab3 4228 ], [
39b959db 4229 'Event',
9099cab3 4230 ]);
4fb4e64f
SL
4231 $mut->stop();
4232 }
4233
d891a273 4234 /**
4235 * Create a Contribution Page with is_email_receipt = TRUE.
4236 *
4237 * @param array $params
4238 * Params to overwrite with.
4239 *
4240 * @return array|int
4241 */
9099cab3
CW
4242 protected function createReceiptableContributionPage($params = []) {
4243 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array_merge([
d891a273 4244 'receipt_from_name' => 'Mickey Mouse',
4245 'receipt_from_email' => 'mickey@mouse.com',
4246 'title' => "Test Contribution Page",
4247 'financial_type_id' => 1,
4248 'currency' => 'CAD',
4249 'is_monetary' => TRUE,
4250 'is_email_receipt' => TRUE,
9099cab3 4251 ], $params));
d891a273 4252 return $contributionPage;
4253 }
4254
121c4616 4255 /**
4256 * function to test card_type and pan truncation.
4257 */
4258 public function testCardTypeAndPanTruncation() {
4259 $creditCardTypeIDs = array_flip(CRM_Financial_DAO_FinancialTrxn::buildOptions('card_type_id'));
4260 $contactId = $this->individualCreate();
9099cab3 4261 $params = [
121c4616 4262 'contact_id' => $contactId,
4263 'receive_date' => '2016-01-20',
4264 'total_amount' => 100,
4265 'financial_type_id' => 1,
4266 'payment_instrument' => 'Credit Card',
4267 'card_type_id' => $creditCardTypeIDs['Visa'],
4268 'pan_truncation' => 4567,
9099cab3 4269 ];
121c4616 4270 $contribution = $this->callAPISuccess('contribution', 'create', $params);
4271 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
4272 $financialTrxn = $this->callAPISuccessGetSingle(
4273 'FinancialTrxn',
9099cab3 4274 [
121c4616 4275 'id' => $lastFinancialTrxnId['financialTrxnId'],
9099cab3
CW
4276 'return' => ['card_type_id', 'pan_truncation'],
4277 ]
121c4616 4278 );
4279 $this->assertEquals(CRM_Utils_Array::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Visa']);
4280 $this->assertEquals(CRM_Utils_Array::value('pan_truncation', $financialTrxn), 4567);
9099cab3 4281 $params = [
121c4616 4282 'id' => $contribution['id'],
4283 'pan_truncation' => 2345,
4284 'card_type_id' => $creditCardTypeIDs['Amex'],
9099cab3 4285 ];
121c4616 4286 $contribution = $this->callAPISuccess('contribution', 'create', $params);
4287 $financialTrxn = $this->callAPISuccessGetSingle(
4288 'FinancialTrxn',
9099cab3 4289 [
121c4616 4290 'id' => $lastFinancialTrxnId['financialTrxnId'],
9099cab3
CW
4291 'return' => ['card_type_id', 'pan_truncation'],
4292 ]
121c4616 4293 );
4294 $this->assertEquals(CRM_Utils_Array::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Amex']);
4295 $this->assertEquals(CRM_Utils_Array::value('pan_truncation', $financialTrxn), 2345);
4296 }
4297
19893cf2
SL
4298 /**
4299 * Test repeat contribution uses non default currency
4300 * @see https://issues.civicrm.org/jira/projects/CRM/issues/CRM-20678
4301 */
4302 public function testRepeatTransactionWithDifferenceCurrency() {
9099cab3
CW
4303 $originalContribution = $this->setUpRepeatTransaction(['currency' => 'AUD'], 'single', ['currency' => 'AUD']);
4304 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', [
19893cf2
SL
4305 'original_contribution_id' => $originalContribution['id'],
4306 'contribution_status_id' => 'Completed',
4307 'trxn_id' => uniqid(),
9099cab3 4308 ]);
19893cf2
SL
4309 $this->assertEquals('AUD', $contribution['values'][$contribution['id']]['currency']);
4310 }
4311
b0e806fa 4312 /**
4313 * Get the financial items for the contribution.
4314 *
4315 * @param int $contributionID
4316 *
4317 * @return array
4318 * Array of associated financial items.
4319 */
4320 protected function getFinancialTransactionsForContribution($contributionID) {
9099cab3 4321 $trxnParams = [
b0e806fa 4322 'entity_id' => $contributionID,
4323 'entity_table' => 'civicrm_contribution',
9099cab3 4324 ];
b0e806fa 4325 // @todo the following function has naming errors & has a weird signature & appears to
4326 // only be called from test classes. Move into test suite & maybe just use api
4327 // from this function.
9099cab3 4328 return array_merge(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($trxnParams, FALSE, []));
b0e806fa 4329 }
4330
1af690c4 4331 /**
4332 * Test getunique api call for Contribution entity
4333 */
4334 public function testContributionGetUnique() {
9099cab3 4335 $result = $this->callAPIAndDocument($this->_entity, 'getunique', [], __FUNCTION__, __FILE__);
1af690c4 4336 $this->assertEquals(2, $result['count']);
9099cab3
CW
4337 $this->assertEquals(['trxn_id'], $result['values']['UI_contrib_trxn_id']);
4338 $this->assertEquals(['invoice_id'], $result['values']['UI_contrib_invoice_id']);
1af690c4 4339 }
4340
d8bd2007
PN
4341 /**
4342 * Test Repeat Transaction Contribution with Tax amount.
4343 * https://lab.civicrm.org/dev/core/issues/806
4344 */
4345 public function testRepeatContributionWithTaxAmount() {
4346 $this->enableTaxAndInvoicing();
4347 $financialType = $this->callAPISuccess('financial_type', 'create', [
4348 'name' => 'Test taxable financial Type',
4349 'is_reserved' => 0,
4350 'is_active' => 1,
4351 ]);
4352 $this->relationForFinancialTypeWithFinancialAccount($financialType['id']);
4353 $contribution = $this->setUpRepeatTransaction(
4354 [],
4355 'single',
4356 [
4357 'financial_type_id' => $financialType['id'],
4358 ]
4359 );
9099cab3 4360 $this->callAPISuccess('contribution', 'repeattransaction', [
d8bd2007
PN
4361 'original_contribution_id' => $contribution['id'],
4362 'contribution_status_id' => 'Completed',
4363 'trxn_id' => uniqid(),
9099cab3 4364 ]);
9583e46d
JP
4365 $payments = $this->callAPISuccess('Contribution', 'get', ['sequential' => 1])['values'];
4366 //Assert if first payment and repeated payment has the same contribution amount.
4367 $this->assertEquals($payments[0]['total_amount'], $payments[1]['total_amount']);
d8bd2007
PN
4368 $this->callAPISuccessGetCount('Contribution', [], 2);
4369 }
4370
a8fdb24e
CW
4371 public function testGetCurrencyOptions() {
4372 $result = $this->callAPISuccess('Contribution', 'getoptions', [
4373 'field' => 'currency',
4374 ]);
4375 $this->assertEquals('US Dollar', $result['values']['USD']);
4376 $this->assertNotContains('$', $result['values']);
4377 $result = $this->callAPISuccess('Contribution', 'getoptions', [
4378 'field' => 'currency',
4379 'context' => "abbreviate",
4380 ]);
4381 $this->assertEquals('$', $result['values']['USD']);
4382 $this->assertNotContains('US Dollar', $result['values']);
4383 }
4384
6a488035 4385}