Merge pull request #7970 from otetard/CRM-18235
[civicrm-core.git] / tests / phpunit / api / v3 / ContributionTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Test APIv3 civicrm_contribute_* functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contribution
33 * @group headless
34 */
35 class api_v3_ContributionTest extends CiviUnitTestCase {
36
37 /**
38 * Assume empty database with just civicrm_data.
39 */
40 protected $_individualId;
41 protected $_contribution;
42 protected $_financialTypeId = 1;
43 protected $_apiversion;
44 protected $_entity = 'Contribution';
45 public $debug = 0;
46 protected $_params;
47 protected $_ids = array();
48 protected $_pageParams = array();
49 /**
50 * Payment processor ID (dummy processor).
51 *
52 * @var int
53 */
54 protected $paymentProcessorID;
55
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
70 /**
71 * Setup function.
72 */
73 public function setUp() {
74 parent::setUp();
75
76 $this->_apiversion = 3;
77 $this->_individualId = $this->individualCreate();
78 $this->_params = array(
79 'contact_id' => $this->_individualId,
80 'receive_date' => '20120511',
81 'total_amount' => 100.00,
82 'financial_type_id' => $this->_financialTypeId,
83 'non_deductible_amount' => 10.00,
84 'fee_amount' => 5.00,
85 'net_amount' => 95.00,
86 'source' => 'SSF',
87 'contribution_status_id' => 1,
88 );
89 $this->_processorParams = array(
90 'domain_id' => 1,
91 'name' => 'Dummy',
92 'payment_processor_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Financial_BAO_PaymentProcessor', 'payment_processor_type_id', 'Dummy'),
93 'financial_account_id' => 12,
94 'is_active' => 1,
95 'user_name' => '',
96 'url_site' => 'http://dummy.com',
97 'url_recur' => 'http://dummy.com',
98 'billing_mode' => 1,
99 );
100 $this->paymentProcessorID = $this->processorCreate();
101 $this->_pageParams = array(
102 'title' => 'Test Contribution Page',
103 'financial_type_id' => 1,
104 'currency' => 'USD',
105 'financial_account_id' => 1,
106 'payment_processor' => $this->paymentProcessorID,
107 'is_active' => 1,
108 'is_allow_other_amount' => 1,
109 'min_amount' => 10,
110 'max_amount' => 1000,
111 );
112 }
113
114 /**
115 * Clean up after each test.
116 */
117 public function tearDown() {
118 $this->quickCleanUpFinancialEntities();
119 $this->quickCleanup(array('civicrm_uf_match'));
120 }
121
122 /**
123 * Test Get.
124 */
125 public function testGetContribution() {
126 $p = array(
127 'contact_id' => $this->_individualId,
128 'receive_date' => '2010-01-20',
129 'total_amount' => 100.00,
130 'financial_type_id' => $this->_financialTypeId,
131 'non_deductible_amount' => 10.00,
132 'fee_amount' => 5.00,
133 'net_amount' => 95.00,
134 'trxn_id' => 23456,
135 'invoice_id' => 78910,
136 'source' => 'SSF',
137 'contribution_status_id' => 1,
138 );
139 $this->_contribution = $this->callAPISuccess('contribution', 'create', $p);
140
141 $params = array(
142 'contribution_id' => $this->_contribution['id'],
143 );
144
145 $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__, __FILE__);
146 $financialParams['id'] = $this->_financialTypeId;
147 $default = NULL;
148 CRM_Financial_BAO_FinancialType::retrieve($financialParams, $default);
149
150 $this->assertEquals(1, $contribution['count']);
151 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
152 // Note there was an assertion converting financial_type_id to 'Donation' which wasn't working.
153 // Passing back a string rather than an id seems like an error/cruft.
154 // If it is to be introduced we should discuss.
155 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
156 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
157 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
158 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
159 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
160 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
161 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
162 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
163 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
164 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
165 $p['trxn_id'] = '3847';
166 $p['invoice_id'] = '3847';
167
168 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
169
170 // Now we have 2 - test getcount.
171 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
172 $this->assertEquals(2, $contribution);
173 // Test id only format.
174 $contribution = $this->callAPISuccess('contribution', 'get', array(
175 'id' => $this->_contribution['id'],
176 'format.only_id' => 1,
177 ));
178 $this->assertEquals($this->_contribution['id'], $contribution, print_r($contribution, TRUE));
179 // Test id only format.
180 $contribution = $this->callAPISuccess('contribution', 'get', array(
181 'id' => $contribution2['id'],
182 'format.only_id' => 1,
183 ));
184 $this->assertEquals($contribution2['id'], $contribution);
185 // Test id as field.
186 $contribution = $this->callAPISuccess('contribution', 'get', array(
187 'id' => $this->_contribution['id'],
188 ));
189 $this->assertEquals(1, $contribution['count']);
190
191 // Test get by contact id works.
192 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId));
193
194 $this->assertEquals(2, $contribution['count']);
195 $this->callAPISuccess('Contribution', 'Delete', array(
196 'id' => $this->_contribution['id'],
197 ));
198 $this->callAPISuccess('Contribution', 'Delete', array(
199 'id' => $contribution2['id'],
200 ));
201 }
202
203 /**
204 * Test that test contributions can be retrieved.
205 */
206 public function testGetTestContribution() {
207 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, array('is_test' => 1)));
208 $this->callAPISuccessGetSingle('Contribution', array('is_test' => 1));
209 }
210
211 /**
212 * We need to ensure previous tested behaviour still works as part of the api contract.
213 */
214 public function testGetContributionLegacyBehaviour() {
215 $p = array(
216 'contact_id' => $this->_individualId,
217 'receive_date' => '2010-01-20',
218 'total_amount' => 100.00,
219 'contribution_type_id' => $this->_financialTypeId,
220 'non_deductible_amount' => 10.00,
221 'fee_amount' => 5.00,
222 'net_amount' => 95.00,
223 'trxn_id' => 23456,
224 'invoice_id' => 78910,
225 'source' => 'SSF',
226 'contribution_status_id' => 1,
227 );
228 $this->_contribution = $this->callAPISuccess('Contribution', 'create', $p);
229
230 $params = array(
231 'contribution_id' => $this->_contribution['id'],
232 );
233 $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__, __FILE__);
234 $financialParams['id'] = $this->_financialTypeId;
235 $default = NULL;
236 CRM_Financial_BAO_FinancialType::retrieve($financialParams, $default);
237
238 $this->assertEquals(1, $contribution['count']);
239 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
240 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->_financialTypeId);
241 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_type_id'], $this->_financialTypeId);
242 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
243 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
244 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
245 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
246 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
247 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
248 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
249 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
250
251 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
252 $p['trxn_id'] = '3847';
253 $p['invoice_id'] = '3847';
254
255 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
256
257 // now we have 2 - test getcount
258 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
259 $this->assertEquals(2, $contribution);
260 //test id only format
261 $contribution = $this->callAPISuccess('contribution', 'get', array(
262 'id' => $this->_contribution['id'],
263 'format.only_id' => 1,
264 ));
265 $this->assertEquals($this->_contribution['id'], $contribution, print_r($contribution, TRUE));
266 //test id only format
267 $contribution = $this->callAPISuccess('contribution', 'get', array(
268 'id' => $contribution2['id'],
269 'format.only_id' => 1,
270 ));
271 $this->assertEquals($contribution2['id'], $contribution);
272 $contribution = $this->callAPISuccess('contribution', 'get', array(
273 'id' => $this->_contribution['id'],
274 ));
275 //test id as field
276 $this->assertEquals(1, $contribution['count']);
277 // $this->assertEquals($this->_contribution['id'], $contribution['id'] ) ;
278 //test get by contact id works
279 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId));
280
281 $this->assertEquals(2, $contribution['count']);
282 $this->callAPISuccess('Contribution', 'Delete', array(
283 'id' => $this->_contribution['id'],
284 ));
285 $this->callAPISuccess('Contribution', 'Delete', array(
286 'id' => $contribution2['id'],
287 ));
288 }
289
290 /**
291 * Create an contribution_id=FALSE and financial_type_id=Donation.
292 */
293 public function testCreateEmptyContributionIDUseDonation() {
294 $params = array(
295 'contribution_id' => FALSE,
296 'contact_id' => 1,
297 'total_amount' => 1,
298 'check_permissions' => FALSE,
299 'financial_type_id' => 'Donation',
300 );
301 $this->callAPISuccess('contribution', 'create', $params);
302 }
303
304 /**
305 * Check with complete array + custom field.
306 *
307 * Note that the test is written on purpose without any
308 * variables specific to participant so it can be replicated into other entities
309 * and / or moved to the automated test suite
310 */
311 public function testCreateWithCustom() {
312 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
313
314 $params = $this->_params;
315 $params['custom_' . $ids['custom_field_id']] = "custom string";
316
317 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
318 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
319 $check = $this->callAPISuccess($this->_entity, 'get', array(
320 'return.custom_' . $ids['custom_field_id'] => 1,
321 'id' => $result['id'],
322 ));
323 $this->customFieldDelete($ids['custom_field_id']);
324 $this->customGroupDelete($ids['custom_group_id']);
325 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']]);
326 }
327
328 /**
329 * Check with complete array + custom field.
330 *
331 * Note that the test is written on purpose without any
332 * variables specific to participant so it can be replicated into other entities
333 * and / or moved to the automated test suite
334 */
335 public function testCreateGetFieldsWithCustom() {
336 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
337 $idsContact = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTest.php');
338 $result = $this->callAPISuccess('Contribution', 'getfields', array());
339 $this->assertArrayHasKey('custom_' . $ids['custom_field_id'], $result['values']);
340 $this->assertArrayNotHasKey('custom_' . $idsContact['custom_field_id'], $result['values']);
341 $this->customFieldDelete($ids['custom_field_id']);
342 $this->customGroupDelete($ids['custom_group_id']);
343 $this->customFieldDelete($idsContact['custom_field_id']);
344 $this->customGroupDelete($idsContact['custom_group_id']);
345 }
346
347 public function testCreateContributionNoLineItems() {
348
349 $params = array(
350 'contact_id' => $this->_individualId,
351 'receive_date' => '20120511',
352 'total_amount' => 100.00,
353 'financial_type_id' => $this->_financialTypeId,
354 'payment_instrument_id' => 1,
355 'non_deductible_amount' => 10.00,
356 'fee_amount' => 50.00,
357 'net_amount' => 90.00,
358 'trxn_id' => 12345,
359 'invoice_id' => 67890,
360 'source' => 'SSF',
361 'contribution_status_id' => 1,
362 'skipLineItem' => 1,
363 );
364
365 $contribution = $this->callAPISuccess('contribution', 'create', $params);
366 $lineItems = $this->callAPISuccess('line_item', 'get', array(
367 'entity_id' => $contribution['id'],
368 'entity_table' => 'civicrm_contribution',
369 'sequential' => 1,
370 ));
371 $this->assertEquals(0, $lineItems['count']);
372 }
373
374 /**
375 * Test checks that passing in line items suppresses the create mechanism.
376 */
377 public function testCreateContributionChainedLineItems() {
378 $params = array(
379 'contact_id' => $this->_individualId,
380 'receive_date' => '20120511',
381 'total_amount' => 100.00,
382 'financial_type_id' => $this->_financialTypeId,
383 'payment_instrument_id' => 1,
384 'non_deductible_amount' => 10.00,
385 'fee_amount' => 50.00,
386 'net_amount' => 90.00,
387 'trxn_id' => 12345,
388 'invoice_id' => 67890,
389 'source' => 'SSF',
390 'contribution_status_id' => 1,
391 'skipLineItem' => 1,
392 'api.line_item.create' => array(
393 array(
394 'price_field_id' => 1,
395 'qty' => 2,
396 'line_total' => '20',
397 'unit_price' => '10',
398 ),
399 array(
400 'price_field_id' => 1,
401 'qty' => 1,
402 'line_total' => '80',
403 'unit_price' => '80',
404 ),
405 ),
406 );
407
408 $description = "Create Contribution with Nested Line Items.";
409 $subfile = "CreateWithNestedLineItems";
410 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
411
412 $lineItems = $this->callAPISuccess('line_item', 'get', array(
413 'entity_id' => $contribution['id'],
414 'contribution_id' => $contribution['id'],
415 'entity_table' => 'civicrm_contribution',
416 'sequential' => 1,
417 ));
418 $this->assertEquals(2, $lineItems['count']);
419 }
420
421 public function testCreateContributionOffline() {
422 $params = array(
423 'contact_id' => $this->_individualId,
424 'receive_date' => '20120511',
425 'total_amount' => 100.00,
426 'financial_type_id' => 1,
427 'trxn_id' => 12345,
428 'invoice_id' => 67890,
429 'source' => 'SSF',
430 'contribution_status_id' => 1,
431 );
432
433 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
434 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
435 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
436 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
437 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
438 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
439 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
440 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
441 $lineItems = $this->callAPISuccess('line_item', 'get', array(
442 'entity_id' => $contribution['id'],
443 'contribution_id' => $contribution['id'],
444 'entity_table' => 'civicrm_contribution',
445 'sequential' => 1,
446 ));
447 $this->assertEquals(1, $lineItems['count']);
448 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
449 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
450 $this->_checkFinancialRecords($contribution, 'offline');
451 $this->contributionGetnCheck($params, $contribution['id']);
452 }
453
454 /**
455 * Test create with valid payment instrument.
456 */
457 public function testCreateContributionWithPaymentInstrument() {
458 $params = $this->_params + array('payment_instrument' => 'EFT');
459 $contribution = $this->callAPISuccess('contribution', 'create', $params);
460 $contribution = $this->callAPISuccess('contribution', 'get', array(
461 'sequential' => 1,
462 'id' => $contribution['id'],
463 ));
464 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
465 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
466
467 $this->callAPISuccess('contribution', 'create', array(
468 'id' => $contribution['id'],
469 'payment_instrument' => 'Credit Card',
470 ));
471 $contribution = $this->callAPISuccess('contribution', 'get', array(
472 'sequential' => 1,
473 'id' => $contribution['id'],
474 ));
475 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
476 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
477 }
478
479 public function testGetContributionByPaymentInstrument() {
480 $params = $this->_params + array('payment_instrument' => 'EFT');
481 $params2 = $this->_params + array('payment_instrument' => 'Cash');
482 $this->callAPISuccess('contribution', 'create', $params);
483 $this->callAPISuccess('contribution', 'create', $params2);
484 $contribution = $this->callAPISuccess('contribution', 'get', array(
485 'sequential' => 1,
486 'contribution_payment_instrument' => 'Cash',
487 ));
488 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
489 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
490 $this->assertEquals(1, $contribution['count']);
491 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'payment_instrument' => 'Cash'));
492 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
493 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
494 $this->assertEquals(1, $contribution['count']);
495 $contribution = $this->callAPISuccess('contribution', 'get', array(
496 'sequential' => 1,
497 'payment_instrument_id' => 5,
498 ));
499 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
500 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
501 $this->assertEquals(1, $contribution['count']);
502 $contribution = $this->callAPISuccess('contribution', 'get', array(
503 'sequential' => 1,
504 'payment_instrument' => 'EFT',
505 ));
506 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
507 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
508 $this->assertEquals(1, $contribution['count']);
509 $contribution = $this->callAPISuccess('contribution', 'create', array(
510 'id' => $contribution['id'],
511 'payment_instrument' => 'Credit Card',
512 ));
513 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'id' => $contribution['id']));
514 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
515 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
516 $this->assertEquals(1, $contribution['count']);
517 }
518
519 /**
520 * CRM-16227 introduces invoice_id as a parameter.
521 */
522 public function testGetContributionByInvoice() {
523 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, array('invoice_id' => 'curly')));
524 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params), array('invoice_id' => 'churlish'));
525 $this->callAPISuccessGetCount('Contribution', array(), 2);
526 $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => 'curly'));
527 // The following don't work. They are the format we are trying to introduce but although the form uses this format
528 // CRM_Contact_BAO_Query::convertFormValues puts them into the other format & the where only supports that.
529 // ideally the where clause would support this format (as it does on contact_BAO_Query) and those lines would
530 // come out of convertFormValues
531 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('LIKE' => '%ish%')));
532 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('NOT IN' => array('curly'))));
533 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('LIKE' => '%ly%')), 2);
534 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('IN' => array('curly', 'churlish'))),
535 // 2);
536 }
537
538 /**
539 * Create test with unique field name on source.
540 */
541 public function testCreateContributionSource() {
542
543 $params = array(
544 'contact_id' => $this->_individualId,
545 'receive_date' => date('Ymd'),
546 'total_amount' => 100.00,
547 'financial_type_id' => $this->_financialTypeId,
548 'payment_instrument_id' => 1,
549 'non_deductible_amount' => 10.00,
550 'fee_amount' => 50.00,
551 'net_amount' => 90.00,
552 'trxn_id' => 12345,
553 'invoice_id' => 67890,
554 'contribution_source' => 'SSF',
555 'contribution_status_id' => 1,
556 );
557
558 $contribution = $this->callAPISuccess('contribution', 'create', $params);
559 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
560 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
561 }
562
563 /**
564 * Create test with unique field name on source.
565 */
566 public function testCreateDefaultNow() {
567
568 $params = $this->_params;
569 unset($params['receive_date']);
570
571 $contribution = $this->callAPISuccess('contribution', 'create', $params);
572 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
573 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date'])));
574 }
575
576 /**
577 * Create test with unique field name on source.
578 */
579 public function testCreateContributionSourceInvalidContact() {
580
581 $params = array(
582 'contact_id' => 999,
583 'receive_date' => date('Ymd'),
584 'total_amount' => 100.00,
585 'financial_type_id' => $this->_financialTypeId,
586 'payment_instrument_id' => 1,
587 'non_deductible_amount' => 10.00,
588 'fee_amount' => 50.00,
589 'net_amount' => 90.00,
590 'trxn_id' => 12345,
591 'invoice_id' => 67890,
592 'contribution_source' => 'SSF',
593 'contribution_status_id' => 1,
594 );
595
596 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
597 }
598
599 public function testCreateContributionSourceInvalidContContact() {
600
601 $params = array(
602 'contribution_contact_id' => 999,
603 'receive_date' => date('Ymd'),
604 'total_amount' => 100.00,
605 'financial_type_id' => $this->_financialTypeId,
606 'payment_instrument_id' => 1,
607 'non_deductible_amount' => 10.00,
608 'fee_amount' => 50.00,
609 'net_amount' => 90.00,
610 'trxn_id' => 12345,
611 'invoice_id' => 67890,
612 'contribution_source' => 'SSF',
613 'contribution_status_id' => 1,
614 );
615
616 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
617 }
618
619 /**
620 * Test note created correctly.
621 */
622 public function testCreateContributionWithNote() {
623 $description = "Demonstrates creating contribution with Note Entity.";
624 $subfile = "ContributionCreateWithNote";
625 $params = array(
626 'contact_id' => $this->_individualId,
627 'receive_date' => '2012-01-01',
628 'total_amount' => 100.00,
629 'financial_type_id' => $this->_financialTypeId,
630 'payment_instrument_id' => 1,
631 'non_deductible_amount' => 10.00,
632 'fee_amount' => 50.00,
633 'net_amount' => 90.00,
634 'trxn_id' => 12345,
635 'invoice_id' => 67890,
636 'source' => 'SSF',
637 'contribution_status_id' => 1,
638 'note' => 'my contribution note',
639 );
640
641 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
642 $result = $this->callAPISuccess('note', 'get', array(
643 'entity_table' => 'civicrm_contribution',
644 'entity_id' => $contribution['id'],
645 'sequential' => 1,
646 ));
647 $this->assertEquals('my contribution note', $result['values'][0]['note']);
648 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
649 }
650
651 public function testCreateContributionWithNoteUniqueNameAliases() {
652 $params = array(
653 'contact_id' => $this->_individualId,
654 'receive_date' => '2012-01-01',
655 'total_amount' => 100.00,
656 'financial_type_id' => $this->_financialTypeId,
657 'payment_instrument_id' => 1,
658 'non_deductible_amount' => 10.00,
659 'fee_amount' => 50.00,
660 'net_amount' => 90.00,
661 'trxn_id' => 12345,
662 'invoice_id' => 67890,
663 'source' => 'SSF',
664 'contribution_status_id' => 1,
665 'contribution_note' => 'my contribution note',
666 );
667
668 $contribution = $this->callAPISuccess('contribution', 'create', $params);
669 $result = $this->callAPISuccess('note', 'get', array(
670 'entity_table' => 'civicrm_contribution',
671 'entity_id' => $contribution['id'],
672 'sequential' => 1,
673 ));
674 $this->assertEquals('my contribution note', $result['values'][0]['note']);
675 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
676 }
677
678 /**
679 * This is the test for creating soft credits.
680 */
681 public function testCreateContributionWithSoftCredit() {
682 $description = "Demonstrates creating contribution with SoftCredit.";
683 $subfile = "ContributionCreateWithSoftCredit";
684 $contact2 = $this->callAPISuccess('Contact', 'create', array(
685 'display_name' => 'superman',
686 'contact_type' => 'Individual',
687 ));
688 $softParams = array(
689 'contact_id' => $contact2['id'],
690 'amount' => 50,
691 'soft_credit_type_id' => 3,
692 );
693
694 $params = $this->_params + array('soft_credit' => array(1 => $softParams));
695 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
696 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
697
698 $this->assertEquals($softParams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
699 $this->assertEquals($softParams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
700 $this->assertEquals($softParams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']);
701
702 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
703 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
704 }
705
706 public function testCreateContributionWithSoftCreditDefaults() {
707 $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type.";
708 $subfile = "ContributionCreateWithSoftCreditDefaults";
709 $contact2 = $this->callAPISuccess('Contact', 'create', array(
710 'display_name' => 'superman',
711 'contact_type' => 'Individual',
712 ));
713 $params = $this->_params + array(
714 'soft_credit_to' => $contact2['id'],
715 );
716 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
717 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
718
719 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
720 // Default soft credit amount = contribution.total_amount
721 $this->assertEquals($this->_params['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
722 $this->assertEquals(CRM_Core_OptionGroup::getDefaultValue("soft_credit_type"), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
723
724 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
725 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
726 }
727
728 public function testCreateContributionWithHonoreeContact() {
729 $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id.";
730 $subfile = "ContributionCreateWithHonoreeContact";
731 $contact2 = $this->callAPISuccess('Contact', 'create', array(
732 'display_name' => 'superman',
733 'contact_type' => 'Individual',
734 ));
735 $params = $this->_params + array(
736 'honor_contact_id' => $contact2['id'],
737 );
738 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
739 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
740
741 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
742 // Default soft credit amount = contribution.total_amount
743 // Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type
744 $this->assertEquals($this->_params['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
745 $this->assertEquals(CRM_Core_OptionGroup::getValue('soft_credit_type', 'in_honor_of', 'name'), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
746
747 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
748 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
749 }
750
751 /**
752 * Test using example code.
753 */
754 public function testContributionCreateExample() {
755 //make sure at least on page exists since there is a truncate in tear down
756 $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
757 require_once 'api/v3/examples/Contribution/Create.php';
758 $result = contribution_create_example();
759 $id = $result['id'];
760 $expectedResult = contribution_create_expectedresult();
761 $this->checkArrayEquals($expectedResult, $result);
762 $this->contributionDelete($id);
763 }
764
765 /**
766 * Function tests that additional financial records are created when fee amount is recorded.
767 */
768 public function testCreateContributionWithFee() {
769 $params = array(
770 'contact_id' => $this->_individualId,
771 'receive_date' => '20120511',
772 'total_amount' => 100.00,
773 'fee_amount' => 50,
774 'financial_type_id' => 1,
775 'trxn_id' => 12345,
776 'invoice_id' => 67890,
777 'source' => 'SSF',
778 'contribution_status_id' => 1,
779 );
780
781 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
782 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
783 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
784 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 50.00);
785 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 50.00);
786 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
787 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
788 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
789 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
790 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
791
792 $lineItems = $this->callAPISuccess('line_item', 'get', array(
793
794 'entity_id' => $contribution['id'],
795 'entity_table' => 'civicrm_contribution',
796 'sequential' => 1,
797 ));
798 $this->assertEquals(1, $lineItems['count']);
799 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
800 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
801 $lineItems = $this->callAPISuccess('line_item', 'get', array(
802
803 'entity_id' => $contribution['id'],
804 'contribution_id' => $contribution['id'],
805 'entity_table' => 'civicrm_contribution',
806 'sequential' => 1,
807 ));
808 $this->assertEquals(1, $lineItems['count']);
809 $this->_checkFinancialRecords($contribution, 'feeAmount');
810 }
811
812
813 /**
814 * Function tests that additional financial records are created when online contribution is created.
815 */
816 public function testCreateContributionOnline() {
817 CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
818 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
819 $this->assertAPISuccess($contributionPage);
820 $params = array(
821 'contact_id' => $this->_individualId,
822 'receive_date' => '20120511',
823 'total_amount' => 100.00,
824 'financial_type_id' => 1,
825 'contribution_page_id' => $contributionPage['id'],
826 'payment_processor' => 1,
827 'trxn_id' => 12345,
828 'invoice_id' => 67890,
829 'source' => 'SSF',
830 'contribution_status_id' => 1,
831
832 );
833
834 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
835 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
836 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
837 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
838 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
839 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
840 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
841 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
842 $this->_checkFinancialRecords($contribution, 'online');
843 }
844
845 /**
846 * Check handling of financial type.
847 *
848 * In the interests of removing financial type / contribution type checks from
849 * legacy format function lets test that the api is doing this for us
850 */
851 public function testCreateInvalidFinancialType() {
852 $params = $this->_params;
853 $params['financial_type_id'] = 99999;
854 $this->callAPIFailure($this->_entity, 'create', $params, "'99999' is not a valid option for field financial_type_id");
855 }
856
857 /**
858 * Check handling of financial type.
859 *
860 * In the interests of removing financial type / contribution type checks from
861 * legacy format function lets test that the api is doing this for us
862 */
863 public function testValidNamedFinancialType() {
864 $params = $this->_params;
865 $params['financial_type_id'] = 'Donation';
866 $this->callAPISuccess($this->_entity, 'create', $params);
867 }
868
869 /**
870 * Tests that additional financial records are created.
871 *
872 * Checks when online contribution with pay later option is created
873 */
874 public function testCreateContributionPayLaterOnline() {
875 CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
876 $this->_pageParams['is_pay_later'] = 1;
877 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
878 $this->assertAPISuccess($contributionPage);
879 $params = array(
880 'contact_id' => $this->_individualId,
881 'receive_date' => '20120511',
882 'total_amount' => 100.00,
883 'financial_type_id' => 1,
884 'contribution_page_id' => $contributionPage['id'],
885 'trxn_id' => 12345,
886 'is_pay_later' => 1,
887 'invoice_id' => 67890,
888 'source' => 'SSF',
889 'contribution_status_id' => 2,
890
891 );
892
893 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
894 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
895 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
896 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
897 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
898 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
899 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
900 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
901 $this->_checkFinancialRecords($contribution, 'payLater');
902 }
903
904 /**
905 * Function tests that additional financial records are created for online contribution with pending option.
906 */
907 public function testCreateContributionPendingOnline() {
908 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
909 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
910 $this->assertAPISuccess($contributionPage);
911 $params = array(
912 'contact_id' => $this->_individualId,
913 'receive_date' => '20120511',
914 'total_amount' => 100.00,
915 'financial_type_id' => 1,
916 'contribution_page_id' => $contributionPage['id'],
917 'trxn_id' => 12345,
918 'invoice_id' => 67890,
919 'source' => 'SSF',
920 'contribution_status_id' => 2,
921 );
922
923 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
924 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
925 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
926 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
927 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
928 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
929 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
930 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
931 $this->_checkFinancialRecords($contribution, 'pending');
932 }
933
934 /**
935 * Test that BAO defaults work.
936 */
937 public function testCreateBAODefaults() {
938 unset($this->_params['contribution_source_id'], $this->_params['payment_instrument_id']);
939 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
940 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
941 'id' => $contribution['id'],
942 'api.contribution.delete' => 1,
943 ));
944 $this->assertEquals(1, $contribution['contribution_status_id']);
945 $this->assertEquals('Check', $contribution['payment_instrument']);
946 }
947
948 /**
949 * Function tests that line items, financial records are updated when contribution amount is changed.
950 */
951 public function testCreateUpdateContributionChangeTotal() {
952 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
953 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
954
955 'entity_id' => $contribution['id'],
956 'entity_table' => 'civicrm_contribution',
957 'sequential' => 1,
958 'return' => 'line_total',
959 ));
960 $this->assertEquals('100.00', $lineItems);
961 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
962 // Financial trxn SUM = 100 + 5 (fee)
963 $this->assertEquals('105.00', $trxnAmount);
964 $newParams = array(
965
966 'id' => $contribution['id'],
967 'total_amount' => '125',
968 );
969 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
970
971 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
972
973 'entity_id' => $contribution['id'],
974 'entity_table' => 'civicrm_contribution',
975 'sequential' => 1,
976 'return' => 'line_total',
977 ));
978
979 $this->assertEquals('125.00', $lineItems);
980 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
981
982 // Financial trxn SUM = 125 + 5 (fee).
983 $this->assertEquals('130.00', $trxnAmount);
984 $this->assertEquals('125.00', $this->_getFinancialItemAmount($contribution['id']));
985 }
986
987 /**
988 * Function tests that line items, financial records are updated when pay later contribution is received.
989 */
990 public function testCreateUpdateContributionPayLater() {
991 $contribParams = array(
992 'contact_id' => $this->_individualId,
993 'receive_date' => '2012-01-01',
994 'total_amount' => 100.00,
995 'financial_type_id' => $this->_financialTypeId,
996 'payment_instrument_id' => 1,
997 'contribution_status_id' => 2,
998 'is_pay_later' => 1,
999
1000 );
1001 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1002
1003 $newParams = array_merge($contribParams, array(
1004 'id' => $contribution['id'],
1005 'contribution_status_id' => 1,
1006 )
1007 );
1008 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1009 $contribution = $contribution['values'][$contribution['id']];
1010 $this->assertEquals($contribution['contribution_status_id'], '1');
1011 $this->_checkFinancialItem($contribution['id'], 'paylater');
1012 $this->_checkFinancialTrxn($contribution, 'payLater');
1013 }
1014
1015 /**
1016 * Function tests that financial records are updated when Payment Instrument is changed.
1017 */
1018 public function testCreateUpdateContributionPaymentInstrument() {
1019 $instrumentId = $this->_addPaymentInstrument();
1020 $contribParams = array(
1021 'contact_id' => $this->_individualId,
1022 'total_amount' => 100.00,
1023 'financial_type_id' => $this->_financialTypeId,
1024 'payment_instrument_id' => 4,
1025 'contribution_status_id' => 1,
1026
1027 );
1028 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1029
1030 $newParams = array_merge($contribParams, array(
1031 'id' => $contribution['id'],
1032 'payment_instrument_id' => $instrumentId,
1033 )
1034 );
1035 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1036 $this->assertAPISuccess($contribution);
1037 $this->_checkFinancialTrxn($contribution, 'paymentInstrument', $instrumentId);
1038 }
1039
1040 /**
1041 * Function tests that financial records are added when Contribution is Refunded.
1042 */
1043 public function testCreateUpdateContributionRefund() {
1044 $contributionParams = array(
1045 'contact_id' => $this->_individualId,
1046 'receive_date' => '2012-01-01',
1047 'total_amount' => 100.00,
1048 'financial_type_id' => $this->_financialTypeId,
1049 'payment_instrument_id' => 4,
1050 'contribution_status_id' => 1,
1051 'trxn_id' => 'original_payment',
1052 );
1053 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1054 $newParams = array_merge($contributionParams, array(
1055 'id' => $contribution['id'],
1056 'contribution_status_id' => 'Refunded',
1057 'cancel_date' => '2015-01-01 09:00',
1058 'refund_trxn_id' => 'the refund',
1059 )
1060 );
1061
1062 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1063 $this->_checkFinancialTrxn($contribution, 'refund');
1064 $this->_checkFinancialItem($contribution['id'], 'refund');
1065 $this->assertEquals('original_payment', $this->callAPISuccessGetValue('Contribution', array(
1066 'id' => $contribution['id'],
1067 'return' => 'trxn_id',
1068 )));
1069 }
1070
1071 /**
1072 * Refund a contribution for a financial type with a contra account.
1073 *
1074 * CRM-17951 the contra account is a financial account with a relationship to a
1075 * financial type. It is not always configured but should be reflected
1076 * in the financial_trxn & financial_item table if it is.
1077 */
1078 public function testCreateUpdateChargebackContributionDefaultAccount() {
1079 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
1080 $this->callAPISuccess('Contribution', 'create', array(
1081 'id' => $contribution['id'],
1082 'contribution_status_id' => 'Chargeback',
1083 ));
1084 $this->callAPISuccessGetSingle('Contribution', array('contribution_status_id' => 'Chargeback'));
1085
1086 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1087 'contribution_id' => $contribution['id'],
1088 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1089 ));
1090 $this->assertEquals(1, $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1091 $this->callAPISuccessGetSingle('FinancialTrxn', array(
1092 'total_amount' => -100,
1093 'status_id' => 'Chargeback',
1094 'to_financial_account_id' => 6,
1095 ));
1096 }
1097
1098 /**
1099 * Refund a contribution for a financial type with a contra account.
1100 *
1101 * CRM-17951 the contra account is a financial account with a relationship to a
1102 * financial type. It is not always configured but should be reflected
1103 * in the financial_trxn & financial_item table if it is.
1104 */
1105 public function testCreateUpdateChargebackContributionCustomAccount() {
1106 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
1107 'name' => 'Chargeback Account',
1108 'is_active' => TRUE,
1109 ));
1110
1111 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', array(
1112 'entity_id' => $this->_financialTypeId,
1113 'entity_table' => 'civicrm_financial_type',
1114 'account_relationship' => 'Chargeback Account is',
1115 'financial_account_id' => 'Chargeback Account',
1116 ));
1117
1118 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
1119 $this->callAPISuccess('Contribution', 'create', array(
1120 'id' => $contribution['id'],
1121 'contribution_status_id' => 'Chargeback',
1122 ));
1123 $this->callAPISuccessGetSingle('Contribution', array('contribution_status_id' => 'Chargeback'));
1124
1125 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1126 'contribution_id' => $contribution['id'],
1127 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1128 ));
1129 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1130
1131 $this->callAPISuccess('Contribution', 'delete', array('id' => $contribution['id']));
1132 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialAccount['id']));
1133 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
1134 }
1135
1136 /**
1137 * Refund a contribution for a financial type with a contra account.
1138 *
1139 * CRM-17951 the contra account is a financial account with a relationship to a
1140 * financial type. It is not always configured but should be reflected
1141 * in the financial_trxn & financial_item table if it is.
1142 */
1143 public function testCreateUpdateRefundContributionConfiguredContraAccount() {
1144 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
1145 'name' => 'Refund Account',
1146 'is_active' => TRUE,
1147 ));
1148
1149 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', array(
1150 'entity_id' => $this->_financialTypeId,
1151 'entity_table' => 'civicrm_financial_type',
1152 'account_relationship' => 'Credit/Contra Revenue Account is',
1153 'financial_account_id' => 'Refund Account',
1154 ));
1155
1156 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
1157 $this->callAPISuccess('Contribution', 'create', array(
1158 'id' => $contribution['id'],
1159 'contribution_status_id' => 'Refunded',
1160 ));
1161
1162 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1163 'contribution_id' => $contribution['id'],
1164 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1165 ));
1166 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1167
1168 $this->callAPISuccess('Contribution', 'delete', array('id' => $contribution['id']));
1169 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialAccount['id']));
1170 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
1171 }
1172
1173 /**
1174 * Function tests that trxn_id is set when passed in.
1175 *
1176 * Here we ensure that the civicrm_financial_trxn.trxn_id & the civicrm_contribution.trxn_id are set
1177 * when trxn_id is passed in.
1178 */
1179 public function testCreateUpdateContributionRefundTrxnIDPassedIn() {
1180 $contributionParams = array(
1181 'contact_id' => $this->_individualId,
1182 'receive_date' => '2012-01-01',
1183 'total_amount' => 100.00,
1184 'financial_type_id' => $this->_financialTypeId,
1185 'payment_instrument_id' => 4,
1186 'contribution_status_id' => 1,
1187 'trxn_id' => 'original_payment',
1188 );
1189 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1190 $newParams = array_merge($contributionParams, array(
1191 'id' => $contribution['id'],
1192 'contribution_status_id' => 'Refunded',
1193 'cancel_date' => '2015-01-01 09:00',
1194 'trxn_id' => 'the refund',
1195 )
1196 );
1197
1198 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1199 $this->_checkFinancialTrxn($contribution, 'refund');
1200 $this->_checkFinancialItem($contribution['id'], 'refund');
1201 $this->assertEquals('the refund', $this->callAPISuccessGetValue('Contribution', array(
1202 'id' => $contribution['id'],
1203 'return' => 'trxn_id',
1204 )));
1205 }
1206
1207 /**
1208 * Function tests that trxn_id is set when passed in.
1209 *
1210 * Here we ensure that the civicrm_contribution.trxn_id is set
1211 * when trxn_id is passed in but if refund_trxn_id is different then that
1212 * is kept for the refund transaction.
1213 */
1214 public function testCreateUpdateContributionRefundRefundAndTrxnIDPassedIn() {
1215 $contributionParams = array(
1216 'contact_id' => $this->_individualId,
1217 'receive_date' => '2012-01-01',
1218 'total_amount' => 100.00,
1219 'financial_type_id' => $this->_financialTypeId,
1220 'payment_instrument_id' => 4,
1221 'contribution_status_id' => 1,
1222 'trxn_id' => 'original_payment',
1223 );
1224 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1225 $newParams = array_merge($contributionParams, array(
1226 'id' => $contribution['id'],
1227 'contribution_status_id' => 'Refunded',
1228 'cancel_date' => '2015-01-01 09:00',
1229 'trxn_id' => 'cont id',
1230 'refund_trxn_id' => 'the refund',
1231 )
1232 );
1233
1234 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1235 $this->_checkFinancialTrxn($contribution, 'refund');
1236 $this->_checkFinancialItem($contribution['id'], 'refund');
1237 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1238 'id' => $contribution['id'],
1239 'return' => 'trxn_id',
1240 )));
1241 }
1242
1243 /**
1244 * Function tests that refund_trxn_id is set when passed in empty.
1245 *
1246 * Here we ensure that the civicrm_contribution.trxn_id is set
1247 * when trxn_id is passed in but if refund_trxn_id isset but empty then that
1248 * is kept for the refund transaction.
1249 */
1250 public function testCreateUpdateContributionRefundRefundNullTrxnIDPassedIn() {
1251 $contributionParams = array(
1252 'contact_id' => $this->_individualId,
1253 'receive_date' => '2012-01-01',
1254 'total_amount' => 100.00,
1255 'financial_type_id' => $this->_financialTypeId,
1256 'payment_instrument_id' => 4,
1257 'contribution_status_id' => 1,
1258 'trxn_id' => 'original_payment',
1259 );
1260 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1261 $newParams = array_merge($contributionParams, array(
1262 'id' => $contribution['id'],
1263 'contribution_status_id' => 'Refunded',
1264 'cancel_date' => '2015-01-01 09:00',
1265 'trxn_id' => 'cont id',
1266 'refund_trxn_id' => '',
1267 )
1268 );
1269
1270 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1271 $this->_checkFinancialTrxn($contribution, 'refund', NULL, array('trxn_id' => NULL));
1272 $this->_checkFinancialItem($contribution['id'], 'refund');
1273 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1274 'id' => $contribution['id'],
1275 'return' => 'trxn_id',
1276 )));
1277 }
1278
1279 /**
1280 * Function tests invalid contribution status change.
1281 */
1282 public function testCreateUpdateContributionInValidStatusChange() {
1283 $contribParams = array(
1284 'contact_id' => 1,
1285 'receive_date' => '2012-01-01',
1286 'total_amount' => 100.00,
1287 'financial_type_id' => 1,
1288 'payment_instrument_id' => 1,
1289 'contribution_status_id' => 1,
1290 );
1291 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1292 $newParams = array_merge($contribParams, array(
1293 'id' => $contribution['id'],
1294 'contribution_status_id' => 2,
1295 )
1296 );
1297 $this->callAPIFailure('contribution', 'create', $newParams, ts('Cannot change contribution status from Completed to Pending.'));
1298
1299 }
1300
1301 /**
1302 * Function tests that financial records are added when Pending Contribution is Canceled.
1303 */
1304 public function testCreateUpdateContributionCancelPending() {
1305 $contribParams = array(
1306 'contact_id' => $this->_individualId,
1307 'receive_date' => '2012-01-01',
1308 'total_amount' => 100.00,
1309 'financial_type_id' => $this->_financialTypeId,
1310 'payment_instrument_id' => 1,
1311 'contribution_status_id' => 2,
1312 'is_pay_later' => 1,
1313
1314 );
1315 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1316 $newParams = array_merge($contribParams, array(
1317 'id' => $contribution['id'],
1318 'contribution_status_id' => 3,
1319 )
1320 );
1321 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1322 $this->_checkFinancialTrxn($contribution, 'cancelPending');
1323 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1324 }
1325
1326 /**
1327 * Function tests that financial records are added when Financial Type is Changed.
1328 */
1329 public function testCreateUpdateContributionChangeFinancialType() {
1330 $contribParams = array(
1331 'contact_id' => $this->_individualId,
1332 'receive_date' => '2012-01-01',
1333 'total_amount' => 100.00,
1334 'financial_type_id' => 1,
1335 'payment_instrument_id' => 1,
1336 'contribution_status_id' => 1,
1337
1338 );
1339 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1340 $newParams = array_merge($contribParams, array(
1341 'id' => $contribution['id'],
1342 'financial_type_id' => 3,
1343 )
1344 );
1345 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1346 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1347 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1348 }
1349
1350 /**
1351 * Test that update does not change status id CRM-15105.
1352 */
1353 public function testCreateUpdateWithoutChangingPendingStatus() {
1354 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params, array('contribution_status_id' => 2)));
1355 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
1356 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1357 'id' => $contribution['id'],
1358 'api.contribution.delete' => 1,
1359 ));
1360 $this->assertEquals(2, $contribution['contribution_status_id']);
1361 }
1362
1363 /**
1364 * Test Updating a Contribution.
1365 *
1366 * CHANGE: we require the API to do an incremental update
1367 */
1368 public function testCreateUpdateContribution() {
1369
1370 $contributionID = $this->contributionCreate(array(
1371 'contact_id' => $this->_individualId,
1372 'trxn_id' => 212355,
1373 'financial_type_id' => $this->_financialTypeId,
1374 'invoice_id' => 'old_invoice',
1375 ));
1376 $old_params = array(
1377 'contribution_id' => $contributionID,
1378 );
1379 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1380 $this->assertEquals($original['id'], $contributionID);
1381 //set up list of old params, verify
1382
1383 //This should not be required on update:
1384 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1385 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1386 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1387 $old_source = $original['values'][$contributionID]['contribution_source'];
1388
1389 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1390 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1391
1392 //check against values in CiviUnitTestCase::createContribution()
1393 $this->assertEquals($old_contact_id, $this->_individualId);
1394 $this->assertEquals($old_fee_amount, 5.00);
1395 $this->assertEquals($old_source, 'SSF');
1396 $this->assertEquals($old_trxn_id, 212355);
1397 $this->assertEquals($old_invoice_id, 'old_invoice');
1398 $params = array(
1399 'id' => $contributionID,
1400 'contact_id' => $this->_individualId,
1401 'total_amount' => 110.00,
1402 'financial_type_id' => $this->_financialTypeId,
1403 'non_deductible_amount' => 10.00,
1404 'net_amount' => 100.00,
1405 'contribution_status_id' => 1,
1406 'note' => 'Donating for Nobel Cause',
1407
1408 );
1409
1410 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1411
1412 $new_params = array(
1413 'contribution_id' => $contribution['id'],
1414
1415 );
1416 $contribution = $this->callAPISuccess('contribution', 'get', $new_params);
1417
1418 $this->assertEquals($contribution['values'][$contributionID]['contact_id'], $this->_individualId);
1419 $this->assertEquals($contribution['values'][$contributionID]['total_amount'], 110.00);
1420 $this->assertEquals($contribution['values'][$contributionID]['financial_type_id'], $this->_financialTypeId);
1421 $this->assertEquals($contribution['values'][$contributionID]['instrument_id'], $old_payment_instrument);
1422 $this->assertEquals($contribution['values'][$contributionID]['non_deductible_amount'], 10.00);
1423 $this->assertEquals($contribution['values'][$contributionID]['fee_amount'], $old_fee_amount);
1424 $this->assertEquals($contribution['values'][$contributionID]['net_amount'], 100.00);
1425 $this->assertEquals($contribution['values'][$contributionID]['trxn_id'], $old_trxn_id);
1426 $this->assertEquals($contribution['values'][$contributionID]['invoice_id'], $old_invoice_id);
1427 $this->assertEquals($contribution['values'][$contributionID]['contribution_source'], $old_source);
1428 $this->assertEquals($contribution['values'][$contributionID]['contribution_status'], 'Completed');
1429 $params = array(
1430 'contribution_id' => $contributionID,
1431
1432 );
1433 $result = $this->callAPISuccess('contribution', 'delete', $params);
1434 $this->assertAPISuccess($result);
1435 }
1436
1437 ///////////////// civicrm_contribution_delete methods
1438
1439 /**
1440 * Attempt (but fail) to delete a contribution without parameters.
1441 */
1442 public function testDeleteEmptyParamsContribution() {
1443 $params = array();
1444 $this->callAPIFailure('contribution', 'delete', $params);
1445 }
1446
1447 public function testDeleteParamsNotArrayContribution() {
1448 $params = 'contribution_id= 1';
1449 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1450 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1451 }
1452
1453 public function testDeleteWrongParamContribution() {
1454 $params = array(
1455 'contribution_source' => 'SSF',
1456
1457 );
1458 $this->callAPIFailure('contribution', 'delete', $params);
1459 }
1460
1461 public function testDeleteContribution() {
1462 $contributionID = $this->contributionCreate(array(
1463 'contact_id' => $this->_individualId,
1464 'financial_type_id' => $this->_financialTypeId,
1465 ));
1466 $params = array(
1467 'id' => $contributionID,
1468 );
1469 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__, __FILE__);
1470 }
1471
1472 /**
1473 * Test civicrm_contribution_search with empty params.
1474 *
1475 * All available contributions expected.
1476 */
1477 public function testSearchEmptyParams() {
1478 $params = array();
1479
1480 $p = array(
1481 'contact_id' => $this->_individualId,
1482 'receive_date' => date('Ymd'),
1483 'total_amount' => 100.00,
1484 'financial_type_id' => $this->_financialTypeId,
1485 'non_deductible_amount' => 10.00,
1486 'fee_amount' => 5.00,
1487 'net_amount' => 95.00,
1488 'trxn_id' => 23456,
1489 'invoice_id' => 78910,
1490 'source' => 'SSF',
1491 'contribution_status_id' => 1,
1492
1493 );
1494 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1495
1496 $result = $this->callAPISuccess('contribution', 'get', $params);
1497 // We're taking the first element.
1498 $res = $result['values'][$contribution['id']];
1499
1500 $this->assertEquals($p['contact_id'], $res['contact_id']);
1501 $this->assertEquals($p['total_amount'], $res['total_amount']);
1502 $this->assertEquals($p['financial_type_id'], $res['financial_type_id']);
1503 $this->assertEquals($p['net_amount'], $res['net_amount']);
1504 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1505 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1506 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1507 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1508 $this->assertEquals($p['source'], $res['contribution_source']);
1509 // contribution_status_id = 1 => Completed
1510 $this->assertEquals('Completed', $res['contribution_status']);
1511
1512 $this->contributionDelete($contribution['id']);
1513 }
1514
1515 /**
1516 * Test civicrm_contribution_search. Success expected.
1517 */
1518 public function testSearch() {
1519 $p1 = array(
1520 'contact_id' => $this->_individualId,
1521 'receive_date' => date('Ymd'),
1522 'total_amount' => 100.00,
1523 'financial_type_id' => $this->_financialTypeId,
1524 'non_deductible_amount' => 10.00,
1525 'contribution_status_id' => 1,
1526
1527 );
1528 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1529
1530 $p2 = array(
1531 'contact_id' => $this->_individualId,
1532 'receive_date' => date('Ymd'),
1533 'total_amount' => 200.00,
1534 'financial_type_id' => $this->_financialTypeId,
1535 'non_deductible_amount' => 20.00,
1536 'trxn_id' => 5454565,
1537 'invoice_id' => 1212124,
1538 'fee_amount' => 50.00,
1539 'net_amount' => 60.00,
1540 'contribution_status_id' => 2,
1541
1542 );
1543 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1544
1545 $params = array(
1546 'contribution_id' => $contribution2['id'],
1547
1548 );
1549 $result = $this->callAPISuccess('contribution', 'get', $params);
1550 $res = $result['values'][$contribution2['id']];
1551
1552 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1553 $this->assertEquals($p2['total_amount'], $res['total_amount']);
1554 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id']);
1555 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1556 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1557 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1558 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1559 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
1560 // contribution_status_id = 2 => Pending
1561 $this->assertEquals('Pending', $res['contribution_status']);
1562
1563 $this->contributionDelete($contribution1['id']);
1564 $this->contributionDelete($contribution2['id']);
1565 }
1566
1567 /**
1568 * Test completing a transaction via the API.
1569 *
1570 * Note that we are creating a logged in user because email goes out from
1571 * that person
1572 */
1573 public function testCompleteTransaction() {
1574 $mut = new CiviMailUtils($this, TRUE);
1575 $this->createLoggedInUser();
1576 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1577 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1578 $this->callAPISuccess('contribution', 'completetransaction', array(
1579 'id' => $contribution['id'],
1580 ));
1581 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
1582 $this->assertEquals('Completed', $contribution['contribution_status']);
1583 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
1584 $mut->checkMailLog(array(
1585 'Receipt - Contribution',
1586 'Please print this confirmation for your records.',
1587 ));
1588 $mut->stop();
1589 }
1590
1591 /**
1592 * Test completing a transaction via the API.
1593 *
1594 * Note that we are creating a logged in user because email goes out from
1595 * that person
1596 */
1597 public function testCompleteTransactionFeeAmount() {
1598 $this->createLoggedInUser();
1599 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1600 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1601 $this->callAPISuccess('contribution', 'completetransaction', array(
1602 'id' => $contribution['id'],
1603 'fee_amount' => '.56',
1604 'trxn_id' => '7778888',
1605 ));
1606 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'], 'sequential' => 1));
1607 $this->assertEquals('Completed', $contribution['contribution_status']);
1608 $this->assertEquals('7778888', $contribution['trxn_id']);
1609 $this->assertEquals('.56', $contribution['fee_amount']);
1610 $this->assertEquals('99.44', $contribution['net_amount']);
1611 }
1612
1613 /**
1614 * Test repeat contribution successfully creates line items.
1615 */
1616 public function testRepeatTransaction() {
1617 $paymentProcessorID = $this->paymentProcessorCreate();
1618 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1619 'contact_id' => $this->_individualId,
1620 'installments' => '12',
1621 'frequency_interval' => '1',
1622 'amount' => '500',
1623 'contribution_status_id' => 1,
1624 'start_date' => '2012-01-01 00:00:00',
1625 'currency' => 'USD',
1626 'frequency_unit' => 'month',
1627 'payment_processor_id' => $paymentProcessorID,
1628 ));
1629 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
1630 $this->_params,
1631 array('contribution_recur_id' => $contributionRecur['id']))
1632 );
1633
1634 $this->callAPISuccess('contribution', 'repeattransaction', array(
1635 'original_contribution_id' => $originalContribution['id'],
1636 'contribution_status_id' => 'Completed',
1637 'trxn_id' => uniqid(),
1638 ));
1639 $lineItemParams = array(
1640 'entity_id' => $originalContribution['id'],
1641 'sequential' => 1,
1642 'return' => array(
1643 'entity_table',
1644 'qty',
1645 'unit_price',
1646 'line_total',
1647 'label',
1648 'financial_type_id',
1649 'deductible_amount',
1650 'price_field_value_id',
1651 'price_field_id',
1652 ),
1653 );
1654 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1655 'entity_id' => $originalContribution['id'],
1656 )));
1657 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1658 'entity_id' => $originalContribution['id'] + 1,
1659 )));
1660 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
1661 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1662 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
1663
1664 $this->quickCleanUpFinancialEntities();
1665 }
1666
1667 /**
1668 * Test repeat contribution accepts recur_id instead of original_contribution_id.
1669 */
1670 public function testRepeatTransactionAcceptRecurID() {
1671 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1672 'contact_id' => $this->_individualId,
1673 'installments' => '12',
1674 'frequency_interval' => '1',
1675 'amount' => '100',
1676 'contribution_status_id' => 1,
1677 'start_date' => '2012-01-01 00:00:00',
1678 'currency' => 'USD',
1679 'frequency_unit' => 'month',
1680 'payment_processor_id' => $this->paymentProcessorID,
1681 ));
1682 $this->callAPISuccess('contribution', 'create', array_merge(
1683 $this->_params,
1684 array('contribution_recur_id' => $contributionRecur['id']))
1685 );
1686
1687 $this->callAPISuccess('contribution', 'repeattransaction', array(
1688 'contribution_recur_id' => $contributionRecur['id'],
1689 'contribution_status_id' => 'Completed',
1690 'trxn_id' => uniqid(),
1691 ));
1692
1693 $this->quickCleanUpFinancialEntities();
1694 }
1695
1696 /**
1697 * CRM-16397 test appropriate action if total amount has changed for single line items.
1698 */
1699 public function testRepeatTransactionAlteredAmount() {
1700 $paymentProcessorID = $this->paymentProcessorCreate();
1701 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1702 'contact_id' => $this->_individualId,
1703 'installments' => '12',
1704 'frequency_interval' => '1',
1705 'amount' => '500',
1706 'contribution_status_id' => 1,
1707 'start_date' => '2012-01-01 00:00:00',
1708 'currency' => 'USD',
1709 'frequency_unit' => 'month',
1710 'payment_processor_id' => $paymentProcessorID,
1711 ));
1712 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
1713 $this->_params,
1714 array(
1715 'contribution_recur_id' => $contributionRecur['id'],
1716 ))
1717 );
1718
1719 $this->callAPISuccess('contribution', 'repeattransaction', array(
1720 'original_contribution_id' => $originalContribution['id'],
1721 'contribution_status_id' => 'Completed',
1722 'trxn_id' => uniqid(),
1723 'total_amount' => '400',
1724 'fee_amount' => 50,
1725 ));
1726 $lineItemParams = array(
1727 'entity_id' => $originalContribution['id'],
1728 'sequential' => 1,
1729 'return' => array(
1730 'entity_table',
1731 'qty',
1732 'unit_price',
1733 'line_total',
1734 'label',
1735 'financial_type_id',
1736 'deductible_amount',
1737 'price_field_value_id',
1738 'price_field_id',
1739 ),
1740 );
1741 $this->callAPISuccessGetSingle('contribution', array(
1742 'total_amount' => 400,
1743 'fee_amount' => 50,
1744 'net_amount' => 350,
1745 ));
1746 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1747 'entity_id' => $originalContribution['id'],
1748 )));
1749 $expectedLineItem = array_merge(
1750 $lineItem1['values'][0], array(
1751 'line_total' => '400.00',
1752 'unit_price' => '400.00',
1753 )
1754 );
1755
1756 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1757 'entity_id' => $originalContribution['id'] + 1,
1758 )));
1759 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
1760 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1761 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
1762 }
1763
1764 /**
1765 * CRM-17718 test appropriate action if financial type has changed for single line items.
1766 */
1767 public function testRepeatTransactionPassedInFinancialType() {
1768 $originalContribution = $this->setUpRecurringContribution();
1769
1770 $this->callAPISuccess('contribution', 'repeattransaction', array(
1771 'original_contribution_id' => $originalContribution['id'],
1772 'contribution_status_id' => 'Completed',
1773 'trxn_id' => uniqid(),
1774 'financial_type_id' => 2,
1775 ));
1776 $lineItemParams = array(
1777 'entity_id' => $originalContribution['id'],
1778 'sequential' => 1,
1779 'return' => array(
1780 'entity_table',
1781 'qty',
1782 'unit_price',
1783 'line_total',
1784 'label',
1785 'financial_type_id',
1786 'deductible_amount',
1787 'price_field_value_id',
1788 'price_field_id',
1789 ),
1790 );
1791
1792 $this->callAPISuccessGetSingle('contribution', array(
1793 'total_amount' => 100,
1794 'financial_type_id' => 2,
1795 ));
1796 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1797 'entity_id' => $originalContribution['id'],
1798 )));
1799 $expectedLineItem = array_merge(
1800 $lineItem1['values'][0], array(
1801 'line_total' => '100.00',
1802 'unit_price' => '100.00',
1803 'financial_type_id' => 2,
1804 )
1805 );
1806
1807 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1808 'entity_id' => $originalContribution['id'] + 1,
1809 )));
1810 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
1811 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1812 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
1813 }
1814
1815 /**
1816 * CRM-17718 test appropriate action if financial type has changed for single line items.
1817 */
1818 public function testRepeatTransactionUpdatedFinancialType() {
1819 $originalContribution = $this->setUpRecurringContribution(array(), array('financial_type_id' => 2));
1820
1821 $this->callAPISuccess('contribution', 'repeattransaction', array(
1822 'contribution_recur_id' => $originalContribution['id'],
1823 'contribution_status_id' => 'Completed',
1824 'trxn_id' => uniqid(),
1825 ));
1826 $lineItemParams = array(
1827 'entity_id' => $originalContribution['id'],
1828 'sequential' => 1,
1829 'return' => array(
1830 'entity_table',
1831 'qty',
1832 'unit_price',
1833 'line_total',
1834 'label',
1835 'financial_type_id',
1836 'deductible_amount',
1837 'price_field_value_id',
1838 'price_field_id',
1839 ),
1840 );
1841
1842 $this->callAPISuccessGetSingle('contribution', array(
1843 'total_amount' => 100,
1844 'financial_type_id' => 2,
1845 ));
1846 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1847 'entity_id' => $originalContribution['id'],
1848 )));
1849 $expectedLineItem = array_merge(
1850 $lineItem1['values'][0], array(
1851 'line_total' => '100.00',
1852 'unit_price' => '100.00',
1853 'financial_type_id' => 2,
1854 )
1855 );
1856
1857 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1858 'entity_id' => $originalContribution['id'] + 1,
1859 )));
1860 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
1861 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1862 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
1863 }
1864
1865 /**
1866 * CRM-16397 test appropriate action if campaign has been passed in.
1867 */
1868 public function testRepeatTransactionPassedInCampaign() {
1869 $paymentProcessorID = $this->paymentProcessorCreate();
1870 $campaignID = $this->campaignCreate();
1871 $campaignID2 = $this->campaignCreate();
1872 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1873 'contact_id' => $this->_individualId,
1874 'installments' => '12',
1875 'frequency_interval' => '1',
1876 'amount' => '100',
1877 'contribution_status_id' => 1,
1878 'start_date' => '2012-01-01 00:00:00',
1879 'currency' => 'USD',
1880 'frequency_unit' => 'month',
1881 'payment_processor_id' => $paymentProcessorID,
1882 ));
1883 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
1884 $this->_params,
1885 array(
1886 'contribution_recur_id' => $contributionRecur['id'],
1887 'campaign_id' => $campaignID,
1888 ))
1889 );
1890
1891 $this->callAPISuccess('contribution', 'repeattransaction', array(
1892 'original_contribution_id' => $originalContribution['id'],
1893 'contribution_status_id' => 'Completed',
1894 'trxn_id' => uniqid(),
1895 'campaign_id' => $campaignID2,
1896 ));
1897
1898 $this->callAPISuccessGetSingle('contribution', array(
1899 'total_amount' => 100,
1900 'campaign_id' => $campaignID2,
1901 ));
1902 }
1903
1904 /**
1905 * CRM-17718 campaign stored on contribution recur gets priority.
1906 *
1907 * This reflects the fact we permit people to update them.
1908 */
1909 public function testRepeatTransactionUpdatedCampaign() {
1910 $paymentProcessorID = $this->paymentProcessorCreate();
1911 $campaignID = $this->campaignCreate();
1912 $campaignID2 = $this->campaignCreate();
1913 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1914 'contact_id' => $this->_individualId,
1915 'installments' => '12',
1916 'frequency_interval' => '1',
1917 'amount' => '100',
1918 'contribution_status_id' => 1,
1919 'start_date' => '2012-01-01 00:00:00',
1920 'currency' => 'USD',
1921 'frequency_unit' => 'month',
1922 'payment_processor_id' => $paymentProcessorID,
1923 'campaign_id' => $campaignID,
1924 ));
1925 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
1926 $this->_params,
1927 array(
1928 'contribution_recur_id' => $contributionRecur['id'],
1929 'campaign_id' => $campaignID2,
1930 ))
1931 );
1932
1933 $this->callAPISuccess('contribution', 'repeattransaction', array(
1934 'original_contribution_id' => $originalContribution['id'],
1935 'contribution_status_id' => 'Completed',
1936 'trxn_id' => uniqid(),
1937 ));
1938
1939 $this->callAPISuccessGetSingle('contribution', array(
1940 'total_amount' => 100,
1941 'campaign_id' => $campaignID,
1942 ));
1943 }
1944
1945 /**
1946 * Test completing a transaction does not 'mess' with net amount (CRM-15960).
1947 */
1948 public function testCompleteTransactionNetAmountOK() {
1949 $this->createLoggedInUser();
1950 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1951 unset($params['net_amount']);
1952 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1953 $this->callAPISuccess('contribution', 'completetransaction', array(
1954 'id' => $contribution['id'],
1955 ));
1956 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
1957 $this->assertEquals('Completed', $contribution['contribution_status']);
1958 $this->assertTrue(($contribution['total_amount'] - $contribution['net_amount']) == $contribution['fee_amount']);
1959 }
1960
1961 /**
1962 * CRM-14151 - Test completing a transaction via the API.
1963 */
1964 public function testCompleteTransactionWithReceiptDateSet() {
1965 $mut = new CiviMailUtils($this, TRUE);
1966 $this->createLoggedInUser();
1967 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
1968 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1969 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id'], 'trxn_date' => date('Y-m-d')));
1970 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
1971 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1972 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['values'][0]['receive_date'])));
1973 $mut->checkMailLog(array(
1974 'Receipt - Contribution',
1975 'Please print this confirmation for your records.',
1976 ));
1977 $mut->stop();
1978 }
1979
1980 /**
1981 * Test completing first transaction in a recurring series.
1982 *
1983 * The status should be set to 'in progress' and the next scheduled payment date calculated.
1984 */
1985 public function testCompleteTransactionSetStatusToInProgress() {
1986 $paymentProcessorID = $this->paymentProcessorCreate();
1987 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1988 'contact_id' => $this->_individualId,
1989 'installments' => '12',
1990 'frequency_interval' => '1',
1991 'amount' => '500',
1992 'contribution_status_id' => 'Pending',
1993 'start_date' => '2012-01-01 00:00:00',
1994 'currency' => 'USD',
1995 'frequency_unit' => 'month',
1996 'payment_processor_id' => $paymentProcessorID,
1997 ));
1998 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(
1999 $this->_params,
2000 array(
2001 'contribution_recur_id' => $contributionRecur['id'],
2002 'contribution_status_id' => 'Pending',
2003 ))
2004 );
2005 $this->callAPISuccess('Contribution', 'completetransaction', array('id' => $contribution));
2006 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array(
2007 'id' => $contributionRecur['id'],
2008 'return' => array('next_sched_contribution_date', 'contribution_status_id'),
2009 ));
2010 $this->assertEquals(5, $contributionRecur['contribution_status_id']);
2011 $this->assertEquals(date('Y-m-d 00:00:00', strtotime('+1 month')), $contributionRecur['next_sched_contribution_date']);
2012 }
2013
2014 /**
2015 * Test completing a transaction with an event via the API.
2016 *
2017 * Note that we are creating a logged in user because email goes out from
2018 * that person
2019 */
2020 public function testCompleteTransactionWithParticipantRecord() {
2021 $mut = new CiviMailUtils($this, TRUE);
2022 $mut->clearMessages();
2023 $this->createLoggedInUser();
2024 $contributionID = $this->createPendingParticipantContribution();
2025 $this->callAPISuccess('contribution', 'completetransaction', array(
2026 'id' => $contributionID,
2027 )
2028 );
2029 $participantStatus = $this->callAPISuccessGetValue('participant', array(
2030 'id' => $this->_ids['participant'],
2031 'return' => 'participant_status_id',
2032 ));
2033 $this->assertEquals(1, $participantStatus);
2034 $mut->checkMailLog(array(
2035 'Annual CiviCRM meet',
2036 'Event',
2037 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
2038 ));
2039 $mut->stop();
2040 }
2041
2042 /**
2043 * Test membership is renewed when transaction completed.
2044 */
2045 public function testCompleteTransactionMembershipPriceSet() {
2046 $this->createPriceSetWithPage('membership');
2047 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', array(
2048 'name' => 'Grace',
2049 'return' => 'id')
2050 );
2051 $this->setUpPendingContribution($this->_ids['price_field_value'][0]);
2052 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
2053 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
2054 'membership_id' => $this->_ids['membership'],
2055 ));
2056 $this->assertEquals(1, $logs['count']);
2057 $this->assertEquals($stateOfGrace, $membership['status_id']);
2058 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
2059 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
2060 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
2061 $this->callAPISuccessGetSingle('LineItem', array(
2062 'entity_id' => $this->_ids['membership'],
2063 'entity_table' => 'civicrm_membership',
2064 ));
2065 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
2066 'membership_id' => $this->_ids['membership'],
2067 ));
2068 $this->assertEquals(2, $logs['count']);
2069 $this->assertNotEquals($stateOfGrace, $logs['values'][2]['status_id']);
2070 $this->cleanUpAfterPriceSets();
2071 }
2072
2073 /**
2074 * Test membership is renewed when transaction completed.
2075 */
2076 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
2077 $this->createPriceSetWithPage('membership');
2078 $this->setUpPendingContribution($this->_ids['price_field_value'][1]);
2079 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
2080 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
2081 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
2082 $this->cleanUpAfterPriceSets();
2083 }
2084
2085 public function cleanUpAfterPriceSets() {
2086 $this->quickCleanUpFinancialEntities();
2087 $this->contactDelete($this->_ids['contact']);
2088 }
2089
2090
2091 /**
2092 * Create price set with contribution test for test setup.
2093 *
2094 * This could be merged with 4.5 function setup in api_v3_ContributionPageTest::setUpContributionPage
2095 * on parent class at some point (fn is not in 4.4).
2096 *
2097 * @param $entity
2098 * @param array $params
2099 */
2100 public function createPriceSetWithPage($entity, $params = array()) {
2101 $membershipTypeID = $this->membershipTypeCreate();
2102 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', array(
2103 'title' => "Test Contribution Page",
2104 'financial_type_id' => 1,
2105 'currency' => 'NZD',
2106 'goal_amount' => 50,
2107 'is_pay_later' => 1,
2108 'is_monetary' => TRUE,
2109 'is_email_receipt' => FALSE,
2110 ));
2111 $priceSet = $this->callAPISuccess('price_set', 'create', array(
2112 'is_quick_config' => 0,
2113 'extends' => 'CiviMember',
2114 'financial_type_id' => 1,
2115 'title' => 'my Page',
2116 ));
2117 $priceSetID = $priceSet['id'];
2118
2119 CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
2120 $priceField = $this->callAPISuccess('price_field', 'create', array(
2121 'price_set_id' => $priceSetID,
2122 'label' => 'Goat Breed',
2123 'html_type' => 'Radio',
2124 ));
2125 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
2126 'price_set_id' => $priceSetID,
2127 'price_field_id' => $priceField['id'],
2128 'label' => 'Long Haired Goat',
2129 'amount' => 20,
2130 'financial_type_id' => 'Donation',
2131 'membership_type_id' => $membershipTypeID,
2132 'membership_num_terms' => 1,
2133 )
2134 );
2135 $this->_ids['price_field_value'] = array($priceFieldValue['id']);
2136 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
2137 'price_set_id' => $priceSetID,
2138 'price_field_id' => $priceField['id'],
2139 'label' => 'Shoe-eating Goat',
2140 'amount' => 10,
2141 'financial_type_id' => 'Donation',
2142 'membership_type_id' => $membershipTypeID,
2143 'membership_num_terms' => 2,
2144 )
2145 );
2146 $this->_ids['price_field_value'][] = $priceFieldValue['id'];
2147 $this->_ids['price_set'] = $priceSetID;
2148 $this->_ids['contribution_page'] = $contributionPageResult['id'];
2149 $this->_ids['price_field'] = array($priceField['id']);
2150
2151 $this->_ids['membership_type'] = $membershipTypeID;
2152 }
2153
2154 /**
2155 * Set up a pending transaction with a specific price field id.
2156 *
2157 * @param int $priceFieldValueID
2158 */
2159 public function setUpPendingContribution($priceFieldValueID) {
2160 $contactID = $this->individualCreate();
2161 $membership = $this->callAPISuccess('membership', 'create', array(
2162 'contact_id' => $contactID,
2163 'membership_type_id' => $this->_ids['membership_type'],
2164 'start_date' => 'yesterday - 1 year',
2165 'end_date' => 'yesterday',
2166 'join_date' => 'yesterday - 1 year',
2167 ));
2168 $contribution = $this->callAPISuccess('contribution', 'create', array(
2169 'domain_id' => 1,
2170 'contact_id' => $contactID,
2171 'receive_date' => date('Ymd'),
2172 'total_amount' => 100.00,
2173 'financial_type_id' => 1,
2174 'payment_instrument_id' => 'Credit Card',
2175 'non_deductible_amount' => 10.00,
2176 'trxn_id' => 'jdhfi88',
2177 'invoice_id' => 'djfhiewuyr',
2178 'source' => 'SSF',
2179 'contribution_status_id' => 2,
2180 'contribution_page_id' => $this->_ids['contribution_page'],
2181 'api.membership_payment.create' => array('membership_id' => $membership['id']),
2182 ));
2183
2184 $this->callAPISuccess('line_item', 'create', array(
2185 'entity_id' => $contribution['id'],
2186 'entity_table' => 'civicrm_contribution',
2187 'contribution_id' => $contribution['id'],
2188 'price_field_id' => $this->_ids['price_field'][0],
2189 'qty' => 1,
2190 'unit_price' => 20,
2191 'line_total' => 20,
2192 'financial_type_id' => 1,
2193 'price_field_value_id' => $priceFieldValueID,
2194 ));
2195 $this->_ids['contact'] = $contactID;
2196 $this->_ids['contribution'] = $contribution['id'];
2197 $this->_ids['membership'] = $membership['id'];
2198 }
2199
2200 /**
2201 * Test sending a mail via the API.
2202 */
2203 public function testSendMail() {
2204 $mut = new CiviMailUtils($this, TRUE);
2205 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
2206 $this->callAPISuccess('contribution', 'sendconfirmation', array(
2207 'id' => $contribution['id'],
2208 'receipt_from_email' => 'api@civicrm.org',
2209 )
2210 );
2211 $mut->checkMailLog(array(
2212 '$ 100.00',
2213 'Contribution Information',
2214 'Please print this confirmation for your records',
2215 ), array(
2216 'Event',
2217 )
2218 );
2219 $mut->stop();
2220 }
2221
2222 /**
2223 * Test sending a mail via the API.
2224 */
2225 public function testSendMailEvent() {
2226 $mut = new CiviMailUtils($this, TRUE);
2227 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
2228 $event = $this->eventCreate(array(
2229 'is_email_confirm' => 1,
2230 'confirm_from_email' => 'test@civicrm.org',
2231 ));
2232 $this->_eventID = $event['id'];
2233 $participantParams = array(
2234 'contact_id' => $this->_individualId,
2235 'event_id' => $this->_eventID,
2236 'status_id' => 1,
2237 'role_id' => 1,
2238 // to ensure it matches later on
2239 'register_date' => '2007-07-21 00:00:00',
2240 'source' => 'Online Event Registration: API Testing',
2241
2242 );
2243 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
2244 $this->callAPISuccess('participant_payment', 'create', array(
2245 'participant_id' => $participant['id'],
2246 'contribution_id' => $contribution['id'],
2247 ));
2248 $this->callAPISuccess('contribution', 'sendconfirmation', array(
2249 'id' => $contribution['id'],
2250 'receipt_from_email' => 'api@civicrm.org',
2251 )
2252 );
2253
2254 $mut->checkMailLog(array(
2255 'Annual CiviCRM meet',
2256 'Event',
2257 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
2258 ), array()
2259 );
2260 $mut->stop();
2261 }
2262
2263 /**
2264 * This function does a GET & compares the result against the $params.
2265 *
2266 * Use as a double check on Creates.
2267 *
2268 * @param array $params
2269 * @param int $id
2270 * @param bool $delete
2271 */
2272 public function contributionGetnCheck($params, $id, $delete = TRUE) {
2273
2274 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
2275 'id' => $id,
2276
2277 ));
2278
2279 if ($delete) {
2280 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
2281 }
2282 $this->assertAPISuccess($contribution, 0);
2283 $values = $contribution['values'][$contribution['id']];
2284 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
2285 // this is not returned in id format
2286 unset($params['payment_instrument_id']);
2287 $params['contribution_source'] = $params['source'];
2288 unset($params['source']);
2289 foreach ($params as $key => $value) {
2290 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
2291 }
2292 }
2293
2294 /**
2295 * Create a pending contribution & linked pending participant record (along with an event).
2296 */
2297 public function createPendingParticipantContribution() {
2298 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'));
2299 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6));
2300 $this->_ids['participant'] = $participantID;
2301 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
2302 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2303 $this->callAPISuccess('participant_payment', 'create', array(
2304 'contribution_id' => $contribution['id'],
2305 'participant_id' => $participantID,
2306 ));
2307 $this->callAPISuccess('line_item', 'get', array(
2308 'entity_id' => $contribution['id'],
2309 'entity_table' => 'civicrm_contribution',
2310 'api.line_item.create' => array(
2311 'entity_id' => $participantID,
2312 'entity_table' => 'civicrm_participant',
2313 ),
2314 ));
2315 return $contribution['id'];
2316 }
2317
2318 /**
2319 * Get financial transaction amount.
2320 *
2321 * @param int $contId
2322 *
2323 * @return null|string
2324 */
2325 public function _getFinancialTrxnAmount($contId) {
2326 $query = "SELECT
2327 SUM( ft.total_amount ) AS total
2328 FROM civicrm_financial_trxn AS ft
2329 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
2330 WHERE ceft.entity_table = 'civicrm_contribution'
2331 AND ceft.entity_id = {$contId}";
2332
2333 $result = CRM_Core_DAO::singleValueQuery($query);
2334 return $result;
2335 }
2336
2337 /**
2338 * @param int $contId
2339 *
2340 * @return null|string
2341 */
2342 public function _getFinancialItemAmount($contId) {
2343 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
2344 $query = "SELECT
2345 SUM(amount)
2346 FROM civicrm_financial_item
2347 WHERE entity_table = 'civicrm_line_item'
2348 AND entity_id = {$lineItem}";
2349 $result = CRM_Core_DAO::singleValueQuery($query);
2350 return $result;
2351 }
2352
2353 /**
2354 * @param int $contId
2355 * @param $context
2356 */
2357 public function _checkFinancialItem($contId, $context) {
2358 if ($context != 'paylater') {
2359 $params = array(
2360 'entity_id' => $contId,
2361 'entity_table' => 'civicrm_contribution',
2362 );
2363 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($params, TRUE));
2364 $entityParams = array(
2365 'financial_trxn_id' => $trxn['financial_trxn_id'],
2366 'entity_table' => 'civicrm_financial_item',
2367 );
2368 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
2369 $params = array(
2370 'id' => $entityTrxn['entity_id'],
2371 );
2372 }
2373 if ($context == 'paylater') {
2374 $lineItems = CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution');
2375 foreach ($lineItems as $key => $item) {
2376 $params = array(
2377 'entity_id' => $key,
2378 'entity_table' => 'civicrm_line_item',
2379 );
2380 $compareParams = array('status_id' => 1);
2381 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
2382 }
2383 }
2384 elseif ($context == 'refund') {
2385 $compareParams = array(
2386 'status_id' => 1,
2387 'financial_account_id' => 1,
2388 'amount' => -100,
2389 );
2390 }
2391 elseif ($context == 'cancelPending') {
2392 $compareParams = array(
2393 'status_id' => 3,
2394 'financial_account_id' => 1,
2395 'amount' => -100,
2396 );
2397 }
2398 elseif ($context == 'changeFinancial') {
2399 $lineKey = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
2400 $params = array(
2401 'entity_id' => $lineKey,
2402 'amount' => -100,
2403 );
2404 $compareParams = array(
2405 'financial_account_id' => 1,
2406 );
2407 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
2408 $params = array(
2409 'financial_account_id' => 3,
2410 'entity_id' => $lineKey,
2411 );
2412 $compareParams = array(
2413 'amount' => 100,
2414 );
2415 }
2416 if ($context != 'paylater') {
2417 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
2418 }
2419 }
2420
2421 /**
2422 * Check financial transaction.
2423 *
2424 * @todo break this down into sensible functions - most calls to it only use a few lines out of the big if.
2425 *
2426 * @param array $contribution
2427 * @param string $context
2428 * @param int $instrumentId
2429 * @param array $extraParams
2430 */
2431 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL, $extraParams = array()) {
2432 $trxnParams = array(
2433 'entity_id' => $contribution['id'],
2434 'entity_table' => 'civicrm_contribution',
2435 );
2436 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($trxnParams, TRUE));
2437 $params = array(
2438 'id' => $trxn['financial_trxn_id'],
2439 );
2440 if ($context == 'payLater') {
2441 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
2442 $compareParams = array(
2443 'status_id' => 1,
2444 'from_financial_account_id' => CRM_Contribute_PseudoConstant::financialAccountType($contribution['financial_type_id'], $relationTypeId),
2445 );
2446 }
2447 elseif ($context == 'refund') {
2448 $compareParams = array(
2449 'to_financial_account_id' => 6,
2450 'total_amount' => -100,
2451 'status_id' => 7,
2452 'trxn_date' => '2015-01-01 09:00:00',
2453 'trxn_id' => 'the refund',
2454 );
2455 }
2456 elseif ($context == 'cancelPending') {
2457 $compareParams = array(
2458 'to_financial_account_id' => 7,
2459 'total_amount' => -100,
2460 'status_id' => 3,
2461 );
2462 }
2463 elseif ($context == 'changeFinancial' || $context == 'paymentInstrument') {
2464 $entityParams = array(
2465 'entity_id' => $contribution['id'],
2466 'entity_table' => 'civicrm_contribution',
2467 'amount' => -100,
2468 );
2469 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
2470 $trxnParams1 = array(
2471 'id' => $trxn['financial_trxn_id'],
2472 );
2473 $compareParams = array(
2474 'total_amount' => -100,
2475 'status_id' => 1,
2476 );
2477 if ($context == 'paymentInstrument') {
2478 $compareParams += array(
2479 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount(4),
2480 'payment_instrument_id' => 4,
2481 );
2482 }
2483 else {
2484 $compareParams['to_financial_account_id'] = 12;
2485 }
2486 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, array_merge($compareParams, $extraParams));
2487 $compareParams['total_amount'] = 100;
2488 if ($context == 'paymentInstrument') {
2489 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($instrumentId);
2490 $compareParams['payment_instrument_id'] = $instrumentId;
2491 }
2492 else {
2493 $compareParams['to_financial_account_id'] = 12;
2494 }
2495 }
2496
2497 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, array_merge($compareParams, $extraParams));
2498 }
2499
2500 /**
2501 * @return mixed
2502 */
2503 public function _addPaymentInstrument() {
2504 $gId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
2505 $optionParams = array(
2506 'option_group_id' => $gId,
2507 'label' => 'Test Card',
2508 'name' => 'Test Card',
2509 'value' => '6',
2510 'weight' => '6',
2511 'is_active' => 1,
2512 );
2513 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
2514 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
2515 $financialParams = array(
2516 'entity_table' => 'civicrm_option_value',
2517 'entity_id' => $optionValue['id'],
2518 'account_relationship' => $relationTypeId,
2519 'financial_account_id' => 7,
2520 );
2521 CRM_Financial_BAO_FinancialTypeAccount::add($financialParams, CRM_Core_DAO::$_nullArray);
2522 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
2523 return $optionValue['values'][$optionValue['id']]['value'];
2524 }
2525
2526 /**
2527 * @param array $params
2528 * @param $context
2529 */
2530 public function _checkFinancialRecords($params, $context) {
2531 $entityParams = array(
2532 'entity_id' => $params['id'],
2533 'entity_table' => 'civicrm_contribution',
2534 );
2535 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $params['id']));
2536 $this->assertEquals($contribution['total_amount'] - $contribution['fee_amount'], $contribution['net_amount']);
2537 if ($context == 'pending') {
2538 $trxn = CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams);
2539 $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
2540 return;
2541 }
2542 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
2543 $trxnParams = array(
2544 'id' => $trxn['financial_trxn_id'],
2545 );
2546 if ($context != 'online' && $context != 'payLater') {
2547 $compareParams = array(
2548 'to_financial_account_id' => 6,
2549 'total_amount' => 100,
2550 'status_id' => 1,
2551 );
2552 }
2553 if ($context == 'feeAmount') {
2554 $compareParams['fee_amount'] = 50;
2555 }
2556 elseif ($context == 'online') {
2557 $compareParams = array(
2558 'to_financial_account_id' => 12,
2559 'total_amount' => 100,
2560 'status_id' => 1,
2561 );
2562 }
2563 elseif ($context == 'payLater') {
2564 $compareParams = array(
2565 'to_financial_account_id' => 7,
2566 'total_amount' => 100,
2567 'status_id' => 2,
2568 );
2569 }
2570 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
2571 $entityParams = array(
2572 'financial_trxn_id' => $trxn['financial_trxn_id'],
2573 'entity_table' => 'civicrm_financial_item',
2574 );
2575 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
2576 $fitemParams = array(
2577 'id' => $entityTrxn['entity_id'],
2578 );
2579 $compareParams = array(
2580 'amount' => 100,
2581 'status_id' => 1,
2582 'financial_account_id' => 1,
2583 );
2584 if ($context == 'payLater') {
2585 $compareParams = array(
2586 'amount' => 100,
2587 'status_id' => 3,
2588 'financial_account_id' => 1,
2589 );
2590 }
2591 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
2592 if ($context == 'feeAmount') {
2593 $maxParams = array(
2594 'entity_id' => $params['id'],
2595 'entity_table' => 'civicrm_contribution',
2596 );
2597 $maxTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($maxParams, TRUE));
2598 $trxnParams = array(
2599 'id' => $maxTrxn['financial_trxn_id'],
2600 );
2601 $compareParams = array(
2602 'to_financial_account_id' => 5,
2603 'from_financial_account_id' => 6,
2604 'total_amount' => 50,
2605 'status_id' => 1,
2606 );
2607 $trxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($params['id'], 'DESC');
2608 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
2609 $fitemParams = array(
2610 'entity_id' => $trxnId['financialTrxnId'],
2611 'entity_table' => 'civicrm_financial_trxn',
2612 );
2613 $compareParams = array(
2614 'amount' => 50,
2615 'status_id' => 1,
2616 'financial_account_id' => 5,
2617 );
2618 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
2619 }
2620 }
2621
2622 /**
2623 * Set up the basic recurring contribution for tests.
2624 *
2625 * @param array $generalParams
2626 * Parameters that can be merged into the recurring AND the contribution.
2627 *
2628 * @param array $recurParams
2629 * Parameters to merge into the recur only.
2630 *
2631 * @return array|int
2632 */
2633 protected function setUpRecurringContribution($generalParams = array(), $recurParams = array()) {
2634 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
2635 'contact_id' => $this->_individualId,
2636 'installments' => '12',
2637 'frequency_interval' => '1',
2638 'amount' => '100',
2639 'contribution_status_id' => 1,
2640 'start_date' => '2012-01-01 00:00:00',
2641 'currency' => 'USD',
2642 'frequency_unit' => 'month',
2643 'payment_processor_id' => $this->paymentProcessorID,
2644 ), $generalParams, $recurParams));
2645 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2646 $this->_params,
2647 array(
2648 'contribution_recur_id' => $contributionRecur['id'],
2649 ), $generalParams)
2650 );
2651 return $originalContribution;
2652 }
2653
2654 }