Add test for uF group one bug
[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.
6a488035 1297 */
00be9182 1298 public function testCreateUpdateContributionRefund() {
9099cab3 1299 $contributionParams = [
6a488035
TO
1300 'contact_id' => $this->_individualId,
1301 'receive_date' => '2012-01-01',
1302 'total_amount' => 100.00,
4ab7d517 1303 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1304 'payment_instrument_id' => 4,
1305 'contribution_status_id' => 1,
797d4c52 1306 'trxn_id' => 'original_payment',
9099cab3 1307 ];
797d4c52 1308 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
9099cab3 1309 $newParams = array_merge($contributionParams, [
39b959db
SL
1310 'id' => $contribution['id'],
1311 'contribution_status_id' => 'Refunded',
1312 'cancel_date' => '2015-01-01 09:00',
1313 'refund_trxn_id' => 'the refund',
9099cab3 1314 ]);
797d4c52 1315
1316 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1317 $this->_checkFinancialTrxn($contribution, 'refund');
1318 $this->_checkFinancialItem($contribution['id'], 'refund');
9099cab3 1319 $this->assertEquals('original_payment', $this->callAPISuccessGetValue('Contribution', [
797d4c52 1320 'id' => $contribution['id'],
1321 'return' => 'trxn_id',
9099cab3 1322 ]));
797d4c52 1323 }
4ab7d517 1324
52da5b1e 1325 /**
1326 * Refund a contribution for a financial type with a contra account.
1327 *
1328 * CRM-17951 the contra account is a financial account with a relationship to a
1329 * financial type. It is not always configured but should be reflected
1330 * in the financial_trxn & financial_item table if it is.
1331 */
1332 public function testCreateUpdateChargebackContributionDefaultAccount() {
1333 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
9099cab3 1334 $this->callAPISuccess('Contribution', 'create', [
52da5b1e 1335 'id' => $contribution['id'],
1336 'contribution_status_id' => 'Chargeback',
9099cab3
CW
1337 ]);
1338 $this->callAPISuccessGetSingle('Contribution', ['contribution_status_id' => 'Chargeback']);
52da5b1e 1339
9099cab3 1340 $lineItems = $this->callAPISuccessGetSingle('LineItem', [
52da5b1e 1341 'contribution_id' => $contribution['id'],
9099cab3
CW
1342 'api.FinancialItem.getsingle' => ['amount' => ['<' => 0]],
1343 ]);
52da5b1e 1344 $this->assertEquals(1, $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
9099cab3 1345 $this->callAPISuccessGetSingle('FinancialTrxn', [
52da5b1e 1346 'total_amount' => -100,
1347 'status_id' => 'Chargeback',
1348 'to_financial_account_id' => 6,
9099cab3 1349 ]);
52da5b1e 1350 }
1351
1352 /**
1353 * Refund a contribution for a financial type with a contra account.
1354 *
1355 * CRM-17951 the contra account is a financial account with a relationship to a
1356 * financial type. It is not always configured but should be reflected
1357 * in the financial_trxn & financial_item table if it is.
1358 */
1359 public function testCreateUpdateChargebackContributionCustomAccount() {
9099cab3 1360 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', [
52da5b1e 1361 'name' => 'Chargeback Account',
1362 'is_active' => TRUE,
9099cab3 1363 ]);
52da5b1e 1364
9099cab3 1365 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', [
52da5b1e 1366 'entity_id' => $this->_financialTypeId,
1367 'entity_table' => 'civicrm_financial_type',
1368 'account_relationship' => 'Chargeback Account is',
1369 'financial_account_id' => 'Chargeback Account',
9099cab3 1370 ]);
52da5b1e 1371
1372 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
9099cab3 1373 $this->callAPISuccess('Contribution', 'create', [
52da5b1e 1374 'id' => $contribution['id'],
1375 'contribution_status_id' => 'Chargeback',
9099cab3
CW
1376 ]);
1377 $this->callAPISuccessGetSingle('Contribution', ['contribution_status_id' => 'Chargeback']);
52da5b1e 1378
9099cab3 1379 $lineItems = $this->callAPISuccessGetSingle('LineItem', [
52da5b1e 1380 'contribution_id' => $contribution['id'],
9099cab3
CW
1381 'api.FinancialItem.getsingle' => ['amount' => ['<' => 0]],
1382 ]);
52da5b1e 1383 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1384
9099cab3
CW
1385 $this->callAPISuccess('Contribution', 'delete', ['id' => $contribution['id']]);
1386 $this->callAPISuccess('EntityFinancialAccount', 'delete', ['id' => $entityFinancialAccount['id']]);
1387 $this->callAPISuccess('FinancialAccount', 'delete', ['id' => $financialAccount['id']]);
52da5b1e 1388 }
1389
bf2cf926 1390 /**
1391 * Refund a contribution for a financial type with a contra account.
1392 *
1393 * CRM-17951 the contra account is a financial account with a relationship to a
1394 * financial type. It is not always configured but should be reflected
1395 * in the financial_trxn & financial_item table if it is.
1396 */
1397 public function testCreateUpdateRefundContributionConfiguredContraAccount() {
9099cab3 1398 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', [
bf2cf926 1399 'name' => 'Refund Account',
1400 'is_active' => TRUE,
9099cab3 1401 ]);
bf2cf926 1402
9099cab3 1403 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', [
bf2cf926 1404 'entity_id' => $this->_financialTypeId,
1405 'entity_table' => 'civicrm_financial_type',
1406 'account_relationship' => 'Credit/Contra Revenue Account is',
1407 'financial_account_id' => 'Refund Account',
9099cab3 1408 ]);
bf2cf926 1409
1410 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
9099cab3 1411 $this->callAPISuccess('Contribution', 'create', [
bf2cf926 1412 'id' => $contribution['id'],
1413 'contribution_status_id' => 'Refunded',
9099cab3 1414 ]);
bf2cf926 1415
9099cab3 1416 $lineItems = $this->callAPISuccessGetSingle('LineItem', [
bf2cf926 1417 'contribution_id' => $contribution['id'],
9099cab3
CW
1418 'api.FinancialItem.getsingle' => ['amount' => ['<' => 0]],
1419 ]);
bf2cf926 1420 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1421
9099cab3
CW
1422 $this->callAPISuccess('Contribution', 'delete', ['id' => $contribution['id']]);
1423 $this->callAPISuccess('EntityFinancialAccount', 'delete', ['id' => $entityFinancialAccount['id']]);
1424 $this->callAPISuccess('FinancialAccount', 'delete', ['id' => $financialAccount['id']]);
bf2cf926 1425 }
1426
797d4c52 1427 /**
1428 * Function tests that trxn_id is set when passed in.
1429 *
1430 * Here we ensure that the civicrm_financial_trxn.trxn_id & the civicrm_contribution.trxn_id are set
1431 * when trxn_id is passed in.
1432 */
1433 public function testCreateUpdateContributionRefundTrxnIDPassedIn() {
9099cab3 1434 $contributionParams = [
797d4c52 1435 'contact_id' => $this->_individualId,
1436 'receive_date' => '2012-01-01',
1437 'total_amount' => 100.00,
1438 'financial_type_id' => $this->_financialTypeId,
1439 'payment_instrument_id' => 4,
1440 'contribution_status_id' => 1,
1441 'trxn_id' => 'original_payment',
9099cab3 1442 ];
797d4c52 1443 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
9099cab3 1444 $newParams = array_merge($contributionParams, [
39b959db
SL
1445 'id' => $contribution['id'],
1446 'contribution_status_id' => 'Refunded',
1447 'cancel_date' => '2015-01-01 09:00',
1448 'trxn_id' => 'the refund',
9099cab3 1449 ]);
797d4c52 1450
1451 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1452 $this->_checkFinancialTrxn($contribution, 'refund');
1453 $this->_checkFinancialItem($contribution['id'], 'refund');
9099cab3 1454 $this->assertEquals('the refund', $this->callAPISuccessGetValue('Contribution', [
797d4c52 1455 'id' => $contribution['id'],
1456 'return' => 'trxn_id',
9099cab3 1457 ]));
797d4c52 1458 }
1459
1460 /**
1461 * Function tests that trxn_id is set when passed in.
1462 *
1463 * Here we ensure that the civicrm_contribution.trxn_id is set
1464 * when trxn_id is passed in but if refund_trxn_id is different then that
1465 * is kept for the refund transaction.
1466 */
1467 public function testCreateUpdateContributionRefundRefundAndTrxnIDPassedIn() {
9099cab3 1468 $contributionParams = [
797d4c52 1469 'contact_id' => $this->_individualId,
1470 'receive_date' => '2012-01-01',
1471 'total_amount' => 100.00,
1472 'financial_type_id' => $this->_financialTypeId,
1473 'payment_instrument_id' => 4,
1474 'contribution_status_id' => 1,
1475 'trxn_id' => 'original_payment',
9099cab3 1476 ];
797d4c52 1477 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
9099cab3 1478 $newParams = array_merge($contributionParams, [
39b959db
SL
1479 'id' => $contribution['id'],
1480 'contribution_status_id' => 'Refunded',
1481 'cancel_date' => '2015-01-01 09:00',
1482 'trxn_id' => 'cont id',
1483 'refund_trxn_id' => 'the refund',
9099cab3 1484 ]);
6a488035 1485
694769da 1486 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
6a488035
TO
1487 $this->_checkFinancialTrxn($contribution, 'refund');
1488 $this->_checkFinancialItem($contribution['id'], 'refund');
9099cab3 1489 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', [
797d4c52 1490 'id' => $contribution['id'],
1491 'return' => 'trxn_id',
9099cab3 1492 ]));
797d4c52 1493 }
1494
1495 /**
1496 * Function tests that refund_trxn_id is set when passed in empty.
1497 *
1498 * Here we ensure that the civicrm_contribution.trxn_id is set
1499 * when trxn_id is passed in but if refund_trxn_id isset but empty then that
1500 * is kept for the refund transaction.
1501 */
1502 public function testCreateUpdateContributionRefundRefundNullTrxnIDPassedIn() {
9099cab3 1503 $contributionParams = [
797d4c52 1504 'contact_id' => $this->_individualId,
1505 'receive_date' => '2012-01-01',
1506 'total_amount' => 100.00,
1507 'financial_type_id' => $this->_financialTypeId,
1508 'payment_instrument_id' => 4,
1509 'contribution_status_id' => 1,
1510 'trxn_id' => 'original_payment',
9099cab3 1511 ];
797d4c52 1512 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
9099cab3 1513 $newParams = array_merge($contributionParams, [
39b959db
SL
1514 'id' => $contribution['id'],
1515 'contribution_status_id' => 'Refunded',
1516 'cancel_date' => '2015-01-01 09:00',
1517 'trxn_id' => 'cont id',
1518 'refund_trxn_id' => '',
9099cab3 1519 ]);
797d4c52 1520
1521 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
9099cab3 1522 $this->_checkFinancialTrxn($contribution, 'refund', NULL, ['trxn_id' => NULL]);
797d4c52 1523 $this->_checkFinancialItem($contribution['id'], 'refund');
9099cab3 1524 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', [
797d4c52 1525 'id' => $contribution['id'],
1526 'return' => 'trxn_id',
9099cab3 1527 ]));
8f39a111 1528 }
c71ae314 1529
a1a2a83d 1530 /**
eceb18cc 1531 * Function tests invalid contribution status change.
c71ae314 1532 */
00be9182 1533 public function testCreateUpdateContributionInValidStatusChange() {
9099cab3 1534 $contribParams = [
c71ae314
PN
1535 'contact_id' => 1,
1536 'receive_date' => '2012-01-01',
1537 'total_amount' => 100.00,
1538 'financial_type_id' => 1,
1539 'payment_instrument_id' => 1,
1540 'contribution_status_id' => 1,
9099cab3 1541 ];
4ab7d517 1542 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
9099cab3 1543 $newParams = array_merge($contribParams, [
39b959db
SL
1544 'id' => $contribution['id'],
1545 'contribution_status_id' => 2,
9099cab3 1546 ]);
6c6e6187 1547 $this->callAPIFailure('contribution', 'create', $newParams, ts('Cannot change contribution status from Completed to Pending.'));
c71ae314 1548
6a488035
TO
1549 }
1550
a1a2a83d 1551 /**
eceb18cc 1552 * Function tests that financial records are added when Pending Contribution is Canceled.
6a488035 1553 */
00be9182 1554 public function testCreateUpdateContributionCancelPending() {
9099cab3 1555 $contribParams = [
6a488035
TO
1556 'contact_id' => $this->_individualId,
1557 'receive_date' => '2012-01-01',
1558 'total_amount' => 100.00,
4ab7d517 1559 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1560 'payment_instrument_id' => 1,
1561 'contribution_status_id' => 2,
c71ae314 1562 'is_pay_later' => 1,
4ab7d517 1563
9099cab3 1564 ];
4ab7d517 1565 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
9099cab3 1566 $newParams = array_merge($contribParams, [
39b959db
SL
1567 'id' => $contribution['id'],
1568 'contribution_status_id' => 3,
1569 'cancel_date' => '2012-02-02 09:00',
9099cab3 1570 ]);
0a8160e8 1571 //Check if trxn_date is same as cancel_date.
9099cab3 1572 $checkTrxnDate = [
0a8160e8 1573 'trxn_date' => '2012-02-02 09:00:00',
9099cab3 1574 ];
694769da 1575 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
0a8160e8 1576 $this->_checkFinancialTrxn($contribution, 'cancelPending', NULL, $checkTrxnDate);
6a488035
TO
1577 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1578 }
1579
a1a2a83d 1580 /**
eceb18cc 1581 * Function tests that financial records are added when Financial Type is Changed.
6a488035 1582 */
00be9182 1583 public function testCreateUpdateContributionChangeFinancialType() {
9099cab3 1584 $contribParams = [
6a488035
TO
1585 'contact_id' => $this->_individualId,
1586 'receive_date' => '2012-01-01',
1587 'total_amount' => 100.00,
1588 'financial_type_id' => 1,
1589 'payment_instrument_id' => 1,
1590 'contribution_status_id' => 1,
4ab7d517 1591
9099cab3 1592 ];
4ab7d517 1593 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
9099cab3 1594 $newParams = array_merge($contribParams, [
39b959db
SL
1595 'id' => $contribution['id'],
1596 'financial_type_id' => 3,
9099cab3 1597 ]);
694769da 1598 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
6a488035
TO
1599 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1600 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1601 }
1602
694769da 1603 /**
1e52837d 1604 * Test that update does not change status id CRM-15105.
694769da 1605 */
00be9182 1606 public function testCreateUpdateWithoutChangingPendingStatus() {
9099cab3
CW
1607 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params, ['contribution_status_id' => 2]));
1608 $this->callAPISuccess('contribution', 'create', ['id' => $contribution['id'], 'source' => 'new source']);
1609 $contribution = $this->callAPISuccess('contribution', 'getsingle', [
92915c55
TO
1610 'id' => $contribution['id'],
1611 'api.contribution.delete' => 1,
9099cab3 1612 ]);
694769da
VU
1613 $this->assertEquals(2, $contribution['contribution_status_id']);
1614 }
a1a2a83d
TO
1615
1616 /**
28de42d1
EM
1617 * Test Updating a Contribution.
1618 *
a1a2a83d
TO
1619 * CHANGE: we require the API to do an incremental update
1620 */
00be9182 1621 public function testCreateUpdateContribution() {
9099cab3 1622 $contributionID = $this->contributionCreate([
78ab0ca4 1623 'contact_id' => $this->_individualId,
1624 'trxn_id' => 212355,
1625 'financial_type_id' => $this->_financialTypeId,
1626 'invoice_id' => 'old_invoice',
9099cab3
CW
1627 ]);
1628 $old_params = [
6a488035 1629 'contribution_id' => $contributionID,
9099cab3 1630 ];
4ab7d517 1631 $original = $this->callAPISuccess('contribution', 'get', $old_params);
2bfae985 1632 $this->assertEquals($original['id'], $contributionID);
6a488035
TO
1633 //set up list of old params, verify
1634
1635 //This should not be required on update:
1636 $old_contact_id = $original['values'][$contributionID]['contact_id'];
7d543448 1637 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
6a488035
TO
1638 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1639 $old_source = $original['values'][$contributionID]['contribution_source'];
1640
6a488035
TO
1641 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1642 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1643
1644 //check against values in CiviUnitTestCase::createContribution()
2bfae985
EM
1645 $this->assertEquals($old_contact_id, $this->_individualId);
1646 $this->assertEquals($old_fee_amount, 5.00);
1647 $this->assertEquals($old_source, 'SSF');
1648 $this->assertEquals($old_trxn_id, 212355);
78ab0ca4 1649 $this->assertEquals($old_invoice_id, 'old_invoice');
9099cab3 1650 $params = [
6a488035
TO
1651 'id' => $contributionID,
1652 'contact_id' => $this->_individualId,
09201732
K
1653 'total_amount' => 105.00,
1654 'fee_amount' => 7.00,
4ab7d517 1655 'financial_type_id' => $this->_financialTypeId,
09201732 1656 'non_deductible_amount' => 22.00,
6a488035 1657 'contribution_status_id' => 1,
ef32adff 1658 'note' => 'Donating for Noble Cause',
9099cab3 1659 ];
6a488035 1660
4ab7d517 1661 $contribution = $this->callAPISuccess('contribution', 'create', $params);
6a488035 1662
9099cab3 1663 $new_params = [
6a488035 1664 'contribution_id' => $contribution['id'],
9099cab3 1665 ];
ef32adff 1666 $contribution = $this->callAPISuccessGetSingle('contribution', $new_params);
1667
1668 $this->assertEquals($contribution['contact_id'], $this->_individualId);
09201732 1669 $this->assertEquals($contribution['total_amount'], 105.00);
ef32adff 1670 $this->assertEquals($contribution['financial_type_id'], $this->_financialTypeId);
1671 $this->assertEquals($contribution['financial_type'], 'Donation');
1672 $this->assertEquals($contribution['instrument_id'], $old_payment_instrument);
09201732
K
1673 $this->assertEquals($contribution['non_deductible_amount'], 22.00);
1674 $this->assertEquals($contribution['fee_amount'], 7.00);
ef32adff 1675 $this->assertEquals($contribution['trxn_id'], $old_trxn_id);
1676 $this->assertEquals($contribution['invoice_id'], $old_invoice_id);
1677 $this->assertEquals($contribution['contribution_source'], $old_source);
1678 $this->assertEquals($contribution['contribution_status'], 'Completed');
09201732
K
1679
1680 $this->assertEquals($contribution['net_amount'], $contribution['total_amount'] - $contribution['fee_amount']);
1681
9099cab3 1682 $params = [
6a488035 1683 'contribution_id' => $contributionID,
9099cab3 1684 ];
4ab7d517 1685 $result = $this->callAPISuccess('contribution', 'delete', $params);
22f80e87 1686 $this->assertAPISuccess($result);
6a488035
TO
1687 }
1688
a3c2cbe6 1689 /**
1690 * Check that net_amount is updated when a contribution is updated.
1691 *
1692 * Update fee amount AND total amount, just fee amount, just total amount
1693 * and neither to check that net_amount is keep updated.
1694 */
1695 public function testUpdateContributionNetAmountVariants() {
1696 $contributionID = $this->contributionCreate(['contact_id' => $this->individualCreate()]);
1697
1698 $this->callAPISuccess('Contribution', 'create', [
1699 'id' => $contributionID,
1700 'total_amount' => 90,
1701 'fee_amount' => 6,
1702 ]);
1703 $contribution = $this->callAPISuccessGetSingle('Contribution', [
1704 'id' => $contributionID,
1705 'return' => ['net_amount', 'fee_amount', 'total_amount'],
1706 ]);
1707 $this->assertEquals(6, $contribution['fee_amount']);
1708 $this->assertEquals(90, $contribution['total_amount']);
1709 $this->assertEquals(84, $contribution['net_amount']);
1710
1711 $this->callAPISuccess('Contribution', 'create', [
1712 'id' => $contributionID,
1713 'fee_amount' => 3,
1714 ]);
1715 $contribution = $this->callAPISuccessGetSingle('Contribution', [
1716 'id' => $contributionID,
1717 'return' => ['net_amount', 'fee_amount', 'total_amount'],
1718 ]);
1719 $this->assertEquals(3, $contribution['fee_amount']);
1720 $this->assertEquals(90, $contribution['total_amount']);
1721 $this->assertEquals(87, $contribution['net_amount']);
1722
1723 $this->callAPISuccess('Contribution', 'create', [
1724 'id' => $contributionID,
1725 'total_amount' => 200,
1726 ]);
1727 $contribution = $this->callAPISuccessGetSingle('Contribution', [
1728 'id' => $contributionID,
1729 'return' => ['net_amount', 'fee_amount', 'total_amount'],
1730 ]);
1731 $this->assertEquals(3, $contribution['fee_amount']);
1732 $this->assertEquals(200, $contribution['total_amount']);
1733 $this->assertEquals(197, $contribution['net_amount']);
1734
1735 $this->callAPISuccess('Contribution', 'create', [
1736 'id' => $contributionID,
39b959db 1737 'payment_instrument' => 'Cash',
a3c2cbe6 1738 ]);
1739 $contribution = $this->callAPISuccessGetSingle('Contribution', [
1740 'id' => $contributionID,
1741 'return' => ['net_amount', 'fee_amount', 'total_amount'],
1742 ]);
1743 $this->assertEquals(3, $contribution['fee_amount']);
1744 $this->assertEquals(200, $contribution['total_amount']);
1745 $this->assertEquals(197, $contribution['net_amount']);
1746 }
1747
a1a2a83d
TO
1748 /**
1749 * Attempt (but fail) to delete a contribution without parameters.
1750 */
00be9182 1751 public function testDeleteEmptyParamsContribution() {
9099cab3 1752 $params = [];
858d0bf8 1753 $this->callAPIFailure('contribution', 'delete', $params);
6a488035
TO
1754 }
1755
00be9182 1756 public function testDeleteParamsNotArrayContribution() {
6a488035 1757 $params = 'contribution_id= 1';
d0e1eff2 1758 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
6a488035
TO
1759 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1760 }
1761
00be9182 1762 public function testDeleteWrongParamContribution() {
9099cab3 1763 $params = [
6a488035 1764 'contribution_source' => 'SSF',
9099cab3 1765 ];
858d0bf8 1766 $this->callAPIFailure('contribution', 'delete', $params);
6a488035
TO
1767 }
1768
00be9182 1769 public function testDeleteContribution() {
9099cab3 1770 $contributionID = $this->contributionCreate([
78ab0ca4 1771 'contact_id' => $this->_individualId,
1772 'financial_type_id' => $this->_financialTypeId,
9099cab3
CW
1773 ]);
1774 $params = [
6a488035 1775 'id' => $contributionID,
9099cab3 1776 ];
4ab7d517 1777 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__, __FILE__);
6a488035
TO
1778 }
1779
1780 /**
d177a2a6 1781 * Test civicrm_contribution_search with empty params.
1e52837d 1782 *
d177a2a6 1783 * All available contributions expected.
6a488035 1784 */
00be9182 1785 public function testSearchEmptyParams() {
9099cab3 1786 $params = [];
6a488035 1787
9099cab3 1788 $p = [
6a488035
TO
1789 'contact_id' => $this->_individualId,
1790 'receive_date' => date('Ymd'),
1791 'total_amount' => 100.00,
4ab7d517 1792 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1793 'non_deductible_amount' => 10.00,
1794 'fee_amount' => 5.00,
1795 'net_amount' => 95.00,
1796 'trxn_id' => 23456,
1797 'invoice_id' => 78910,
1798 'source' => 'SSF',
1799 'contribution_status_id' => 1,
9099cab3 1800 ];
4ab7d517 1801 $contribution = $this->callAPISuccess('contribution', 'create', $p);
6a488035 1802
4ab7d517 1803 $result = $this->callAPISuccess('contribution', 'get', $params);
6a488035
TO
1804 // We're taking the first element.
1805 $res = $result['values'][$contribution['id']];
1806
2bfae985
EM
1807 $this->assertEquals($p['contact_id'], $res['contact_id']);
1808 $this->assertEquals($p['total_amount'], $res['total_amount']);
5896d037 1809 $this->assertEquals($p['financial_type_id'], $res['financial_type_id']);
2bfae985
EM
1810 $this->assertEquals($p['net_amount'], $res['net_amount']);
1811 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1812 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1813 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1814 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1815 $this->assertEquals($p['source'], $res['contribution_source']);
6a488035 1816 // contribution_status_id = 1 => Completed
2bfae985 1817 $this->assertEquals('Completed', $res['contribution_status']);
6a488035
TO
1818
1819 $this->contributionDelete($contribution['id']);
1820 }
1821
1822 /**
d177a2a6 1823 * Test civicrm_contribution_search. Success expected.
6a488035 1824 */
00be9182 1825 public function testSearch() {
9099cab3 1826 $p1 = [
6a488035
TO
1827 'contact_id' => $this->_individualId,
1828 'receive_date' => date('Ymd'),
1829 'total_amount' => 100.00,
4ab7d517 1830 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1831 'non_deductible_amount' => 10.00,
1832 'contribution_status_id' => 1,
9099cab3 1833 ];
4ab7d517 1834 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
6a488035 1835
9099cab3 1836 $p2 = [
6a488035
TO
1837 'contact_id' => $this->_individualId,
1838 'receive_date' => date('Ymd'),
1839 'total_amount' => 200.00,
4ab7d517 1840 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1841 'non_deductible_amount' => 20.00,
1842 'trxn_id' => 5454565,
1843 'invoice_id' => 1212124,
1844 'fee_amount' => 50.00,
1845 'net_amount' => 60.00,
1846 'contribution_status_id' => 2,
9099cab3 1847 ];
2f45e1c2 1848 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
6a488035 1849
9099cab3 1850 $params = [
6a488035 1851 'contribution_id' => $contribution2['id'],
9099cab3 1852 ];
2f45e1c2 1853 $result = $this->callAPISuccess('contribution', 'get', $params);
6a488035
TO
1854 $res = $result['values'][$contribution2['id']];
1855
2bfae985
EM
1856 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1857 $this->assertEquals($p2['total_amount'], $res['total_amount']);
5896d037 1858 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id']);
2bfae985
EM
1859 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1860 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1861 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1862 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1863 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
6a488035 1864 // contribution_status_id = 2 => Pending
2bfae985 1865 $this->assertEquals('Pending', $res['contribution_status']);
6a488035
TO
1866
1867 $this->contributionDelete($contribution1['id']);
1868 $this->contributionDelete($contribution2['id']);
1869 }
2f45e1c2 1870
0efa8efe 1871 /**
eceb18cc 1872 * Test completing a transaction via the API.
0efa8efe 1873 *
1874 * Note that we are creating a logged in user because email goes out from
1875 * that person
1876 */
00be9182 1877 public function testCompleteTransaction() {
5896d037 1878 $mut = new CiviMailUtils($this, TRUE);
ec7e3954 1879 $this->swapMessageTemplateForTestTemplate();
0efa8efe 1880 $this->createLoggedInUser();
9099cab3 1881 $params = array_merge($this->_params, ['contribution_status_id' => 2]);
6c6e6187 1882 $contribution = $this->callAPISuccess('contribution', 'create', $params);
9099cab3 1883 $this->callAPISuccess('contribution', 'completetransaction', [
0efa8efe 1884 'id' => $contribution['id'],
9099cab3
CW
1885 ]);
1886 $contribution = $this->callAPISuccess('contribution', 'getsingle', ['id' => $contribution['id']]);
d5580ed4 1887 $this->assertEquals('SSF', $contribution['contribution_source']);
cc7b912f 1888 $this->assertEquals('Completed', $contribution['contribution_status']);
1889 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
9099cab3 1890 $mut->checkMailLog([
ec7e3954
E
1891 'email:::anthony_anderson@civicrm.org',
1892 'is_monetary:::1',
1893 'amount:::100.00',
1894 'currency:::USD',
1895 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])),
76e8d9c4 1896 "receipt_date:::\n",
8a40179e 1897 'contributeMode:::notify',
1898 'title:::Contribution',
1899 'displayName:::Mr. Anthony Anderson II',
1900 'contributionStatus:::Completed',
9099cab3 1901 ]);
8a40179e 1902 $mut->stop();
1903 $this->revertTemplateToReservedTemplate();
1904 }
1905
1906 /**
1907 * Test completing a transaction via the API with a non-USD transaction.
1908 */
1909 public function testCompleteTransactionEuro() {
1910 $mut = new CiviMailUtils($this, TRUE);
1911 $this->swapMessageTemplateForTestTemplate();
1912 $this->createLoggedInUser();
9099cab3 1913 $params = array_merge($this->_params, ['contribution_status_id' => 2, 'currency' => 'EUR']);
8a40179e 1914 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1915
9099cab3 1916 $this->callAPISuccess('contribution', 'completetransaction', [
8a40179e 1917 'id' => $contribution['id'],
9099cab3 1918 ]);
8a40179e 1919
9099cab3 1920 $contribution = $this->callAPISuccess('contribution', 'getsingle', ['id' => $contribution['id']]);
8a40179e 1921 $this->assertEquals('SSF', $contribution['contribution_source']);
1922 $this->assertEquals('Completed', $contribution['contribution_status']);
1923 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
1924
1925 $entityFinancialTransactions = $this->getFinancialTransactionsForContribution($contribution['id']);
1926 $entityFinancialTransaction = reset($entityFinancialTransactions);
9099cab3 1927 $financialTrxn = $this->callAPISuccessGetSingle('FinancialTrxn', ['id' => $entityFinancialTransaction['financial_trxn_id']]);
8a40179e 1928 $this->assertEquals('EUR', $financialTrxn['currency']);
1929
9099cab3 1930 $mut->checkMailLog([
8a40179e 1931 'email:::anthony_anderson@civicrm.org',
1932 'is_monetary:::1',
1933 'amount:::100.00',
1934 'currency:::EUR',
1935 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])),
1936 "receipt_date:::\n",
ec7e3954
E
1937 'contributeMode:::notify',
1938 'title:::Contribution',
1939 'displayName:::Mr. Anthony Anderson II',
3b28799d 1940 'contributionStatus:::Completed',
9099cab3 1941 ]);
46fa5206 1942 $mut->stop();
ec7e3954 1943 $this->revertTemplateToReservedTemplate();
46fa5206
EM
1944 }
1945
e05d2e11 1946 /**
1947 * Test to ensure mail is sent on chosing pay later
1948 */
1949 public function testpayLater() {
1950 $mut = new CiviMailUtils($this, TRUE);
1951 $this->swapMessageTemplateForTestTemplate();
1952 $this->createLoggedInUser();
1953
1954 // create contribution page first
9099cab3 1955 $contributionPageParams = [
e05d2e11 1956 'title' => 'Help Support CiviCRM!',
1957 'financial_type_id' => 1,
1958 'is_monetary' => TRUE,
1959 'is_pay_later' => 1,
1960 'is_quick_config' => TRUE,
1961 'pay_later_text' => 'I will send payment by check',
1962 'pay_later_receipt' => 'This is a pay later reciept',
1963 'is_allow_other_amount' => 1,
1964 'min_amount' => 10.00,
1965 'max_amount' => 10000.00,
1966 'goal_amount' => 100000.00,
1967 'is_email_receipt' => 1,
1968 'is_active' => 1,
1969 'amount_block_is_active' => 1,
1970 'currency' => 'USD',
1971 'is_billing_required' => 0,
9099cab3 1972 ];
e05d2e11 1973 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', $contributionPageParams);
1974
1975 // submit form values
9099cab3
CW
1976 $priceSet = $this->callAPISuccess('price_set', 'getsingle', ['name' => 'default_contribution_amount']);
1977 $params = [
e05d2e11 1978 'id' => $contributionPageResult['id'],
1979 'contact_id' => $this->_individualId,
1980 'email-5' => 'anthony_anderson@civicrm.org',
1981 'payment_processor_id' => 0,
1982 'amount' => 100.00,
1983 'tax_amount' => '',
1984 'currencyID' => 'USD',
1985 'is_pay_later' => 1,
1986 'invoiceID' => 'f28e1ddc86f8c4a0ff5bcf46393e4bc8',
1987 'is_quick_config' => 1,
1988 'description' => 'Online Contribution: Help Support CiviCRM!',
1989 'price_set_id' => $priceSet['id'],
9099cab3 1990 ];
e05d2e11 1991 $this->callAPISuccess('contribution_page', 'submit', $params);
1992
9099cab3 1993 $mut->checkMailLog([
e05d2e11 1994 'is_pay_later:::1',
1995 'email:::anthony_anderson@civicrm.org',
1996 'pay_later_receipt:::' . $contributionPageParams['pay_later_receipt'],
1997 'displayName:::Mr. Anthony Anderson II',
1998 'contributionPageId:::' . $contributionPageResult['id'],
1999 'title:::' . $contributionPageParams['title'],
8beee0e8 2000 'amount:::' . $params['amount'],
9099cab3 2001 ]);
e05d2e11 2002 $mut->stop();
2003 $this->revertTemplateToReservedTemplate();
2004 }
2005
2a0df9d9 2006 /**
2007 * Test to check whether contact billing address is used when no contribution address
2008 */
2009 public function testBillingAddress() {
2010 $mut = new CiviMailUtils($this, TRUE);
2011 $this->swapMessageTemplateForTestTemplate();
2012 $this->createLoggedInUser();
2013
2014 //Scenario 1: When Contact don't have any address
9099cab3 2015 $params = array_merge($this->_params, ['contribution_status_id' => 2]);
2a0df9d9 2016 $contribution = $this->callAPISuccess('contribution', 'create', $params);
9099cab3 2017 $this->callAPISuccess('contribution', 'completetransaction', [
2a0df9d9 2018 'id' => $contribution['id'],
9099cab3
CW
2019 ]);
2020 $mut->checkMailLog([
2a0df9d9 2021 'address:::',
9099cab3 2022 ]);
2a0df9d9 2023
2024 // Scenario 2: Contribution using address
9099cab3 2025 $address = $this->callAPISuccess('address', 'create', [
2a0df9d9 2026 'street_address' => 'contribution billing st',
2027 'location_type_id' => 2,
2028 'contact_id' => $this->_params['contact_id'],
9099cab3
CW
2029 ]);
2030 $params = array_merge($this->_params, [
76e8d9c4 2031 'contribution_status_id' => 2,
2a0df9d9 2032 'address_id' => $address['id'],
9099cab3 2033 ]
2a0df9d9 2034 );
2035 $contribution = $this->callAPISuccess('contribution', 'create', $params);
9099cab3 2036 $this->callAPISuccess('contribution', 'completetransaction', [
2a0df9d9 2037 'id' => $contribution['id'],
9099cab3
CW
2038 ]);
2039 $mut->checkMailLog([
2a0df9d9 2040 'address:::contribution billing st',
9099cab3 2041 ]);
2a0df9d9 2042
2043 // Scenario 3: Contribution wtth no address but contact has a billing address
9099cab3 2044 $this->callAPISuccess('address', 'create', [
2a0df9d9 2045 'id' => $address['id'],
2046 'street_address' => 'is billing st',
2047 'contact_id' => $this->_params['contact_id'],
9099cab3
CW
2048 ]);
2049 $params = array_merge($this->_params, ['contribution_status_id' => 2]);
2a0df9d9 2050 $contribution = $this->callAPISuccess('contribution', 'create', $params);
9099cab3 2051 $this->callAPISuccess('contribution', 'completetransaction', [
2a0df9d9 2052 'id' => $contribution['id'],
9099cab3
CW
2053 ]);
2054 $mut->checkMailLog([
2a0df9d9 2055 'address:::is billing st',
9099cab3 2056 ]);
2a0df9d9 2057
2058 $mut->stop();
2059 $this->revertTemplateToReservedTemplate();
2060 }
2061
080a561b 2062 /**
2063 * Test completing a transaction via the API.
2064 *
2065 * Note that we are creating a logged in user because email goes out from
2066 * that person
2067 */
2068 public function testCompleteTransactionFeeAmount() {
2069 $this->createLoggedInUser();
9099cab3 2070 $params = array_merge($this->_params, ['contribution_status_id' => 2]);
080a561b 2071 $contribution = $this->callAPISuccess('contribution', 'create', $params);
9099cab3 2072 $this->callAPISuccess('contribution', 'completetransaction', [
080a561b 2073 'id' => $contribution['id'],
2074 'fee_amount' => '.56',
2075 'trxn_id' => '7778888',
9099cab3
CW
2076 ]);
2077 $contribution = $this->callAPISuccess('contribution', 'getsingle', ['id' => $contribution['id'], 'sequential' => 1]);
080a561b 2078 $this->assertEquals('Completed', $contribution['contribution_status']);
2079 $this->assertEquals('7778888', $contribution['trxn_id']);
2080 $this->assertEquals('.56', $contribution['fee_amount']);
2081 $this->assertEquals('99.44', $contribution['net_amount']);
2082 }
2083
effb4d85 2084 /**
83644f47 2085 * CRM-19126 Add test to verify when complete transaction is called tax amount is not changed.
2086 *
2087 * @param string $thousandSeparator
2088 * punctuation used to refer to thousands.
2089 *
2090 * @dataProvider getThousandSeparators
effb4d85 2091 */
83644f47 2092 public function testCheckTaxAmount($thousandSeparator) {
2093 $this->setCurrencySeparators($thousandSeparator);
effb4d85 2094 $contact = $this->createLoggedInUser();
9099cab3 2095 $financialType = $this->callAPISuccess('financial_type', 'create', [
effb4d85
SL
2096 'name' => 'Test taxable financial Type',
2097 'is_reserved' => 0,
2098 'is_active' => 1,
9099cab3
CW
2099 ]);
2100 $financialAccount = $this->callAPISuccess('financial_account', 'create', [
39b959db
SL
2101 'name' => 'Test Tax financial account ',
2102 'contact_id' => $contact,
2103 'financial_account_type_id' => 2,
2104 'is_tax' => 1,
2105 'tax_rate' => 5.00,
2106 'is_reserved' => 0,
2107 'is_active' => 1,
2108 'is_default' => 0,
9099cab3 2109 ]);
effb4d85
SL
2110 $financialTypeId = $financialType['id'];
2111 $financialAccountId = $financialAccount['id'];
9099cab3 2112 $financialAccountParams = [
effb4d85
SL
2113 'entity_table' => 'civicrm_financial_type',
2114 'entity_id' => $financialTypeId,
2115 'account_relationship' => 10,
2116 'financial_account_id' => $financialAccountId,
9099cab3 2117 ];
a76b8bd8 2118 CRM_Financial_BAO_FinancialTypeAccount::add($financialAccountParams);
f527e012 2119
9099cab3 2120 $params = array_merge($this->_params, ['contribution_status_id' => 2, 'financial_type_id' => $financialTypeId]);
effb4d85 2121 $contribution = $this->callAPISuccess('contribution', 'create', $params);
9099cab3
CW
2122 $contribution1 = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id'], 'return' => 'tax_amount', 'sequential' => 1]);
2123 $this->callAPISuccess('contribution', 'completetransaction', [
39b959db
SL
2124 'id' => $contribution['id'],
2125 'trxn_id' => '777788888',
2126 'fee_amount' => '6.00',
9099cab3
CW
2127 ]);
2128 $contribution2 = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id'], 'return' => ['tax_amount', 'fee_amount', 'net_amount'], 'sequential' => 1]);
effb4d85 2129 $this->assertEquals($contribution1['values'][0]['tax_amount'], $contribution2['values'][0]['tax_amount']);
99a4cd32 2130 $this->assertEquals('6.00', $contribution2['values'][0]['fee_amount']);
f836984d 2131 $this->assertEquals('99.00', $contribution2['values'][0]['net_amount']);
effb4d85
SL
2132 }
2133
d97c96dc 2134 /**
0e6ccb2e 2135 * Test repeat contribution successfully creates line item.
d97c96dc 2136 */
1e52837d 2137 public function testRepeatTransaction() {
9099cab3
CW
2138 $originalContribution = $this->setUpRepeatTransaction($recurParams = [], 'single');
2139 $this->callAPISuccess('contribution', 'repeattransaction', [
d97c96dc
EM
2140 'original_contribution_id' => $originalContribution['id'],
2141 'contribution_status_id' => 'Completed',
2142 'trxn_id' => uniqid(),
9099cab3
CW
2143 ]);
2144 $lineItemParams = [
d97c96dc
EM
2145 'entity_id' => $originalContribution['id'],
2146 'sequential' => 1,
9099cab3 2147 'return' => [
d97c96dc
EM
2148 'entity_table',
2149 'qty',
2150 'unit_price',
2151 'line_total',
2152 'label',
2153 'financial_type_id',
2154 'deductible_amount',
2155 'price_field_value_id',
2156 'price_field_id',
9099cab3
CW
2157 ],
2158 ];
2159 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
d97c96dc 2160 'entity_id' => $originalContribution['id'],
9099cab3
CW
2161 ]));
2162 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
d97c96dc 2163 'entity_id' => $originalContribution['id'] + 1,
9099cab3 2164 ]));
d97c96dc
EM
2165 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
2166 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2167 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
9099cab3 2168 $this->_checkFinancialRecords([
f69a9ac3 2169 'id' => $originalContribution['id'] + 1,
9099cab3 2170 'payment_instrument_id' => $this->callAPISuccessGetValue('PaymentProcessor', [
f69a9ac3 2171 'id' => $originalContribution['payment_processor_id'],
2172 'return' => 'payment_instrument_id',
9099cab3
CW
2173 ]),
2174 ], 'online');
d97c96dc
EM
2175 $this->quickCleanUpFinancialEntities();
2176 }
2177
893a550c 2178 /**
0e6ccb2e
K
2179 * Test repeat contribution successfully creates line items (plural).
2180 */
2181 public function testRepeatTransactionLineItems() {
7150b1c8 2182 // CRM-19309
9099cab3
CW
2183 $originalContribution = $this->setUpRepeatTransaction($recurParams = [], 'multiple');
2184 $this->callAPISuccess('contribution', 'repeattransaction', [
0e6ccb2e
K
2185 'original_contribution_id' => $originalContribution['id'],
2186 'contribution_status_id' => 'Completed',
2187 'trxn_id' => uniqid(),
9099cab3 2188 ]);
0e6ccb2e 2189
9099cab3 2190 $lineItemParams = [
0e6ccb2e
K
2191 'entity_id' => $originalContribution['id'],
2192 'sequential' => 1,
9099cab3 2193 'return' => [
0e6ccb2e
K
2194 'entity_table',
2195 'qty',
2196 'unit_price',
2197 'line_total',
2198 'label',
2199 'financial_type_id',
2200 'deductible_amount',
2201 'price_field_value_id',
2202 'price_field_id',
9099cab3
CW
2203 ],
2204 ];
2205 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
0e6ccb2e 2206 'entity_id' => $originalContribution['id'],
9099cab3
CW
2207 ]));
2208 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
0e6ccb2e 2209 'entity_id' => $originalContribution['id'] + 1,
9099cab3 2210 ]));
0e6ccb2e
K
2211
2212 // unset id and entity_id for all of them to be able to compare the lineItems:
2213 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
2214 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2215 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
2216
2217 unset($lineItem1['values'][1]['id'], $lineItem1['values'][1]['entity_id']);
2218 unset($lineItem2['values'][1]['id'], $lineItem2['values'][1]['entity_id']);
2219 $this->assertEquals($lineItem1['values'][1], $lineItem2['values'][1]);
2220
7150b1c8
K
2221 // CRM-19309 so in future we also want to:
2222 // check that financial_line_items have been created for entity_id 3 and 4;
0e6ccb2e 2223
9099cab3 2224 $this->callAPISuccessGetCount('FinancialItem', ['description' => 'Sales Tax', 'amount' => 0], 0);
0e6ccb2e
K
2225 $this->quickCleanUpFinancialEntities();
2226 }
2227
2228 /**
2229 * Test repeat contribution successfully creates is_test transaction.
893a550c 2230 */
2231 public function testRepeatTransactionIsTest() {
2232 $this->_params['is_test'] = 1;
9099cab3 2233 $originalContribution = $this->setUpRepeatTransaction(['is_test' => 1], 'single');
893a550c 2234
9099cab3 2235 $this->callAPISuccess('contribution', 'repeattransaction', [
893a550c 2236 'original_contribution_id' => $originalContribution['id'],
2237 'contribution_status_id' => 'Completed',
2238 'trxn_id' => uniqid(),
9099cab3
CW
2239 ]);
2240 $this->callAPISuccessGetCount('Contribution', ['contribution_test' => 1], 2);
893a550c 2241 }
2242
d5580ed4 2243 /**
2244 * Test repeat contribution passed in status.
2245 */
2246 public function testRepeatTransactionPassedInStatus() {
9099cab3 2247 $originalContribution = $this->setUpRepeatTransaction($recurParams = [], 'single');
d5580ed4 2248
9099cab3 2249 $this->callAPISuccess('contribution', 'repeattransaction', [
d5580ed4 2250 'original_contribution_id' => $originalContribution['id'],
2251 'contribution_status_id' => 'Pending',
2252 'trxn_id' => uniqid(),
9099cab3
CW
2253 ]);
2254 $this->callAPISuccessGetCount('Contribution', ['contribution_status_id' => 2], 1);
d5580ed4 2255 }
2256
1eade77d 2257 /**
2258 * Test repeat contribution accepts recur_id instead of original_contribution_id.
2259 */
2260 public function testRepeatTransactionAcceptRecurID() {
9099cab3 2261 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', [
1eade77d 2262 'contact_id' => $this->_individualId,
2263 'installments' => '12',
2264 'frequency_interval' => '1',
2265 'amount' => '100',
2266 'contribution_status_id' => 1,
2267 'start_date' => '2012-01-01 00:00:00',
2268 'currency' => 'USD',
2269 'frequency_unit' => 'month',
2270 'payment_processor_id' => $this->paymentProcessorID,
9099cab3 2271 ]);
1eade77d 2272 $this->callAPISuccess('contribution', 'create', array_merge(
2273 $this->_params,
9099cab3 2274 ['contribution_recur_id' => $contributionRecur['id']])
1eade77d 2275 );
2276
9099cab3 2277 $this->callAPISuccess('contribution', 'repeattransaction', [
1eade77d 2278 'contribution_recur_id' => $contributionRecur['id'],
2279 'contribution_status_id' => 'Completed',
2280 'trxn_id' => uniqid(),
9099cab3 2281 ]);
1eade77d 2282
2283 $this->quickCleanUpFinancialEntities();
2284 }
2285
28124368
PH
2286 /**
2287 * CRM-19873 Test repattransaction if contribution_recur_id is a test.
2288 */
2289 public function testRepeatTransactionTestRecurId() {
9099cab3 2290 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', [
28124368
PH
2291 'contact_id' => $this->_individualId,
2292 'frequency_interval' => '1',
2293 'amount' => '1.00',
2294 'contribution_status_id' => 1,
2295 'start_date' => '2017-01-01 00:00:00',
2296 'currency' => 'USD',
2297 'frequency_unit' => 'month',
2298 'payment_processor_id' => $this->paymentProcessorID,
2299 'is_test' => 1,
9099cab3 2300 ]);
28124368
PH
2301 $this->callAPISuccess('contribution', 'create', array_merge(
2302 $this->_params,
9099cab3 2303 [
28124368
PH
2304 'contribution_recur_id' => $contributionRecur['id'],
2305 'is_test' => 1,
9099cab3 2306 ])
28124368
PH
2307 );
2308
9099cab3 2309 $repeatedContribution = $this->callAPISuccess('contribution', 'repeattransaction', [
28124368
PH
2310 'contribution_recur_id' => $contributionRecur['id'],
2311 'contribution_status_id' => 'Completed',
2312 'trxn_id' => uniqid(),
9099cab3 2313 ]);
28124368
PH
2314
2315 $this->assertEquals($contributionRecur['values'][1]['is_test'], $repeatedContribution['values'][2]['is_test']);
2316 $this->quickCleanUpFinancialEntities();
2317 }
37f29fcf 2318
d2334242 2319 /**
37f29fcf 2320 * CRM-19945 Tests that Contribute.repeattransaction renews a membership when contribution status=Completed
7c3f3d59 2321 *
d2334242 2322 */
37f29fcf 2323 public function testRepeatTransactionMembershipRenewCompletedContribution() {
d2334242
PH
2324 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
2325
9099cab3 2326 $this->callAPISuccess('contribution', 'create', [
7c3f3d59 2327 'contact_id' => $originalContribution['values'][1]['contact_id'],
2328 'financial_type_id' => $originalContribution['values'][1]['financial_type_id'],
2329 'total_amount' => $originalContribution['values'][1]['total_amount'],
2330 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2331 'contribution_status_id' => "Failed",
9099cab3 2332 ]);
d2334242 2333
9099cab3 2334 $this->callAPISuccess('membership', 'create', [
39b959db
SL
2335 'id' => $membership['id'],
2336 'end_date' => 'yesterday',
2337 'status_id' => 'Expired',
9099cab3 2338 ]);
d2334242 2339
9099cab3 2340 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', [
39b959db
SL
2341 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2342 'contribution_status_id' => 'Completed',
2343 'trxn_id' => 'bobsled',
9099cab3 2344 ]);
d2334242 2345
9099cab3 2346 $membershipStatusId = $this->callAPISuccess('membership', 'getvalue', [
d2334242
PH
2347 'id' => $membership['id'],
2348 'return' => 'status_id',
9099cab3 2349 ]);
d2334242 2350
9099cab3 2351 $membership = $this->callAPISuccess('membership', 'get', [
37f29fcf 2352 'id' => $membership['id'],
9099cab3 2353 ]);
37f29fcf
MW
2354
2355 $this->assertEquals('New', CRM_Core_PseudoConstant::getName('CRM_Member_BAO_Membership', 'status_id', $membershipStatusId));
2356
9099cab3 2357 $lineItem = $this->callAPISuccessGetSingle('LineItem', ['contribution_id' => $contribution['id']]);
37f29fcf 2358 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
9099cab3 2359 $this->callAPISuccessGetCount('MembershipPayment', ['membership_id' => $membership['id']]);
37f29fcf
MW
2360 $this->quickCleanUpFinancialEntities();
2361 $this->contactDelete($originalContribution['values'][1]['contact_id']);
2362 }
2363
2364 /**
2365 * CRM-19945 Tests that Contribute.repeattransaction DOES NOT renew a membership when contribution status=Failed
2366 *
2367 * @dataProvider contributionStatusProvider
2368 */
2369 public function testRepeatTransactionMembershipRenewContributionNotCompleted($contributionStatus) {
2370 // Completed status should renew so we don't test that here
2371 // In Progress status is only for recurring contributions so we don't test that here
2372 if (in_array($contributionStatus['name'], ['Completed', 'In Progress'])) {
2373 return;
2374 }
2375 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
2376
9099cab3 2377 $this->callAPISuccess('contribution', 'create', [
37f29fcf
MW
2378 'contact_id' => $originalContribution['values'][1]['contact_id'],
2379 'financial_type_id' => $originalContribution['values'][1]['financial_type_id'],
2380 'total_amount' => $originalContribution['values'][1]['total_amount'],
2381 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2382 'contribution_status_id' => "Completed",
9099cab3 2383 ]);
37f29fcf 2384
9099cab3 2385 $this->callAPISuccess('membership', 'create', [
37f29fcf
MW
2386 'id' => $membership['id'],
2387 'end_date' => 'yesterday',
2388 'status_id' => 'Expired',
9099cab3 2389 ]);
37f29fcf 2390
9099cab3 2391 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', [
37f29fcf
MW
2392 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2393 'contribution_status_id' => $contributionStatus['name'],
2394 'trxn_id' => 'bobsled',
9099cab3 2395 ]);
37f29fcf 2396
9099cab3 2397 $updatedMembership = $this->callAPISuccess('membership', 'getsingle', [
37f29fcf 2398 'id' => $membership['id'],
9099cab3 2399 ]);
37f29fcf
MW
2400
2401 $dateTime = new DateTime('yesterday');
2402 $this->assertEquals($dateTime->format('Y-m-d'), $updatedMembership['end_date']);
2403 $this->assertEquals(CRM_Core_PseudoConstant::getKey('CRM_Member_BAO_Membership', 'status_id', 'Expired'), $updatedMembership['status_id']);
7c3f3d59 2404
9099cab3 2405 $lineItem = $this->callAPISuccessGetSingle('LineItem', ['contribution_id' => $contribution['id']]);
7c3f3d59 2406 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
9099cab3 2407 $this->callAPISuccessGetCount('MembershipPayment', ['membership_id' => $membership['id']]);
d2334242
PH
2408 $this->quickCleanUpFinancialEntities();
2409 $this->contactDelete($originalContribution['values'][1]['contact_id']);
2410 }
28124368 2411
37f29fcf
MW
2412 /**
2413 * Dataprovider provides contribution status as [optionvalue=>contribution_status_name]
2414 * FIXME: buildOptions seems to die in CRM_Core_Config::_construct when in test mode.
2415 *
2416 * @return array
2417 * @throws \CiviCRM_API3_Exception
2418 */
2419 public function contributionStatusProvider() {
2420 $contributionStatuses = civicrm_api3('OptionValue', 'get', [
2421 'return' => ["id", "name"],
2422 'option_group_id' => "contribution_status",
2423 ]);
2424 foreach ($contributionStatuses['values'] as $statusName) {
2425 $statuses[] = [$statusName];
2426 }
2427 return $statuses;
2428 }
2429
c03f1689
EM
2430 /**
2431 * CRM-16397 test appropriate action if total amount has changed for single line items.
2432 */
2433 public function testRepeatTransactionAlteredAmount() {
2434 $paymentProcessorID = $this->paymentProcessorCreate();
9099cab3 2435 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', [
c03f1689
EM
2436 'contact_id' => $this->_individualId,
2437 'installments' => '12',
2438 'frequency_interval' => '1',
2439 'amount' => '500',
2440 'contribution_status_id' => 1,
2441 'start_date' => '2012-01-01 00:00:00',
2442 'currency' => 'USD',
2443 'frequency_unit' => 'month',
2444 'payment_processor_id' => $paymentProcessorID,
9099cab3 2445 ]);
c03f1689
EM
2446 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2447 $this->_params,
9099cab3 2448 [
c03f1689 2449 'contribution_recur_id' => $contributionRecur['id'],
9099cab3 2450 ])
c03f1689
EM
2451 );
2452
9099cab3 2453 $this->callAPISuccess('contribution', 'repeattransaction', [
c03f1689
EM
2454 'original_contribution_id' => $originalContribution['id'],
2455 'contribution_status_id' => 'Completed',
2456 'trxn_id' => uniqid(),
2457 'total_amount' => '400',
2458 'fee_amount' => 50,
9099cab3 2459 ]);
0e6ccb2e 2460
9099cab3 2461 $lineItemParams = [
c03f1689
EM
2462 'entity_id' => $originalContribution['id'],
2463 'sequential' => 1,
9099cab3 2464 'return' => [
c03f1689
EM
2465 'entity_table',
2466 'qty',
2467 'unit_price',
2468 'line_total',
2469 'label',
2470 'financial_type_id',
2471 'deductible_amount',
2472 'price_field_value_id',
2473 'price_field_id',
9099cab3
CW
2474 ],
2475 ];
2476 $this->callAPISuccessGetSingle('contribution', [
c03f1689 2477 'total_amount' => 400,
c03f1689
EM
2478 'fee_amount' => 50,
2479 'net_amount' => 350,
9099cab3
CW
2480 ]);
2481 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
c03f1689 2482 'entity_id' => $originalContribution['id'],
9099cab3 2483 ]));
c03f1689 2484 $expectedLineItem = array_merge(
9099cab3 2485 $lineItem1['values'][0], [
c03f1689
EM
2486 'line_total' => '400.00',
2487 'unit_price' => '400.00',
9099cab3 2488 ]
542d9e2c 2489 );
c03f1689 2490
9099cab3 2491 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
c03f1689 2492 'entity_id' => $originalContribution['id'] + 1,
9099cab3 2493 ]));
0e6ccb2e 2494
c03f1689
EM
2495 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2496 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2497 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
c02c17df 2498 }
c03f1689 2499
3c49d90c 2500 /**
2501 * CRM-17718 test appropriate action if financial type has changed for single line items.
2502 */
2503 public function testRepeatTransactionPassedInFinancialType() {
2504 $originalContribution = $this->setUpRecurringContribution();
2505
9099cab3 2506 $this->callAPISuccess('contribution', 'repeattransaction', [
3c49d90c 2507 'original_contribution_id' => $originalContribution['id'],
2508 'contribution_status_id' => 'Completed',
2509 'trxn_id' => uniqid(),
2510 'financial_type_id' => 2,
9099cab3
CW
2511 ]);
2512 $lineItemParams = [
3c49d90c 2513 'entity_id' => $originalContribution['id'],
2514 'sequential' => 1,
9099cab3 2515 'return' => [
3c49d90c 2516 'entity_table',
2517 'qty',
2518 'unit_price',
2519 'line_total',
2520 'label',
2521 'financial_type_id',
2522 'deductible_amount',
2523 'price_field_value_id',
2524 'price_field_id',
9099cab3
CW
2525 ],
2526 ];
3c49d90c 2527
9099cab3 2528 $this->callAPISuccessGetSingle('contribution', [
3c49d90c 2529 'total_amount' => 100,
2530 'financial_type_id' => 2,
9099cab3
CW
2531 ]);
2532 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
3c49d90c 2533 'entity_id' => $originalContribution['id'],
9099cab3 2534 ]));
3c49d90c 2535 $expectedLineItem = array_merge(
9099cab3 2536 $lineItem1['values'][0], [
3c49d90c 2537 'line_total' => '100.00',
2538 'unit_price' => '100.00',
2539 'financial_type_id' => 2,
257ab382 2540 'contribution_type_id' => 2,
9099cab3 2541 ]
3c49d90c 2542 );
9099cab3 2543 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
3c49d90c 2544 'entity_id' => $originalContribution['id'] + 1,
9099cab3 2545 ]));
3c49d90c 2546 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2547 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2548 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2549 }
2550
7f4ef731 2551 /**
2552 * CRM-17718 test appropriate action if financial type has changed for single line items.
2553 */
2554 public function testRepeatTransactionUpdatedFinancialType() {
9099cab3 2555 $originalContribution = $this->setUpRecurringContribution([], ['financial_type_id' => 2]);
7f4ef731 2556
9099cab3 2557 $this->callAPISuccess('contribution', 'repeattransaction', [
7f4ef731 2558 'contribution_recur_id' => $originalContribution['id'],
2559 'contribution_status_id' => 'Completed',
2560 'trxn_id' => uniqid(),
9099cab3
CW
2561 ]);
2562 $lineItemParams = [
7f4ef731 2563 'entity_id' => $originalContribution['id'],
2564 'sequential' => 1,
9099cab3 2565 'return' => [
7f4ef731 2566 'entity_table',
2567 'qty',
2568 'unit_price',
2569 'line_total',
2570 'label',
2571 'financial_type_id',
2572 'deductible_amount',
2573 'price_field_value_id',
2574 'price_field_id',
9099cab3
CW
2575 ],
2576 ];
7f4ef731 2577
9099cab3 2578 $this->callAPISuccessGetSingle('contribution', [
7f4ef731 2579 'total_amount' => 100,
2580 'financial_type_id' => 2,
9099cab3
CW
2581 ]);
2582 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
7f4ef731 2583 'entity_id' => $originalContribution['id'],
9099cab3 2584 ]));
7f4ef731 2585 $expectedLineItem = array_merge(
9099cab3 2586 $lineItem1['values'][0], [
7f4ef731 2587 'line_total' => '100.00',
2588 'unit_price' => '100.00',
2589 'financial_type_id' => 2,
257ab382 2590 'contribution_type_id' => 2,
9099cab3 2591 ]
7f4ef731 2592 );
2593
9099cab3 2594 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
7f4ef731 2595 'entity_id' => $originalContribution['id'] + 1,
9099cab3 2596 ]));
7f4ef731 2597 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2598 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2599 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2600 }
2601
c02c17df 2602 /**
1eade77d 2603 * CRM-16397 test appropriate action if campaign has been passed in.
c02c17df 2604 */
2605 public function testRepeatTransactionPassedInCampaign() {
2606 $paymentProcessorID = $this->paymentProcessorCreate();
2607 $campaignID = $this->campaignCreate();
2608 $campaignID2 = $this->campaignCreate();
9099cab3 2609 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', [
c02c17df 2610 'contact_id' => $this->_individualId,
2611 'installments' => '12',
2612 'frequency_interval' => '1',
2613 'amount' => '100',
2614 'contribution_status_id' => 1,
2615 'start_date' => '2012-01-01 00:00:00',
2616 'currency' => 'USD',
2617 'frequency_unit' => 'month',
2618 'payment_processor_id' => $paymentProcessorID,
9099cab3 2619 ]);
c02c17df 2620 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2621 $this->_params,
9099cab3 2622 [
c02c17df 2623 'contribution_recur_id' => $contributionRecur['id'],
2624 'campaign_id' => $campaignID,
9099cab3 2625 ])
c02c17df 2626 );
2627
9099cab3 2628 $this->callAPISuccess('contribution', 'repeattransaction', [
c02c17df 2629 'original_contribution_id' => $originalContribution['id'],
2630 'contribution_status_id' => 'Completed',
2631 'trxn_id' => uniqid(),
2632 'campaign_id' => $campaignID2,
9099cab3 2633 ]);
c02c17df 2634
9099cab3 2635 $this->callAPISuccessGetSingle('contribution', [
c02c17df 2636 'total_amount' => 100,
2637 'campaign_id' => $campaignID2,
9099cab3 2638 ]);
c02c17df 2639 }
2640
2641 /**
2642 * CRM-17718 campaign stored on contribution recur gets priority.
2643 *
2644 * This reflects the fact we permit people to update them.
2645 */
2646 public function testRepeatTransactionUpdatedCampaign() {
2647 $paymentProcessorID = $this->paymentProcessorCreate();
2648 $campaignID = $this->campaignCreate();
2649 $campaignID2 = $this->campaignCreate();
9099cab3 2650 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', [
c02c17df 2651 'contact_id' => $this->_individualId,
2652 'installments' => '12',
2653 'frequency_interval' => '1',
2654 'amount' => '100',
2655 'contribution_status_id' => 1,
2656 'start_date' => '2012-01-01 00:00:00',
2657 'currency' => 'USD',
2658 'frequency_unit' => 'month',
2659 'payment_processor_id' => $paymentProcessorID,
2660 'campaign_id' => $campaignID,
9099cab3 2661 ]);
c02c17df 2662 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2663 $this->_params,
9099cab3 2664 [
c02c17df 2665 'contribution_recur_id' => $contributionRecur['id'],
2666 'campaign_id' => $campaignID2,
9099cab3 2667 ])
c02c17df 2668 );
2669
9099cab3 2670 $this->callAPISuccess('contribution', 'repeattransaction', [
c02c17df 2671 'original_contribution_id' => $originalContribution['id'],
2672 'contribution_status_id' => 'Completed',
2673 'trxn_id' => uniqid(),
9099cab3 2674 ]);
c02c17df 2675
9099cab3 2676 $this->callAPISuccessGetSingle('contribution', [
c02c17df 2677 'total_amount' => 100,
2678 'campaign_id' => $campaignID,
9099cab3 2679 ]);
c03f1689
EM
2680 }
2681
2b0de476
E
2682 /**
2683 * CRM-20685 Repeattransaction produces incorrect Financial Type ID (in specific circumstance) - if number of lineItems = 1.
2684 *
2685 * This case happens when the line item & contribution do not have the same type in his initiating transaction.
2686 */
2687 public function testRepeatTransactionUpdatedFinancialTypeAndNotEquals() {
9099cab3 2688 $originalContribution = $this->setUpRecurringContribution([], ['financial_type_id' => 2]);
2b0de476 2689 // This will made the trick to get the not equals behaviour.
9099cab3
CW
2690 $this->callAPISuccess('line_item', 'create', ['id' => 1, 'financial_type_id' => 4]);
2691 $this->callAPISuccess('contribution', 'repeattransaction', [
2b0de476
E
2692 'contribution_recur_id' => $originalContribution['id'],
2693 'contribution_status_id' => 'Completed',
2694 'trxn_id' => uniqid(),
9099cab3
CW
2695 ]);
2696 $lineItemParams = [
2b0de476
E
2697 'entity_id' => $originalContribution['id'],
2698 'sequential' => 1,
9099cab3 2699 'return' => [
2b0de476
E
2700 'entity_table',
2701 'qty',
2702 'unit_price',
2703 'line_total',
2704 'label',
2705 'financial_type_id',
2706 'deductible_amount',
2707 'price_field_value_id',
2708 'price_field_id',
9099cab3
CW
2709 ],
2710 ];
2711 $this->callAPISuccessGetSingle('contribution', [
2b0de476
E
2712 'total_amount' => 100,
2713 'financial_type_id' => 2,
9099cab3
CW
2714 ]);
2715 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
2b0de476 2716 'entity_id' => $originalContribution['id'],
9099cab3 2717 ]));
2b0de476 2718 $expectedLineItem = array_merge(
9099cab3 2719 $lineItem1['values'][0], [
2b0de476
E
2720 'line_total' => '100.00',
2721 'unit_price' => '100.00',
2722 'financial_type_id' => 4,
2723 'contribution_type_id' => 4,
9099cab3 2724 ]
2b0de476
E
2725 );
2726
9099cab3 2727 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
2b0de476 2728 'entity_id' => $originalContribution['id'] + 1,
9099cab3
CW
2729 ]));
2730 $this->callAPISuccess('line_item', 'create', ['id' => 1, 'financial_type_id' => 1]);
2b0de476
E
2731 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2732 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2733 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2734 }
2735
2936c3b5
EM
2736 /**
2737 * Test completing a transaction does not 'mess' with net amount (CRM-15960).
2738 */
2739 public function testCompleteTransactionNetAmountOK() {
2740 $this->createLoggedInUser();
9099cab3 2741 $params = array_merge($this->_params, ['contribution_status_id' => 2]);
2936c3b5
EM
2742 unset($params['net_amount']);
2743 $contribution = $this->callAPISuccess('contribution', 'create', $params);
9099cab3 2744 $this->callAPISuccess('contribution', 'completetransaction', [
2936c3b5 2745 'id' => $contribution['id'],
9099cab3
CW
2746 ]);
2747 $contribution = $this->callAPISuccess('contribution', 'getsingle', ['id' => $contribution['id']]);
2936c3b5
EM
2748 $this->assertEquals('Completed', $contribution['contribution_status']);
2749 $this->assertTrue(($contribution['total_amount'] - $contribution['net_amount']) == $contribution['fee_amount']);
2750 }
2751
46fa5206 2752 /**
1e52837d 2753 * CRM-14151 - Test completing a transaction via the API.
46fa5206 2754 */
00be9182 2755 public function testCompleteTransactionWithReceiptDateSet() {
76e8d9c4 2756 $this->swapMessageTemplateForTestTemplate();
5896d037 2757 $mut = new CiviMailUtils($this, TRUE);
46fa5206 2758 $this->createLoggedInUser();
9099cab3 2759 $params = array_merge($this->_params, ['contribution_status_id' => 2, 'receipt_date' => 'now']);
6c6e6187 2760 $contribution = $this->callAPISuccess('contribution', 'create', $params);
9099cab3
CW
2761 $this->callAPISuccess('contribution', 'completetransaction', ['id' => $contribution['id'], 'trxn_date' => date('Y-m-d')]);
2762 $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id'], 'sequential' => 1]);
46fa5206 2763 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
7104593e 2764 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['values'][0]['receive_date'])));
9099cab3 2765 $mut->checkMailLog([
46fa5206 2766 'Receipt - Contribution',
76e8d9c4 2767 'receipt_date:::' . date('Ymd'),
9099cab3 2768 ]);
0efa8efe 2769 $mut->stop();
76e8d9c4 2770 $this->revertTemplateToReservedTemplate();
0efa8efe 2771 }
2772
e2ca457d
KG
2773 /**
2774 * CRM-1960 - Test to ensure that completetransaction respects the is_email_receipt setting
2775 */
2776 public function testCompleteTransactionWithEmailReceiptInput() {
d891a273 2777 $contributionPage = $this->createReceiptableContributionPage();
2778
e2ca457d 2779 $this->_params['contribution_page_id'] = $contributionPage['id'];
9099cab3 2780 $params = array_merge($this->_params, ['contribution_status_id' => 2]);
e2ca457d
KG
2781 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2782 // Complete the transaction overriding is_email_receipt to = FALSE
9099cab3 2783 $this->callAPISuccess('contribution', 'completetransaction', [
e2ca457d
KG
2784 'id' => $contribution['id'],
2785 'trxn_date' => date('2011-04-09'),
2786 'trxn_id' => 'kazam',
2787 'is_email_receipt' => 0,
9099cab3 2788 ]);
e2ca457d 2789 // Check if a receipt was issued
9099cab3 2790 $receipt_date = $this->callAPISuccess('Contribution', 'getvalue', ['id' => $contribution['id'], 'return' => 'receipt_date']);
e2ca457d
KG
2791 $this->assertEquals('', $receipt_date);
2792 }
3b8c739e 2793
d891a273 2794 /**
2795 * Test that $is_recur is assigned to the receipt.
2796 */
2797 public function testCompleteTransactionForRecurring() {
2798
2799 $this->swapMessageTemplateForTestTemplate();
2800 $recurring = $this->setUpRecurringContribution();
9099cab3 2801 $contributionPage = $this->createReceiptableContributionPage(['is_recur' => TRUE, 'recur_frequency_unit' => 'month', 'recur_interval' => 1]);
d891a273 2802
2803 $this->_params['contribution_page_id'] = $contributionPage['id'];
2804 $this->_params['contribution_recur_id'] = $recurring['id'];
2805
2806 $contribution = $this->setUpForCompleteTransaction();
2807
9099cab3 2808 $this->callAPISuccess('contribution', 'completetransaction', [
d891a273 2809 'id' => $contribution['id'],
2810 'trxn_date' => date('2011-04-09'),
2811 'trxn_id' => 'kazam',
2812 'is_email_receipt' => 1,
9099cab3 2813 ]);
d891a273 2814
9099cab3 2815 $this->mut->checkMailLog([
d891a273 2816 'is_recur:::1',
0131a2ce 2817 'cancelSubscriptionUrl:::' . CIVICRM_UF_BASEURL,
9099cab3 2818 ]);
d891a273 2819 $this->mut->stop();
2820 $this->revertTemplateToReservedTemplate();
2821 }
39b959db 2822
55df1211
AS
2823 /**
2824 * CRM-19710 - Test to ensure that completetransaction respects the input for is_email_receipt setting.
2825 *
2826 * If passed in it will override the default from contribution page.
2827 */
2828 public function testCompleteTransactionWithEmailReceiptInputTrue() {
2829 $mut = new CiviMailUtils($this, TRUE);
2830 $this->createLoggedInUser();
2831 // Create a Contribution Page with is_email_receipt = FALSE
9099cab3 2832 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', [
55df1211
AS
2833 'receipt_from_name' => 'Mickey Mouse',
2834 'receipt_from_email' => 'mickey@mouse.com',
2835 'title' => "Test Contribution Page",
2836 'financial_type_id' => 1,
2837 'currency' => 'CAD',
2838 'is_monetary' => TRUE,
2839 'is_email_receipt' => 0,
9099cab3 2840 ]);
55df1211 2841 $this->_params['contribution_page_id'] = $contributionPage['id'];
9099cab3 2842 $params = array_merge($this->_params, ['contribution_status_id' => 2, 'receipt_date' => 'now']);
55df1211
AS
2843 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2844 // Complete the transaction overriding is_email_receipt to = TRUE
9099cab3 2845 $this->callAPISuccess('contribution', 'completetransaction', [
55df1211
AS
2846 'id' => $contribution['id'],
2847 'is_email_receipt' => 1,
9099cab3
CW
2848 ]);
2849 $mut->checkMailLog([
55df1211 2850 'Please print this receipt for your records.',
9099cab3 2851 ]);
55df1211
AS
2852 $mut->stop();
2853 }
d891a273 2854
b80f2ad1
E
2855 /**
2856 * Complete the transaction using the template with all the possible.
2857 */
2858 public function testCompleteTransactionWithTestTemplate() {
2859 $this->swapMessageTemplateForTestTemplate();
ec7e3954 2860 $contribution = $this->setUpForCompleteTransaction();
9099cab3 2861 $this->callAPISuccess('contribution', 'completetransaction', [
b80f2ad1
E
2862 'id' => $contribution['id'],
2863 'trxn_date' => date('2011-04-09'),
2864 'trxn_id' => 'kazam',
9099cab3
CW
2865 ]);
2866 $receive_date = $this->callAPISuccess('Contribution', 'getvalue', ['id' => $contribution['id'], 'return' => 'receive_date']);
2867 $this->mut->checkMailLog([
b80f2ad1
E
2868 'email:::anthony_anderson@civicrm.org',
2869 'is_monetary:::1',
2870 'amount:::100.00',
2871 'currency:::USD',
2872 'receive_date:::' . date('Ymd', strtotime($receive_date)),
2873 'receipt_date:::' . date('Ymd'),
2874 'contributeMode:::notify',
2875 'title:::Contribution',
2876 'displayName:::Mr. Anthony Anderson II',
2877 'trxn_id:::kazam',
ec7e3954 2878 'contactID:::' . $this->_params['contact_id'],
b80f2ad1
E
2879 'contributionID:::' . $contribution['id'],
2880 'financialTypeId:::1',
2881 'financialTypeName:::Donation',
9099cab3 2882 ]);
ec7e3954 2883 $this->mut->stop();
b80f2ad1
E
2884 $this->revertTemplateToReservedTemplate();
2885 }
2886
ec7e3954
E
2887 /**
2888 * Complete the transaction using the template with all the possible.
2889 */
2890 public function testCompleteTransactionContributionPageFromAddress() {
9099cab3 2891 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', [
ec7e3954
E
2892 'receipt_from_name' => 'Mickey Mouse',
2893 'receipt_from_email' => 'mickey@mouse.com',
2894 'title' => "Test Contribution Page",
2895 'financial_type_id' => 1,
2896 'currency' => 'NZD',
2897 'goal_amount' => 50,
2898 'is_pay_later' => 1,
2899 'is_monetary' => TRUE,
2900 'is_email_receipt' => TRUE,
9099cab3 2901 ]);
ec7e3954
E
2902 $this->_params['contribution_page_id'] = $contributionPage['id'];
2903 $contribution = $this->setUpForCompleteTransaction();
9099cab3
CW
2904 $this->callAPISuccess('contribution', 'completetransaction', ['id' => $contribution['id']]);
2905 $this->mut->checkMailLog([
ec7e3954
E
2906 'mickey@mouse.com',
2907 'Mickey Mouse <',
9099cab3 2908 ]);
ec7e3954
E
2909 $this->mut->stop();
2910 }
2911
91259407 2912 /**
2913 * Test completing first transaction in a recurring series.
2914 *
2915 * The status should be set to 'in progress' and the next scheduled payment date calculated.
050e11d5 2916 *
2917 * @dataProvider getScheduledDateData
2918 *
2919 * @param array $dataSet
2920 *
2921 * @throws \Exception
91259407 2922 */
050e11d5 2923 public function testCompleteTransactionSetStatusToInProgress($dataSet) {
91259407 2924 $paymentProcessorID = $this->paymentProcessorCreate();
9099cab3 2925 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge([
91259407 2926 'contact_id' => $this->_individualId,
050e11d5 2927 'installments' => '2',
91259407 2928 'frequency_interval' => '1',
2929 'amount' => '500',
2930 'contribution_status_id' => 'Pending',
2931 'start_date' => '2012-01-01 00:00:00',
2932 'currency' => 'USD',
2933 'frequency_unit' => 'month',
2934 'payment_processor_id' => $paymentProcessorID,
9099cab3 2935 ], $dataSet['data']));
91259407 2936 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(
2937 $this->_params,
9099cab3 2938 [
91259407 2939 'contribution_recur_id' => $contributionRecur['id'],
2940 'contribution_status_id' => 'Pending',
050e11d5 2941 'receive_date' => $dataSet['receive_date'],
9099cab3 2942 ])
91259407 2943 );
9099cab3 2944 $this->callAPISuccess('Contribution', 'completetransaction', [
050e11d5 2945 'id' => $contribution,
2946 'receive_date' => $dataSet['receive_date'],
9099cab3
CW
2947 ]);
2948 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', [
91259407 2949 'id' => $contributionRecur['id'],
9099cab3
CW
2950 'return' => ['next_sched_contribution_date', 'contribution_status_id'],
2951 ]);
91259407 2952 $this->assertEquals(5, $contributionRecur['contribution_status_id']);
050e11d5 2953 $this->assertEquals($dataSet['expected'], $contributionRecur['next_sched_contribution_date']);
2954 $this->callAPISuccess('Contribution', 'create', array_merge(
2955 $this->_params,
9099cab3 2956 [
050e11d5 2957 'contribution_recur_id' => $contributionRecur['id'],
2958 'contribution_status_id' => 'Completed',
9099cab3 2959 ]
050e11d5 2960 ));
9099cab3 2961 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', [
050e11d5 2962 'id' => $contributionRecur['id'],
9099cab3
CW
2963 'return' => ['contribution_status_id'],
2964 ]);
050e11d5 2965 $this->assertEquals(1, $contributionRecur['contribution_status_id']);
2966 }
2967
2968 /**
2969 * Get dates for testing.
2970 *
2971 * @return array
2972 */
2973 public function getScheduledDateData() {
9099cab3
CW
2974 $result = [];
2975 $result[]['2016-08-31-1-month'] = [
2976 'data' => [
050e11d5 2977 'start_date' => '2016-08-31',
2978 'frequency_interval' => 1,
2979 'frequency_unit' => 'month',
9099cab3 2980 ],
050e11d5 2981 'receive_date' => '2016-08-31',
2982 'expected' => '2016-10-01 00:00:00',
9099cab3
CW
2983 ];
2984 $result[]['2012-01-01-1-month'] = [
2985 'data' => [
050e11d5 2986 'start_date' => '2012-01-01',
2987 'frequency_interval' => 1,
2988 'frequency_unit' => 'month',
9099cab3 2989 ],
050e11d5 2990 'receive_date' => '2012-01-01',
2991 'expected' => '2012-02-01 00:00:00',
9099cab3
CW
2992 ];
2993 $result[]['2012-01-01-1-month'] = [
2994 'data' => [
050e11d5 2995 'start_date' => '2012-01-01',
2996 'frequency_interval' => 1,
2997 'frequency_unit' => 'month',
9099cab3 2998 ],
050e11d5 2999 'receive_date' => '2012-02-29',
3000 'expected' => '2012-03-29 00:00:00',
9099cab3
CW
3001 ];
3002 $result['receive_date_includes_time']['2012-01-01-1-month'] = [
3003 'data' => [
46f459f2 3004 'start_date' => '2012-01-01',
3005 'frequency_interval' => 1,
3006 'frequency_unit' => 'month',
3007 'next_sched_contribution_date' => '2012-02-29',
9099cab3 3008 ],
46f459f2 3009 'receive_date' => '2012-02-29 16:00:00',
3010 'expected' => '2012-03-29 00:00:00',
9099cab3 3011 ];
050e11d5 3012 return $result;
91259407 3013 }
3014
294cc627 3015 /**
3016 * Test completing a pledge with the completeTransaction api..
3017 *
3018 * Note that we are creating a logged in user because email goes out from
3019 * that person.
3020 */
3021 public function testCompleteTransactionUpdatePledgePayment() {
9f9fa558 3022 $this->swapMessageTemplateForTestTemplate();
294cc627 3023 $mut = new CiviMailUtils($this, TRUE);
3024 $mut->clearMessages();
3025 $this->createLoggedInUser();
3026 $contributionID = $this->createPendingPledgeContribution();
9099cab3 3027 $this->callAPISuccess('contribution', 'completetransaction', [
294cc627 3028 'id' => $contributionID,
3029 'trxn_date' => '1 Feb 2013',
9099cab3
CW
3030 ]);
3031 $pledge = $this->callAPISuccessGetSingle('Pledge', [
294cc627 3032 'id' => $this->_ids['pledge'],
9099cab3 3033 ]);
294cc627 3034 $this->assertEquals('Completed', $pledge['pledge_status']);
3035
9099cab3 3036 $status = $this->callAPISuccessGetValue('PledgePayment', [
294cc627 3037 'pledge_id' => $this->_ids['pledge'],
3038 'return' => 'status_id',
9099cab3 3039 ]);
294cc627 3040 $this->assertEquals(1, $status);
9099cab3 3041 $mut->checkMailLog([
9f9fa558
EM
3042 'amount:::500.00',
3043 'receive_date:::20130201000000',
76e8d9c4 3044 "receipt_date:::\n",
9099cab3 3045 ]);
294cc627 3046 $mut->stop();
9f9fa558 3047 $this->revertTemplateToReservedTemplate();
294cc627 3048 }
3049
0efa8efe 3050 /**
eceb18cc 3051 * Test completing a transaction with an event via the API.
0efa8efe 3052 *
3053 * Note that we are creating a logged in user because email goes out from
3054 * that person
ed7e2e99 3055 *
3056 * @throws \CRM_Core_Exception
0efa8efe 3057 */
00be9182 3058 public function testCompleteTransactionWithParticipantRecord() {
5896d037 3059 $mut = new CiviMailUtils($this, TRUE);
0efa8efe 3060 $mut->clearMessages();
71acd4bf 3061 $this->_individualId = $this->createLoggedInUser();
0efa8efe 3062 $contributionID = $this->createPendingParticipantContribution();
ed7e2e99 3063 $this->createJoinedProfile(['entity_id' => $this->_ids['event']['test'], 'entity_table' => 'civicrm_event']);
f736929b 3064 $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']);
3065 $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']);
3066 $this->eliminateUFGroupOne();
ed7e2e99 3067
9099cab3 3068 $this->callAPISuccess('contribution', 'completetransaction', [
39b959db 3069 'id' => $contributionID,
9099cab3 3070 ]
0efa8efe 3071 );
9099cab3 3072 $participantStatus = $this->callAPISuccessGetValue('participant', [
92915c55
TO
3073 'id' => $this->_ids['participant'],
3074 'return' => 'participant_status_id',
9099cab3 3075 ]);
0efa8efe 3076 $this->assertEquals(1, $participantStatus);
71acd4bf
JP
3077
3078 //Assert only three activities are created.
ed7e2e99 3079 $activities = $this->callAPISuccess('Activity', 'get', [
3080 'contact_id' => $this->_individualId,
3081 ])['values'];
3082
f736929b 3083 $this->assertCount(3, $activities);
71acd4bf 3084 $activityNames = array_count_values(CRM_Utils_Array::collect('activity_name', $activities));
b56b628c 3085 // record two activities before and after completing payment for Event registration
71acd4bf 3086 $this->assertEquals(2, $activityNames['Event Registration']);
b56b628c 3087 // update the original 'Contribution' activity created after completing payment
71acd4bf
JP
3088 $this->assertEquals(1, $activityNames['Contribution']);
3089
9099cab3 3090 $mut->checkMailLog([
0efa8efe 3091 'Annual CiviCRM meet',
3092 'Event',
afc59fef 3093 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
ed7e2e99 3094 'First Name: Logged In',
3095 'Public title',
f736929b 3096 'public 2',
3097 'public 3',
3098 ], ['Back end title', 'title_post_2', 'title_post_3']);
66d3f9be
EM
3099 $mut->stop();
3100 }
3101
3102 /**
eceb18cc 3103 * Test membership is renewed when transaction completed.
66d3f9be 3104 */
00be9182 3105 public function testCompleteTransactionMembershipPriceSet() {
66d3f9be 3106 $this->createPriceSetWithPage('membership');
9099cab3 3107 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', [
4ff927bc 3108 'name' => 'Grace',
39b959db 3109 'return' => 'id',
9099cab3 3110 ]);
66d3f9be 3111 $this->setUpPendingContribution($this->_ids['price_field_value'][0]);
9099cab3
CW
3112 $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $this->_ids['membership']]);
3113 $logs = $this->callAPISuccess('MembershipLog', 'get', [
4ff927bc 3114 'membership_id' => $this->_ids['membership'],
9099cab3 3115 ]);
4ff927bc 3116 $this->assertEquals(1, $logs['count']);
3117 $this->assertEquals($stateOfGrace, $membership['status_id']);
9099cab3
CW
3118 $this->callAPISuccess('contribution', 'completetransaction', ['id' => $this->_ids['contribution']]);
3119 $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $this->_ids['membership']]);
66d3f9be 3120 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
9099cab3 3121 $this->callAPISuccessGetSingle('LineItem', [
4ff927bc 3122 'entity_id' => $this->_ids['membership'],
3123 'entity_table' => 'civicrm_membership',
9099cab3
CW
3124 ]);
3125 $logs = $this->callAPISuccess('MembershipLog', 'get', [
4ff927bc 3126 'membership_id' => $this->_ids['membership'],
9099cab3 3127 ]);
4ff927bc 3128 $this->assertEquals(2, $logs['count']);
3129 $this->assertNotEquals($stateOfGrace, $logs['values'][2]['status_id']);
98f0683a
JP
3130 //Assert only three activities are created.
3131 $activities = CRM_Activity_BAO_Activity::getContactActivity($this->_ids['contact']);
3132 $this->assertEquals(3, count($activities));
3133 $activityNames = array_flip(CRM_Utils_Array::collect('activity_name', $activities));
3134 $this->assertArrayHasKey('Contribution', $activityNames);
3135 $this->assertArrayHasKey('Membership Signup', $activityNames);
3136 $this->assertArrayHasKey('Change Membership Status', $activityNames);
66d3f9be
EM
3137 $this->cleanUpAfterPriceSets();
3138 }
3139
0f07bb06 3140 /**
3141 * Test if renewal activity is create after changing Pending contribution to Completed via offline
3142 */
3143 public function testPendingToCompleteContribution() {
c77986c0 3144 $this->createPriceSetWithPage('membership');
0f07bb06 3145 $this->setUpPendingContribution($this->_ids['price_field_value'][0]);
9099cab3 3146 $this->callAPISuccess('membership', 'getsingle', ['id' => $this->_ids['membership']]);
66a1e31f 3147 // Case 1: Assert that Membership Signup Activity is created on Pending to Completed Contribution via backoffice
9099cab3 3148 $activity = $this->callAPISuccess('Activity', 'get', [
b6d493f3
MD
3149 'activity_type_id' => 'Membership Signup',
3150 'source_record_id' => $this->_ids['membership'],
3151 'status_id' => 'Scheduled',
9099cab3 3152 ]);
b6d493f3 3153 $this->assertEquals(1, $activity['count']);
0f07bb06 3154
3155 // change pending contribution to completed
3156 $form = new CRM_Contribute_Form_Contribution();
c77986c0 3157
9099cab3 3158 $form->_params = [
0f07bb06 3159 'id' => $this->_ids['contribution'],
3160 'total_amount' => 20,
3161 'net_amount' => 20,
3162 'fee_amount' => 0,
3163 'financial_type_id' => 1,
0f07bb06 3164 'contact_id' => $this->_individualId,
3165 'contribution_status_id' => 1,
3166 'billing_middle_name' => '',
3167 'billing_last_name' => 'Adams',
3168 'billing_street_address-5' => '790L Lincoln St S',
3169 'billing_city-5' => 'Maryknoll',
3170 'billing_state_province_id-5' => 1031,
3171 'billing_postal_code-5' => 10545,
3172 'billing_country_id-5' => 1228,
3173 'frequency_interval' => 1,
3174 'frequency_unit' => 'month',
3175 'installments' => '',
3176 'hidden_AdditionalDetail' => 1,
3177 'hidden_Premium' => 1,
3178 'from_email_address' => '"civi45" <civi45@civicrm.com>',
3179 'receipt_date' => '',
3180 'receipt_date_time' => '',
3181 'payment_processor_id' => $this->paymentProcessorID,
3182 'currency' => 'USD',
3183 'contribution_page_id' => $this->_ids['contribution_page'],
3184 'contribution_mode' => 'membership',
3185 'source' => 'Membership Signup and Renewal',
9099cab3 3186 ];
c77986c0 3187
3188 $form->testSubmit($form->_params, CRM_Core_Action::UPDATE);
3189
66a1e31f 3190 // Case 2: After successful payment for Pending backoffice there are three activities created
b6d493f3 3191 // 2.a Update status of existing Scheduled Membership Signup (created in step 1) to Completed
9099cab3 3192 $activity = $this->callAPISuccess('Activity', 'get', [
767d3e2e 3193 'activity_type_id' => 'Membership Signup',
b6d493f3
MD
3194 'source_record_id' => $this->_ids['membership'],
3195 'status_id' => 'Completed',
9099cab3 3196 ]);
767d3e2e 3197 $this->assertEquals(1, $activity['count']);
b6d493f3 3198 // 2.b Contribution activity created to record successful payment
9099cab3 3199 $activity = $this->callAPISuccess('Activity', 'get', [
b6d493f3 3200 'activity_type_id' => 'Contribution',
767d3e2e 3201 'source_record_id' => $this->_ids['contribution'],
b6d493f3 3202 'status_id' => 'Completed',
9099cab3 3203 ]);
b6d493f3 3204 $this->assertEquals(1, $activity['count']);
fde55343 3205
b6d493f3 3206 // 2.c 'Change membership type' activity created to record Membership status change from Grace to Current
9099cab3 3207 $activity = $this->callAPISuccess('Activity', 'get', [
b6d493f3
MD
3208 'activity_type_id' => 'Change Membership Status',
3209 'source_record_id' => $this->_ids['membership'],
3210 'status_id' => 'Completed',
9099cab3 3211 ]);
d2460a89 3212 $this->assertEquals(1, $activity['count']);
b6d493f3 3213 $this->assertEquals('Status changed from Grace to Current', $activity['values'][$activity['id']]['subject']);
fde55343
JP
3214
3215 //Create another pending contribution for renewal
9099cab3 3216 $contribution = $this->callAPISuccess('contribution', 'create', [
fde55343
JP
3217 'domain_id' => 1,
3218 'contact_id' => $this->_ids['contact'],
3219 'receive_date' => date('Ymd'),
3220 'total_amount' => 20.00,
3221 'financial_type_id' => 1,
3222 'payment_instrument_id' => 'Credit Card',
3223 'non_deductible_amount' => 10.00,
3224 'trxn_id' => 'rdhfi88',
3225 'invoice_id' => 'dofhiewuyr',
3226 'source' => 'SSF',
3227 'contribution_status_id' => 2,
3228 'contribution_page_id' => $this->_ids['contribution_page'],
c77986c0 3229 // We can't rely on contribution api to link line items correctly to membership
3230 'skipLineItem' => TRUE,
9099cab3
CW
3231 'api.membership_payment.create' => ['membership_id' => $this->_ids['membership']],
3232 ]);
fde55343 3233
9099cab3 3234 $this->callAPISuccess('line_item', 'create', [
fde55343
JP
3235 'entity_id' => $contribution['id'],
3236 'entity_table' => 'civicrm_contribution',
3237 'contribution_id' => $contribution['id'],
3238 'price_field_id' => $this->_ids['price_field'][0],
3239 'qty' => 1,
3240 'unit_price' => 20,
3241 'line_total' => 20,
3242 'financial_type_id' => 1,
c77986c0 3243 'price_field_value_id' => $this->_ids['price_field_value']['cont'],
9099cab3
CW
3244 ]);
3245 $this->callAPISuccess('line_item', 'create', [
c77986c0 3246 'entity_id' => $this->_ids['membership'],
3247 'entity_table' => 'civicrm_membership',
3248 'contribution_id' => $contribution['id'],
3249 'price_field_id' => $this->_ids['price_field'][0],
3250 'qty' => 1,
3251 'unit_price' => 20,
3252 'line_total' => 20,
3253 'financial_type_id' => 1,
fde55343 3254 'price_field_value_id' => $this->_ids['price_field_value'][0],
c77986c0 3255 'membership_type_id' => $this->_ids['membership_type'],
9099cab3 3256 ]);
fde55343
JP
3257
3258 //Update it to Failed.
3259 $form->_params['id'] = $contribution['id'];
3260 $form->_params['contribution_status_id'] = 4;
c77986c0 3261
3262 $form->testSubmit($form->_params, CRM_Core_Action::UPDATE);
fde55343 3263 //Existing membership should not get updated to expired.
9099cab3 3264 $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $this->_ids['membership']]);
fde55343 3265 $this->assertNotEquals($membership['status_id'], 4);
0f07bb06 3266 }
3267
66d3f9be 3268 /**
eceb18cc 3269 * Test membership is renewed when transaction completed.
66d3f9be 3270 */
00be9182 3271 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
66d3f9be
EM
3272 $this->createPriceSetWithPage('membership');
3273 $this->setUpPendingContribution($this->_ids['price_field_value'][1]);
9099cab3
CW
3274 $this->callAPISuccess('contribution', 'completetransaction', ['id' => $this->_ids['contribution']]);
3275 $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $this->_ids['membership']]);
66d3f9be
EM
3276 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
3277 $this->cleanUpAfterPriceSets();
3278 }
3279
00be9182 3280 public function cleanUpAfterPriceSets() {
1cf3c2b1 3281 $this->quickCleanUpFinancialEntities();
66d3f9be 3282 $this->contactDelete($this->_ids['contact']);
66d3f9be
EM
3283 }
3284
66d3f9be 3285 /**
eceb18cc 3286 * Set up a pending transaction with a specific price field id.
1e52837d 3287 *
100fef9d 3288 * @param int $priceFieldValueID
39b959db 3289 * @param array $contriParams
66d3f9be 3290 */
9099cab3 3291 public function setUpPendingContribution($priceFieldValueID, $contriParams = []) {
66d3f9be 3292 $contactID = $this->individualCreate();
9099cab3 3293 $membership = $this->callAPISuccess('membership', 'create', [
66d3f9be
EM
3294 'contact_id' => $contactID,
3295 'membership_type_id' => $this->_ids['membership_type'],
3296 'start_date' => 'yesterday - 1 year',
3297 'end_date' => 'yesterday',
4ff927bc 3298 'join_date' => 'yesterday - 1 year',
9099cab3
CW
3299 ]);
3300 $contribution = $this->callAPISuccess('contribution', 'create', array_merge([
66d3f9be
EM
3301 'domain_id' => 1,
3302 'contact_id' => $contactID,
3303 'receive_date' => date('Ymd'),
0f07bb06 3304 'total_amount' => 20.00,
66d3f9be
EM
3305 'financial_type_id' => 1,
3306 'payment_instrument_id' => 'Credit Card',
3307 'non_deductible_amount' => 10.00,
98f0683a
JP
3308 'trxn_id' => 'jdhfi' . rand(1, 100),
3309 'invoice_id' => 'djfhiew' . rand(5, 100),
66d3f9be
EM
3310 'source' => 'SSF',
3311 'contribution_status_id' => 2,
3312 'contribution_page_id' => $this->_ids['contribution_page'],
9099cab3
CW
3313 'api.membership_payment.create' => ['membership_id' => $membership['id']],
3314 ], $contriParams));
66d3f9be 3315
9099cab3 3316 $this->callAPISuccess('line_item', 'create', [
66d3f9be
EM
3317 'entity_id' => $contribution['id'],
3318 'entity_table' => 'civicrm_contribution',
1274acef 3319 'contribution_id' => $contribution['id'],
66d3f9be
EM
3320 'price_field_id' => $this->_ids['price_field'][0],
3321 'qty' => 1,
3322 'unit_price' => 20,
3323 'line_total' => 20,
3324 'financial_type_id' => 1,
3325 'price_field_value_id' => $priceFieldValueID,
9099cab3 3326 ]);
66d3f9be
EM
3327 $this->_ids['contact'] = $contactID;
3328 $this->_ids['contribution'] = $contribution['id'];
3329 $this->_ids['membership'] = $membership['id'];
0efa8efe 3330 }
3331
2f45e1c2 3332 /**
eceb18cc 3333 * Test sending a mail via the API.
6a488035 3334 */
00be9182 3335 public function testSendMail() {
5896d037 3336 $mut = new CiviMailUtils($this, TRUE);
6c6e6187 3337 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
9099cab3 3338 $this->callAPISuccess('contribution', 'sendconfirmation', [
39b959db
SL
3339 'id' => $contribution['id'],
3340 'receipt_from_email' => 'api@civicrm.org',
9099cab3
CW
3341 ]);
3342 $mut->checkMailLog([
39b959db
SL
3343 '$ 100.00',
3344 'Contribution Information',
3345 'Please print this confirmation for your records',
9099cab3 3346 ], [
39b959db 3347 'Event',
9099cab3 3348 ]);
128d44a1 3349
3350 $this->checkCreditCardDetails($mut, $contribution['id']);
6a488035
TO
3351 $mut->stop();
3352 }
3353
dbacb875
AS
3354 /**
3355 * Test sending a mail via the API.
3356 * This simulates webform_civicrm using pay later contribution page
3357 */
3358 public function testSendconfirmationPayLater() {
3359 $mut = new CiviMailUtils($this, TRUE);
3360
3361 // Create contribution page
9099cab3 3362 $pageParams = [
dbacb875
AS
3363 'title' => 'Webform Contributions',
3364 'financial_type_id' => 1,
3365 'contribution_type_id' => 1,
3366 'is_confirm_enabled' => 1,
3367 'is_pay_later' => 1,
3368 'pay_later_text' => 'I will send payment by cheque',
3369 'pay_later_receipt' => 'Send your cheque payable to "CiviCRM LLC" to the office',
9099cab3 3370 ];
dbacb875
AS
3371 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $pageParams);
3372
3373 // Create pay later contribution
9099cab3 3374 $contribParams = [
dbacb875
AS
3375 'contact_id' => $this->_individualId,
3376 'financial_type_id' => 1,
3377 'is_pay_later' => 1,
3378 'contribution_status_id' => 2,
3379 'contribution_page_id' => $contributionPage['id'],
3380 'total_amount' => '10.00',
9099cab3 3381 ];
dbacb875
AS
3382 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
3383
3384 // Create line item
9099cab3 3385 $lineItemParams = [
dbacb875
AS
3386 'contribution_id' => $contribution['id'],
3387 'entity_id' => $contribution['id'],
3388 'entity_table' => 'civicrm_contribution',
3389 'label' => 'My lineitem label',
3390 'qty' => 1,
3391 'unit_price' => "10.00",
3392 'line_total' => "10.00",
9099cab3 3393 ];
dbacb875
AS
3394 $lineItem = $this->callAPISuccess('lineItem', 'create', $lineItemParams);
3395
3396 // Create email
3397 try {
9099cab3 3398 civicrm_api3('contribution', 'sendconfirmation', [
39b959db
SL
3399 'id' => $contribution['id'],
3400 'receipt_from_email' => 'api@civicrm.org',
9099cab3 3401 ]);
717fdb8a
AS
3402 }
3403 catch (Exception $e) {
dbacb875
AS
3404 // Need to figure out how to stop this some other day
3405 // We don't care about the Payment Processor because this is Pay Later
3406 // The point of this test is to check we get the pay_later version of the mail
3407 if ($e->getMessage() != "Undefined variable: CRM16923AnUnreliableMethodHasBeenUserToDeterminePaymentProcessorFromContributionPage") {
3408 throw $e;
3409 }
3410 }
3411
3412 // Retrieve mail & check it has the pay_later_receipt info
3413 $mut->getMostRecentEmail('raw');
9099cab3 3414 $mut->checkMailLog([
39b959db
SL
3415 (string) $contribParams['total_amount'],
3416 $pageParams['pay_later_receipt'],
9099cab3 3417 ], [
39b959db 3418 'Event',
9099cab3 3419 ]);
dbacb875
AS
3420 $mut->stop();
3421 }
3422
128d44a1 3423 /**
3424 * Check credit card details in sent mail via API
3425 *
3426 * @param $mut obj CiviMailUtils instance
3427 * @param int $contributionID Contribution ID
3428 *
3429 */
3430 public function checkCreditCardDetails($mut, $contributionID) {
3431 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
9099cab3 3432 $this->callAPISuccess('contribution', 'sendconfirmation', [
39b959db
SL
3433 'id' => $contributionID,
3434 'receipt_from_email' => 'api@civicrm.org',
3435 'payment_processor_id' => $this->paymentProcessorID,
9099cab3
CW
3436 ]);
3437 $mut->checkMailLog([
39b959db
SL
3438 // credit card header
3439 'Credit Card Information',
3440 // billing header
3441 'Billing Name and Address',
3442 // billing name
3443 'anthony_anderson@civicrm.org',
9099cab3 3444 ], [
39b959db 3445 'Event',
9099cab3 3446 ]);
128d44a1 3447 }
3448
2f45e1c2 3449 /**
eceb18cc 3450 * Test sending a mail via the API.
6a488035 3451 */
00be9182 3452 public function testSendMailEvent() {
5896d037 3453 $mut = new CiviMailUtils($this, TRUE);
6c6e6187 3454 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
9099cab3 3455 $event = $this->eventCreate([
6a488035
TO
3456 'is_email_confirm' => 1,
3457 'confirm_from_email' => 'test@civicrm.org',
9099cab3 3458 ]);
6a488035 3459 $this->_eventID = $event['id'];
9099cab3 3460 $participantParams = [
6a488035
TO
3461 'contact_id' => $this->_individualId,
3462 'event_id' => $this->_eventID,
3463 'status_id' => 1,
3464 'role_id' => 1,
3465 // to ensure it matches later on
3466 'register_date' => '2007-07-21 00:00:00',
3467 'source' => 'Online Event Registration: API Testing',
4ab7d517 3468
9099cab3 3469 ];
2f45e1c2 3470 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
9099cab3 3471 $this->callAPISuccess('participant_payment', 'create', [
6a488035
TO
3472 'participant_id' => $participant['id'],
3473 'contribution_id' => $contribution['id'],
9099cab3
CW
3474 ]);
3475 $this->callAPISuccess('contribution', 'sendconfirmation', [
39b959db
SL
3476 'id' => $contribution['id'],
3477 'receipt_from_email' => 'api@civicrm.org',
9099cab3 3478 ]);
6a488035 3479
9099cab3 3480 $mut->checkMailLog([
39b959db
SL
3481 'Annual CiviCRM meet',
3482 'Event',
3483 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
9099cab3 3484 ], []);
6a488035
TO
3485 $mut->stop();
3486 }
3487
4302618d 3488 /**
1e52837d
EM
3489 * This function does a GET & compares the result against the $params.
3490 *
3491 * Use as a double check on Creates.
3492 *
3493 * @param array $params
3494 * @param int $id
67f947ac 3495 * @param bool $delete
6c6e6187 3496 */
1e52837d 3497 public function contributionGetnCheck($params, $id, $delete = TRUE) {
6a488035 3498
9099cab3 3499 $contribution = $this->callAPISuccess('Contribution', 'Get', [
6a488035 3500 'id' => $id,
9099cab3 3501 ]);
6a488035
TO
3502
3503 if ($delete) {
9099cab3 3504 $this->callAPISuccess('contribution', 'delete', ['id' => $id]);
6a488035 3505 }
2bfae985 3506 $this->assertAPISuccess($contribution, 0);
6a488035
TO
3507 $values = $contribution['values'][$contribution['id']];
3508 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
3509 // this is not returned in id format
3510 unset($params['payment_instrument_id']);
3511 $params['contribution_source'] = $params['source'];
3512 unset($params['source']);
3513 foreach ($params as $key => $value) {
22f80e87 3514 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
6a488035
TO
3515 }
3516 }
3517
294cc627 3518 /**
3519 * Create a pending contribution & linked pending pledge record.
3520 */
3521 public function createPendingPledgeContribution() {
3522
9099cab3 3523 $pledgeID = $this->pledgeCreate(['contact_id' => $this->_individualId, 'installments' => 1, 'amount' => 500]);
294cc627 3524 $this->_ids['pledge'] = $pledgeID;
9099cab3 3525 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params, [
294cc627 3526 'contribution_status_id' => 'Pending',
39b959db 3527 'total_amount' => 500,
9099cab3
CW
3528 ]));
3529 $paymentID = $this->callAPISuccessGetValue('PledgePayment', [
3530 'options' => ['limit' => 1],
294cc627 3531 'return' => 'id',
9099cab3
CW
3532 ]);
3533 $this->callAPISuccess('PledgePayment', 'create', [
294cc627 3534 'id' => $paymentID,
3535 'contribution_id' =>
3536 $contribution['id'],
3537 'status_id' => 'Pending',
3538 'scheduled_amount' => 500,
9099cab3 3539 ]);
294cc627 3540
3541 return $contribution['id'];
3542 }
3543
0efa8efe 3544 /**
1e52837d 3545 * Create a pending contribution & linked pending participant record (along with an event).
0efa8efe 3546 */
5896d037 3547 public function createPendingParticipantContribution() {
ed7e2e99 3548 $this->_ids['event']['test'] = $this->eventCreate(['is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'])['id'];
3549 $participantID = $this->participantCreate(['event_id' => $this->_ids['event']['test'], 'status_id' => 6, 'contact_id' => $this->_individualId]);
5896d037 3550 $this->_ids['participant'] = $participantID;
9099cab3 3551 $params = array_merge($this->_params, ['contact_id' => $this->_individualId, 'contribution_status_id' => 2, 'financial_type_id' => 'Event Fee']);
6c6e6187 3552 $contribution = $this->callAPISuccess('contribution', 'create', $params);
9099cab3 3553 $this->callAPISuccess('participant_payment', 'create', [
92915c55
TO
3554 'contribution_id' => $contribution['id'],
3555 'participant_id' => $participantID,
9099cab3
CW
3556 ]);
3557 $this->callAPISuccess('line_item', 'get', [
0efa8efe 3558 'entity_id' => $contribution['id'],
3559 'entity_table' => 'civicrm_contribution',
9099cab3 3560 'api.line_item.create' => [
0efa8efe 3561 'entity_id' => $participantID,
3562 'entity_table' => 'civicrm_participant',
9099cab3
CW
3563 ],
3564 ]);
0efa8efe 3565 return $contribution['id'];
3566 }
3567
4cbe18b8 3568 /**
1e52837d
EM
3569 * Get financial transaction amount.
3570 *
100fef9d 3571 * @param int $contId
4cbe18b8
EM
3572 *
3573 * @return null|string
f4d89200 3574 */
2da40d21 3575 public function _getFinancialTrxnAmount($contId) {
6c6e6187 3576 $query = "SELECT
6a488035
TO
3577 SUM( ft.total_amount ) AS total
3578 FROM civicrm_financial_trxn AS ft
3579 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
3580 WHERE ceft.entity_table = 'civicrm_contribution'
3581 AND ceft.entity_id = {$contId}";
3582
6c6e6187
TO
3583 $result = CRM_Core_DAO::singleValueQuery($query);
3584 return $result;
3585 }
6a488035 3586
4cbe18b8 3587 /**
100fef9d 3588 * @param int $contId
4cbe18b8
EM
3589 *
3590 * @return null|string
f4d89200 3591 */
2da40d21 3592 public function _getFinancialItemAmount($contId) {
6c6e6187
TO
3593 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
3594 $query = "SELECT
6a488035
TO
3595 SUM(amount)
3596 FROM civicrm_financial_item
3597 WHERE entity_table = 'civicrm_line_item'
3598 AND entity_id = {$lineItem}";
6c6e6187
TO
3599 $result = CRM_Core_DAO::singleValueQuery($query);
3600 return $result;
3601 }
6a488035 3602
4cbe18b8 3603 /**
100fef9d 3604 * @param int $contId
4cbe18b8
EM
3605 * @param $context
3606 */
00be9182 3607 public function _checkFinancialItem($contId, $context) {
6c6e6187 3608 if ($context != 'paylater') {
9099cab3 3609 $params = [
5896d037
TO
3610 'entity_id' => $contId,
3611 'entity_table' => 'civicrm_contribution',
9099cab3 3612 ];
6c6e6187 3613 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($params, TRUE));
9099cab3 3614 $entityParams = [
6a488035
TO
3615 'financial_trxn_id' => $trxn['financial_trxn_id'],
3616 'entity_table' => 'civicrm_financial_item',
9099cab3 3617 ];
6c6e6187 3618 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
9099cab3 3619 $params = [
6a488035 3620 'id' => $entityTrxn['entity_id'],
9099cab3 3621 ];
6c6e6187
TO
3622 }
3623 if ($context == 'paylater') {
3624 $lineItems = CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution');
3625 foreach ($lineItems as $key => $item) {
9099cab3 3626 $params = [
5896d037
TO
3627 'entity_id' => $key,
3628 'entity_table' => 'civicrm_line_item',
9099cab3
CW
3629 ];
3630 $compareParams = ['status_id' => 1];
6c6e6187
TO
3631 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3632 }
3633 }
3634 elseif ($context == 'refund') {
9099cab3 3635 $compareParams = [
5896d037
TO
3636 'status_id' => 1,
3637 'financial_account_id' => 1,
3638 'amount' => -100,
9099cab3 3639 ];
6c6e6187
TO
3640 }
3641 elseif ($context == 'cancelPending') {
9099cab3 3642 $compareParams = [
5896d037
TO
3643 'status_id' => 3,
3644 'financial_account_id' => 1,
3645 'amount' => -100,
9099cab3 3646 ];
6c6e6187
TO
3647 }
3648 elseif ($context == 'changeFinancial') {
3649 $lineKey = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
9099cab3 3650 $params = [
5896d037
TO
3651 'entity_id' => $lineKey,
3652 'amount' => -100,
9099cab3
CW
3653 ];
3654 $compareParams = [
5896d037 3655 'financial_account_id' => 1,
9099cab3 3656 ];
6c6e6187 3657 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
9099cab3 3658 $params = [
5896d037
TO
3659 'financial_account_id' => 3,
3660 'entity_id' => $lineKey,
9099cab3
CW
3661 ];
3662 $compareParams = [
5896d037 3663 'amount' => 100,
9099cab3 3664 ];
6c6e6187
TO
3665 }
3666 if ($context != 'paylater') {
3667 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3668 }
3669 }
6a488035 3670
b0e806fa 3671 /**
3672 * Check correct financial transaction entries were created for the change in payment instrument.
3673 *
3674 * @param int $contributionID
3675 * @param int $originalInstrumentID
3676 * @param int $newInstrumentID
39b959db 3677 * @param int $amount
b0e806fa 3678 */
3679 public function checkFinancialTrxnPaymentInstrumentChange($contributionID, $originalInstrumentID, $newInstrumentID, $amount = 100) {
3680
3681 $entityFinancialTrxns = $this->getFinancialTransactionsForContribution($contributionID);
3682
9099cab3 3683 $originalTrxnParams = [
b0e806fa 3684 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($originalInstrumentID),
3685 'payment_instrument_id' => $originalInstrumentID,
3686 'amount' => $amount,
3687 'status_id' => 1,
9099cab3 3688 ];
b0e806fa 3689
9099cab3 3690 $reversalTrxnParams = [
b0e806fa 3691 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($originalInstrumentID),
3692 'payment_instrument_id' => $originalInstrumentID,
3693 'amount' => -$amount,
3694 'status_id' => 1,
9099cab3 3695 ];
b0e806fa 3696
9099cab3 3697 $newTrxnParams = [
b0e806fa 3698 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($newInstrumentID),
3699 'payment_instrument_id' => $newInstrumentID,
3700 'amount' => $amount,
3701 'status_id' => 1,
9099cab3 3702 ];
b0e806fa 3703
9099cab3 3704 foreach ([$originalTrxnParams, $reversalTrxnParams, $newTrxnParams] as $index => $transaction) {
b0e806fa 3705 $entityFinancialTrxn = $entityFinancialTrxns[$index];
3706 $this->assertEquals($entityFinancialTrxn['amount'], $transaction['amount']);
3707
9099cab3 3708 $financialTrxn = $this->callAPISuccessGetSingle('FinancialTrxn', [
b0e806fa 3709 'id' => $entityFinancialTrxn['financial_trxn_id'],
9099cab3 3710 ]);
b0e806fa 3711 $this->assertEquals($transaction['status_id'], $financialTrxn['status_id']);
3712 $this->assertEquals($transaction['amount'], $financialTrxn['total_amount']);
3713 $this->assertEquals($transaction['amount'], $financialTrxn['net_amount']);
3714 $this->assertEquals(0, $financialTrxn['fee_amount']);
3715 $this->assertEquals($transaction['payment_instrument_id'], $financialTrxn['payment_instrument_id']);
3716 $this->assertEquals($transaction['to_financial_account_id'], $financialTrxn['to_financial_account_id']);
3717
3718 // Generic checks.
3719 $this->assertEquals(1, $financialTrxn['is_payment']);
3720 $this->assertEquals('USD', $financialTrxn['currency']);
3721 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($financialTrxn['trxn_date'])));
3722 }
3723 }
3724
4cbe18b8 3725 /**
52da5b1e 3726 * Check financial transaction.
3727 *
3728 * @todo break this down into sensible functions - most calls to it only use a few lines out of the big if.
3729 *
4ff927bc 3730 * @param array $contribution
3731 * @param string $context
100fef9d 3732 * @param int $instrumentId
52da5b1e 3733 * @param array $extraParams
4cbe18b8 3734 */
9099cab3 3735 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL, $extraParams = []) {
b0e806fa 3736 $financialTrxns = $this->getFinancialTransactionsForContribution($contribution['id']);
3737 $trxn = array_pop($financialTrxns);
3738
9099cab3 3739 $params = [
5896d037 3740 'id' => $trxn['financial_trxn_id'],
9099cab3 3741 ];
6c6e6187 3742 if ($context == 'payLater') {
9099cab3 3743 $compareParams = [
5896d037 3744 'status_id' => 1,
876b8ab0 3745 'from_financial_account_id' => CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($contribution['financial_type_id'], 'Accounts Receivable Account is'),
9099cab3 3746 ];
6c6e6187
TO
3747 }
3748 elseif ($context == 'refund') {
9099cab3 3749 $compareParams = [
5896d037
TO
3750 'to_financial_account_id' => 6,
3751 'total_amount' => -100,
3752 'status_id' => 7,
b7990bb6 3753 'trxn_date' => '2015-01-01 09:00:00',
797d4c52 3754 'trxn_id' => 'the refund',
9099cab3 3755 ];
6c6e6187
TO
3756 }
3757 elseif ($context == 'cancelPending') {
9099cab3 3758 $compareParams = [
ed4d0aea 3759 'to_financial_account_id' => 7,
5896d037
TO
3760 'total_amount' => -100,
3761 'status_id' => 3,
9099cab3 3762 ];
6c6e6187
TO
3763 }
3764 elseif ($context == 'changeFinancial' || $context == 'paymentInstrument') {
b0e806fa 3765 // @todo checkFinancialTrxnPaymentInstrumentChange instead for paymentInstrument.
3766 // It does the same thing with greater readability.
3767 // @todo remove handling for
3768
9099cab3 3769 $entityParams = [
5896d037
TO
3770 'entity_id' => $contribution['id'],
3771 'entity_table' => 'civicrm_contribution',
3772 'amount' => -100,
9099cab3 3773 ];
6c6e6187 3774 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
9099cab3 3775 $trxnParams1 = [
6a488035 3776 'id' => $trxn['financial_trxn_id'],
9099cab3 3777 ];
122250ec 3778 if (empty($extraParams)) {
9099cab3 3779 $compareParams = [
122250ec
SL
3780 'total_amount' => -100,
3781 'status_id' => 1,
9099cab3 3782 ];
122250ec
SL
3783 }
3784 else {
9099cab3 3785 $compareParams = [
122250ec
SL
3786 'total_amount' => 100,
3787 'status_id' => 1,
9099cab3 3788 ];
122250ec 3789 }
6c6e6187
TO
3790 if ($context == 'paymentInstrument') {
3791 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($instrumentId);
3792 $compareParams['payment_instrument_id'] = $instrumentId;
3793 }
3794 else {
3795 $compareParams['to_financial_account_id'] = 12;
3796 }
5ca657dd 3797 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, array_merge($compareParams, $extraParams));
3798 $compareParams['total_amount'] = 100;
6c6e6187
TO
3799 }
3800
797d4c52 3801 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, array_merge($compareParams, $extraParams));
6c6e6187 3802 }
6a488035 3803
4cbe18b8
EM
3804 /**
3805 * @return mixed
3806 */
5896d037 3807 public function _addPaymentInstrument() {
6c6e6187 3808 $gId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
9099cab3 3809 $optionParams = [
5896d037
TO
3810 'option_group_id' => $gId,
3811 'label' => 'Test Card',
3812 'name' => 'Test Card',
3813 'value' => '6',
3814 'weight' => '6',
3815 'is_active' => 1,
9099cab3 3816 ];
6c6e6187
TO
3817 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
3818 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
9099cab3 3819 $financialParams = [
5896d037
TO
3820 'entity_table' => 'civicrm_option_value',
3821 'entity_id' => $optionValue['id'],
3822 'account_relationship' => $relationTypeId,
3823 'financial_account_id' => 7,
9099cab3 3824 ];
db62d3a5 3825 CRM_Financial_BAO_FinancialTypeAccount::add($financialParams);
6c6e6187
TO
3826 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
3827 return $optionValue['values'][$optionValue['id']]['value'];
3828 }
6a488035 3829
02a9c0a4 3830 public function _deletedAddedPaymentInstrument() {
9099cab3 3831 $result = $this->callAPISuccess('OptionValue', 'get', [
02a9c0a4 3832 'option_group_id' => 'payment_instrument',
3833 'name' => 'Test Card',
3834 'value' => '6',
3835 'is_active' => 1,
9099cab3 3836 ]);
02a9c0a4 3837 if ($id = CRM_Utils_Array::value('id', $result)) {
9099cab3 3838 $this->callAPISuccess('OptionValue', 'delete', ['id' => $id]);
02a9c0a4 3839 }
3840 }
3841
3c49d90c 3842 /**
3843 * Set up the basic recurring contribution for tests.
3844 *
3845 * @param array $generalParams
3846 * Parameters that can be merged into the recurring AND the contribution.
7f4ef731 3847 *
3848 * @param array $recurParams
3849 * Parameters to merge into the recur only.
3c49d90c 3850 *
3851 * @return array|int
3852 */
9099cab3
CW
3853 protected function setUpRecurringContribution($generalParams = [], $recurParams = []) {
3854 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge([
3c49d90c 3855 'contact_id' => $this->_individualId,
3856 'installments' => '12',
3857 'frequency_interval' => '1',
3858 'amount' => '100',
3859 'contribution_status_id' => 1,
3860 'start_date' => '2012-01-01 00:00:00',
3861 'currency' => 'USD',
3862 'frequency_unit' => 'month',
3863 'payment_processor_id' => $this->paymentProcessorID,
9099cab3 3864 ], $generalParams, $recurParams));
3c49d90c 3865 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3866 $this->_params,
9099cab3 3867 [
3c49d90c 3868 'contribution_recur_id' => $contributionRecur['id'],
9099cab3 3869 ], $generalParams)
3c49d90c 3870 );
3871 return $originalContribution;
3872 }
3873
d2334242
PH
3874 /**
3875 * Set up a basic auto-renew membership for tests.
3876 *
3877 * @param array $generalParams
3878 * Parameters that can be merged into the recurring AND the contribution.
3879 *
3880 * @param array $recurParams
3881 * Parameters to merge into the recur only.
3882 *
3883 * @return array|int
3884 */
9099cab3
CW
3885 protected function setUpAutoRenewMembership($generalParams = [], $recurParams = []) {
3886 $newContact = $this->callAPISuccess('Contact', 'create', [
39b959db
SL
3887 'contact_type' => 'Individual',
3888 'sort_name' => 'McTesterson, Testy',
3889 'display_name' => 'Testy McTesterson',
3890 'preferred_language' => 'en_US',
3891 'preferred_mail_format' => 'Both',
3892 'first_name' => 'Testy',
3893 'last_name' => 'McTesterson',
3894 'contact_is_deleted' => '0',
3895 'email_id' => '4',
3896 'email' => 'tmctesterson@example.com',
3897 'on_hold' => '0',
9099cab3
CW
3898 ]);
3899 $membershipType = $this->callAPISuccess('MembershipType', 'create', [
d2334242
PH
3900 'domain_id' => "Default Domain Name",
3901 'member_of_contact_id' => 1,
3902 'financial_type_id' => "Member Dues",
3903 'duration_unit' => "month",
3904 'duration_interval' => 1,
5b1b8db2 3905 'period_type' => 'rolling',
d2334242
PH
3906 'name' => "Standard Member",
3907 'minimum_fee' => 100,
9099cab3
CW
3908 ]);
3909 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge([
76e80087 3910 'contact_id' => $newContact['id'],
d2334242
PH
3911 'installments' => '12',
3912 'frequency_interval' => '1',
3913 'amount' => '100',
3914 'contribution_status_id' => 1,
3915 'start_date' => '2012-01-01 00:00:00',
3916 'currency' => 'USD',
3917 'frequency_unit' => 'month',
3918 'payment_processor_id' => $this->paymentProcessorID,
9099cab3 3919 ], $generalParams, $recurParams));
7c3f3d59 3920
9099cab3 3921 $membership = $this->callAPISuccess('membership', 'create', [
7c3f3d59 3922 'contact_id' => $newContact['id'],
3923 'contribution_recur_id' => $contributionRecur['id'],
3924 'financial_type_id' => "Member Dues",
3925 'membership_type_id' => $membershipType['id'],
3926 'num_terms' => 1,
3927 'skipLineItem' => TRUE,
9099cab3 3928 ]);
7c3f3d59 3929
3930 CRM_Price_BAO_LineItem::getLineItemArray($this->_params, NULL, 'membership', $membershipType['id']);
d2334242
PH
3931 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3932 $this->_params,
9099cab3 3933 [
d2334242
PH
3934 'contact_id' => $newContact['id'],
3935 'contribution_recur_id' => $contributionRecur['id'],
3936 'financial_type_id' => "Member Dues",
3937 'contribution_status_id' => 1,
3938 'invoice_id' => uniqid(),
9099cab3 3939 ], $generalParams)
d2334242 3940 );
9099cab3 3941 $lineItem = $this->callAPISuccess('LineItem', 'getsingle', []);
7c3f3d59 3942 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
9099cab3
CW
3943 $membership = $this->callAPISuccess('Membership', 'getsingle', ['id' => $lineItem['entity_id']]);
3944 $this->callAPISuccess('LineItem', 'getsingle', []);
3945 $this->callAPISuccessGetCount('MembershipPayment', ['membership_id' => $membership['id']], 1);
d2334242 3946
9099cab3 3947 return [$originalContribution, $membership];
d2334242 3948 }
39b959db 3949
893a550c 3950 /**
3951 * Set up a repeat transaction.
3952 *
3953 * @param array $recurParams
39b959db
SL
3954 * @param mixed $flag
3955 * @param array $contributionParams
893a550c 3956 * @return array
3957 */
9099cab3 3958 protected function setUpRepeatTransaction($recurParams = [], $flag, $contributionParams = []) {
893a550c 3959 $paymentProcessorID = $this->paymentProcessorCreate();
9099cab3 3960 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge([
893a550c 3961 'contact_id' => $this->_individualId,
3962 'installments' => '12',
3963 'frequency_interval' => '1',
3964 'amount' => '500',
3965 'contribution_status_id' => 1,
3966 'start_date' => '2012-01-01 00:00:00',
3967 'currency' => 'USD',
3968 'frequency_unit' => 'month',
3969 'payment_processor_id' => $paymentProcessorID,
9099cab3 3970 ], $recurParams));
0e6ccb2e 3971
7150b1c8 3972 $originalContribution = '';
0e6ccb2e 3973 if ($flag == 'multiple') {
7150b1c8 3974 // CRM-19309 create a contribution + also add in line_items (plural):
19893cf2 3975 $params = array_merge($this->_params, $contributionParams);
0e6ccb2e 3976 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
19893cf2 3977 $params,
9099cab3 3978 [
0e6ccb2e
K
3979 'contribution_recur_id' => $contributionRecur['id'],
3980 'skipLineItem' => 1,
9099cab3
CW
3981 'api.line_item.create' => [
3982 [
0e6ccb2e
K
3983 'price_field_id' => 1,
3984 'qty' => 2,
3985 'line_total' => '20',
3986 'unit_price' => '10',
3987 'financial_type_id' => 1,
9099cab3
CW
3988 ],
3989 [
0e6ccb2e
K
3990 'price_field_id' => 1,
3991 'qty' => 1,
3992 'line_total' => '80',
3993 'unit_price' => '80',
3994 'financial_type_id' => 2,
9099cab3
CW
3995 ],
3996 ],
3997 ]
0e6ccb2e
K
3998 )
3999 );
4000 }
4001 elseif ($flag == 'single') {
9099cab3 4002 $params = array_merge($this->_params, ['contribution_recur_id' => $contributionRecur['id']]);
19893cf2
SL
4003 $params = array_merge($params, $contributionParams);
4004 $originalContribution = $this->callAPISuccess('contribution', 'create', $params);
0e6ccb2e 4005 }
f69a9ac3 4006 $originalContribution['payment_processor_id'] = $paymentProcessorID;
893a550c 4007 return $originalContribution;
4008 }
4009
ec7e3954
E
4010 /**
4011 * Common set up routine.
4012 *
4013 * @return array
4014 */
4015 protected function setUpForCompleteTransaction() {
4016 $this->mut = new CiviMailUtils($this, TRUE);
4017 $this->createLoggedInUser();
9099cab3 4018 $params = array_merge($this->_params, ['contribution_status_id' => 2, 'receipt_date' => 'now']);
ec7e3954
E
4019 $contribution = $this->callAPISuccess('contribution', 'create', $params);
4020 return $contribution;
4021 }
4022
9c01d961
SL
4023 /**
4024 * Test repeat contribution uses the Payment Processor' payment_instrument setting.
4025 */
4026 public function testRepeatTransactionWithNonCreditCardDefault() {
9099cab3 4027 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', [
9c01d961
SL
4028 'contact_id' => $this->_individualId,
4029 'installments' => '12',
4030 'frequency_interval' => '1',
4031 'amount' => '100',
4032 'contribution_status_id' => 1,
4033 'start_date' => '2012-01-01 00:00:00',
4034 'currency' => 'USD',
4035 'frequency_unit' => 'month',
4036 'payment_processor_id' => $this->paymentProcessorID,
9099cab3 4037 ]);
9c01d961
SL
4038 $contribution1 = $this->callAPISuccess('contribution', 'create', array_merge(
4039 $this->_params,
9099cab3 4040 ['contribution_recur_id' => $contributionRecur['id'], 'payment_instrument_id' => 2])
9c01d961
SL
4041 );
4042 $paymentInstruments = CRM_Contribute_PseudoConstant::paymentInstrument('name');
9099cab3 4043 $contribution2 = $this->callAPISuccess('contribution', 'repeattransaction', [
9c01d961
SL
4044 'contribution_status_id' => 'Completed',
4045 'trxn_id' => uniqid(),
4046 'original_contribution_id' => $contribution1,
9099cab3 4047 ]);
9c01d961
SL
4048 $this->assertEquals(array_search('Debit Card', $paymentInstruments), $contribution2['values'][$contribution2['id']]['payment_instrument_id']);
4049 $this->quickCleanUpFinancialEntities();
4050 }
4051
ee63135d 4052 /**
4053 * CRM-20008 Tests repeattransaction creates pending membership.
4054 */
37f29fcf 4055 public function testRepeatTransactionMembershipCreatePendingContribution() {
ee63135d 4056 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
9099cab3 4057 $this->callAPISuccess('membership', 'create', [
ee63135d 4058 'id' => $membership['id'],
4059 'end_date' => 'yesterday',
4060 'status_id' => 'Expired',
9099cab3
CW
4061 ]);
4062 $repeatedContribution = $this->callAPISuccess('contribution', 'repeattransaction', [
ee63135d 4063 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
4064 'contribution_status_id' => 'Pending',
4065 'trxn_id' => uniqid(),
9099cab3
CW
4066 ]);
4067 $membershipStatusId = $this->callAPISuccess('membership', 'getvalue', [
ee63135d 4068 'id' => $membership['id'],
4069 'return' => 'status_id',
9099cab3 4070 ]);
ee63135d 4071
4072 // Let's see if the membership payments got created while we're at it.
9099cab3 4073 $membershipPayments = $this->callAPISuccess('MembershipPayment', 'get', [
37f29fcf 4074 'membership_id' => $membership['id'],
9099cab3 4075 ]);
ee63135d 4076 $this->assertEquals(2, $membershipPayments['count']);
4077
4078 $this->assertEquals('Expired', CRM_Core_PseudoConstant::getLabel('CRM_Member_BAO_Membership', 'status_id', $membershipStatusId));
9099cab3
CW
4079 $this->callAPISuccess('Contribution', 'completetransaction', ['id' => $repeatedContribution['id']]);
4080 $membership = $this->callAPISuccessGetSingle('membership', [
ee63135d 4081 'id' => $membership['id'],
4082 'return' => 'status_id, end_date',
9099cab3 4083 ]);
37f29fcf 4084 $this->assertEquals('New', CRM_Core_PseudoConstant::getName('CRM_Member_BAO_Membership', 'status_id', $membership['status_id']));
ee63135d 4085 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 month')), $membership['end_date']);
4086
4087 $this->quickCleanUpFinancialEntities();
4088 $this->contactDelete($originalContribution['values'][1]['contact_id']);
4089 }
4090
cefed6df
SL
4091 /**
4092 * Test sending a mail via the API.
4093 */
4094 public function testSendMailWithAPISetFromDetails() {
4095 $mut = new CiviMailUtils($this, TRUE);
4096 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
9099cab3 4097 $this->callAPISuccess('contribution', 'sendconfirmation', [
cefed6df
SL
4098 'id' => $contribution['id'],
4099 'receipt_from_email' => 'api@civicrm.org',
4100 'receipt_from_name' => 'CiviCRM LLC',
9099cab3
CW
4101 ]);
4102 $mut->checkMailLog([
39b959db
SL
4103 'From: CiviCRM LLC <api@civicrm.org>',
4104 'Contribution Information',
4105 'Please print this confirmation for your records',
9099cab3 4106 ], [
39b959db 4107 'Event',
9099cab3 4108 ]);
cefed6df
SL
4109 $mut->stop();
4110 }
4111
4112 /**
4113 * Test sending a mail via the API.
4114 */
4115 public function testSendMailWithNoFromSetFallToDomain() {
4116 $this->createLoggedInUser();
4117 $mut = new CiviMailUtils($this, TRUE);
4118 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
9099cab3 4119 $this->callAPISuccess('contribution', 'sendconfirmation', [
cefed6df 4120 'id' => $contribution['id'],
9099cab3
CW
4121 ]);
4122 $domain = $this->callAPISuccess('domain', 'getsingle', ['id' => 1]);
4123 $mut->checkMailLog([
39b959db
SL
4124 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>',
4125 'Contribution Information',
4126 'Please print this confirmation for your records',
9099cab3 4127 ], [
39b959db 4128 'Event',
9099cab3 4129 ]);
cefed6df
SL
4130 $mut->stop();
4131 }
4132
4133 /**
4134 * Test sending a mail via the API.
4135 */
4136 public function testSendMailWithRepeatTransactionAPIFalltoDomain() {
4137 $this->createLoggedInUser();
4138 $mut = new CiviMailUtils($this, TRUE);
9099cab3
CW
4139 $contribution = $this->setUpRepeatTransaction([], 'single');
4140 $this->callAPISuccess('contribution', 'repeattransaction', [
cefed6df
SL
4141 'contribution_status_id' => 'Completed',
4142 'trxn_id' => uniqid(),
4143 'original_contribution_id' => $contribution,
9099cab3
CW
4144 ]);
4145 $domain = $this->callAPISuccess('domain', 'getsingle', ['id' => 1]);
4146 $mut->checkMailLog([
39b959db
SL
4147 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>',
4148 'Contribution Information',
4149 'Please print this confirmation for your records',
9099cab3 4150 ], [
39b959db 4151 'Event',
9099cab3 4152 ]
cefed6df
SL
4153 );
4154 $mut->stop();
4155 }
4156
4157 /**
4158 * Test sending a mail via the API.
4159 */
4160 public function testSendMailWithRepeatTransactionAPIFalltoContributionPage() {
4161 $mut = new CiviMailUtils($this, TRUE);
9099cab3 4162 $contributionPage = $this->contributionPageCreate(['receipt_from_name' => 'CiviCRM LLC', 'receipt_from_email' => 'contributionpage@civicrm.org', 'is_email_receipt' => 1]);
cefed6df 4163 $paymentProcessorID = $this->paymentProcessorCreate();
9099cab3 4164 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', [
cefed6df
SL
4165 'contact_id' => $this->_individualId,
4166 'installments' => '12',
4167 'frequency_interval' => '1',
4168 'amount' => '500',
4169 'contribution_status_id' => 1,
4170 'start_date' => '2012-01-01 00:00:00',
4171 'currency' => 'USD',
4172 'frequency_unit' => 'month',
4173 'payment_processor_id' => $paymentProcessorID,
9099cab3 4174 ]);
cefed6df
SL
4175 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
4176 $this->_params,
9099cab3 4177 [
cefed6df 4178 'contribution_recur_id' => $contributionRecur['id'],
39b959db 4179 'contribution_page_id' => $contributionPage['id'],
9099cab3 4180 ])
cefed6df 4181 );
9099cab3 4182 $this->callAPISuccess('contribution', 'repeattransaction', [
cefed6df
SL
4183 'contribution_status_id' => 'Completed',
4184 'trxn_id' => uniqid(),
4185 'original_contribution_id' => $originalContribution,
9099cab3 4186 ]
cefed6df 4187 );
9099cab3 4188 $mut->checkMailLog([
39b959db
SL
4189 'From: CiviCRM LLC <contributionpage@civicrm.org>',
4190 'Contribution Information',
4191 'Please print this confirmation for your records',
9099cab3 4192 ], [
39b959db 4193 'Event',
9099cab3 4194 ]);
cefed6df
SL
4195 $mut->stop();
4196 }
4197
4fb4e64f
SL
4198 /**
4199 * Test sending a mail via the API.
4200 */
4201 public function testSendMailWithRepeatTransactionAPIFalltoSystemFromNoDefaultFrom() {
4202 $mut = new CiviMailUtils($this, TRUE);
9099cab3
CW
4203 $originalContribution = $contribution = $this->setUpRepeatTransaction([], 'single');
4204 $fromEmail = $this->CallAPISuccess('optionValue', 'get', ['is_default' => 1, 'option_group_id' => 'from_email_address', 'sequential' => 1]);
4fb4e64f 4205 foreach ($fromEmail['values'] as $from) {
9099cab3 4206 $this->callAPISuccess('optionValue', 'create', ['is_default' => 0, 'id' => $from['id']]);
4fb4e64f 4207 }
9099cab3
CW
4208 $domain = $this->callAPISuccess('domain', 'getsingle', ['id' => CRM_Core_Config::domainID()]);
4209 $this->callAPISuccess('contribution', 'repeattransaction', [
4fb4e64f
SL
4210 'contribution_status_id' => 'Completed',
4211 'trxn_id' => uniqid(),
4212 'original_contribution_id' => $originalContribution,
9099cab3
CW
4213 ]);
4214 $mut->checkMailLog([
39b959db
SL
4215 'From: ' . $domain['name'] . ' <' . $domain['domain_email'] . '>',
4216 'Contribution Information',
4217 'Please print this confirmation for your records',
9099cab3 4218 ], [
39b959db 4219 'Event',
9099cab3 4220 ]);
4fb4e64f
SL
4221 $mut->stop();
4222 }
4223
d891a273 4224 /**
4225 * Create a Contribution Page with is_email_receipt = TRUE.
4226 *
4227 * @param array $params
4228 * Params to overwrite with.
4229 *
4230 * @return array|int
4231 */
9099cab3
CW
4232 protected function createReceiptableContributionPage($params = []) {
4233 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array_merge([
d891a273 4234 'receipt_from_name' => 'Mickey Mouse',
4235 'receipt_from_email' => 'mickey@mouse.com',
4236 'title' => "Test Contribution Page",
4237 'financial_type_id' => 1,
4238 'currency' => 'CAD',
4239 'is_monetary' => TRUE,
4240 'is_email_receipt' => TRUE,
9099cab3 4241 ], $params));
d891a273 4242 return $contributionPage;
4243 }
4244
121c4616 4245 /**
4246 * function to test card_type and pan truncation.
4247 */
4248 public function testCardTypeAndPanTruncation() {
4249 $creditCardTypeIDs = array_flip(CRM_Financial_DAO_FinancialTrxn::buildOptions('card_type_id'));
4250 $contactId = $this->individualCreate();
9099cab3 4251 $params = [
121c4616 4252 'contact_id' => $contactId,
4253 'receive_date' => '2016-01-20',
4254 'total_amount' => 100,
4255 'financial_type_id' => 1,
4256 'payment_instrument' => 'Credit Card',
4257 'card_type_id' => $creditCardTypeIDs['Visa'],
4258 'pan_truncation' => 4567,
9099cab3 4259 ];
121c4616 4260 $contribution = $this->callAPISuccess('contribution', 'create', $params);
4261 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
4262 $financialTrxn = $this->callAPISuccessGetSingle(
4263 'FinancialTrxn',
9099cab3 4264 [
121c4616 4265 'id' => $lastFinancialTrxnId['financialTrxnId'],
9099cab3
CW
4266 'return' => ['card_type_id', 'pan_truncation'],
4267 ]
121c4616 4268 );
4269 $this->assertEquals(CRM_Utils_Array::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Visa']);
4270 $this->assertEquals(CRM_Utils_Array::value('pan_truncation', $financialTrxn), 4567);
9099cab3 4271 $params = [
121c4616 4272 'id' => $contribution['id'],
4273 'pan_truncation' => 2345,
4274 'card_type_id' => $creditCardTypeIDs['Amex'],
9099cab3 4275 ];
121c4616 4276 $contribution = $this->callAPISuccess('contribution', 'create', $params);
4277 $financialTrxn = $this->callAPISuccessGetSingle(
4278 'FinancialTrxn',
9099cab3 4279 [
121c4616 4280 'id' => $lastFinancialTrxnId['financialTrxnId'],
9099cab3
CW
4281 'return' => ['card_type_id', 'pan_truncation'],
4282 ]
121c4616 4283 );
4284 $this->assertEquals(CRM_Utils_Array::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Amex']);
4285 $this->assertEquals(CRM_Utils_Array::value('pan_truncation', $financialTrxn), 2345);
4286 }
4287
19893cf2
SL
4288 /**
4289 * Test repeat contribution uses non default currency
4290 * @see https://issues.civicrm.org/jira/projects/CRM/issues/CRM-20678
4291 */
4292 public function testRepeatTransactionWithDifferenceCurrency() {
9099cab3
CW
4293 $originalContribution = $this->setUpRepeatTransaction(['currency' => 'AUD'], 'single', ['currency' => 'AUD']);
4294 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', [
19893cf2
SL
4295 'original_contribution_id' => $originalContribution['id'],
4296 'contribution_status_id' => 'Completed',
4297 'trxn_id' => uniqid(),
9099cab3 4298 ]);
19893cf2
SL
4299 $this->assertEquals('AUD', $contribution['values'][$contribution['id']]['currency']);
4300 }
4301
b0e806fa 4302 /**
4303 * Get the financial items for the contribution.
4304 *
4305 * @param int $contributionID
4306 *
4307 * @return array
4308 * Array of associated financial items.
4309 */
4310 protected function getFinancialTransactionsForContribution($contributionID) {
9099cab3 4311 $trxnParams = [
b0e806fa 4312 'entity_id' => $contributionID,
4313 'entity_table' => 'civicrm_contribution',
9099cab3 4314 ];
b0e806fa 4315 // @todo the following function has naming errors & has a weird signature & appears to
4316 // only be called from test classes. Move into test suite & maybe just use api
4317 // from this function.
9099cab3 4318 return array_merge(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($trxnParams, FALSE, []));
b0e806fa 4319 }
4320
1af690c4 4321 /**
4322 * Test getunique api call for Contribution entity
4323 */
4324 public function testContributionGetUnique() {
9099cab3 4325 $result = $this->callAPIAndDocument($this->_entity, 'getunique', [], __FUNCTION__, __FILE__);
1af690c4 4326 $this->assertEquals(2, $result['count']);
9099cab3
CW
4327 $this->assertEquals(['trxn_id'], $result['values']['UI_contrib_trxn_id']);
4328 $this->assertEquals(['invoice_id'], $result['values']['UI_contrib_invoice_id']);
1af690c4 4329 }
4330
d8bd2007
PN
4331 /**
4332 * Test Repeat Transaction Contribution with Tax amount.
4333 * https://lab.civicrm.org/dev/core/issues/806
4334 */
4335 public function testRepeatContributionWithTaxAmount() {
4336 $this->enableTaxAndInvoicing();
4337 $financialType = $this->callAPISuccess('financial_type', 'create', [
4338 'name' => 'Test taxable financial Type',
4339 'is_reserved' => 0,
4340 'is_active' => 1,
4341 ]);
4342 $this->relationForFinancialTypeWithFinancialAccount($financialType['id']);
4343 $contribution = $this->setUpRepeatTransaction(
4344 [],
4345 'single',
4346 [
4347 'financial_type_id' => $financialType['id'],
4348 ]
4349 );
9099cab3 4350 $this->callAPISuccess('contribution', 'repeattransaction', [
d8bd2007
PN
4351 'original_contribution_id' => $contribution['id'],
4352 'contribution_status_id' => 'Completed',
4353 'trxn_id' => uniqid(),
9099cab3 4354 ]);
d8bd2007
PN
4355 $this->callAPISuccessGetCount('Contribution', [], 2);
4356 }
4357
a8fdb24e
CW
4358 public function testGetCurrencyOptions() {
4359 $result = $this->callAPISuccess('Contribution', 'getoptions', [
4360 'field' => 'currency',
4361 ]);
4362 $this->assertEquals('US Dollar', $result['values']['USD']);
4363 $this->assertNotContains('$', $result['values']);
4364 $result = $this->callAPISuccess('Contribution', 'getoptions', [
4365 'field' => 'currency',
4366 'context' => "abbreviate",
4367 ]);
4368 $this->assertEquals('$', $result['values']['USD']);
4369 $this->assertNotContains('US Dollar', $result['values']);
4370 }
4371
6a488035 4372}