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