Merge pull request #9831 from yashodha/CRM-19982
[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'));
6a488035
TO
125 }
126
22f80e87
EM
127 /**
128 * Test Get.
129 */
00be9182 130 public function testGetContribution() {
6a488035
TO
131 $p = array(
132 'contact_id' => $this->_individualId,
133 'receive_date' => '2010-01-20',
134 'total_amount' => 100.00,
4ab7d517 135 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
136 'non_deductible_amount' => 10.00,
137 'fee_amount' => 5.00,
138 'net_amount' => 95.00,
139 'trxn_id' => 23456,
140 'invoice_id' => 78910,
141 'source' => 'SSF',
142 'contribution_status_id' => 1,
6a488035 143 );
2f45e1c2 144 $this->_contribution = $this->callAPISuccess('contribution', 'create', $p);
6a488035
TO
145
146 $params = array(
147 'contribution_id' => $this->_contribution['id'],
6a488035 148 );
e0e3c51b 149
3af96592 150 $contributions = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__, __FILE__);
4ab7d517 151 $financialParams['id'] = $this->_financialTypeId;
6c6e6187 152 $default = NULL;
858d0bf8 153 CRM_Financial_BAO_FinancialType::retrieve($financialParams, $default);
2f45e1c2 154
3af96592 155 $this->assertEquals(1, $contributions['count']);
156 $contribution = $contributions['values'][$contributions['id']];
157 $this->assertEquals($contribution['contact_id'], $this->_individualId);
22f80e87
EM
158 // Note there was an assertion converting financial_type_id to 'Donation' which wasn't working.
159 // Passing back a string rather than an id seems like an error/cruft.
160 // If it is to be introduced we should discuss.
3af96592 161 $this->assertEquals($contribution['financial_type_id'], 1);
162 $this->assertEquals($contribution['total_amount'], 100.00);
163 $this->assertEquals($contribution['non_deductible_amount'], 10.00);
164 $this->assertEquals($contribution['fee_amount'], 5.00);
165 $this->assertEquals($contribution['net_amount'], 95.00);
166 $this->assertEquals($contribution['trxn_id'], 23456);
167 $this->assertEquals($contribution['invoice_id'], 78910);
168 $this->assertEquals($contribution['contribution_source'], 'SSF');
169 $this->assertEquals($contribution['contribution_status'], 'Completed');
22f80e87 170 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
6a488035
TO
171 $p['trxn_id'] = '3847';
172 $p['invoice_id'] = '3847';
173
2f45e1c2 174 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
175
22f80e87 176 // Now we have 2 - test getcount.
4ab7d517 177 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
6a488035 178 $this->assertEquals(2, $contribution);
22f80e87 179 // Test id only format.
4ab7d517 180 $contribution = $this->callAPISuccess('contribution', 'get', array(
181 'id' => $this->_contribution['id'],
182 'format.only_id' => 1,
183 ));
22f80e87
EM
184 $this->assertEquals($this->_contribution['id'], $contribution, print_r($contribution, TRUE));
185 // Test id only format.
2f45e1c2 186 $contribution = $this->callAPISuccess('contribution', 'get', array(
4ab7d517 187 'id' => $contribution2['id'],
188 'format.only_id' => 1,
189 ));
190 $this->assertEquals($contribution2['id'], $contribution);
22f80e87 191 // Test id as field.
4ab7d517 192 $contribution = $this->callAPISuccess('contribution', 'get', array(
193 'id' => $this->_contribution['id'],
194 ));
2bfae985 195 $this->assertEquals(1, $contribution['count']);
4ab7d517 196
22f80e87 197 // Test get by contact id works.
4ab7d517 198 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId));
6a488035 199
2bfae985 200 $this->assertEquals(2, $contribution['count']);
2f45e1c2 201 $this->callAPISuccess('Contribution', 'Delete', array(
6a488035 202 'id' => $this->_contribution['id'],
4ab7d517 203 ));
2f45e1c2 204 $this->callAPISuccess('Contribution', 'Delete', array(
6a488035 205 'id' => $contribution2['id'],
4ab7d517 206 ));
6a488035
TO
207 }
208
71d5a412 209 /**
210 * Test that test contributions can be retrieved.
211 */
212 public function testGetTestContribution() {
213 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, array('is_test' => 1)));
214 $this->callAPISuccessGetSingle('Contribution', array('is_test' => 1));
215 }
216
59f9e5a6 217 /**
218 * Test the 'return' param works for all fields.
219 */
220 public function testGetContributionReturnFunctionality() {
221 $params = $this->_params;
222 $params['check_number'] = 'bouncer';
223 $params['payment_instrument_id'] = 'Check';
224 $params['cancel_date'] = 'yesterday';
225 $params['receipt_date'] = 'yesterday';
226 $params['thankyou_date'] = 'yesterday';
227 $params['revenue_recognition_date'] = 'yesterday';
228 $params['amount_level'] = 'Unreasonable';
229 $params['cancel_reason'] = 'You lose sucker';
230 $params['creditnote_id'] = 'sudo rm -rf';
231 $params['tax_amount'] = '1';
232 $address = $this->callAPISuccess('Address', 'create', array(
233 'street_address' => 'Knockturn Alley',
234 'contact_id' => $this->_individualId,
235 'location_type_id' => 'Home',
236 ));
237 $params['address_id'] = $address['id'];
238 $contributionPage = $this->contributionPageCreate();
239 $params['contribution_page_id'] = $contributionPage['id'];
240 $contributionRecur = $this->callAPISuccess('ContributionRecur', 'create', array(
241 'contact_id' => $this->_individualId,
242 'frequency_interval' => 1,
243 'amount' => 5,
244 ));
245 $params['contribution_recur_id'] = $contributionRecur['id'];
246
247 $params['campaign_id'] = $this->campaignCreate();
248
249 $contributionID = $this->contributionCreate($params);
250 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $contributionID));
251 $this->assertEquals('bouncer', $contribution['check_number']);
252 $this->assertEquals('bouncer', $contribution['contribution_check_number']);
253
254 $fields = CRM_Contribute_BAO_Contribution::fields();
3123273f 255 // Re-add these 2 to the fields to check. They were locked in but the metadata changed so we
256 // need to specify them.
257 $fields['address_id'] = $fields['contribution_address_id'];
258 $fields['check_number'] = $fields['contribution_check_number'];
259
59f9e5a6 260 $fieldsLockedIn = array(
261 'contribution_id', 'contribution_contact_id', 'financial_type_id', 'contribution_page_id',
262 'payment_instrument_id', 'receive_date', 'non_deductible_amount', 'total_amount',
263 'fee_amount', 'net_amount', 'trxn_id', 'invoice_id', 'currency', 'cancel_date', 'cancel_reason',
264 'receipt_date', 'thankyou_date', 'contribution_source', 'amount_level', 'contribution_recur_id',
265 'is_test', 'is_pay_later', 'contribution_status_id', 'address_id', 'check_number', 'contribution_campaign_id',
266 'creditnote_id', 'tax_amount', 'revenue_recognition_date', 'decoy',
267 );
268 $missingFields = array_diff($fieldsLockedIn, array_keys($fields));
269 // If any of the locked in fields disappear from the $fields array we need to make sure it is still
270 // covered as the test contract now guarantees them in the return array.
271 $this->assertEquals($missingFields, array(29 => 'decoy'), 'A field which was covered by the test contract has changed.');
272 foreach ($fields as $fieldName => $fieldSpec) {
273 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $contributionID, 'return' => $fieldName));
274 $returnField = $fieldName;
275 if ($returnField == 'contribution_contact_id') {
276 $returnField = 'contact_id';
277 }
278 $this->assertTrue((!empty($contribution[$returnField]) || $contribution[$returnField] === "0"), $returnField);
279 }
280 }
281
6c6e6187 282 /**
22f80e87 283 * We need to ensure previous tested behaviour still works as part of the api contract.
6c6e6187 284 */
00be9182 285 public function testGetContributionLegacyBehaviour() {
6a488035
TO
286 $p = array(
287 'contact_id' => $this->_individualId,
288 'receive_date' => '2010-01-20',
289 'total_amount' => 100.00,
4ab7d517 290 'contribution_type_id' => $this->_financialTypeId,
6a488035
TO
291 'non_deductible_amount' => 10.00,
292 'fee_amount' => 5.00,
293 'net_amount' => 95.00,
294 'trxn_id' => 23456,
295 'invoice_id' => 78910,
296 'source' => 'SSF',
297 'contribution_status_id' => 1,
6a488035 298 );
71d5a412 299 $this->_contribution = $this->callAPISuccess('Contribution', 'create', $p);
6a488035
TO
300
301 $params = array(
302 'contribution_id' => $this->_contribution['id'],
6a488035 303 );
5be22f39 304 $contribution = $this->callAPISuccess('contribution', 'get', $params);
4ab7d517 305 $financialParams['id'] = $this->_financialTypeId;
6c6e6187 306 $default = NULL;
858d0bf8 307 CRM_Financial_BAO_FinancialType::retrieve($financialParams, $default);
2f45e1c2 308
6c6e6187 309 $this->assertEquals(1, $contribution['count']);
2bfae985 310 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
4ab7d517 311 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->_financialTypeId);
312 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_type_id'], $this->_financialTypeId);
2bfae985
EM
313 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
314 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
315 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
316 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
317 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
318 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
319 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
320 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
22f80e87
EM
321
322 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
6a488035
TO
323 $p['trxn_id'] = '3847';
324 $p['invoice_id'] = '3847';
325
2f45e1c2 326 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
6a488035 327
6a488035 328 // now we have 2 - test getcount
4ab7d517 329 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
6a488035
TO
330 $this->assertEquals(2, $contribution);
331 //test id only format
4ab7d517 332 $contribution = $this->callAPISuccess('contribution', 'get', array(
333 'id' => $this->_contribution['id'],
334 'format.only_id' => 1,
335 ));
22f80e87 336 $this->assertEquals($this->_contribution['id'], $contribution, print_r($contribution, TRUE));
6a488035 337 //test id only format
4ab7d517 338 $contribution = $this->callAPISuccess('contribution', 'get', array(
339 'id' => $contribution2['id'],
340 'format.only_id' => 1,
341 ));
6a488035 342 $this->assertEquals($contribution2['id'], $contribution);
2f45e1c2 343 $contribution = $this->callAPISuccess('contribution', 'get', array(
6a488035
TO
344 'id' => $this->_contribution['id'],
345 ));
346 //test id as field
2bfae985 347 $this->assertEquals(1, $contribution['count']);
6a488035
TO
348 // $this->assertEquals($this->_contribution['id'], $contribution['id'] ) ;
349 //test get by contact id works
4ab7d517 350 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId));
6a488035 351
2bfae985 352 $this->assertEquals(2, $contribution['count']);
2f45e1c2 353 $this->callAPISuccess('Contribution', 'Delete', array(
5896d037 354 'id' => $this->_contribution['id'],
6a488035 355 ));
2f45e1c2 356 $this->callAPISuccess('Contribution', 'Delete', array(
5896d037 357 'id' => $contribution2['id'],
6a488035
TO
358 ));
359 }
5896d037 360
a1a2a83d
TO
361 /**
362 * Create an contribution_id=FALSE and financial_type_id=Donation.
363 */
00be9182 364 public function testCreateEmptyContributionIDUseDonation() {
6a488035
TO
365 $params = array(
366 'contribution_id' => FALSE,
367 'contact_id' => 1,
368 'total_amount' => 1,
6c6e6187 369 'check_permissions' => FALSE,
6a488035
TO
370 'financial_type_id' => 'Donation',
371 );
858d0bf8 372 $this->callAPISuccess('contribution', 'create', $params);
6c6e6187 373 }
6a488035 374
6a488035 375 /**
1e52837d
EM
376 * Check with complete array + custom field.
377 *
6a488035
TO
378 * Note that the test is written on purpose without any
379 * variables specific to participant so it can be replicated into other entities
380 * and / or moved to the automated test suite
381 */
00be9182 382 public function testCreateWithCustom() {
6a488035
TO
383 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
384
385 $params = $this->_params;
386 $params['custom_' . $ids['custom_field_id']] = "custom string";
387
6c6e6187 388 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
6a488035 389 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
2f45e1c2 390 $check = $this->callAPISuccess($this->_entity, 'get', array(
4ab7d517 391 'return.custom_' . $ids['custom_field_id'] => 1,
392 'id' => $result['id'],
393 ));
6a488035
TO
394 $this->customFieldDelete($ids['custom_field_id']);
395 $this->customGroupDelete($ids['custom_group_id']);
22f80e87 396 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']]);
6a488035
TO
397 }
398
399 /**
fd786d03
EM
400 * Check with complete array + custom field.
401 *
6a488035
TO
402 * Note that the test is written on purpose without any
403 * variables specific to participant so it can be replicated into other entities
404 * and / or moved to the automated test suite
405 */
00be9182 406 public function testCreateGetFieldsWithCustom() {
5896d037 407 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
6a488035 408 $idsContact = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTest.php');
5896d037 409 $result = $this->callAPISuccess('Contribution', 'getfields', array());
6a488035
TO
410 $this->assertArrayHasKey('custom_' . $ids['custom_field_id'], $result['values']);
411 $this->assertArrayNotHasKey('custom_' . $idsContact['custom_field_id'], $result['values']);
412 $this->customFieldDelete($ids['custom_field_id']);
413 $this->customGroupDelete($ids['custom_group_id']);
414 $this->customFieldDelete($idsContact['custom_field_id']);
415 $this->customGroupDelete($idsContact['custom_group_id']);
416 }
417
00be9182 418 public function testCreateContributionNoLineItems() {
6a488035
TO
419
420 $params = array(
421 'contact_id' => $this->_individualId,
422 'receive_date' => '20120511',
423 'total_amount' => 100.00,
5896d037 424 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
425 'payment_instrument_id' => 1,
426 'non_deductible_amount' => 10.00,
427 'fee_amount' => 50.00,
428 'net_amount' => 90.00,
429 'trxn_id' => 12345,
430 'invoice_id' => 67890,
431 'source' => 'SSF',
432 'contribution_status_id' => 1,
6a488035
TO
433 'skipLineItem' => 1,
434 );
435
2f45e1c2 436 $contribution = $this->callAPISuccess('contribution', 'create', $params);
6c6e6187 437 $lineItems = $this->callAPISuccess('line_item', 'get', array(
6a488035
TO
438 'entity_id' => $contribution['id'],
439 'entity_table' => 'civicrm_contribution',
440 'sequential' => 1,
441 ));
442 $this->assertEquals(0, $lineItems['count']);
443 }
5896d037 444
a1a2a83d 445 /**
eceb18cc 446 * Test checks that passing in line items suppresses the create mechanism.
6a488035 447 */
00be9182 448 public function testCreateContributionChainedLineItems() {
6a488035
TO
449 $params = array(
450 'contact_id' => $this->_individualId,
451 'receive_date' => '20120511',
452 'total_amount' => 100.00,
4ab7d517 453 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
454 'payment_instrument_id' => 1,
455 'non_deductible_amount' => 10.00,
456 'fee_amount' => 50.00,
457 'net_amount' => 90.00,
458 'trxn_id' => 12345,
459 'invoice_id' => 67890,
460 'source' => 'SSF',
461 'contribution_status_id' => 1,
6a488035
TO
462 'skipLineItem' => 1,
463 'api.line_item.create' => array(
464 array(
465 'price_field_id' => 1,
466 'qty' => 2,
467 'line_total' => '20',
468 'unit_price' => '10',
469 ),
470 array(
471 'price_field_id' => 1,
472 'qty' => 1,
473 'line_total' => '80',
474 'unit_price' => '80',
475 ),
476 ),
477 );
478
5c49fee0 479 $description = "Create Contribution with Nested Line Items.";
6a488035 480 $subfile = "CreateWithNestedLineItems";
6c6e6187 481 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
2f45e1c2 482
6c6e6187 483 $lineItems = $this->callAPISuccess('line_item', 'get', array(
6a488035 484 'entity_id' => $contribution['id'],
4ede4532 485 'contribution_id' => $contribution['id'],
6a488035
TO
486 'entity_table' => 'civicrm_contribution',
487 'sequential' => 1,
488 ));
489 $this->assertEquals(2, $lineItems['count']);
490 }
491
00be9182 492 public function testCreateContributionOffline() {
6a488035
TO
493 $params = array(
494 'contact_id' => $this->_individualId,
495 'receive_date' => '20120511',
496 'total_amount' => 100.00,
497 'financial_type_id' => 1,
498 'trxn_id' => 12345,
499 'invoice_id' => 67890,
500 'source' => 'SSF',
501 'contribution_status_id' => 1,
6a488035
TO
502 );
503
5be22f39 504 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2bfae985
EM
505 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
506 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
5896d037 507 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
2bfae985
EM
508 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
509 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
510 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
511 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
6c6e6187 512 $lineItems = $this->callAPISuccess('line_item', 'get', array(
6a488035 513 'entity_id' => $contribution['id'],
4ede4532 514 'contribution_id' => $contribution['id'],
6a488035
TO
515 'entity_table' => 'civicrm_contribution',
516 'sequential' => 1,
5896d037 517 ));
6a488035
TO
518 $this->assertEquals(1, $lineItems['count']);
519 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
4ede4532 520 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
6a488035
TO
521 $this->_checkFinancialRecords($contribution, 'offline');
522 $this->contributionGetnCheck($params, $contribution['id']);
523 }
5896d037 524
f70a6752 525 /**
28de42d1 526 * Test create with valid payment instrument.
6a488035 527 */
00be9182 528 public function testCreateContributionWithPaymentInstrument() {
6a488035 529 $params = $this->_params + array('payment_instrument' => 'EFT');
53191813 530 $contribution = $this->callAPISuccess('contribution', 'create', $params);
6c6e6187 531 $contribution = $this->callAPISuccess('contribution', 'get', array(
53191813 532 'sequential' => 1,
21dfd5f5 533 'id' => $contribution['id'],
53191813 534 ));
6a488035 535 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
53191813
CW
536 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
537
5896d037 538 $this->callAPISuccess('contribution', 'create', array(
92915c55
TO
539 'id' => $contribution['id'],
540 'payment_instrument' => 'Credit Card',
541 ));
6c6e6187 542 $contribution = $this->callAPISuccess('contribution', 'get', array(
53191813 543 'sequential' => 1,
21dfd5f5 544 'id' => $contribution['id'],
53191813
CW
545 ));
546 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
547 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
6a488035
TO
548 }
549
00be9182 550 public function testGetContributionByPaymentInstrument() {
6a488035 551 $params = $this->_params + array('payment_instrument' => 'EFT');
53191813 552 $params2 = $this->_params + array('payment_instrument' => 'Cash');
6c6e6187
TO
553 $this->callAPISuccess('contribution', 'create', $params);
554 $this->callAPISuccess('contribution', 'create', $params2);
5896d037 555 $contribution = $this->callAPISuccess('contribution', 'get', array(
92915c55 556 'sequential' => 1,
7d543448 557 'contribution_payment_instrument' => 'Cash',
92915c55 558 ));
6a488035 559 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
ff977830
EM
560 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
561 $this->assertEquals(1, $contribution['count']);
868e247d 562 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'payment_instrument' => 'Cash'));
6a488035 563 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
868e247d 564 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
6a488035 565 $this->assertEquals(1, $contribution['count']);
5896d037 566 $contribution = $this->callAPISuccess('contribution', 'get', array(
92915c55
TO
567 'sequential' => 1,
568 'payment_instrument_id' => 5,
569 ));
6a488035 570 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
6c6e6187
TO
571 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
572 $this->assertEquals(1, $contribution['count']);
5896d037 573 $contribution = $this->callAPISuccess('contribution', 'get', array(
92915c55
TO
574 'sequential' => 1,
575 'payment_instrument' => 'EFT',
576 ));
6a488035 577 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
53191813
CW
578 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
579 $this->assertEquals(1, $contribution['count']);
5896d037 580 $contribution = $this->callAPISuccess('contribution', 'create', array(
92915c55
TO
581 'id' => $contribution['id'],
582 'payment_instrument' => 'Credit Card',
583 ));
5896d037 584 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'id' => $contribution['id']));
6a488035 585 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
6c6e6187
TO
586 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
587 $this->assertEquals(1, $contribution['count']);
6a488035
TO
588 }
589
b5a37491
EM
590 /**
591 * CRM-16227 introduces invoice_id as a parameter.
592 */
593 public function testGetContributionByInvoice() {
594 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, array('invoice_id' => 'curly')));
595 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params), array('invoice_id' => 'churlish'));
596 $this->callAPISuccessGetCount('Contribution', array(), 2);
597 $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => 'curly'));
598 // The following don't work. They are the format we are trying to introduce but although the form uses this format
599 // CRM_Contact_BAO_Query::convertFormValues puts them into the other format & the where only supports that.
600 // ideally the where clause would support this format (as it does on contact_BAO_Query) and those lines would
601 // come out of convertFormValues
602 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('LIKE' => '%ish%')));
603 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('NOT IN' => array('curly'))));
604 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('LIKE' => '%ly%')), 2);
605 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('IN' => array('curly', 'churlish'))),
606 // 2);
607 }
608
2b3e31ac 609 /**
610 * Check the credit note retrieval is case insensitive.
611 */
612 public function testGetCreditNoteCaseInsensitive() {
613 $this->contributionCreate(array('contact_id' => $this->_individualId));
614 $this->contributionCreate(array('creditnote_id' => 'cN1234', 'contact_id' => $this->_individualId, 'invoice_id' => rand(), 'trxn_id' => rand()));
615 $contribution = $this->callAPISuccess('Contribution', 'getsingle', array('creditnote_id' => 'CN1234'));
616 $this->assertEquals($contribution['creditnote_id'], 'cN1234');
617 }
618
e58a3abb 619 /**
620 * Test retrieval by total_amount works.
621 *
622 * @throws Exception
623 */
624 public function testGetContributionByTotalAmount() {
625 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, array('total_amount' => '5')));
626 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, array('total_amount' => '10')));
627 $this->callAPISuccessGetCount('Contribution', array('total_amount' => 10), 1);
628 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('>' => 6)), 1);
629 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('>' => 0)), 2);
630 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('>' => -5)), 2);
631 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('<' => 0)), 0);
632 $this->callAPISuccessGetCount('Contribution', array(), 2);
633 }
634
c490a46a 635 /**
eceb18cc 636 * Create test with unique field name on source.
c490a46a 637 */
00be9182 638 public function testCreateContributionSource() {
6a488035
TO
639
640 $params = array(
641 'contact_id' => $this->_individualId,
642 'receive_date' => date('Ymd'),
643 'total_amount' => 100.00,
4ab7d517 644 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
645 'payment_instrument_id' => 1,
646 'non_deductible_amount' => 10.00,
647 'fee_amount' => 50.00,
648 'net_amount' => 90.00,
649 'trxn_id' => 12345,
650 'invoice_id' => 67890,
651 'contribution_source' => 'SSF',
652 'contribution_status_id' => 1,
6a488035
TO
653 );
654
4ab7d517 655 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2bfae985
EM
656 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
657 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
6a488035 658 }
d5d148ab 659
d424ffde 660 /**
eceb18cc 661 * Create test with unique field name on source.
d424ffde 662 */
00be9182 663 public function testCreateDefaultNow() {
d5d148ab
EM
664
665 $params = $this->_params;
666 unset($params['receive_date']);
667
668 $contribution = $this->callAPISuccess('contribution', 'create', $params);
669 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
670 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date'])));
671 }
672
c490a46a 673 /**
55d2c6f1 674 * Create test with unique field name on source.
c490a46a 675 */
55d2c6f1 676 public function testCreateContributionSourceInvalidContact() {
6a488035
TO
677
678 $params = array(
679 'contact_id' => 999,
680 'receive_date' => date('Ymd'),
681 'total_amount' => 100.00,
4ab7d517 682 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
683 'payment_instrument_id' => 1,
684 'non_deductible_amount' => 10.00,
685 'fee_amount' => 50.00,
686 'net_amount' => 90.00,
687 'trxn_id' => 12345,
688 'invoice_id' => 67890,
689 'contribution_source' => 'SSF',
690 'contribution_status_id' => 1,
6a488035
TO
691 );
692
858d0bf8 693 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
6a488035
TO
694 }
695
00be9182 696 public function testCreateContributionSourceInvalidContContact() {
6a488035
TO
697
698 $params = array(
699 'contribution_contact_id' => 999,
700 'receive_date' => date('Ymd'),
701 'total_amount' => 100.00,
4ab7d517 702 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
703 'payment_instrument_id' => 1,
704 'non_deductible_amount' => 10.00,
705 'fee_amount' => 50.00,
706 'net_amount' => 90.00,
707 'trxn_id' => 12345,
708 'invoice_id' => 67890,
709 'contribution_source' => 'SSF',
710 'contribution_status_id' => 1,
6a488035
TO
711 );
712
33139905 713 $this->callAPIFailure('contribution', 'create', $params);
6a488035
TO
714 }
715
858d0bf8 716 /**
442cf836 717 * Test note created correctly.
858d0bf8 718 */
00be9182 719 public function testCreateContributionWithNote() {
5c49fee0 720 $description = "Demonstrates creating contribution with Note Entity.";
5896d037
TO
721 $subfile = "ContributionCreateWithNote";
722 $params = array(
6a488035
TO
723 'contact_id' => $this->_individualId,
724 'receive_date' => '2012-01-01',
725 'total_amount' => 100.00,
4ab7d517 726 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
727 'payment_instrument_id' => 1,
728 'non_deductible_amount' => 10.00,
729 'fee_amount' => 50.00,
730 'net_amount' => 90.00,
731 'trxn_id' => 12345,
732 'invoice_id' => 67890,
733 'source' => 'SSF',
734 'contribution_status_id' => 1,
6a488035
TO
735 'note' => 'my contribution note',
736 );
737
4ab7d517 738 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
5896d037 739 $result = $this->callAPISuccess('note', 'get', array(
92915c55
TO
740 'entity_table' => 'civicrm_contribution',
741 'entity_id' => $contribution['id'],
742 'sequential' => 1,
743 ));
6a488035 744 $this->assertEquals('my contribution note', $result['values'][0]['note']);
4ab7d517 745 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
6a488035
TO
746 }
747
00be9182 748 public function testCreateContributionWithNoteUniqueNameAliases() {
5896d037 749 $params = array(
6a488035
TO
750 'contact_id' => $this->_individualId,
751 'receive_date' => '2012-01-01',
752 'total_amount' => 100.00,
4ab7d517 753 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
754 'payment_instrument_id' => 1,
755 'non_deductible_amount' => 10.00,
756 'fee_amount' => 50.00,
757 'net_amount' => 90.00,
758 'trxn_id' => 12345,
759 'invoice_id' => 67890,
760 'source' => 'SSF',
761 'contribution_status_id' => 1,
6a488035
TO
762 'contribution_note' => 'my contribution note',
763 );
764
4ab7d517 765 $contribution = $this->callAPISuccess('contribution', 'create', $params);
5896d037 766 $result = $this->callAPISuccess('note', 'get', array(
92915c55
TO
767 'entity_table' => 'civicrm_contribution',
768 'entity_id' => $contribution['id'],
769 'sequential' => 1,
770 ));
6a488035 771 $this->assertEquals('my contribution note', $result['values'][0]['note']);
4ab7d517 772 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
6a488035 773 }
c490a46a
CW
774
775 /**
55d2c6f1 776 * This is the test for creating soft credits.
c490a46a 777 */
55d2c6f1 778 public function testCreateContributionWithSoftCredit() {
5c49fee0 779 $description = "Demonstrates creating contribution with SoftCredit.";
5896d037
TO
780 $subfile = "ContributionCreateWithSoftCredit";
781 $contact2 = $this->callAPISuccess('Contact', 'create', array(
92915c55
TO
782 'display_name' => 'superman',
783 'contact_type' => 'Individual',
784 ));
55d2c6f1 785 $softParams = array(
bcc03b98 786 'contact_id' => $contact2['id'],
787 'amount' => 50,
21dfd5f5 788 'soft_credit_type_id' => 3,
6a488035
TO
789 );
790
55d2c6f1 791 $params = $this->_params + array('soft_credit' => array(1 => $softParams));
4ab7d517 792 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
6c6e6187 793 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
a1c68fd2 794
55d2c6f1
EM
795 $this->assertEquals($softParams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
796 $this->assertEquals($softParams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
797 $this->assertEquals($softParams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']);
6a516bd6
DG
798
799 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
800 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
801 }
802
00be9182 803 public function testCreateContributionWithSoftCreditDefaults() {
5c49fee0 804 $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type.";
5896d037
TO
805 $subfile = "ContributionCreateWithSoftCreditDefaults";
806 $contact2 = $this->callAPISuccess('Contact', 'create', array(
92915c55
TO
807 'display_name' => 'superman',
808 'contact_type' => 'Individual',
809 ));
6a516bd6 810 $params = $this->_params + array(
442cf836
EM
811 'soft_credit_to' => $contact2['id'],
812 );
6a516bd6 813 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
6c6e6187 814 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
6a516bd6
DG
815
816 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
817 // Default soft credit amount = contribution.total_amount
818 $this->assertEquals($this->_params['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
819 $this->assertEquals(CRM_Core_OptionGroup::getDefaultValue("soft_credit_type"), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
820
821 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
822 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
823 }
824
00be9182 825 public function testCreateContributionWithHonoreeContact() {
5c49fee0 826 $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id.";
5896d037
TO
827 $subfile = "ContributionCreateWithHonoreeContact";
828 $contact2 = $this->callAPISuccess('Contact', 'create', array(
92915c55
TO
829 'display_name' => 'superman',
830 'contact_type' => 'Individual',
831 ));
6a516bd6 832 $params = $this->_params + array(
442cf836
EM
833 'honor_contact_id' => $contact2['id'],
834 );
6a516bd6 835 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
6c6e6187 836 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
6a516bd6
DG
837
838 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
839 // Default soft credit amount = contribution.total_amount
840 // Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type
841 $this->assertEquals($this->_params['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
842 $this->assertEquals(CRM_Core_OptionGroup::getValue('soft_credit_type', 'in_honor_of', 'name'), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
6a488035 843
4ab7d517 844 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
845 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
6a488035
TO
846 }
847
848 /**
92c99a4a 849 * Test using example code.
6a488035 850 */
00be9182 851 public function testContributionCreateExample() {
6a488035 852 //make sure at least on page exists since there is a truncate in tear down
8be629ac 853 $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
3ec6e38d 854 require_once 'api/v3/examples/Contribution/Create.php';
5896d037 855 $result = contribution_create_example();
006d6361 856 $id = $result['id'];
6a488035 857 $expectedResult = contribution_create_expectedresult();
8e342a79 858 $this->checkArrayEquals($expectedResult, $result);
006d6361 859 $this->contributionDelete($id);
6a488035
TO
860 }
861
a1a2a83d 862 /**
f55c5fa8 863 * Function tests that additional financial records are created when fee amount is recorded.
6a488035 864 */
00be9182 865 public function testCreateContributionWithFee() {
6a488035
TO
866 $params = array(
867 'contact_id' => $this->_individualId,
868 'receive_date' => '20120511',
869 'total_amount' => 100.00,
870 'fee_amount' => 50,
871 'financial_type_id' => 1,
872 'trxn_id' => 12345,
873 'invoice_id' => 67890,
874 'source' => 'SSF',
875 'contribution_status_id' => 1,
6a488035
TO
876 );
877
5be22f39 878 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2bfae985
EM
879 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
880 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
e0e3c51b
EM
881 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 50.00);
882 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 50.00);
5896d037 883 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
2bfae985
EM
884 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
885 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
886 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
887 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
e0e3c51b 888
6c6e6187 889 $lineItems = $this->callAPISuccess('line_item', 'get', array(
4ab7d517 890
6a488035
TO
891 'entity_id' => $contribution['id'],
892 'entity_table' => 'civicrm_contribution',
893 'sequential' => 1,
5896d037 894 ));
6a488035
TO
895 $this->assertEquals(1, $lineItems['count']);
896 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
4ede4532 897 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
6c6e6187 898 $lineItems = $this->callAPISuccess('line_item', 'get', array(
4ab7d517 899
5896d037
TO
900 'entity_id' => $contribution['id'],
901 'contribution_id' => $contribution['id'],
902 'entity_table' => 'civicrm_contribution',
903 'sequential' => 1,
6a488035
TO
904 ));
905 $this->assertEquals(1, $lineItems['count']);
906 $this->_checkFinancialRecords($contribution, 'feeAmount');
907 }
908
909
f70a6752 910 /**
442cf836 911 * Function tests that additional financial records are created when online contribution is created.
6a488035 912 */
00be9182 913 public function testCreateContributionOnline() {
858d0bf8 914 CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
5896d037 915 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
fc928539 916 $this->assertAPISuccess($contributionPage);
6a488035
TO
917 $params = array(
918 'contact_id' => $this->_individualId,
919 'receive_date' => '20120511',
920 'total_amount' => 100.00,
921 'financial_type_id' => 1,
922 'contribution_page_id' => $contributionPage['id'],
16f3bd02 923 'payment_processor' => $this->paymentProcessorID,
6a488035
TO
924 'trxn_id' => 12345,
925 'invoice_id' => 67890,
926 'source' => 'SSF',
927 'contribution_status_id' => 1,
4ab7d517 928
6a488035
TO
929 );
930
5be22f39 931 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2bfae985
EM
932 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
933 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
5896d037 934 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
2bfae985
EM
935 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
936 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
937 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
938 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
16f3bd02 939 $contribution['payment_instrument_id'] = $this->callAPISuccessGetValue('PaymentProcessor', array(
940 'id' => $this->paymentProcessorID,
941 'return' => 'payment_instrument_id',
942 ));
6a488035
TO
943 $this->_checkFinancialRecords($contribution, 'online');
944 }
945
f70a6752 946 /**
442cf836
EM
947 * Check handling of financial type.
948 *
100fef9d 949 * In the interests of removing financial type / contribution type checks from
f70a6752 950 * legacy format function lets test that the api is doing this for us
951 */
00be9182 952 public function testCreateInvalidFinancialType() {
f70a6752 953 $params = $this->_params;
954 $params['financial_type_id'] = 99999;
858d0bf8 955 $this->callAPIFailure($this->_entity, 'create', $params, "'99999' is not a valid option for field financial_type_id");
f70a6752 956 }
957
4302618d 958 /**
442cf836
EM
959 * Check handling of financial type.
960 *
100fef9d 961 * In the interests of removing financial type / contribution type checks from
4302618d 962 * legacy format function lets test that the api is doing this for us
963 */
00be9182 964 public function testValidNamedFinancialType() {
4302618d 965 $params = $this->_params;
966 $params['financial_type_id'] = 'Donation';
858d0bf8 967 $this->callAPISuccess($this->_entity, 'create', $params);
4302618d 968 }
969
f70a6752 970 /**
92c99a4a
EM
971 * Tests that additional financial records are created.
972 *
973 * Checks when online contribution with pay later option is created
6a488035 974 */
00be9182 975 public function testCreateContributionPayLaterOnline() {
858d0bf8 976 CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
6a488035 977 $this->_pageParams['is_pay_later'] = 1;
5896d037 978 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
fc928539 979 $this->assertAPISuccess($contributionPage);
6a488035
TO
980 $params = array(
981 'contact_id' => $this->_individualId,
982 'receive_date' => '20120511',
983 'total_amount' => 100.00,
984 'financial_type_id' => 1,
985 'contribution_page_id' => $contributionPage['id'],
986 'trxn_id' => 12345,
987 'is_pay_later' => 1,
988 'invoice_id' => 67890,
989 'source' => 'SSF',
990 'contribution_status_id' => 2,
4ab7d517 991
6a488035
TO
992 );
993
4ab7d517 994 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
2bfae985
EM
995 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
996 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
5896d037 997 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
2bfae985
EM
998 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
999 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
1000 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
1001 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
6a488035
TO
1002 $this->_checkFinancialRecords($contribution, 'payLater');
1003 }
1004
a1a2a83d 1005 /**
1e52837d 1006 * Function tests that additional financial records are created for online contribution with pending option.
6a488035 1007 */
00be9182 1008 public function testCreateContributionPendingOnline() {
6a488035 1009 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
5896d037 1010 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
fc928539 1011 $this->assertAPISuccess($contributionPage);
6a488035
TO
1012 $params = array(
1013 'contact_id' => $this->_individualId,
1014 'receive_date' => '20120511',
1015 'total_amount' => 100.00,
1016 'financial_type_id' => 1,
1017 'contribution_page_id' => $contributionPage['id'],
1018 'trxn_id' => 12345,
1019 'invoice_id' => 67890,
1020 'source' => 'SSF',
1021 'contribution_status_id' => 2,
6a488035
TO
1022 );
1023
5be22f39 1024 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2bfae985
EM
1025 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
1026 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
5896d037 1027 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
2bfae985
EM
1028 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
1029 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
1030 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
1031 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
6a488035
TO
1032 $this->_checkFinancialRecords($contribution, 'pending');
1033 }
1034
e748bf60 1035 /**
92c99a4a 1036 * Test that BAO defaults work.
e748bf60 1037 */
00be9182 1038 public function testCreateBAODefaults() {
e748bf60
EM
1039 unset($this->_params['contribution_source_id'], $this->_params['payment_instrument_id']);
1040 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
5896d037 1041 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
92915c55
TO
1042 'id' => $contribution['id'],
1043 'api.contribution.delete' => 1,
1044 ));
e748bf60 1045 $this->assertEquals(1, $contribution['contribution_status_id']);
12879069 1046 $this->assertEquals('Check', $contribution['payment_instrument']);
e748bf60
EM
1047 }
1048
a1a2a83d 1049 /**
1e52837d 1050 * Function tests that line items, financial records are updated when contribution amount is changed.
6a488035 1051 */
00be9182 1052 public function testCreateUpdateContributionChangeTotal() {
4ab7d517 1053 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
6c6e6187 1054 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
4ab7d517 1055
6a488035
TO
1056 'entity_id' => $contribution['id'],
1057 'entity_table' => 'civicrm_contribution',
1058 'sequential' => 1,
1059 'return' => 'line_total',
1060 ));
1061 $this->assertEquals('100.00', $lineItems);
1062 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
1063 // Financial trxn SUM = 100 + 5 (fee)
1064 $this->assertEquals('105.00', $trxnAmount);
1065 $newParams = array(
4ab7d517 1066
6a488035 1067 'id' => $contribution['id'],
21dfd5f5 1068 'total_amount' => '125',
5896d037 1069 );
694769da 1070 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
4ab7d517 1071
6c6e6187 1072 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
6a488035 1073
5896d037
TO
1074 'entity_id' => $contribution['id'],
1075 'entity_table' => 'civicrm_contribution',
1076 'sequential' => 1,
1077 'return' => 'line_total',
6a488035
TO
1078 ));
1079
1080 $this->assertEquals('125.00', $lineItems);
1081 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
28de42d1
EM
1082
1083 // Financial trxn SUM = 125 + 5 (fee).
6a488035 1084 $this->assertEquals('130.00', $trxnAmount);
28de42d1 1085 $this->assertEquals('125.00', $this->_getFinancialItemAmount($contribution['id']));
6a488035
TO
1086 }
1087
a1a2a83d 1088 /**
1e52837d 1089 * Function tests that line items, financial records are updated when pay later contribution is received.
6a488035 1090 */
00be9182 1091 public function testCreateUpdateContributionPayLater() {
6a488035
TO
1092 $contribParams = array(
1093 'contact_id' => $this->_individualId,
1094 'receive_date' => '2012-01-01',
1095 'total_amount' => 100.00,
4ab7d517 1096 'financial_type_id' => $this->_financialTypeId,
6a488035 1097 'payment_instrument_id' => 1,
8f39a111 1098 'contribution_status_id' => 2,
1099 'is_pay_later' => 1,
4ab7d517 1100
6a488035 1101 );
4ab7d517 1102 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
6a488035
TO
1103
1104 $newParams = array_merge($contribParams, array(
5896d037
TO
1105 'id' => $contribution['id'],
1106 'contribution_status_id' => 1,
1107 )
c71ae314 1108 );
694769da 1109 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
6a488035 1110 $contribution = $contribution['values'][$contribution['id']];
6c6e6187 1111 $this->assertEquals($contribution['contribution_status_id'], '1');
6a488035
TO
1112 $this->_checkFinancialItem($contribution['id'], 'paylater');
1113 $this->_checkFinancialTrxn($contribution, 'payLater');
1114 }
1115
a1a2a83d 1116 /**
eceb18cc 1117 * Function tests that financial records are updated when Payment Instrument is changed.
6a488035 1118 */
00be9182 1119 public function testCreateUpdateContributionPaymentInstrument() {
6a488035
TO
1120 $instrumentId = $this->_addPaymentInstrument();
1121 $contribParams = array(
1122 'contact_id' => $this->_individualId,
1123 'total_amount' => 100.00,
4ab7d517 1124 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1125 'payment_instrument_id' => 4,
1126 'contribution_status_id' => 1,
4ab7d517 1127
6a488035 1128 );
4ab7d517 1129 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
6a488035
TO
1130
1131 $newParams = array_merge($contribParams, array(
5896d037
TO
1132 'id' => $contribution['id'],
1133 'payment_instrument_id' => $instrumentId,
1134 )
6a488035 1135 );
694769da 1136 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
fc928539 1137 $this->assertAPISuccess($contribution);
4ecc6d4b 1138 $this->_checkFinancialTrxn($contribution, 'paymentInstrument', $instrumentId);
6a488035
TO
1139 }
1140
122250ec
SL
1141 /**
1142 * Function tests that financial records are updated when Payment Instrument is changed when amount is negative.
1143 */
1144 public function testCreateUpdateNegativeContributionPaymentInstrument() {
1145 $instrumentId = $this->_addPaymentInstrument();
1146 $contribParams = array(
1147 'contact_id' => $this->_individualId,
1148 'total_amount' => -100.00,
1149 'financial_type_id' => $this->_financialTypeId,
1150 'payment_instrument_id' => 4,
1151 'contribution_status_id' => 1,
1152
1153 );
1154 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1155
1156 $newParams = array_merge($contribParams, array(
1157 'id' => $contribution['id'],
1158 'payment_instrument_id' => $instrumentId,
1159 )
1160 );
1161 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1162 $this->assertAPISuccess($contribution);
1163 $this->_checkFinancialTrxn($contribution, 'paymentInstrument', $instrumentId, array('total_amount' => '-100.00'));
1164 }
1165
a1a2a83d 1166 /**
eceb18cc 1167 * Function tests that financial records are added when Contribution is Refunded.
6a488035 1168 */
00be9182 1169 public function testCreateUpdateContributionRefund() {
797d4c52 1170 $contributionParams = array(
6a488035
TO
1171 'contact_id' => $this->_individualId,
1172 'receive_date' => '2012-01-01',
1173 'total_amount' => 100.00,
4ab7d517 1174 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1175 'payment_instrument_id' => 4,
1176 'contribution_status_id' => 1,
797d4c52 1177 'trxn_id' => 'original_payment',
1178 );
1179 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1180 $newParams = array_merge($contributionParams, array(
1181 'id' => $contribution['id'],
1182 'contribution_status_id' => 'Refunded',
1183 'cancel_date' => '2015-01-01 09:00',
1184 'refund_trxn_id' => 'the refund',
1185 )
1186 );
1187
1188 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1189 $this->_checkFinancialTrxn($contribution, 'refund');
1190 $this->_checkFinancialItem($contribution['id'], 'refund');
1191 $this->assertEquals('original_payment', $this->callAPISuccessGetValue('Contribution', array(
1192 'id' => $contribution['id'],
1193 'return' => 'trxn_id',
1194 )));
1195 }
4ab7d517 1196
52da5b1e 1197 /**
1198 * Refund a contribution for a financial type with a contra account.
1199 *
1200 * CRM-17951 the contra account is a financial account with a relationship to a
1201 * financial type. It is not always configured but should be reflected
1202 * in the financial_trxn & financial_item table if it is.
1203 */
1204 public function testCreateUpdateChargebackContributionDefaultAccount() {
1205 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
1206 $this->callAPISuccess('Contribution', 'create', array(
1207 'id' => $contribution['id'],
1208 'contribution_status_id' => 'Chargeback',
1209 ));
1210 $this->callAPISuccessGetSingle('Contribution', array('contribution_status_id' => 'Chargeback'));
1211
1212 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1213 'contribution_id' => $contribution['id'],
1214 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1215 ));
1216 $this->assertEquals(1, $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1217 $this->callAPISuccessGetSingle('FinancialTrxn', array(
1218 'total_amount' => -100,
1219 'status_id' => 'Chargeback',
1220 'to_financial_account_id' => 6,
1221 ));
1222 }
1223
1224 /**
1225 * Refund a contribution for a financial type with a contra account.
1226 *
1227 * CRM-17951 the contra account is a financial account with a relationship to a
1228 * financial type. It is not always configured but should be reflected
1229 * in the financial_trxn & financial_item table if it is.
1230 */
1231 public function testCreateUpdateChargebackContributionCustomAccount() {
1232 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
1233 'name' => 'Chargeback Account',
1234 'is_active' => TRUE,
1235 ));
1236
1237 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', array(
1238 'entity_id' => $this->_financialTypeId,
1239 'entity_table' => 'civicrm_financial_type',
1240 'account_relationship' => 'Chargeback Account is',
1241 'financial_account_id' => 'Chargeback Account',
1242 ));
1243
1244 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
1245 $this->callAPISuccess('Contribution', 'create', array(
1246 'id' => $contribution['id'],
1247 'contribution_status_id' => 'Chargeback',
1248 ));
1249 $this->callAPISuccessGetSingle('Contribution', array('contribution_status_id' => 'Chargeback'));
1250
1251 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1252 'contribution_id' => $contribution['id'],
1253 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1254 ));
1255 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1256
1257 $this->callAPISuccess('Contribution', 'delete', array('id' => $contribution['id']));
1258 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialAccount['id']));
1259 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
1260 }
1261
bf2cf926 1262 /**
1263 * Refund a contribution for a financial type with a contra account.
1264 *
1265 * CRM-17951 the contra account is a financial account with a relationship to a
1266 * financial type. It is not always configured but should be reflected
1267 * in the financial_trxn & financial_item table if it is.
1268 */
1269 public function testCreateUpdateRefundContributionConfiguredContraAccount() {
1270 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
1271 'name' => 'Refund Account',
1272 'is_active' => TRUE,
1273 ));
1274
1275 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', array(
1276 'entity_id' => $this->_financialTypeId,
1277 'entity_table' => 'civicrm_financial_type',
1278 'account_relationship' => 'Credit/Contra Revenue Account is',
1279 'financial_account_id' => 'Refund Account',
1280 ));
1281
1282 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
1283 $this->callAPISuccess('Contribution', 'create', array(
1284 'id' => $contribution['id'],
1285 'contribution_status_id' => 'Refunded',
1286 ));
1287
52da5b1e 1288 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
bf2cf926 1289 'contribution_id' => $contribution['id'],
1290 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1291 ));
1292 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1293
1294 $this->callAPISuccess('Contribution', 'delete', array('id' => $contribution['id']));
1295 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialAccount['id']));
1296 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
bf2cf926 1297 }
1298
797d4c52 1299 /**
1300 * Function tests that trxn_id is set when passed in.
1301 *
1302 * Here we ensure that the civicrm_financial_trxn.trxn_id & the civicrm_contribution.trxn_id are set
1303 * when trxn_id is passed in.
1304 */
1305 public function testCreateUpdateContributionRefundTrxnIDPassedIn() {
1306 $contributionParams = array(
1307 'contact_id' => $this->_individualId,
1308 'receive_date' => '2012-01-01',
1309 'total_amount' => 100.00,
1310 'financial_type_id' => $this->_financialTypeId,
1311 'payment_instrument_id' => 4,
1312 'contribution_status_id' => 1,
1313 'trxn_id' => 'original_payment',
6a488035 1314 );
797d4c52 1315 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1316 $newParams = array_merge($contributionParams, array(
1317 'id' => $contribution['id'],
1318 'contribution_status_id' => 'Refunded',
1319 'cancel_date' => '2015-01-01 09:00',
1320 'trxn_id' => 'the refund',
1321 )
1322 );
1323
1324 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1325 $this->_checkFinancialTrxn($contribution, 'refund');
1326 $this->_checkFinancialItem($contribution['id'], 'refund');
1327 $this->assertEquals('the refund', $this->callAPISuccessGetValue('Contribution', array(
1328 'id' => $contribution['id'],
1329 'return' => 'trxn_id',
1330 )));
1331 }
1332
1333 /**
1334 * Function tests that trxn_id is set when passed in.
1335 *
1336 * Here we ensure that the civicrm_contribution.trxn_id is set
1337 * when trxn_id is passed in but if refund_trxn_id is different then that
1338 * is kept for the refund transaction.
1339 */
1340 public function testCreateUpdateContributionRefundRefundAndTrxnIDPassedIn() {
1341 $contributionParams = array(
1342 'contact_id' => $this->_individualId,
1343 'receive_date' => '2012-01-01',
1344 'total_amount' => 100.00,
1345 'financial_type_id' => $this->_financialTypeId,
1346 'payment_instrument_id' => 4,
1347 'contribution_status_id' => 1,
1348 'trxn_id' => 'original_payment',
1349 );
1350 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1351 $newParams = array_merge($contributionParams, array(
5896d037 1352 'id' => $contribution['id'],
b7990bb6 1353 'contribution_status_id' => 'Refunded',
1354 'cancel_date' => '2015-01-01 09:00',
797d4c52 1355 'trxn_id' => 'cont id',
1356 'refund_trxn_id' => 'the refund',
6a488035
TO
1357 )
1358 );
1359
694769da 1360 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
6a488035
TO
1361 $this->_checkFinancialTrxn($contribution, 'refund');
1362 $this->_checkFinancialItem($contribution['id'], 'refund');
797d4c52 1363 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1364 'id' => $contribution['id'],
1365 'return' => 'trxn_id',
1366 )));
1367 }
1368
1369 /**
1370 * Function tests that refund_trxn_id is set when passed in empty.
1371 *
1372 * Here we ensure that the civicrm_contribution.trxn_id is set
1373 * when trxn_id is passed in but if refund_trxn_id isset but empty then that
1374 * is kept for the refund transaction.
1375 */
1376 public function testCreateUpdateContributionRefundRefundNullTrxnIDPassedIn() {
1377 $contributionParams = array(
1378 'contact_id' => $this->_individualId,
1379 'receive_date' => '2012-01-01',
1380 'total_amount' => 100.00,
1381 'financial_type_id' => $this->_financialTypeId,
1382 'payment_instrument_id' => 4,
1383 'contribution_status_id' => 1,
1384 'trxn_id' => 'original_payment',
1385 );
1386 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1387 $newParams = array_merge($contributionParams, array(
1388 'id' => $contribution['id'],
1389 'contribution_status_id' => 'Refunded',
1390 'cancel_date' => '2015-01-01 09:00',
1391 'trxn_id' => 'cont id',
1392 'refund_trxn_id' => '',
1393 )
1394 );
1395
1396 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1397 $this->_checkFinancialTrxn($contribution, 'refund', NULL, array('trxn_id' => NULL));
1398 $this->_checkFinancialItem($contribution['id'], 'refund');
1399 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1400 'id' => $contribution['id'],
1401 'return' => 'trxn_id',
1402 )));
8f39a111 1403 }
c71ae314 1404
a1a2a83d 1405 /**
eceb18cc 1406 * Function tests invalid contribution status change.
c71ae314 1407 */
00be9182 1408 public function testCreateUpdateContributionInValidStatusChange() {
c71ae314
PN
1409 $contribParams = array(
1410 'contact_id' => 1,
1411 'receive_date' => '2012-01-01',
1412 'total_amount' => 100.00,
1413 'financial_type_id' => 1,
1414 'payment_instrument_id' => 1,
1415 'contribution_status_id' => 1,
c71ae314 1416 );
4ab7d517 1417 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
c71ae314 1418 $newParams = array_merge($contribParams, array(
5896d037
TO
1419 'id' => $contribution['id'],
1420 'contribution_status_id' => 2,
c71ae314
PN
1421 )
1422 );
6c6e6187 1423 $this->callAPIFailure('contribution', 'create', $newParams, ts('Cannot change contribution status from Completed to Pending.'));
c71ae314 1424
6a488035
TO
1425 }
1426
a1a2a83d 1427 /**
eceb18cc 1428 * Function tests that financial records are added when Pending Contribution is Canceled.
6a488035 1429 */
00be9182 1430 public function testCreateUpdateContributionCancelPending() {
6a488035
TO
1431 $contribParams = array(
1432 'contact_id' => $this->_individualId,
1433 'receive_date' => '2012-01-01',
1434 'total_amount' => 100.00,
4ab7d517 1435 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1436 'payment_instrument_id' => 1,
1437 'contribution_status_id' => 2,
c71ae314 1438 'is_pay_later' => 1,
4ab7d517 1439
6a488035 1440 );
4ab7d517 1441 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
6a488035 1442 $newParams = array_merge($contribParams, array(
5896d037
TO
1443 'id' => $contribution['id'],
1444 'contribution_status_id' => 3,
0a8160e8 1445 'cancel_date' => '2012-02-02 09:00',
6a488035
TO
1446 )
1447 );
0a8160e8 1448 //Check if trxn_date is same as cancel_date.
1449 $checkTrxnDate = array(
1450 'trxn_date' => '2012-02-02 09:00:00',
1451 );
694769da 1452 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
0a8160e8 1453 $this->_checkFinancialTrxn($contribution, 'cancelPending', NULL, $checkTrxnDate);
6a488035
TO
1454 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1455 }
1456
a1a2a83d 1457 /**
eceb18cc 1458 * Function tests that financial records are added when Financial Type is Changed.
6a488035 1459 */
00be9182 1460 public function testCreateUpdateContributionChangeFinancialType() {
6a488035
TO
1461 $contribParams = array(
1462 'contact_id' => $this->_individualId,
1463 'receive_date' => '2012-01-01',
1464 'total_amount' => 100.00,
1465 'financial_type_id' => 1,
1466 'payment_instrument_id' => 1,
1467 'contribution_status_id' => 1,
4ab7d517 1468
6a488035 1469 );
4ab7d517 1470 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
6a488035 1471 $newParams = array_merge($contribParams, array(
5896d037
TO
1472 'id' => $contribution['id'],
1473 'financial_type_id' => 3,
6a488035
TO
1474 )
1475 );
694769da 1476 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
6a488035
TO
1477 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1478 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1479 }
1480
694769da 1481 /**
1e52837d 1482 * Test that update does not change status id CRM-15105.
694769da 1483 */
00be9182 1484 public function testCreateUpdateWithoutChangingPendingStatus() {
694769da
VU
1485 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params, array('contribution_status_id' => 2)));
1486 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
5896d037 1487 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
92915c55
TO
1488 'id' => $contribution['id'],
1489 'api.contribution.delete' => 1,
1490 ));
694769da
VU
1491 $this->assertEquals(2, $contribution['contribution_status_id']);
1492 }
a1a2a83d
TO
1493
1494 /**
28de42d1
EM
1495 * Test Updating a Contribution.
1496 *
a1a2a83d
TO
1497 * CHANGE: we require the API to do an incremental update
1498 */
00be9182 1499 public function testCreateUpdateContribution() {
6a488035 1500
78ab0ca4 1501 $contributionID = $this->contributionCreate(array(
1502 'contact_id' => $this->_individualId,
1503 'trxn_id' => 212355,
1504 'financial_type_id' => $this->_financialTypeId,
1505 'invoice_id' => 'old_invoice',
1506 ));
6a488035
TO
1507 $old_params = array(
1508 'contribution_id' => $contributionID,
6a488035 1509 );
4ab7d517 1510 $original = $this->callAPISuccess('contribution', 'get', $old_params);
2bfae985 1511 $this->assertEquals($original['id'], $contributionID);
6a488035
TO
1512 //set up list of old params, verify
1513
1514 //This should not be required on update:
1515 $old_contact_id = $original['values'][$contributionID]['contact_id'];
7d543448 1516 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
6a488035
TO
1517 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1518 $old_source = $original['values'][$contributionID]['contribution_source'];
1519
6a488035
TO
1520 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1521 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1522
1523 //check against values in CiviUnitTestCase::createContribution()
2bfae985
EM
1524 $this->assertEquals($old_contact_id, $this->_individualId);
1525 $this->assertEquals($old_fee_amount, 5.00);
1526 $this->assertEquals($old_source, 'SSF');
1527 $this->assertEquals($old_trxn_id, 212355);
78ab0ca4 1528 $this->assertEquals($old_invoice_id, 'old_invoice');
6a488035
TO
1529 $params = array(
1530 'id' => $contributionID,
1531 'contact_id' => $this->_individualId,
1532 'total_amount' => 110.00,
4ab7d517 1533 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1534 'non_deductible_amount' => 10.00,
1535 'net_amount' => 100.00,
1536 'contribution_status_id' => 1,
ef32adff 1537 'note' => 'Donating for Noble Cause',
4ab7d517 1538
6a488035
TO
1539 );
1540
4ab7d517 1541 $contribution = $this->callAPISuccess('contribution', 'create', $params);
6a488035
TO
1542
1543 $new_params = array(
1544 'contribution_id' => $contribution['id'],
4ab7d517 1545
6a488035 1546 );
ef32adff 1547 $contribution = $this->callAPISuccessGetSingle('contribution', $new_params);
1548
1549 $this->assertEquals($contribution['contact_id'], $this->_individualId);
1550 $this->assertEquals($contribution['total_amount'], 110.00);
1551 $this->assertEquals($contribution['financial_type_id'], $this->_financialTypeId);
1552 $this->assertEquals($contribution['financial_type'], 'Donation');
1553 $this->assertEquals($contribution['instrument_id'], $old_payment_instrument);
1554 $this->assertEquals($contribution['non_deductible_amount'], 10.00);
1555 $this->assertEquals($contribution['fee_amount'], $old_fee_amount);
1556 $this->assertEquals($contribution['net_amount'], 100.00);
1557 $this->assertEquals($contribution['trxn_id'], $old_trxn_id);
1558 $this->assertEquals($contribution['invoice_id'], $old_invoice_id);
1559 $this->assertEquals($contribution['contribution_source'], $old_source);
1560 $this->assertEquals($contribution['contribution_status'], 'Completed');
6a488035
TO
1561 $params = array(
1562 'contribution_id' => $contributionID,
4ab7d517 1563
6a488035 1564 );
4ab7d517 1565 $result = $this->callAPISuccess('contribution', 'delete', $params);
22f80e87 1566 $this->assertAPISuccess($result);
6a488035
TO
1567 }
1568
a1a2a83d
TO
1569 /**
1570 * Attempt (but fail) to delete a contribution without parameters.
1571 */
00be9182 1572 public function testDeleteEmptyParamsContribution() {
4ab7d517 1573 $params = array();
858d0bf8 1574 $this->callAPIFailure('contribution', 'delete', $params);
6a488035
TO
1575 }
1576
00be9182 1577 public function testDeleteParamsNotArrayContribution() {
6a488035 1578 $params = 'contribution_id= 1';
d0e1eff2 1579 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
6a488035
TO
1580 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1581 }
1582
00be9182 1583 public function testDeleteWrongParamContribution() {
6a488035
TO
1584 $params = array(
1585 'contribution_source' => 'SSF',
4ab7d517 1586
6a488035 1587 );
858d0bf8 1588 $this->callAPIFailure('contribution', 'delete', $params);
6a488035
TO
1589 }
1590
00be9182 1591 public function testDeleteContribution() {
78ab0ca4 1592 $contributionID = $this->contributionCreate(array(
1593 'contact_id' => $this->_individualId,
1594 'financial_type_id' => $this->_financialTypeId,
1595 ));
6a488035
TO
1596 $params = array(
1597 'id' => $contributionID,
6a488035 1598 );
4ab7d517 1599 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__, __FILE__);
6a488035
TO
1600 }
1601
1602 /**
d177a2a6 1603 * Test civicrm_contribution_search with empty params.
1e52837d 1604 *
d177a2a6 1605 * All available contributions expected.
6a488035 1606 */
00be9182 1607 public function testSearchEmptyParams() {
4ab7d517 1608 $params = array();
6a488035
TO
1609
1610 $p = array(
1611 'contact_id' => $this->_individualId,
1612 'receive_date' => date('Ymd'),
1613 'total_amount' => 100.00,
4ab7d517 1614 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1615 'non_deductible_amount' => 10.00,
1616 'fee_amount' => 5.00,
1617 'net_amount' => 95.00,
1618 'trxn_id' => 23456,
1619 'invoice_id' => 78910,
1620 'source' => 'SSF',
1621 'contribution_status_id' => 1,
4ab7d517 1622
6a488035 1623 );
4ab7d517 1624 $contribution = $this->callAPISuccess('contribution', 'create', $p);
6a488035 1625
4ab7d517 1626 $result = $this->callAPISuccess('contribution', 'get', $params);
6a488035
TO
1627 // We're taking the first element.
1628 $res = $result['values'][$contribution['id']];
1629
2bfae985
EM
1630 $this->assertEquals($p['contact_id'], $res['contact_id']);
1631 $this->assertEquals($p['total_amount'], $res['total_amount']);
5896d037 1632 $this->assertEquals($p['financial_type_id'], $res['financial_type_id']);
2bfae985
EM
1633 $this->assertEquals($p['net_amount'], $res['net_amount']);
1634 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1635 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1636 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1637 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1638 $this->assertEquals($p['source'], $res['contribution_source']);
6a488035 1639 // contribution_status_id = 1 => Completed
2bfae985 1640 $this->assertEquals('Completed', $res['contribution_status']);
6a488035
TO
1641
1642 $this->contributionDelete($contribution['id']);
1643 }
1644
1645 /**
d177a2a6 1646 * Test civicrm_contribution_search. Success expected.
6a488035 1647 */
00be9182 1648 public function testSearch() {
6a488035
TO
1649 $p1 = array(
1650 'contact_id' => $this->_individualId,
1651 'receive_date' => date('Ymd'),
1652 'total_amount' => 100.00,
4ab7d517 1653 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1654 'non_deductible_amount' => 10.00,
1655 'contribution_status_id' => 1,
4ab7d517 1656
6a488035 1657 );
4ab7d517 1658 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
6a488035
TO
1659
1660 $p2 = array(
1661 'contact_id' => $this->_individualId,
1662 'receive_date' => date('Ymd'),
1663 'total_amount' => 200.00,
4ab7d517 1664 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1665 'non_deductible_amount' => 20.00,
1666 'trxn_id' => 5454565,
1667 'invoice_id' => 1212124,
1668 'fee_amount' => 50.00,
1669 'net_amount' => 60.00,
1670 'contribution_status_id' => 2,
4ab7d517 1671
6a488035 1672 );
2f45e1c2 1673 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
6a488035
TO
1674
1675 $params = array(
1676 'contribution_id' => $contribution2['id'],
4ab7d517 1677
6a488035 1678 );
2f45e1c2 1679 $result = $this->callAPISuccess('contribution', 'get', $params);
6a488035
TO
1680 $res = $result['values'][$contribution2['id']];
1681
2bfae985
EM
1682 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1683 $this->assertEquals($p2['total_amount'], $res['total_amount']);
5896d037 1684 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id']);
2bfae985
EM
1685 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1686 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1687 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1688 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1689 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
6a488035 1690 // contribution_status_id = 2 => Pending
2bfae985 1691 $this->assertEquals('Pending', $res['contribution_status']);
6a488035
TO
1692
1693 $this->contributionDelete($contribution1['id']);
1694 $this->contributionDelete($contribution2['id']);
1695 }
2f45e1c2 1696
0efa8efe 1697 /**
eceb18cc 1698 * Test completing a transaction via the API.
0efa8efe 1699 *
1700 * Note that we are creating a logged in user because email goes out from
1701 * that person
1702 */
00be9182 1703 public function testCompleteTransaction() {
5896d037 1704 $mut = new CiviMailUtils($this, TRUE);
ec7e3954 1705 $this->swapMessageTemplateForTestTemplate();
0efa8efe 1706 $this->createLoggedInUser();
6c6e6187
TO
1707 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1708 $contribution = $this->callAPISuccess('contribution', 'create', $params);
66d3f9be 1709 $this->callAPISuccess('contribution', 'completetransaction', array(
0efa8efe 1710 'id' => $contribution['id'],
66d3f9be 1711 ));
cc7b912f 1712 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
d5580ed4 1713 $this->assertEquals('SSF', $contribution['contribution_source']);
cc7b912f 1714 $this->assertEquals('Completed', $contribution['contribution_status']);
1715 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
0efa8efe 1716 $mut->checkMailLog(array(
ec7e3954
E
1717 'email:::anthony_anderson@civicrm.org',
1718 'is_monetary:::1',
1719 'amount:::100.00',
1720 'currency:::USD',
1721 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])),
76e8d9c4 1722 "receipt_date:::\n",
ec7e3954
E
1723 'contributeMode:::notify',
1724 'title:::Contribution',
1725 'displayName:::Mr. Anthony Anderson II',
0efa8efe 1726 ));
46fa5206 1727 $mut->stop();
ec7e3954 1728 $this->revertTemplateToReservedTemplate();
46fa5206
EM
1729 }
1730
e05d2e11 1731 /**
1732 * Test to ensure mail is sent on chosing pay later
1733 */
1734 public function testpayLater() {
1735 $mut = new CiviMailUtils($this, TRUE);
1736 $this->swapMessageTemplateForTestTemplate();
1737 $this->createLoggedInUser();
1738
1739 // create contribution page first
1740 $contributionPageParams = array(
1741 'title' => 'Help Support CiviCRM!',
1742 'financial_type_id' => 1,
1743 'is_monetary' => TRUE,
1744 'is_pay_later' => 1,
1745 'is_quick_config' => TRUE,
1746 'pay_later_text' => 'I will send payment by check',
1747 'pay_later_receipt' => 'This is a pay later reciept',
1748 'is_allow_other_amount' => 1,
1749 'min_amount' => 10.00,
1750 'max_amount' => 10000.00,
1751 'goal_amount' => 100000.00,
1752 'is_email_receipt' => 1,
1753 'is_active' => 1,
1754 'amount_block_is_active' => 1,
1755 'currency' => 'USD',
1756 'is_billing_required' => 0,
1757 );
1758 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', $contributionPageParams);
1759
1760 // submit form values
1761 $priceSet = $this->callAPISuccess('price_set', 'getsingle', array('name' => 'default_contribution_amount'));
1762 $params = array(
1763 'id' => $contributionPageResult['id'],
1764 'contact_id' => $this->_individualId,
1765 'email-5' => 'anthony_anderson@civicrm.org',
1766 'payment_processor_id' => 0,
1767 'amount' => 100.00,
1768 'tax_amount' => '',
1769 'currencyID' => 'USD',
1770 'is_pay_later' => 1,
1771 'invoiceID' => 'f28e1ddc86f8c4a0ff5bcf46393e4bc8',
1772 'is_quick_config' => 1,
1773 'description' => 'Online Contribution: Help Support CiviCRM!',
1774 'price_set_id' => $priceSet['id'],
1775 );
1776 $this->callAPISuccess('contribution_page', 'submit', $params);
1777
1778 $mut->checkMailLog(array(
1779 'is_pay_later:::1',
1780 'email:::anthony_anderson@civicrm.org',
1781 'pay_later_receipt:::' . $contributionPageParams['pay_later_receipt'],
1782 'displayName:::Mr. Anthony Anderson II',
1783 'contributionPageId:::' . $contributionPageResult['id'],
1784 'title:::' . $contributionPageParams['title'],
8beee0e8 1785 'amount:::' . $params['amount'],
e05d2e11 1786 ));
1787 $mut->stop();
1788 $this->revertTemplateToReservedTemplate();
1789 }
1790
2a0df9d9 1791 /**
1792 * Test to check whether contact billing address is used when no contribution address
1793 */
1794 public function testBillingAddress() {
1795 $mut = new CiviMailUtils($this, TRUE);
1796 $this->swapMessageTemplateForTestTemplate();
1797 $this->createLoggedInUser();
1798
1799 //Scenario 1: When Contact don't have any address
1800 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1801 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1802 $this->callAPISuccess('contribution', 'completetransaction', array(
1803 'id' => $contribution['id'],
1804 ));
1805 $mut->checkMailLog(array(
1806 'address:::',
1807 ));
1808
1809 // Scenario 2: Contribution using address
1810 $address = $this->callAPISuccess('address', 'create', array(
1811 'street_address' => 'contribution billing st',
1812 'location_type_id' => 2,
1813 'contact_id' => $this->_params['contact_id'],
1814 ));
76e8d9c4
E
1815 $params = array_merge($this->_params, array(
1816 'contribution_status_id' => 2,
2a0df9d9 1817 'address_id' => $address['id'],
1818 )
1819 );
1820 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1821 $this->callAPISuccess('contribution', 'completetransaction', array(
1822 'id' => $contribution['id'],
1823 ));
1824 $mut->checkMailLog(array(
1825 'address:::contribution billing st',
1826 ));
1827
1828 // Scenario 3: Contribution wtth no address but contact has a billing address
1829 $this->callAPISuccess('address', 'create', array(
1830 'id' => $address['id'],
1831 'street_address' => 'is billing st',
1832 'contact_id' => $this->_params['contact_id'],
1833 ));
1834 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1835 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1836 $this->callAPISuccess('contribution', 'completetransaction', array(
1837 'id' => $contribution['id'],
1838 ));
1839 $mut->checkMailLog(array(
1840 'address:::is billing st',
1841 ));
1842
1843 $mut->stop();
1844 $this->revertTemplateToReservedTemplate();
1845 }
1846
080a561b 1847 /**
1848 * Test completing a transaction via the API.
1849 *
1850 * Note that we are creating a logged in user because email goes out from
1851 * that person
1852 */
1853 public function testCompleteTransactionFeeAmount() {
1854 $this->createLoggedInUser();
1855 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1856 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1857 $this->callAPISuccess('contribution', 'completetransaction', array(
1858 'id' => $contribution['id'],
1859 'fee_amount' => '.56',
1860 'trxn_id' => '7778888',
1861 ));
1862 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'], 'sequential' => 1));
1863 $this->assertEquals('Completed', $contribution['contribution_status']);
1864 $this->assertEquals('7778888', $contribution['trxn_id']);
1865 $this->assertEquals('.56', $contribution['fee_amount']);
1866 $this->assertEquals('99.44', $contribution['net_amount']);
1867 }
1868
effb4d85
SL
1869 /**
1870 * CRM-19126 Add test to verify when complete transaction is called tax amount is not changed
1871 */
1872 public function testCheckTaxAmount() {
1873 $contact = $this->createLoggedInUser();
1874 $financialType = $this->callAPISuccess('financial_type', 'create', array(
1875 'name' => 'Test taxable financial Type',
1876 'is_reserved' => 0,
1877 'is_active' => 1,
1878 ));
1879 $financialAccount = $this->callAPISuccess('financial_account', 'create', array(
1880 'name' => 'Test Tax financial account ',
1881 'contact_id' => $contact,
1882 'financial_account_type_id' => 2,
1883 'is_tax' => 1,
1884 'tax_rate' => 5.00,
1885 'is_reserved' => 0,
1886 'is_active' => 1,
1887 'is_default' => 0,
1888 ));
1889 $financialTypeId = $financialType['id'];
1890 $financialAccountId = $financialAccount['id'];
1891 $financialAccountParams = array(
1892 'entity_table' => 'civicrm_financial_type',
1893 'entity_id' => $financialTypeId,
1894 'account_relationship' => 10,
1895 'financial_account_id' => $financialAccountId,
1896 );
a76b8bd8 1897 CRM_Financial_BAO_FinancialTypeAccount::add($financialAccountParams);
effb4d85
SL
1898 $taxRates = CRM_Core_PseudoConstant::getTaxRates();
1899 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'financial_type_id' => $financialTypeId));
1900 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1901 $contribution1 = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'return' => 'tax_amount', 'sequential' => 1));
1902 $this->callAPISuccess('contribution', 'completetransaction', array(
1903 'id' => $contribution['id'],
1904 'trxn_id' => '777788888',
99a4cd32 1905 'fee_amount' => '6.00',
effb4d85 1906 ));
99a4cd32 1907 $contribution2 = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'return' => array('tax_amount', 'fee_amount', 'net_amount'), 'sequential' => 1));
effb4d85 1908 $this->assertEquals($contribution1['values'][0]['tax_amount'], $contribution2['values'][0]['tax_amount']);
99a4cd32 1909 $this->assertEquals('6.00', $contribution2['values'][0]['fee_amount']);
f836984d 1910 $this->assertEquals('99.00', $contribution2['values'][0]['net_amount']);
effb4d85
SL
1911 }
1912
d97c96dc 1913 /**
0e6ccb2e 1914 * Test repeat contribution successfully creates line item.
d97c96dc 1915 */
1e52837d 1916 public function testRepeatTransaction() {
0e6ccb2e 1917 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'single');
d97c96dc
EM
1918 $this->callAPISuccess('contribution', 'repeattransaction', array(
1919 'original_contribution_id' => $originalContribution['id'],
1920 'contribution_status_id' => 'Completed',
1921 'trxn_id' => uniqid(),
1922 ));
1923 $lineItemParams = array(
1924 'entity_id' => $originalContribution['id'],
1925 'sequential' => 1,
1926 'return' => array(
1927 'entity_table',
1928 'qty',
1929 'unit_price',
1930 'line_total',
1931 'label',
1932 'financial_type_id',
1933 'deductible_amount',
1934 'price_field_value_id',
1935 'price_field_id',
1e52837d 1936 ),
d97c96dc
EM
1937 );
1938 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1939 'entity_id' => $originalContribution['id'],
1940 )));
1941 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1942 'entity_id' => $originalContribution['id'] + 1,
1943 )));
1944 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
1945 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1946 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
f69a9ac3 1947 $this->_checkFinancialRecords(array(
1948 'id' => $originalContribution['id'] + 1,
1949 'payment_instrument_id' => $this->callAPISuccessGetValue('PaymentProcessor', array(
1950 'id' => $originalContribution['payment_processor_id'],
1951 'return' => 'payment_instrument_id',
1952 )),
1953 ), 'online');
d97c96dc
EM
1954 $this->quickCleanUpFinancialEntities();
1955 }
1956
893a550c 1957 /**
0e6ccb2e
K
1958 * Test repeat contribution successfully creates line items (plural).
1959 */
1960 public function testRepeatTransactionLineItems() {
7150b1c8 1961 // CRM-19309
0e6ccb2e
K
1962 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'multiple');
1963 $this->callAPISuccess('contribution', 'repeattransaction', array(
1964 'original_contribution_id' => $originalContribution['id'],
1965 'contribution_status_id' => 'Completed',
1966 'trxn_id' => uniqid(),
1967 ));
1968
1969 $lineItemParams = array(
1970 'entity_id' => $originalContribution['id'],
1971 'sequential' => 1,
1972 'return' => array(
1973 'entity_table',
1974 'qty',
1975 'unit_price',
1976 'line_total',
1977 'label',
1978 'financial_type_id',
1979 'deductible_amount',
1980 'price_field_value_id',
1981 'price_field_id',
1982 ),
1983 );
1984 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1985 'entity_id' => $originalContribution['id'],
1986 )));
1987 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1988 'entity_id' => $originalContribution['id'] + 1,
1989 )));
1990
1991 // unset id and entity_id for all of them to be able to compare the lineItems:
1992 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
1993 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1994 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
1995
1996 unset($lineItem1['values'][1]['id'], $lineItem1['values'][1]['entity_id']);
1997 unset($lineItem2['values'][1]['id'], $lineItem2['values'][1]['entity_id']);
1998 $this->assertEquals($lineItem1['values'][1], $lineItem2['values'][1]);
1999
7150b1c8
K
2000 // CRM-19309 so in future we also want to:
2001 // check that financial_line_items have been created for entity_id 3 and 4;
0e6ccb2e
K
2002
2003 $this->callAPISuccessGetCount('FinancialItem', array('description' => 'Sales Tax', 'amount' => 0), 0);
2004 $this->quickCleanUpFinancialEntities();
2005 }
2006
2007 /**
2008 * Test repeat contribution successfully creates is_test transaction.
893a550c 2009 */
2010 public function testRepeatTransactionIsTest() {
2011 $this->_params['is_test'] = 1;
0e6ccb2e 2012 $originalContribution = $this->setUpRepeatTransaction(array('is_test' => 1), 'single');
893a550c 2013
2014 $this->callAPISuccess('contribution', 'repeattransaction', array(
2015 'original_contribution_id' => $originalContribution['id'],
2016 'contribution_status_id' => 'Completed',
2017 'trxn_id' => uniqid(),
2018 ));
2019 $this->callAPISuccessGetCount('Contribution', array('contribution_test' => 1), 2);
2020 }
2021
d5580ed4 2022 /**
2023 * Test repeat contribution passed in status.
2024 */
2025 public function testRepeatTransactionPassedInStatus() {
0e6ccb2e 2026 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'single');
d5580ed4 2027
2028 $this->callAPISuccess('contribution', 'repeattransaction', array(
2029 'original_contribution_id' => $originalContribution['id'],
2030 'contribution_status_id' => 'Pending',
2031 'trxn_id' => uniqid(),
2032 ));
2033 $this->callAPISuccessGetCount('Contribution', array('contribution_status_id' => 2), 1);
2034 }
2035
1eade77d 2036 /**
2037 * Test repeat contribution accepts recur_id instead of original_contribution_id.
2038 */
2039 public function testRepeatTransactionAcceptRecurID() {
2040 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2041 'contact_id' => $this->_individualId,
2042 'installments' => '12',
2043 'frequency_interval' => '1',
2044 'amount' => '100',
2045 'contribution_status_id' => 1,
2046 'start_date' => '2012-01-01 00:00:00',
2047 'currency' => 'USD',
2048 'frequency_unit' => 'month',
2049 'payment_processor_id' => $this->paymentProcessorID,
2050 ));
2051 $this->callAPISuccess('contribution', 'create', array_merge(
2052 $this->_params,
2053 array('contribution_recur_id' => $contributionRecur['id']))
2054 );
2055
2056 $this->callAPISuccess('contribution', 'repeattransaction', array(
2057 'contribution_recur_id' => $contributionRecur['id'],
2058 'contribution_status_id' => 'Completed',
2059 'trxn_id' => uniqid(),
2060 ));
2061
2062 $this->quickCleanUpFinancialEntities();
2063 }
2064
28124368
PH
2065 /**
2066 * CRM-19873 Test repattransaction if contribution_recur_id is a test.
2067 */
2068 public function testRepeatTransactionTestRecurId() {
2069 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2070 'contact_id' => $this->_individualId,
2071 'frequency_interval' => '1',
2072 'amount' => '1.00',
2073 'contribution_status_id' => 1,
2074 'start_date' => '2017-01-01 00:00:00',
2075 'currency' => 'USD',
2076 'frequency_unit' => 'month',
2077 'payment_processor_id' => $this->paymentProcessorID,
2078 'is_test' => 1,
2079 ));
2080 $this->callAPISuccess('contribution', 'create', array_merge(
2081 $this->_params,
2082 array(
2083 'contribution_recur_id' => $contributionRecur['id'],
2084 'is_test' => 1,
2085 ))
2086 );
2087
2088 $repeatedContribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
2089 'contribution_recur_id' => $contributionRecur['id'],
2090 'contribution_status_id' => 'Completed',
2091 'trxn_id' => uniqid(),
2092 ));
2093
2094 $this->assertEquals($contributionRecur['values'][1]['is_test'], $repeatedContribution['values'][2]['is_test']);
2095 $this->quickCleanUpFinancialEntities();
2096 }
d2334242
PH
2097 /**
2098 * CRM-19945 Tests repeattransaction is using a completed contribution for the template.
2099 * ( Tests membership is renewed after repeattransaction. )
2100 */
2101 public function testRepeatTransactionUsesCompleted() {
2102 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
2103
2104 $this->callAPISuccess('contribution', 'create', array(
2105 'contact_id' => $originalContribution['values'][1]['contact_id'],
2106 'financial_type_id' => $originalContribution['values'][1]['financial_type_id'],
2107 'total_amount' => $originalContribution['values'][1]['total_amount'],
2108 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2109 'contribution_status_id' => "Failed",
2110 ));
2111
2112 $this->callAPISuccess('membership', 'create', array(
2113 'id' => $membership['id'],
2114 'end_date' => 'yesterday',
2115 'status_id' => 4,
2116 ));
2117
2118 $this->callAPISuccess('contribution', 'repeattransaction', array(
2119 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2120 'contribution_status_id' => 'Completed',
2121 'trxn_id' => uniqid(),
2122 ));
2123
2124 $membershipStatus = $this->callAPISuccess('membership', 'getvalue', array(
2125 'id' => $membership['id'],
2126 'return' => 'status_id',
2127 ));
2128
2129 $this->assertEquals('1', $membershipStatus);
2130 $this->quickCleanUpFinancialEntities();
2131 $this->contactDelete($originalContribution['values'][1]['contact_id']);
2132 }
28124368 2133
c03f1689
EM
2134 /**
2135 * CRM-16397 test appropriate action if total amount has changed for single line items.
2136 */
2137 public function testRepeatTransactionAlteredAmount() {
2138 $paymentProcessorID = $this->paymentProcessorCreate();
c03f1689
EM
2139 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2140 'contact_id' => $this->_individualId,
2141 'installments' => '12',
2142 'frequency_interval' => '1',
2143 'amount' => '500',
2144 'contribution_status_id' => 1,
2145 'start_date' => '2012-01-01 00:00:00',
2146 'currency' => 'USD',
2147 'frequency_unit' => 'month',
2148 'payment_processor_id' => $paymentProcessorID,
2149 ));
2150 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2151 $this->_params,
2152 array(
2153 'contribution_recur_id' => $contributionRecur['id'],
c03f1689
EM
2154 ))
2155 );
2156
2157 $this->callAPISuccess('contribution', 'repeattransaction', array(
2158 'original_contribution_id' => $originalContribution['id'],
2159 'contribution_status_id' => 'Completed',
2160 'trxn_id' => uniqid(),
2161 'total_amount' => '400',
2162 'fee_amount' => 50,
c03f1689 2163 ));
0e6ccb2e 2164
c03f1689
EM
2165 $lineItemParams = array(
2166 'entity_id' => $originalContribution['id'],
2167 'sequential' => 1,
2168 'return' => array(
2169 'entity_table',
2170 'qty',
2171 'unit_price',
2172 'line_total',
2173 'label',
2174 'financial_type_id',
2175 'deductible_amount',
2176 'price_field_value_id',
2177 'price_field_id',
2178 ),
2179 );
2180 $this->callAPISuccessGetSingle('contribution', array(
2181 'total_amount' => 400,
c03f1689
EM
2182 'fee_amount' => 50,
2183 'net_amount' => 350,
2184 ));
2185 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2186 'entity_id' => $originalContribution['id'],
2187 )));
2188 $expectedLineItem = array_merge(
2189 $lineItem1['values'][0], array(
2190 'line_total' => '400.00',
2191 'unit_price' => '400.00',
542d9e2c
EM
2192 )
2193 );
c03f1689
EM
2194
2195 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2196 'entity_id' => $originalContribution['id'] + 1,
2197 )));
0e6ccb2e 2198
c03f1689
EM
2199 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2200 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2201 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
c02c17df 2202 }
c03f1689 2203
3c49d90c 2204 /**
2205 * CRM-17718 test appropriate action if financial type has changed for single line items.
2206 */
2207 public function testRepeatTransactionPassedInFinancialType() {
2208 $originalContribution = $this->setUpRecurringContribution();
2209
2210 $this->callAPISuccess('contribution', 'repeattransaction', array(
2211 'original_contribution_id' => $originalContribution['id'],
2212 'contribution_status_id' => 'Completed',
2213 'trxn_id' => uniqid(),
2214 'financial_type_id' => 2,
2215 ));
2216 $lineItemParams = array(
2217 'entity_id' => $originalContribution['id'],
2218 'sequential' => 1,
2219 'return' => array(
2220 'entity_table',
2221 'qty',
2222 'unit_price',
2223 'line_total',
2224 'label',
2225 'financial_type_id',
2226 'deductible_amount',
2227 'price_field_value_id',
2228 'price_field_id',
2229 ),
2230 );
2231
2232 $this->callAPISuccessGetSingle('contribution', array(
2233 'total_amount' => 100,
2234 'financial_type_id' => 2,
2235 ));
2236 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2237 'entity_id' => $originalContribution['id'],
2238 )));
2239 $expectedLineItem = array_merge(
2240 $lineItem1['values'][0], array(
2241 'line_total' => '100.00',
2242 'unit_price' => '100.00',
2243 'financial_type_id' => 2,
257ab382 2244 'contribution_type_id' => 2,
3c49d90c 2245 )
2246 );
3c49d90c 2247 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2248 'entity_id' => $originalContribution['id'] + 1,
2249 )));
2250 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2251 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2252 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2253 }
2254
7f4ef731 2255 /**
2256 * CRM-17718 test appropriate action if financial type has changed for single line items.
2257 */
2258 public function testRepeatTransactionUpdatedFinancialType() {
2259 $originalContribution = $this->setUpRecurringContribution(array(), array('financial_type_id' => 2));
2260
2261 $this->callAPISuccess('contribution', 'repeattransaction', array(
2262 'contribution_recur_id' => $originalContribution['id'],
2263 'contribution_status_id' => 'Completed',
2264 'trxn_id' => uniqid(),
2265 ));
2266 $lineItemParams = array(
2267 'entity_id' => $originalContribution['id'],
2268 'sequential' => 1,
2269 'return' => array(
2270 'entity_table',
2271 'qty',
2272 'unit_price',
2273 'line_total',
2274 'label',
2275 'financial_type_id',
2276 'deductible_amount',
2277 'price_field_value_id',
2278 'price_field_id',
2279 ),
2280 );
2281
2282 $this->callAPISuccessGetSingle('contribution', array(
2283 'total_amount' => 100,
2284 'financial_type_id' => 2,
2285 ));
2286 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2287 'entity_id' => $originalContribution['id'],
2288 )));
2289 $expectedLineItem = array_merge(
2290 $lineItem1['values'][0], array(
2291 'line_total' => '100.00',
2292 'unit_price' => '100.00',
2293 'financial_type_id' => 2,
257ab382 2294 'contribution_type_id' => 2,
7f4ef731 2295 )
2296 );
2297
2298 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2299 'entity_id' => $originalContribution['id'] + 1,
2300 )));
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]);
2304 }
2305
c02c17df 2306 /**
1eade77d 2307 * CRM-16397 test appropriate action if campaign has been passed in.
c02c17df 2308 */
2309 public function testRepeatTransactionPassedInCampaign() {
2310 $paymentProcessorID = $this->paymentProcessorCreate();
2311 $campaignID = $this->campaignCreate();
2312 $campaignID2 = $this->campaignCreate();
2313 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2314 'contact_id' => $this->_individualId,
2315 'installments' => '12',
2316 'frequency_interval' => '1',
2317 'amount' => '100',
2318 'contribution_status_id' => 1,
2319 'start_date' => '2012-01-01 00:00:00',
2320 'currency' => 'USD',
2321 'frequency_unit' => 'month',
2322 'payment_processor_id' => $paymentProcessorID,
2323 ));
2324 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2325 $this->_params,
2326 array(
2327 'contribution_recur_id' => $contributionRecur['id'],
2328 'campaign_id' => $campaignID,
2329 ))
2330 );
2331
2332 $this->callAPISuccess('contribution', 'repeattransaction', array(
2333 'original_contribution_id' => $originalContribution['id'],
2334 'contribution_status_id' => 'Completed',
2335 'trxn_id' => uniqid(),
2336 'campaign_id' => $campaignID2,
2337 ));
2338
2339 $this->callAPISuccessGetSingle('contribution', array(
2340 'total_amount' => 100,
2341 'campaign_id' => $campaignID2,
2342 ));
2343 }
2344
2345 /**
2346 * CRM-17718 campaign stored on contribution recur gets priority.
2347 *
2348 * This reflects the fact we permit people to update them.
2349 */
2350 public function testRepeatTransactionUpdatedCampaign() {
2351 $paymentProcessorID = $this->paymentProcessorCreate();
2352 $campaignID = $this->campaignCreate();
2353 $campaignID2 = $this->campaignCreate();
2354 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2355 'contact_id' => $this->_individualId,
2356 'installments' => '12',
2357 'frequency_interval' => '1',
2358 'amount' => '100',
2359 'contribution_status_id' => 1,
2360 'start_date' => '2012-01-01 00:00:00',
2361 'currency' => 'USD',
2362 'frequency_unit' => 'month',
2363 'payment_processor_id' => $paymentProcessorID,
2364 'campaign_id' => $campaignID,
2365 ));
2366 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2367 $this->_params,
2368 array(
2369 'contribution_recur_id' => $contributionRecur['id'],
2370 'campaign_id' => $campaignID2,
2371 ))
2372 );
2373
2374 $this->callAPISuccess('contribution', 'repeattransaction', array(
2375 'original_contribution_id' => $originalContribution['id'],
2376 'contribution_status_id' => 'Completed',
2377 'trxn_id' => uniqid(),
2378 ));
2379
2380 $this->callAPISuccessGetSingle('contribution', array(
2381 'total_amount' => 100,
2382 'campaign_id' => $campaignID,
2383 ));
c03f1689
EM
2384 }
2385
2936c3b5
EM
2386 /**
2387 * Test completing a transaction does not 'mess' with net amount (CRM-15960).
2388 */
2389 public function testCompleteTransactionNetAmountOK() {
2390 $this->createLoggedInUser();
2391 $params = array_merge($this->_params, array('contribution_status_id' => 2));
2392 unset($params['net_amount']);
2393 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2394 $this->callAPISuccess('contribution', 'completetransaction', array(
2395 'id' => $contribution['id'],
2396 ));
2397 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
2398 $this->assertEquals('Completed', $contribution['contribution_status']);
2399 $this->assertTrue(($contribution['total_amount'] - $contribution['net_amount']) == $contribution['fee_amount']);
2400 }
2401
46fa5206 2402 /**
1e52837d 2403 * CRM-14151 - Test completing a transaction via the API.
46fa5206 2404 */
00be9182 2405 public function testCompleteTransactionWithReceiptDateSet() {
76e8d9c4 2406 $this->swapMessageTemplateForTestTemplate();
5896d037 2407 $mut = new CiviMailUtils($this, TRUE);
46fa5206 2408 $this->createLoggedInUser();
6c6e6187
TO
2409 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
2410 $contribution = $this->callAPISuccess('contribution', 'create', $params);
7104593e 2411 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id'], 'trxn_date' => date('Y-m-d')));
6c6e6187 2412 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
46fa5206 2413 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
7104593e 2414 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['values'][0]['receive_date'])));
46fa5206
EM
2415 $mut->checkMailLog(array(
2416 'Receipt - Contribution',
76e8d9c4 2417 'receipt_date:::' . date('Ymd'),
46fa5206 2418 ));
0efa8efe 2419 $mut->stop();
76e8d9c4 2420 $this->revertTemplateToReservedTemplate();
0efa8efe 2421 }
2422
e2ca457d
KG
2423 /**
2424 * CRM-1960 - Test to ensure that completetransaction respects the is_email_receipt setting
2425 */
2426 public function testCompleteTransactionWithEmailReceiptInput() {
2427 // Create a Contribution Page with is_email_receipt = TRUE
2428 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array(
2429 'receipt_from_name' => 'Mickey Mouse',
2430 'receipt_from_email' => 'mickey@mouse.com',
2431 'title' => "Test Contribution Page",
2432 'financial_type_id' => 1,
2433 'currency' => 'CAD',
2434 'is_monetary' => TRUE,
2435 'is_email_receipt' => TRUE,
2436 ));
2437 $this->_params['contribution_page_id'] = $contributionPage['id'];
2438 $params = array_merge($this->_params, array('contribution_status_id' => 2));
2439 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2440 // Complete the transaction overriding is_email_receipt to = FALSE
2441 $this->callAPISuccess('contribution', 'completetransaction', array(
2442 'id' => $contribution['id'],
2443 'trxn_date' => date('2011-04-09'),
2444 'trxn_id' => 'kazam',
2445 'is_email_receipt' => 0,
2446 ));
2447 // Check if a receipt was issued
2448 $receipt_date = $this->callAPISuccess('Contribution', 'getvalue', array('id' => $contribution['id'], 'return' => 'receipt_date'));
2449 $this->assertEquals('', $receipt_date);
2450 }
3b8c739e 2451
b80f2ad1
E
2452 /**
2453 * Complete the transaction using the template with all the possible.
2454 */
2455 public function testCompleteTransactionWithTestTemplate() {
2456 $this->swapMessageTemplateForTestTemplate();
ec7e3954 2457 $contribution = $this->setUpForCompleteTransaction();
b80f2ad1
E
2458 $this->callAPISuccess('contribution', 'completetransaction', array(
2459 'id' => $contribution['id'],
2460 'trxn_date' => date('2011-04-09'),
2461 'trxn_id' => 'kazam',
2462 ));
2463 $receive_date = $this->callAPISuccess('Contribution', 'getvalue', array('id' => $contribution['id'], 'return' => 'receive_date'));
ec7e3954 2464 $this->mut->checkMailLog(array(
b80f2ad1
E
2465 'email:::anthony_anderson@civicrm.org',
2466 'is_monetary:::1',
2467 'amount:::100.00',
2468 'currency:::USD',
2469 'receive_date:::' . date('Ymd', strtotime($receive_date)),
2470 'receipt_date:::' . date('Ymd'),
2471 'contributeMode:::notify',
2472 'title:::Contribution',
2473 'displayName:::Mr. Anthony Anderson II',
2474 'trxn_id:::kazam',
ec7e3954 2475 'contactID:::' . $this->_params['contact_id'],
b80f2ad1
E
2476 'contributionID:::' . $contribution['id'],
2477 'financialTypeId:::1',
2478 'financialTypeName:::Donation',
2479 ));
ec7e3954 2480 $this->mut->stop();
b80f2ad1
E
2481 $this->revertTemplateToReservedTemplate();
2482 }
2483
ec7e3954
E
2484 /**
2485 * Complete the transaction using the template with all the possible.
2486 */
2487 public function testCompleteTransactionContributionPageFromAddress() {
2488 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array(
2489 'receipt_from_name' => 'Mickey Mouse',
2490 'receipt_from_email' => 'mickey@mouse.com',
2491 'title' => "Test Contribution Page",
2492 'financial_type_id' => 1,
2493 'currency' => 'NZD',
2494 'goal_amount' => 50,
2495 'is_pay_later' => 1,
2496 'is_monetary' => TRUE,
2497 'is_email_receipt' => TRUE,
2498 ));
2499 $this->_params['contribution_page_id'] = $contributionPage['id'];
2500 $contribution = $this->setUpForCompleteTransaction();
2501 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id']));
2502 $this->mut->checkMailLog(array(
2503 'mickey@mouse.com',
2504 'Mickey Mouse <',
2505 ));
2506 $this->mut->stop();
2507 }
2508
91259407 2509 /**
2510 * Test completing first transaction in a recurring series.
2511 *
2512 * The status should be set to 'in progress' and the next scheduled payment date calculated.
2513 */
2514 public function testCompleteTransactionSetStatusToInProgress() {
2515 $paymentProcessorID = $this->paymentProcessorCreate();
2516 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2517 'contact_id' => $this->_individualId,
2518 'installments' => '12',
2519 'frequency_interval' => '1',
2520 'amount' => '500',
2521 'contribution_status_id' => 'Pending',
2522 'start_date' => '2012-01-01 00:00:00',
2523 'currency' => 'USD',
2524 'frequency_unit' => 'month',
2525 'payment_processor_id' => $paymentProcessorID,
2526 ));
2527 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(
2528 $this->_params,
2529 array(
2530 'contribution_recur_id' => $contributionRecur['id'],
2531 'contribution_status_id' => 'Pending',
2532 ))
2533 );
2534 $this->callAPISuccess('Contribution', 'completetransaction', array('id' => $contribution));
2535 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array(
2536 'id' => $contributionRecur['id'],
2537 'return' => array('next_sched_contribution_date', 'contribution_status_id'),
2538 ));
2539 $this->assertEquals(5, $contributionRecur['contribution_status_id']);
2540 $this->assertEquals(date('Y-m-d 00:00:00', strtotime('+1 month')), $contributionRecur['next_sched_contribution_date']);
2541 }
2542
294cc627 2543 /**
2544 * Test completing a pledge with the completeTransaction api..
2545 *
2546 * Note that we are creating a logged in user because email goes out from
2547 * that person.
2548 */
2549 public function testCompleteTransactionUpdatePledgePayment() {
9f9fa558 2550 $this->swapMessageTemplateForTestTemplate();
294cc627 2551 $mut = new CiviMailUtils($this, TRUE);
2552 $mut->clearMessages();
2553 $this->createLoggedInUser();
2554 $contributionID = $this->createPendingPledgeContribution();
2555 $this->callAPISuccess('contribution', 'completetransaction', array(
2556 'id' => $contributionID,
2557 'trxn_date' => '1 Feb 2013',
2558 ));
2559 $pledge = $this->callAPISuccessGetSingle('Pledge', array(
2560 'id' => $this->_ids['pledge'],
2561 ));
2562 $this->assertEquals('Completed', $pledge['pledge_status']);
2563
2564 $status = $this->callAPISuccessGetValue('PledgePayment', array(
2565 'pledge_id' => $this->_ids['pledge'],
2566 'return' => 'status_id',
2567 ));
2568 $this->assertEquals(1, $status);
2569 $mut->checkMailLog(array(
9f9fa558
EM
2570 'amount:::500.00',
2571 'receive_date:::20130201000000',
76e8d9c4 2572 "receipt_date:::\n",
294cc627 2573 ));
2574 $mut->stop();
9f9fa558 2575 $this->revertTemplateToReservedTemplate();
294cc627 2576 }
2577
0efa8efe 2578 /**
eceb18cc 2579 * Test completing a transaction with an event via the API.
0efa8efe 2580 *
2581 * Note that we are creating a logged in user because email goes out from
2582 * that person
2583 */
00be9182 2584 public function testCompleteTransactionWithParticipantRecord() {
5896d037 2585 $mut = new CiviMailUtils($this, TRUE);
0efa8efe 2586 $mut->clearMessages();
2587 $this->createLoggedInUser();
2588 $contributionID = $this->createPendingParticipantContribution();
66d3f9be 2589 $this->callAPISuccess('contribution', 'completetransaction', array(
5896d037
TO
2590 'id' => $contributionID,
2591 )
0efa8efe 2592 );
5896d037 2593 $participantStatus = $this->callAPISuccessGetValue('participant', array(
92915c55
TO
2594 'id' => $this->_ids['participant'],
2595 'return' => 'participant_status_id',
2596 ));
0efa8efe 2597 $this->assertEquals(1, $participantStatus);
2598 $mut->checkMailLog(array(
2599 'Annual CiviCRM meet',
2600 'Event',
afc59fef 2601 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
0efa8efe 2602 ));
66d3f9be
EM
2603 $mut->stop();
2604 }
2605
2606 /**
eceb18cc 2607 * Test membership is renewed when transaction completed.
66d3f9be 2608 */
00be9182 2609 public function testCompleteTransactionMembershipPriceSet() {
66d3f9be 2610 $this->createPriceSetWithPage('membership');
4ff927bc 2611 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', array(
2612 'name' => 'Grace',
2613 'return' => 'id')
2614 );
66d3f9be 2615 $this->setUpPendingContribution($this->_ids['price_field_value'][0]);
4ff927bc 2616 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
2617 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
2618 'membership_id' => $this->_ids['membership'],
2619 ));
2620 $this->assertEquals(1, $logs['count']);
2621 $this->assertEquals($stateOfGrace, $membership['status_id']);
0445e085 2622 $contribution = $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
66d3f9be
EM
2623 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
2624 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
4ff927bc 2625 $this->callAPISuccessGetSingle('LineItem', array(
2626 'entity_id' => $this->_ids['membership'],
2627 'entity_table' => 'civicrm_membership',
2628 ));
2629 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
2630 'membership_id' => $this->_ids['membership'],
2631 ));
0445e085 2632 //CRM-19600: Ensure that 'Membership Renewal' activity is created after successful membership regsitration
2633 $activity = $this->callAPISuccess('Activity', 'get', array(
2634 'activity_type_id' => 'Membership Renewal',
2635 'source_record_id' => $contribution['id'],
2636 ));
2637 $this->assertEquals(1, $activity['count']);
4ff927bc 2638 $this->assertEquals(2, $logs['count']);
2639 $this->assertNotEquals($stateOfGrace, $logs['values'][2]['status_id']);
66d3f9be
EM
2640 $this->cleanUpAfterPriceSets();
2641 }
2642
0f07bb06 2643 /**
2644 * Test if renewal activity is create after changing Pending contribution to Completed via offline
2645 */
2646 public function testPendingToCompleteContribution() {
2647 $contributionPage = $this->createPriceSetWithPage('membership');
2648 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', array(
2649 'name' => 'Grace',
2650 'return' => 'id')
2651 );
2652 $this->setUpPendingContribution($this->_ids['price_field_value'][0]);
2653 $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
2654
2655 // change pending contribution to completed
2656 $form = new CRM_Contribute_Form_Contribution();
2657 $error = FALSE;
2658 $form->_params = array(
2659 'id' => $this->_ids['contribution'],
2660 'total_amount' => 20,
2661 'net_amount' => 20,
2662 'fee_amount' => 0,
2663 'financial_type_id' => 1,
2664 'receive_date' => '04/21/2015',
2665 'receive_date_time' => '11:27PM',
2666 'contact_id' => $this->_individualId,
2667 'contribution_status_id' => 1,
2668 'billing_middle_name' => '',
2669 'billing_last_name' => 'Adams',
2670 'billing_street_address-5' => '790L Lincoln St S',
2671 'billing_city-5' => 'Maryknoll',
2672 'billing_state_province_id-5' => 1031,
2673 'billing_postal_code-5' => 10545,
2674 'billing_country_id-5' => 1228,
2675 'frequency_interval' => 1,
2676 'frequency_unit' => 'month',
2677 'installments' => '',
2678 'hidden_AdditionalDetail' => 1,
2679 'hidden_Premium' => 1,
2680 'from_email_address' => '"civi45" <civi45@civicrm.com>',
2681 'receipt_date' => '',
2682 'receipt_date_time' => '',
2683 'payment_processor_id' => $this->paymentProcessorID,
2684 'currency' => 'USD',
2685 'contribution_page_id' => $this->_ids['contribution_page'],
2686 'contribution_mode' => 'membership',
2687 'source' => 'Membership Signup and Renewal',
2688 );
2689 try {
2690 $form->testSubmit($form->_params, CRM_Core_Action::UPDATE);
2691 }
2692 catch (Civi\Payment\Exception\PaymentProcessorException $e) {
2693 $error = TRUE;
2694 }
2695 $activity = $this->callAPISuccess('Activity', 'get', array(
2696 'activity_type_id' => 'Membership Renewal',
2697 'source_record_id' => $this->_ids['contribution'],
2698 ));
2699 $this->assertEquals(1, $activity['count']);
2700 }
2701
66d3f9be 2702 /**
eceb18cc 2703 * Test membership is renewed when transaction completed.
66d3f9be 2704 */
00be9182 2705 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
66d3f9be
EM
2706 $this->createPriceSetWithPage('membership');
2707 $this->setUpPendingContribution($this->_ids['price_field_value'][1]);
6c6e6187 2708 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
66d3f9be
EM
2709 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
2710 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
2711 $this->cleanUpAfterPriceSets();
2712 }
2713
00be9182 2714 public function cleanUpAfterPriceSets() {
1cf3c2b1 2715 $this->quickCleanUpFinancialEntities();
66d3f9be 2716 $this->contactDelete($this->_ids['contact']);
66d3f9be
EM
2717 }
2718
66d3f9be 2719 /**
eceb18cc 2720 * Set up a pending transaction with a specific price field id.
1e52837d 2721 *
100fef9d 2722 * @param int $priceFieldValueID
66d3f9be 2723 */
5896d037 2724 public function setUpPendingContribution($priceFieldValueID) {
66d3f9be
EM
2725 $contactID = $this->individualCreate();
2726 $membership = $this->callAPISuccess('membership', 'create', array(
2727 'contact_id' => $contactID,
2728 'membership_type_id' => $this->_ids['membership_type'],
2729 'start_date' => 'yesterday - 1 year',
2730 'end_date' => 'yesterday',
4ff927bc 2731 'join_date' => 'yesterday - 1 year',
66d3f9be
EM
2732 ));
2733 $contribution = $this->callAPISuccess('contribution', 'create', array(
2734 'domain_id' => 1,
2735 'contact_id' => $contactID,
2736 'receive_date' => date('Ymd'),
0f07bb06 2737 'total_amount' => 20.00,
66d3f9be
EM
2738 'financial_type_id' => 1,
2739 'payment_instrument_id' => 'Credit Card',
2740 'non_deductible_amount' => 10.00,
2741 'trxn_id' => 'jdhfi88',
2742 'invoice_id' => 'djfhiewuyr',
2743 'source' => 'SSF',
2744 'contribution_status_id' => 2,
2745 'contribution_page_id' => $this->_ids['contribution_page'],
2746 'api.membership_payment.create' => array('membership_id' => $membership['id']),
2747 ));
2748
2749 $this->callAPISuccess('line_item', 'create', array(
2750 'entity_id' => $contribution['id'],
2751 'entity_table' => 'civicrm_contribution',
1274acef 2752 'contribution_id' => $contribution['id'],
66d3f9be
EM
2753 'price_field_id' => $this->_ids['price_field'][0],
2754 'qty' => 1,
2755 'unit_price' => 20,
2756 'line_total' => 20,
2757 'financial_type_id' => 1,
2758 'price_field_value_id' => $priceFieldValueID,
2759 ));
2760 $this->_ids['contact'] = $contactID;
2761 $this->_ids['contribution'] = $contribution['id'];
2762 $this->_ids['membership'] = $membership['id'];
0efa8efe 2763 }
2764
2f45e1c2 2765 /**
eceb18cc 2766 * Test sending a mail via the API.
6a488035 2767 */
00be9182 2768 public function testSendMail() {
5896d037 2769 $mut = new CiviMailUtils($this, TRUE);
6c6e6187 2770 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
858d0bf8 2771 $this->callAPISuccess('contribution', 'sendconfirmation', array(
5896d037
TO
2772 'id' => $contribution['id'],
2773 'receipt_from_email' => 'api@civicrm.org',
6a488035
TO
2774 )
2775 );
6a488035
TO
2776 $mut->checkMailLog(array(
2777 '$ 100.00',
2778 'Contribution Information',
2779 'Please print this confirmation for your records',
2780 ), array(
21dfd5f5 2781 'Event',
6a488035
TO
2782 )
2783 );
128d44a1 2784
2785 $this->checkCreditCardDetails($mut, $contribution['id']);
6a488035
TO
2786 $mut->stop();
2787 }
2788
128d44a1 2789 /**
2790 * Check credit card details in sent mail via API
2791 *
2792 * @param $mut obj CiviMailUtils instance
2793 * @param int $contributionID Contribution ID
2794 *
2795 */
2796 public function checkCreditCardDetails($mut, $contributionID) {
2797 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
2798 $this->callAPISuccess('contribution', 'sendconfirmation', array(
2799 'id' => $contributionID,
2800 'receipt_from_email' => 'api@civicrm.org',
2801 'payment_processor_id' => $this->paymentProcessorID,
2802 )
2803 );
2804 $mut->checkMailLog(array(
2805 'Credit Card Information', // credit card header
2806 'Billing Name and Address', // billing header
2807 'anthony_anderson@civicrm.org', // billing name
2808 ), array(
2809 'Event',
2810 )
2811 );
2812 }
2813
2f45e1c2 2814 /**
eceb18cc 2815 * Test sending a mail via the API.
6a488035 2816 */
00be9182 2817 public function testSendMailEvent() {
5896d037 2818 $mut = new CiviMailUtils($this, TRUE);
6c6e6187 2819 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
2f45e1c2 2820 $event = $this->eventCreate(array(
6a488035
TO
2821 'is_email_confirm' => 1,
2822 'confirm_from_email' => 'test@civicrm.org',
2823 ));
2824 $this->_eventID = $event['id'];
2825 $participantParams = array(
2826 'contact_id' => $this->_individualId,
2827 'event_id' => $this->_eventID,
2828 'status_id' => 1,
2829 'role_id' => 1,
2830 // to ensure it matches later on
2831 'register_date' => '2007-07-21 00:00:00',
2832 'source' => 'Online Event Registration: API Testing',
4ab7d517 2833
6a488035 2834 );
2f45e1c2 2835 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
2836 $this->callAPISuccess('participant_payment', 'create', array(
6a488035
TO
2837 'participant_id' => $participant['id'],
2838 'contribution_id' => $contribution['id'],
2f45e1c2 2839 ));
66d3f9be 2840 $this->callAPISuccess('contribution', 'sendconfirmation', array(
5896d037
TO
2841 'id' => $contribution['id'],
2842 'receipt_from_email' => 'api@civicrm.org',
6a488035
TO
2843 )
2844 );
2845
6a488035
TO
2846 $mut->checkMailLog(array(
2847 'Annual CiviCRM meet',
2848 'Event',
2849 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
6c6e6187 2850 ), array()
6a488035
TO
2851 );
2852 $mut->stop();
2853 }
2854
4302618d 2855 /**
1e52837d
EM
2856 * This function does a GET & compares the result against the $params.
2857 *
2858 * Use as a double check on Creates.
2859 *
2860 * @param array $params
2861 * @param int $id
67f947ac 2862 * @param bool $delete
6c6e6187 2863 */
1e52837d 2864 public function contributionGetnCheck($params, $id, $delete = TRUE) {
6a488035 2865
4ab7d517 2866 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
6a488035 2867 'id' => $id,
4ab7d517 2868
5896d037 2869 ));
6a488035
TO
2870
2871 if ($delete) {
4ab7d517 2872 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
6a488035 2873 }
2bfae985 2874 $this->assertAPISuccess($contribution, 0);
6a488035
TO
2875 $values = $contribution['values'][$contribution['id']];
2876 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
2877 // this is not returned in id format
2878 unset($params['payment_instrument_id']);
2879 $params['contribution_source'] = $params['source'];
2880 unset($params['source']);
2881 foreach ($params as $key => $value) {
22f80e87 2882 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
6a488035
TO
2883 }
2884 }
2885
294cc627 2886 /**
2887 * Create a pending contribution & linked pending pledge record.
2888 */
2889 public function createPendingPledgeContribution() {
2890
2891 $pledgeID = $this->pledgeCreate(array('contact_id' => $this->_individualId, 'installments' => 1, 'amount' => 500));
2892 $this->_ids['pledge'] = $pledgeID;
2893 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params, array(
2894 'contribution_status_id' => 'Pending',
2895 'total_amount' => 500,
2896 ))
2897 );
2898 $paymentID = $this->callAPISuccessGetValue('PledgePayment', array(
2899 'options' => array('limit' => 1),
2900 'return' => 'id',
2901 ));
2902 $this->callAPISuccess('PledgePayment', 'create', array(
2903 'id' => $paymentID,
2904 'contribution_id' =>
2905 $contribution['id'],
2906 'status_id' => 'Pending',
2907 'scheduled_amount' => 500,
2908 ));
2909
2910 return $contribution['id'];
2911 }
2912
0efa8efe 2913 /**
1e52837d 2914 * Create a pending contribution & linked pending participant record (along with an event).
0efa8efe 2915 */
5896d037 2916 public function createPendingParticipantContribution() {
6c6e6187 2917 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'));
0efa8efe 2918 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6));
5896d037 2919 $this->_ids['participant'] = $participantID;
0efa8efe 2920 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
6c6e6187 2921 $contribution = $this->callAPISuccess('contribution', 'create', $params);
5896d037 2922 $this->callAPISuccess('participant_payment', 'create', array(
92915c55
TO
2923 'contribution_id' => $contribution['id'],
2924 'participant_id' => $participantID,
2925 ));
858d0bf8 2926 $this->callAPISuccess('line_item', 'get', array(
0efa8efe 2927 'entity_id' => $contribution['id'],
2928 'entity_table' => 'civicrm_contribution',
2929 'api.line_item.create' => array(
2930 'entity_id' => $participantID,
2931 'entity_table' => 'civicrm_participant',
2932 ),
2933 ));
2934 return $contribution['id'];
2935 }
2936
4cbe18b8 2937 /**
1e52837d
EM
2938 * Get financial transaction amount.
2939 *
100fef9d 2940 * @param int $contId
4cbe18b8
EM
2941 *
2942 * @return null|string
f4d89200 2943 */
2da40d21 2944 public function _getFinancialTrxnAmount($contId) {
6c6e6187 2945 $query = "SELECT
6a488035
TO
2946 SUM( ft.total_amount ) AS total
2947 FROM civicrm_financial_trxn AS ft
2948 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
2949 WHERE ceft.entity_table = 'civicrm_contribution'
2950 AND ceft.entity_id = {$contId}";
2951
6c6e6187
TO
2952 $result = CRM_Core_DAO::singleValueQuery($query);
2953 return $result;
2954 }
6a488035 2955
4cbe18b8 2956 /**
100fef9d 2957 * @param int $contId
4cbe18b8
EM
2958 *
2959 * @return null|string
f4d89200 2960 */
2da40d21 2961 public function _getFinancialItemAmount($contId) {
6c6e6187
TO
2962 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
2963 $query = "SELECT
6a488035
TO
2964 SUM(amount)
2965 FROM civicrm_financial_item
2966 WHERE entity_table = 'civicrm_line_item'
2967 AND entity_id = {$lineItem}";
6c6e6187
TO
2968 $result = CRM_Core_DAO::singleValueQuery($query);
2969 return $result;
2970 }
6a488035 2971
4cbe18b8 2972 /**
100fef9d 2973 * @param int $contId
4cbe18b8
EM
2974 * @param $context
2975 */
00be9182 2976 public function _checkFinancialItem($contId, $context) {
6c6e6187
TO
2977 if ($context != 'paylater') {
2978 $params = array(
5896d037
TO
2979 'entity_id' => $contId,
2980 'entity_table' => 'civicrm_contribution',
6c6e6187
TO
2981 );
2982 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($params, TRUE));
2983 $entityParams = array(
6a488035
TO
2984 'financial_trxn_id' => $trxn['financial_trxn_id'],
2985 'entity_table' => 'civicrm_financial_item',
6c6e6187
TO
2986 );
2987 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
2988 $params = array(
6a488035 2989 'id' => $entityTrxn['entity_id'],
6c6e6187
TO
2990 );
2991 }
2992 if ($context == 'paylater') {
2993 $lineItems = CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution');
2994 foreach ($lineItems as $key => $item) {
2995 $params = array(
5896d037
TO
2996 'entity_id' => $key,
2997 'entity_table' => 'civicrm_line_item',
6c6e6187
TO
2998 );
2999 $compareParams = array('status_id' => 1);
3000 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3001 }
3002 }
3003 elseif ($context == 'refund') {
3004 $compareParams = array(
5896d037
TO
3005 'status_id' => 1,
3006 'financial_account_id' => 1,
3007 'amount' => -100,
6c6e6187
TO
3008 );
3009 }
3010 elseif ($context == 'cancelPending') {
3011 $compareParams = array(
5896d037
TO
3012 'status_id' => 3,
3013 'financial_account_id' => 1,
3014 'amount' => -100,
6c6e6187
TO
3015 );
3016 }
3017 elseif ($context == 'changeFinancial') {
3018 $lineKey = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
3019 $params = array(
5896d037
TO
3020 'entity_id' => $lineKey,
3021 'amount' => -100,
6c6e6187
TO
3022 );
3023 $compareParams = array(
5896d037 3024 'financial_account_id' => 1,
6c6e6187
TO
3025 );
3026 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3027 $params = array(
5896d037
TO
3028 'financial_account_id' => 3,
3029 'entity_id' => $lineKey,
6c6e6187
TO
3030 );
3031 $compareParams = array(
5896d037 3032 'amount' => 100,
6c6e6187
TO
3033 );
3034 }
3035 if ($context != 'paylater') {
3036 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3037 }
3038 }
6a488035 3039
4cbe18b8 3040 /**
52da5b1e 3041 * Check financial transaction.
3042 *
3043 * @todo break this down into sensible functions - most calls to it only use a few lines out of the big if.
3044 *
4ff927bc 3045 * @param array $contribution
3046 * @param string $context
100fef9d 3047 * @param int $instrumentId
52da5b1e 3048 * @param array $extraParams
4cbe18b8 3049 */
797d4c52 3050 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL, $extraParams = array()) {
6c6e6187 3051 $trxnParams = array(
5896d037
TO
3052 'entity_id' => $contribution['id'],
3053 'entity_table' => 'civicrm_contribution',
6c6e6187
TO
3054 );
3055 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($trxnParams, TRUE));
3056 $params = array(
5896d037 3057 'id' => $trxn['financial_trxn_id'],
6c6e6187
TO
3058 );
3059 if ($context == 'payLater') {
3060 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
3061 $compareParams = array(
5896d037
TO
3062 'status_id' => 1,
3063 'from_financial_account_id' => CRM_Contribute_PseudoConstant::financialAccountType($contribution['financial_type_id'], $relationTypeId),
6c6e6187
TO
3064 );
3065 }
3066 elseif ($context == 'refund') {
3067 $compareParams = array(
5896d037
TO
3068 'to_financial_account_id' => 6,
3069 'total_amount' => -100,
3070 'status_id' => 7,
b7990bb6 3071 'trxn_date' => '2015-01-01 09:00:00',
797d4c52 3072 'trxn_id' => 'the refund',
6c6e6187
TO
3073 );
3074 }
3075 elseif ($context == 'cancelPending') {
3076 $compareParams = array(
ed4d0aea 3077 'to_financial_account_id' => 7,
5896d037
TO
3078 'total_amount' => -100,
3079 'status_id' => 3,
6c6e6187
TO
3080 );
3081 }
3082 elseif ($context == 'changeFinancial' || $context == 'paymentInstrument') {
3083 $entityParams = array(
5896d037
TO
3084 'entity_id' => $contribution['id'],
3085 'entity_table' => 'civicrm_contribution',
3086 'amount' => -100,
6c6e6187
TO
3087 );
3088 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
3089 $trxnParams1 = array(
6a488035 3090 'id' => $trxn['financial_trxn_id'],
6c6e6187 3091 );
122250ec
SL
3092 if (empty($extraParams)) {
3093 $compareParams = array(
3094 'total_amount' => -100,
3095 'status_id' => 1,
3096 );
3097 }
3098 else {
3099 $compareParams = array(
3100 'total_amount' => 100,
3101 'status_id' => 1,
3102 );
3103 }
6c6e6187
TO
3104 if ($context == 'paymentInstrument') {
3105 $compareParams += array(
5896d037
TO
3106 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount(4),
3107 'payment_instrument_id' => 4,
6c6e6187
TO
3108 );
3109 }
3110 else {
3111 $compareParams['to_financial_account_id'] = 12;
3112 }
797d4c52 3113 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, array_merge($compareParams, $extraParams));
6c6e6187
TO
3114 $compareParams['total_amount'] = 100;
3115 if ($context == 'paymentInstrument') {
3116 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($instrumentId);
3117 $compareParams['payment_instrument_id'] = $instrumentId;
3118 }
3119 else {
3120 $compareParams['to_financial_account_id'] = 12;
3121 }
3122 }
3123
797d4c52 3124 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, array_merge($compareParams, $extraParams));
6c6e6187 3125 }
6a488035 3126
4cbe18b8
EM
3127 /**
3128 * @return mixed
3129 */
5896d037 3130 public function _addPaymentInstrument() {
6c6e6187
TO
3131 $gId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
3132 $optionParams = array(
5896d037
TO
3133 'option_group_id' => $gId,
3134 'label' => 'Test Card',
3135 'name' => 'Test Card',
3136 'value' => '6',
3137 'weight' => '6',
3138 'is_active' => 1,
6c6e6187
TO
3139 );
3140 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
3141 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
3142 $financialParams = array(
5896d037
TO
3143 'entity_table' => 'civicrm_option_value',
3144 'entity_id' => $optionValue['id'],
3145 'account_relationship' => $relationTypeId,
3146 'financial_account_id' => 7,
6c6e6187
TO
3147 );
3148 CRM_Financial_BAO_FinancialTypeAccount::add($financialParams, CRM_Core_DAO::$_nullArray);
3149 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
3150 return $optionValue['values'][$optionValue['id']]['value'];
3151 }
6a488035 3152
3c49d90c 3153 /**
3154 * Set up the basic recurring contribution for tests.
3155 *
3156 * @param array $generalParams
3157 * Parameters that can be merged into the recurring AND the contribution.
7f4ef731 3158 *
3159 * @param array $recurParams
3160 * Parameters to merge into the recur only.
3c49d90c 3161 *
3162 * @return array|int
3163 */
7f4ef731 3164 protected function setUpRecurringContribution($generalParams = array(), $recurParams = array()) {
3c49d90c 3165 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3166 'contact_id' => $this->_individualId,
3167 'installments' => '12',
3168 'frequency_interval' => '1',
3169 'amount' => '100',
3170 'contribution_status_id' => 1,
3171 'start_date' => '2012-01-01 00:00:00',
3172 'currency' => 'USD',
3173 'frequency_unit' => 'month',
3174 'payment_processor_id' => $this->paymentProcessorID,
7f4ef731 3175 ), $generalParams, $recurParams));
3c49d90c 3176 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3177 $this->_params,
3178 array(
3179 'contribution_recur_id' => $contributionRecur['id'],
3180 ), $generalParams)
3181 );
3182 return $originalContribution;
3183 }
3184
d2334242
PH
3185 /**
3186 * Set up a basic auto-renew membership for tests.
3187 *
3188 * @param array $generalParams
3189 * Parameters that can be merged into the recurring AND the contribution.
3190 *
3191 * @param array $recurParams
3192 * Parameters to merge into the recur only.
3193 *
3194 * @return array|int
3195 */
3196 protected function setUpAutoRenewMembership($generalParams = array(), $recurParams = array()) {
3197 $newContact = $this->callAPISuccess('Contact', 'create', array(
3198 'contact_type' => 'Individual',
3199 'sort_name' => 'McTesterson, Testy',
3200 'display_name' => 'Testy McTesterson',
3201 'preferred_language' => 'en_US',
3202 'preferred_mail_format' => 'Both',
3203 'first_name' => 'Testy',
3204 'last_name' => 'McTesterson',
3205 'contact_is_deleted' => '0',
3206 'email_id' => '4',
3207 'email' => 'tmctesterson@example.com',
3208 'on_hold' => '0',
3209 ));
3210 $membershipType = $this->callAPISuccess('MembershipType', 'create', array(
3211 'domain_id' => "Default Domain Name",
3212 'member_of_contact_id' => 1,
3213 'financial_type_id' => "Member Dues",
3214 'duration_unit' => "month",
3215 'duration_interval' => 1,
3216 'name' => "Standard Member",
3217 'minimum_fee' => 100,
3218 ));
3219 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
76e80087 3220 'contact_id' => $newContact['id'],
d2334242
PH
3221 'installments' => '12',
3222 'frequency_interval' => '1',
3223 'amount' => '100',
3224 'contribution_status_id' => 1,
3225 'start_date' => '2012-01-01 00:00:00',
3226 'currency' => 'USD',
3227 'frequency_unit' => 'month',
3228 'payment_processor_id' => $this->paymentProcessorID,
3229 ), $generalParams, $recurParams));
3230 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3231 $this->_params,
3232 array(
3233 'contact_id' => $newContact['id'],
3234 'contribution_recur_id' => $contributionRecur['id'],
3235 'financial_type_id' => "Member Dues",
3236 'contribution_status_id' => 1,
3237 'invoice_id' => uniqid(),
3238 ), $generalParams)
3239 );
3240 $membership = $this->callAPISuccess('membership', 'create', array(
3241 'contact_id' => $newContact['id'],
3242 'contribution_recur_id' => $contributionRecur['id'],
3243 'financial_type_id' => "Member Dues",
76e80087 3244 'membership_type_id' => $membershipType['id'],
d2334242
PH
3245 'num_terms' => 1,
3246 ));
3247
3248 $this->callAPISuccess('MembershipPayment', 'create', array(
3249 'contribution_id' => $originalContribution['id'],
3250 'membership_id' => $membership['id'],
3251 ));
3252 return array($originalContribution, $membership);
3253 }
893a550c 3254 /**
3255 * Set up a repeat transaction.
3256 *
3257 * @param array $recurParams
3258 *
3259 * @return array
3260 */
0e6ccb2e 3261 protected function setUpRepeatTransaction($recurParams = array(), $flag) {
893a550c 3262 $paymentProcessorID = $this->paymentProcessorCreate();
3263 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3264 'contact_id' => $this->_individualId,
3265 'installments' => '12',
3266 'frequency_interval' => '1',
3267 'amount' => '500',
3268 'contribution_status_id' => 1,
3269 'start_date' => '2012-01-01 00:00:00',
3270 'currency' => 'USD',
3271 'frequency_unit' => 'month',
3272 'payment_processor_id' => $paymentProcessorID,
3273 ), $recurParams));
0e6ccb2e 3274
7150b1c8 3275 $originalContribution = '';
0e6ccb2e 3276 if ($flag == 'multiple') {
7150b1c8 3277 // CRM-19309 create a contribution + also add in line_items (plural):
0e6ccb2e
K
3278 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3279 $this->_params,
3280 array(
3281 'contribution_recur_id' => $contributionRecur['id'],
3282 'skipLineItem' => 1,
3283 'api.line_item.create' => array(
3284 array(
3285 'price_field_id' => 1,
3286 'qty' => 2,
3287 'line_total' => '20',
3288 'unit_price' => '10',
3289 'financial_type_id' => 1,
3290 ),
3291 array(
3292 'price_field_id' => 1,
3293 'qty' => 1,
3294 'line_total' => '80',
3295 'unit_price' => '80',
3296 'financial_type_id' => 2,
3297 ),
3298 ),
3299 )
3300 )
3301 );
3302 }
3303 elseif ($flag == 'single') {
3304 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3305 $this->_params,
3306 array('contribution_recur_id' => $contributionRecur['id']))
3307 );
3308 }
f69a9ac3 3309 $originalContribution['payment_processor_id'] = $paymentProcessorID;
893a550c 3310 return $originalContribution;
3311 }
3312
ec7e3954
E
3313 /**
3314 * Common set up routine.
3315 *
3316 * @return array
3317 */
3318 protected function setUpForCompleteTransaction() {
3319 $this->mut = new CiviMailUtils($this, TRUE);
3320 $this->createLoggedInUser();
3321 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
3322 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3323 return $contribution;
3324 }
3325
9c01d961
SL
3326 /**
3327 * Test repeat contribution uses the Payment Processor' payment_instrument setting.
3328 */
3329 public function testRepeatTransactionWithNonCreditCardDefault() {
3330 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
3331 'contact_id' => $this->_individualId,
3332 'installments' => '12',
3333 'frequency_interval' => '1',
3334 'amount' => '100',
3335 'contribution_status_id' => 1,
3336 'start_date' => '2012-01-01 00:00:00',
3337 'currency' => 'USD',
3338 'frequency_unit' => 'month',
3339 'payment_processor_id' => $this->paymentProcessorID,
3340 ));
3341 $contribution1 = $this->callAPISuccess('contribution', 'create', array_merge(
3342 $this->_params,
4f0fadfa 3343 array('contribution_recur_id' => $contributionRecur['id'], 'payment_instrument_id' => 2))
9c01d961
SL
3344 );
3345 $paymentInstruments = CRM_Contribute_PseudoConstant::paymentInstrument('name');
3346 $contribution2 = $this->callAPISuccess('contribution', 'repeattransaction', array(
3347 'contribution_status_id' => 'Completed',
3348 'trxn_id' => uniqid(),
3349 'original_contribution_id' => $contribution1,
3350 ));
3351 $this->assertEquals(array_search('Debit Card', $paymentInstruments), $contribution2['values'][$contribution2['id']]['payment_instrument_id']);
3352 $this->quickCleanUpFinancialEntities();
3353 }
3354
6a488035 3355}