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