Merge pull request #8068 from colemanw/CRM-17832
[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('SSF', $contribution['contribution_source']);
1583 $this->assertEquals('Completed', $contribution['contribution_status']);
1584 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
1585 $mut->checkMailLog(array(
1586 'Receipt - Contribution',
1587 'Please print this confirmation for your records.',
1588 'May 11th, 2012',
1589 ));
1590 $mut->stop();
1591 }
1592
1593 /**
1594 * Test completing a transaction via the API.
1595 *
1596 * Note that we are creating a logged in user because email goes out from
1597 * that person
1598 */
1599 public function testCompleteTransactionFeeAmount() {
1600 $this->createLoggedInUser();
1601 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1602 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1603 $this->callAPISuccess('contribution', 'completetransaction', array(
1604 'id' => $contribution['id'],
1605 'fee_amount' => '.56',
1606 'trxn_id' => '7778888',
1607 ));
1608 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'], 'sequential' => 1));
1609 $this->assertEquals('Completed', $contribution['contribution_status']);
1610 $this->assertEquals('7778888', $contribution['trxn_id']);
1611 $this->assertEquals('.56', $contribution['fee_amount']);
1612 $this->assertEquals('99.44', $contribution['net_amount']);
1613 }
1614
1615 /**
1616 * Test repeat contribution successfully creates line items.
1617 */
1618 public function testRepeatTransaction() {
1619 $originalContribution = $this->setUpRepeatTransaction();
1620
1621 $this->callAPISuccess('contribution', 'repeattransaction', array(
1622 'original_contribution_id' => $originalContribution['id'],
1623 'contribution_status_id' => 'Completed',
1624 'trxn_id' => uniqid(),
1625 ));
1626 $lineItemParams = array(
1627 'entity_id' => $originalContribution['id'],
1628 'sequential' => 1,
1629 'return' => array(
1630 'entity_table',
1631 'qty',
1632 'unit_price',
1633 'line_total',
1634 'label',
1635 'financial_type_id',
1636 'deductible_amount',
1637 'price_field_value_id',
1638 'price_field_id',
1639 ),
1640 );
1641 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1642 'entity_id' => $originalContribution['id'],
1643 )));
1644 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1645 'entity_id' => $originalContribution['id'] + 1,
1646 )));
1647 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
1648 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1649 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
1650
1651 $this->quickCleanUpFinancialEntities();
1652 }
1653
1654 /**
1655 * Test repeat contribution successfully creates line items.
1656 */
1657 public function testRepeatTransactionIsTest() {
1658 $this->_params['is_test'] = 1;
1659 $originalContribution = $this->setUpRepeatTransaction(array('is_test' => 1));
1660
1661 $this->callAPISuccess('contribution', 'repeattransaction', array(
1662 'original_contribution_id' => $originalContribution['id'],
1663 'contribution_status_id' => 'Completed',
1664 'trxn_id' => uniqid(),
1665 ));
1666 $this->callAPISuccessGetCount('Contribution', array('contribution_test' => 1), 2);
1667 }
1668
1669 /**
1670 * Test repeat contribution passed in status.
1671 */
1672 public function testRepeatTransactionPassedInStatus() {
1673 $originalContribution = $this->setUpRepeatTransaction();
1674
1675 $this->callAPISuccess('contribution', 'repeattransaction', array(
1676 'original_contribution_id' => $originalContribution['id'],
1677 'contribution_status_id' => 'Pending',
1678 'trxn_id' => uniqid(),
1679 ));
1680 $this->callAPISuccessGetCount('Contribution', array('contribution_status_id' => 2), 1);
1681 }
1682
1683 /**
1684 * Test repeat contribution accepts recur_id instead of original_contribution_id.
1685 */
1686 public function testRepeatTransactionAcceptRecurID() {
1687 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1688 'contact_id' => $this->_individualId,
1689 'installments' => '12',
1690 'frequency_interval' => '1',
1691 'amount' => '100',
1692 'contribution_status_id' => 1,
1693 'start_date' => '2012-01-01 00:00:00',
1694 'currency' => 'USD',
1695 'frequency_unit' => 'month',
1696 'payment_processor_id' => $this->paymentProcessorID,
1697 ));
1698 $this->callAPISuccess('contribution', 'create', array_merge(
1699 $this->_params,
1700 array('contribution_recur_id' => $contributionRecur['id']))
1701 );
1702
1703 $this->callAPISuccess('contribution', 'repeattransaction', array(
1704 'contribution_recur_id' => $contributionRecur['id'],
1705 'contribution_status_id' => 'Completed',
1706 'trxn_id' => uniqid(),
1707 ));
1708
1709 $this->quickCleanUpFinancialEntities();
1710 }
1711
1712 /**
1713 * CRM-16397 test appropriate action if total amount has changed for single line items.
1714 */
1715 public function testRepeatTransactionAlteredAmount() {
1716 $paymentProcessorID = $this->paymentProcessorCreate();
1717 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1718 'contact_id' => $this->_individualId,
1719 'installments' => '12',
1720 'frequency_interval' => '1',
1721 'amount' => '500',
1722 'contribution_status_id' => 1,
1723 'start_date' => '2012-01-01 00:00:00',
1724 'currency' => 'USD',
1725 'frequency_unit' => 'month',
1726 'payment_processor_id' => $paymentProcessorID,
1727 ));
1728 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
1729 $this->_params,
1730 array(
1731 'contribution_recur_id' => $contributionRecur['id'],
1732 ))
1733 );
1734
1735 $this->callAPISuccess('contribution', 'repeattransaction', array(
1736 'original_contribution_id' => $originalContribution['id'],
1737 'contribution_status_id' => 'Completed',
1738 'trxn_id' => uniqid(),
1739 'total_amount' => '400',
1740 'fee_amount' => 50,
1741 ));
1742 $lineItemParams = array(
1743 'entity_id' => $originalContribution['id'],
1744 'sequential' => 1,
1745 'return' => array(
1746 'entity_table',
1747 'qty',
1748 'unit_price',
1749 'line_total',
1750 'label',
1751 'financial_type_id',
1752 'deductible_amount',
1753 'price_field_value_id',
1754 'price_field_id',
1755 ),
1756 );
1757 $this->callAPISuccessGetSingle('contribution', array(
1758 'total_amount' => 400,
1759 'fee_amount' => 50,
1760 'net_amount' => 350,
1761 ));
1762 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1763 'entity_id' => $originalContribution['id'],
1764 )));
1765 $expectedLineItem = array_merge(
1766 $lineItem1['values'][0], array(
1767 'line_total' => '400.00',
1768 'unit_price' => '400.00',
1769 )
1770 );
1771
1772 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1773 'entity_id' => $originalContribution['id'] + 1,
1774 )));
1775 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
1776 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1777 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
1778 }
1779
1780 /**
1781 * CRM-17718 test appropriate action if financial type has changed for single line items.
1782 */
1783 public function testRepeatTransactionPassedInFinancialType() {
1784 $originalContribution = $this->setUpRecurringContribution();
1785
1786 $this->callAPISuccess('contribution', 'repeattransaction', array(
1787 'original_contribution_id' => $originalContribution['id'],
1788 'contribution_status_id' => 'Completed',
1789 'trxn_id' => uniqid(),
1790 'financial_type_id' => 2,
1791 ));
1792 $lineItemParams = array(
1793 'entity_id' => $originalContribution['id'],
1794 'sequential' => 1,
1795 'return' => array(
1796 'entity_table',
1797 'qty',
1798 'unit_price',
1799 'line_total',
1800 'label',
1801 'financial_type_id',
1802 'deductible_amount',
1803 'price_field_value_id',
1804 'price_field_id',
1805 ),
1806 );
1807
1808 $this->callAPISuccessGetSingle('contribution', array(
1809 'total_amount' => 100,
1810 'financial_type_id' => 2,
1811 ));
1812 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1813 'entity_id' => $originalContribution['id'],
1814 )));
1815 $expectedLineItem = array_merge(
1816 $lineItem1['values'][0], array(
1817 'line_total' => '100.00',
1818 'unit_price' => '100.00',
1819 'financial_type_id' => 2,
1820 )
1821 );
1822
1823 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1824 'entity_id' => $originalContribution['id'] + 1,
1825 )));
1826 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
1827 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1828 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
1829 }
1830
1831 /**
1832 * CRM-17718 test appropriate action if financial type has changed for single line items.
1833 */
1834 public function testRepeatTransactionUpdatedFinancialType() {
1835 $originalContribution = $this->setUpRecurringContribution(array(), array('financial_type_id' => 2));
1836
1837 $this->callAPISuccess('contribution', 'repeattransaction', array(
1838 'contribution_recur_id' => $originalContribution['id'],
1839 'contribution_status_id' => 'Completed',
1840 'trxn_id' => uniqid(),
1841 ));
1842 $lineItemParams = array(
1843 'entity_id' => $originalContribution['id'],
1844 'sequential' => 1,
1845 'return' => array(
1846 'entity_table',
1847 'qty',
1848 'unit_price',
1849 'line_total',
1850 'label',
1851 'financial_type_id',
1852 'deductible_amount',
1853 'price_field_value_id',
1854 'price_field_id',
1855 ),
1856 );
1857
1858 $this->callAPISuccessGetSingle('contribution', array(
1859 'total_amount' => 100,
1860 'financial_type_id' => 2,
1861 ));
1862 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1863 'entity_id' => $originalContribution['id'],
1864 )));
1865 $expectedLineItem = array_merge(
1866 $lineItem1['values'][0], array(
1867 'line_total' => '100.00',
1868 'unit_price' => '100.00',
1869 'financial_type_id' => 2,
1870 )
1871 );
1872
1873 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1874 'entity_id' => $originalContribution['id'] + 1,
1875 )));
1876 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
1877 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1878 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
1879 }
1880
1881 /**
1882 * CRM-16397 test appropriate action if campaign has been passed in.
1883 */
1884 public function testRepeatTransactionPassedInCampaign() {
1885 $paymentProcessorID = $this->paymentProcessorCreate();
1886 $campaignID = $this->campaignCreate();
1887 $campaignID2 = $this->campaignCreate();
1888 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1889 'contact_id' => $this->_individualId,
1890 'installments' => '12',
1891 'frequency_interval' => '1',
1892 'amount' => '100',
1893 'contribution_status_id' => 1,
1894 'start_date' => '2012-01-01 00:00:00',
1895 'currency' => 'USD',
1896 'frequency_unit' => 'month',
1897 'payment_processor_id' => $paymentProcessorID,
1898 ));
1899 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
1900 $this->_params,
1901 array(
1902 'contribution_recur_id' => $contributionRecur['id'],
1903 'campaign_id' => $campaignID,
1904 ))
1905 );
1906
1907 $this->callAPISuccess('contribution', 'repeattransaction', array(
1908 'original_contribution_id' => $originalContribution['id'],
1909 'contribution_status_id' => 'Completed',
1910 'trxn_id' => uniqid(),
1911 'campaign_id' => $campaignID2,
1912 ));
1913
1914 $this->callAPISuccessGetSingle('contribution', array(
1915 'total_amount' => 100,
1916 'campaign_id' => $campaignID2,
1917 ));
1918 }
1919
1920 /**
1921 * CRM-17718 campaign stored on contribution recur gets priority.
1922 *
1923 * This reflects the fact we permit people to update them.
1924 */
1925 public function testRepeatTransactionUpdatedCampaign() {
1926 $paymentProcessorID = $this->paymentProcessorCreate();
1927 $campaignID = $this->campaignCreate();
1928 $campaignID2 = $this->campaignCreate();
1929 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1930 'contact_id' => $this->_individualId,
1931 'installments' => '12',
1932 'frequency_interval' => '1',
1933 'amount' => '100',
1934 'contribution_status_id' => 1,
1935 'start_date' => '2012-01-01 00:00:00',
1936 'currency' => 'USD',
1937 'frequency_unit' => 'month',
1938 'payment_processor_id' => $paymentProcessorID,
1939 'campaign_id' => $campaignID,
1940 ));
1941 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
1942 $this->_params,
1943 array(
1944 'contribution_recur_id' => $contributionRecur['id'],
1945 'campaign_id' => $campaignID2,
1946 ))
1947 );
1948
1949 $this->callAPISuccess('contribution', 'repeattransaction', array(
1950 'original_contribution_id' => $originalContribution['id'],
1951 'contribution_status_id' => 'Completed',
1952 'trxn_id' => uniqid(),
1953 ));
1954
1955 $this->callAPISuccessGetSingle('contribution', array(
1956 'total_amount' => 100,
1957 'campaign_id' => $campaignID,
1958 ));
1959 }
1960
1961 /**
1962 * Test completing a transaction does not 'mess' with net amount (CRM-15960).
1963 */
1964 public function testCompleteTransactionNetAmountOK() {
1965 $this->createLoggedInUser();
1966 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1967 unset($params['net_amount']);
1968 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1969 $this->callAPISuccess('contribution', 'completetransaction', array(
1970 'id' => $contribution['id'],
1971 ));
1972 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
1973 $this->assertEquals('Completed', $contribution['contribution_status']);
1974 $this->assertTrue(($contribution['total_amount'] - $contribution['net_amount']) == $contribution['fee_amount']);
1975 }
1976
1977 /**
1978 * CRM-14151 - Test completing a transaction via the API.
1979 */
1980 public function testCompleteTransactionWithReceiptDateSet() {
1981 $mut = new CiviMailUtils($this, TRUE);
1982 $this->createLoggedInUser();
1983 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
1984 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1985 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id'], 'trxn_date' => date('Y-m-d')));
1986 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
1987 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1988 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['values'][0]['receive_date'])));
1989 $mut->checkMailLog(array(
1990 'Receipt - Contribution',
1991 'Please print this confirmation for your records.',
1992 ));
1993 $mut->stop();
1994 }
1995
1996 /**
1997 * Test completing first transaction in a recurring series.
1998 *
1999 * The status should be set to 'in progress' and the next scheduled payment date calculated.
2000 */
2001 public function testCompleteTransactionSetStatusToInProgress() {
2002 $paymentProcessorID = $this->paymentProcessorCreate();
2003 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2004 'contact_id' => $this->_individualId,
2005 'installments' => '12',
2006 'frequency_interval' => '1',
2007 'amount' => '500',
2008 'contribution_status_id' => 'Pending',
2009 'start_date' => '2012-01-01 00:00:00',
2010 'currency' => 'USD',
2011 'frequency_unit' => 'month',
2012 'payment_processor_id' => $paymentProcessorID,
2013 ));
2014 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(
2015 $this->_params,
2016 array(
2017 'contribution_recur_id' => $contributionRecur['id'],
2018 'contribution_status_id' => 'Pending',
2019 ))
2020 );
2021 $this->callAPISuccess('Contribution', 'completetransaction', array('id' => $contribution));
2022 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array(
2023 'id' => $contributionRecur['id'],
2024 'return' => array('next_sched_contribution_date', 'contribution_status_id'),
2025 ));
2026 $this->assertEquals(5, $contributionRecur['contribution_status_id']);
2027 $this->assertEquals(date('Y-m-d 00:00:00', strtotime('+1 month')), $contributionRecur['next_sched_contribution_date']);
2028 }
2029
2030 /**
2031 * Test completing a transaction with an event via the API.
2032 *
2033 * Note that we are creating a logged in user because email goes out from
2034 * that person
2035 */
2036 public function testCompleteTransactionWithParticipantRecord() {
2037 $mut = new CiviMailUtils($this, TRUE);
2038 $mut->clearMessages();
2039 $this->createLoggedInUser();
2040 $contributionID = $this->createPendingParticipantContribution();
2041 $this->callAPISuccess('contribution', 'completetransaction', array(
2042 'id' => $contributionID,
2043 )
2044 );
2045 $participantStatus = $this->callAPISuccessGetValue('participant', array(
2046 'id' => $this->_ids['participant'],
2047 'return' => 'participant_status_id',
2048 ));
2049 $this->assertEquals(1, $participantStatus);
2050 $mut->checkMailLog(array(
2051 'Annual CiviCRM meet',
2052 'Event',
2053 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
2054 ));
2055 $mut->stop();
2056 }
2057
2058 /**
2059 * Test membership is renewed when transaction completed.
2060 */
2061 public function testCompleteTransactionMembershipPriceSet() {
2062 $this->createPriceSetWithPage('membership');
2063 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', array(
2064 'name' => 'Grace',
2065 'return' => 'id')
2066 );
2067 $this->setUpPendingContribution($this->_ids['price_field_value'][0]);
2068 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
2069 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
2070 'membership_id' => $this->_ids['membership'],
2071 ));
2072 $this->assertEquals(1, $logs['count']);
2073 $this->assertEquals($stateOfGrace, $membership['status_id']);
2074 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
2075 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
2076 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
2077 $this->callAPISuccessGetSingle('LineItem', array(
2078 'entity_id' => $this->_ids['membership'],
2079 'entity_table' => 'civicrm_membership',
2080 ));
2081 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
2082 'membership_id' => $this->_ids['membership'],
2083 ));
2084 $this->assertEquals(2, $logs['count']);
2085 $this->assertNotEquals($stateOfGrace, $logs['values'][2]['status_id']);
2086 $this->cleanUpAfterPriceSets();
2087 }
2088
2089 /**
2090 * Test membership is renewed when transaction completed.
2091 */
2092 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
2093 $this->createPriceSetWithPage('membership');
2094 $this->setUpPendingContribution($this->_ids['price_field_value'][1]);
2095 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
2096 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
2097 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
2098 $this->cleanUpAfterPriceSets();
2099 }
2100
2101 public function cleanUpAfterPriceSets() {
2102 $this->quickCleanUpFinancialEntities();
2103 $this->contactDelete($this->_ids['contact']);
2104 }
2105
2106
2107 /**
2108 * Create price set with contribution test for test setup.
2109 *
2110 * This could be merged with 4.5 function setup in api_v3_ContributionPageTest::setUpContributionPage
2111 * on parent class at some point (fn is not in 4.4).
2112 *
2113 * @param $entity
2114 * @param array $params
2115 */
2116 public function createPriceSetWithPage($entity, $params = array()) {
2117 $membershipTypeID = $this->membershipTypeCreate();
2118 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', array(
2119 'title' => "Test Contribution Page",
2120 'financial_type_id' => 1,
2121 'currency' => 'NZD',
2122 'goal_amount' => 50,
2123 'is_pay_later' => 1,
2124 'is_monetary' => TRUE,
2125 'is_email_receipt' => FALSE,
2126 ));
2127 $priceSet = $this->callAPISuccess('price_set', 'create', array(
2128 'is_quick_config' => 0,
2129 'extends' => 'CiviMember',
2130 'financial_type_id' => 1,
2131 'title' => 'my Page',
2132 ));
2133 $priceSetID = $priceSet['id'];
2134
2135 CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
2136 $priceField = $this->callAPISuccess('price_field', 'create', array(
2137 'price_set_id' => $priceSetID,
2138 'label' => 'Goat Breed',
2139 'html_type' => 'Radio',
2140 ));
2141 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
2142 'price_set_id' => $priceSetID,
2143 'price_field_id' => $priceField['id'],
2144 'label' => 'Long Haired Goat',
2145 'amount' => 20,
2146 'financial_type_id' => 'Donation',
2147 'membership_type_id' => $membershipTypeID,
2148 'membership_num_terms' => 1,
2149 )
2150 );
2151 $this->_ids['price_field_value'] = array($priceFieldValue['id']);
2152 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
2153 'price_set_id' => $priceSetID,
2154 'price_field_id' => $priceField['id'],
2155 'label' => 'Shoe-eating Goat',
2156 'amount' => 10,
2157 'financial_type_id' => 'Donation',
2158 'membership_type_id' => $membershipTypeID,
2159 'membership_num_terms' => 2,
2160 )
2161 );
2162 $this->_ids['price_field_value'][] = $priceFieldValue['id'];
2163 $this->_ids['price_set'] = $priceSetID;
2164 $this->_ids['contribution_page'] = $contributionPageResult['id'];
2165 $this->_ids['price_field'] = array($priceField['id']);
2166
2167 $this->_ids['membership_type'] = $membershipTypeID;
2168 }
2169
2170 /**
2171 * Set up a pending transaction with a specific price field id.
2172 *
2173 * @param int $priceFieldValueID
2174 */
2175 public function setUpPendingContribution($priceFieldValueID) {
2176 $contactID = $this->individualCreate();
2177 $membership = $this->callAPISuccess('membership', 'create', array(
2178 'contact_id' => $contactID,
2179 'membership_type_id' => $this->_ids['membership_type'],
2180 'start_date' => 'yesterday - 1 year',
2181 'end_date' => 'yesterday',
2182 'join_date' => 'yesterday - 1 year',
2183 ));
2184 $contribution = $this->callAPISuccess('contribution', 'create', array(
2185 'domain_id' => 1,
2186 'contact_id' => $contactID,
2187 'receive_date' => date('Ymd'),
2188 'total_amount' => 100.00,
2189 'financial_type_id' => 1,
2190 'payment_instrument_id' => 'Credit Card',
2191 'non_deductible_amount' => 10.00,
2192 'trxn_id' => 'jdhfi88',
2193 'invoice_id' => 'djfhiewuyr',
2194 'source' => 'SSF',
2195 'contribution_status_id' => 2,
2196 'contribution_page_id' => $this->_ids['contribution_page'],
2197 'api.membership_payment.create' => array('membership_id' => $membership['id']),
2198 ));
2199
2200 $this->callAPISuccess('line_item', 'create', array(
2201 'entity_id' => $contribution['id'],
2202 'entity_table' => 'civicrm_contribution',
2203 'contribution_id' => $contribution['id'],
2204 'price_field_id' => $this->_ids['price_field'][0],
2205 'qty' => 1,
2206 'unit_price' => 20,
2207 'line_total' => 20,
2208 'financial_type_id' => 1,
2209 'price_field_value_id' => $priceFieldValueID,
2210 ));
2211 $this->_ids['contact'] = $contactID;
2212 $this->_ids['contribution'] = $contribution['id'];
2213 $this->_ids['membership'] = $membership['id'];
2214 }
2215
2216 /**
2217 * Test sending a mail via the API.
2218 */
2219 public function testSendMail() {
2220 $mut = new CiviMailUtils($this, TRUE);
2221 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
2222 $this->callAPISuccess('contribution', 'sendconfirmation', array(
2223 'id' => $contribution['id'],
2224 'receipt_from_email' => 'api@civicrm.org',
2225 )
2226 );
2227 $mut->checkMailLog(array(
2228 '$ 100.00',
2229 'Contribution Information',
2230 'Please print this confirmation for your records',
2231 ), array(
2232 'Event',
2233 )
2234 );
2235 $mut->stop();
2236 }
2237
2238 /**
2239 * Test sending a mail via the API.
2240 */
2241 public function testSendMailEvent() {
2242 $mut = new CiviMailUtils($this, TRUE);
2243 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
2244 $event = $this->eventCreate(array(
2245 'is_email_confirm' => 1,
2246 'confirm_from_email' => 'test@civicrm.org',
2247 ));
2248 $this->_eventID = $event['id'];
2249 $participantParams = array(
2250 'contact_id' => $this->_individualId,
2251 'event_id' => $this->_eventID,
2252 'status_id' => 1,
2253 'role_id' => 1,
2254 // to ensure it matches later on
2255 'register_date' => '2007-07-21 00:00:00',
2256 'source' => 'Online Event Registration: API Testing',
2257
2258 );
2259 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
2260 $this->callAPISuccess('participant_payment', 'create', array(
2261 'participant_id' => $participant['id'],
2262 'contribution_id' => $contribution['id'],
2263 ));
2264 $this->callAPISuccess('contribution', 'sendconfirmation', array(
2265 'id' => $contribution['id'],
2266 'receipt_from_email' => 'api@civicrm.org',
2267 )
2268 );
2269
2270 $mut->checkMailLog(array(
2271 'Annual CiviCRM meet',
2272 'Event',
2273 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
2274 ), array()
2275 );
2276 $mut->stop();
2277 }
2278
2279 /**
2280 * This function does a GET & compares the result against the $params.
2281 *
2282 * Use as a double check on Creates.
2283 *
2284 * @param array $params
2285 * @param int $id
2286 * @param bool $delete
2287 */
2288 public function contributionGetnCheck($params, $id, $delete = TRUE) {
2289
2290 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
2291 'id' => $id,
2292
2293 ));
2294
2295 if ($delete) {
2296 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
2297 }
2298 $this->assertAPISuccess($contribution, 0);
2299 $values = $contribution['values'][$contribution['id']];
2300 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
2301 // this is not returned in id format
2302 unset($params['payment_instrument_id']);
2303 $params['contribution_source'] = $params['source'];
2304 unset($params['source']);
2305 foreach ($params as $key => $value) {
2306 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
2307 }
2308 }
2309
2310 /**
2311 * Create a pending contribution & linked pending participant record (along with an event).
2312 */
2313 public function createPendingParticipantContribution() {
2314 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'));
2315 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6));
2316 $this->_ids['participant'] = $participantID;
2317 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
2318 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2319 $this->callAPISuccess('participant_payment', 'create', array(
2320 'contribution_id' => $contribution['id'],
2321 'participant_id' => $participantID,
2322 ));
2323 $this->callAPISuccess('line_item', 'get', array(
2324 'entity_id' => $contribution['id'],
2325 'entity_table' => 'civicrm_contribution',
2326 'api.line_item.create' => array(
2327 'entity_id' => $participantID,
2328 'entity_table' => 'civicrm_participant',
2329 ),
2330 ));
2331 return $contribution['id'];
2332 }
2333
2334 /**
2335 * Get financial transaction amount.
2336 *
2337 * @param int $contId
2338 *
2339 * @return null|string
2340 */
2341 public function _getFinancialTrxnAmount($contId) {
2342 $query = "SELECT
2343 SUM( ft.total_amount ) AS total
2344 FROM civicrm_financial_trxn AS ft
2345 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
2346 WHERE ceft.entity_table = 'civicrm_contribution'
2347 AND ceft.entity_id = {$contId}";
2348
2349 $result = CRM_Core_DAO::singleValueQuery($query);
2350 return $result;
2351 }
2352
2353 /**
2354 * @param int $contId
2355 *
2356 * @return null|string
2357 */
2358 public function _getFinancialItemAmount($contId) {
2359 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
2360 $query = "SELECT
2361 SUM(amount)
2362 FROM civicrm_financial_item
2363 WHERE entity_table = 'civicrm_line_item'
2364 AND entity_id = {$lineItem}";
2365 $result = CRM_Core_DAO::singleValueQuery($query);
2366 return $result;
2367 }
2368
2369 /**
2370 * @param int $contId
2371 * @param $context
2372 */
2373 public function _checkFinancialItem($contId, $context) {
2374 if ($context != 'paylater') {
2375 $params = array(
2376 'entity_id' => $contId,
2377 'entity_table' => 'civicrm_contribution',
2378 );
2379 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($params, TRUE));
2380 $entityParams = array(
2381 'financial_trxn_id' => $trxn['financial_trxn_id'],
2382 'entity_table' => 'civicrm_financial_item',
2383 );
2384 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
2385 $params = array(
2386 'id' => $entityTrxn['entity_id'],
2387 );
2388 }
2389 if ($context == 'paylater') {
2390 $lineItems = CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution');
2391 foreach ($lineItems as $key => $item) {
2392 $params = array(
2393 'entity_id' => $key,
2394 'entity_table' => 'civicrm_line_item',
2395 );
2396 $compareParams = array('status_id' => 1);
2397 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
2398 }
2399 }
2400 elseif ($context == 'refund') {
2401 $compareParams = array(
2402 'status_id' => 1,
2403 'financial_account_id' => 1,
2404 'amount' => -100,
2405 );
2406 }
2407 elseif ($context == 'cancelPending') {
2408 $compareParams = array(
2409 'status_id' => 3,
2410 'financial_account_id' => 1,
2411 'amount' => -100,
2412 );
2413 }
2414 elseif ($context == 'changeFinancial') {
2415 $lineKey = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
2416 $params = array(
2417 'entity_id' => $lineKey,
2418 'amount' => -100,
2419 );
2420 $compareParams = array(
2421 'financial_account_id' => 1,
2422 );
2423 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
2424 $params = array(
2425 'financial_account_id' => 3,
2426 'entity_id' => $lineKey,
2427 );
2428 $compareParams = array(
2429 'amount' => 100,
2430 );
2431 }
2432 if ($context != 'paylater') {
2433 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
2434 }
2435 }
2436
2437 /**
2438 * Check financial transaction.
2439 *
2440 * @todo break this down into sensible functions - most calls to it only use a few lines out of the big if.
2441 *
2442 * @param array $contribution
2443 * @param string $context
2444 * @param int $instrumentId
2445 * @param array $extraParams
2446 */
2447 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL, $extraParams = array()) {
2448 $trxnParams = array(
2449 'entity_id' => $contribution['id'],
2450 'entity_table' => 'civicrm_contribution',
2451 );
2452 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($trxnParams, TRUE));
2453 $params = array(
2454 'id' => $trxn['financial_trxn_id'],
2455 );
2456 if ($context == 'payLater') {
2457 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
2458 $compareParams = array(
2459 'status_id' => 1,
2460 'from_financial_account_id' => CRM_Contribute_PseudoConstant::financialAccountType($contribution['financial_type_id'], $relationTypeId),
2461 );
2462 }
2463 elseif ($context == 'refund') {
2464 $compareParams = array(
2465 'to_financial_account_id' => 6,
2466 'total_amount' => -100,
2467 'status_id' => 7,
2468 'trxn_date' => '2015-01-01 09:00:00',
2469 'trxn_id' => 'the refund',
2470 );
2471 }
2472 elseif ($context == 'cancelPending') {
2473 $compareParams = array(
2474 'to_financial_account_id' => 7,
2475 'total_amount' => -100,
2476 'status_id' => 3,
2477 );
2478 }
2479 elseif ($context == 'changeFinancial' || $context == 'paymentInstrument') {
2480 $entityParams = array(
2481 'entity_id' => $contribution['id'],
2482 'entity_table' => 'civicrm_contribution',
2483 'amount' => -100,
2484 );
2485 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
2486 $trxnParams1 = array(
2487 'id' => $trxn['financial_trxn_id'],
2488 );
2489 $compareParams = array(
2490 'total_amount' => -100,
2491 'status_id' => 1,
2492 );
2493 if ($context == 'paymentInstrument') {
2494 $compareParams += array(
2495 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount(4),
2496 'payment_instrument_id' => 4,
2497 );
2498 }
2499 else {
2500 $compareParams['to_financial_account_id'] = 12;
2501 }
2502 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, array_merge($compareParams, $extraParams));
2503 $compareParams['total_amount'] = 100;
2504 if ($context == 'paymentInstrument') {
2505 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($instrumentId);
2506 $compareParams['payment_instrument_id'] = $instrumentId;
2507 }
2508 else {
2509 $compareParams['to_financial_account_id'] = 12;
2510 }
2511 }
2512
2513 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, array_merge($compareParams, $extraParams));
2514 }
2515
2516 /**
2517 * @return mixed
2518 */
2519 public function _addPaymentInstrument() {
2520 $gId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
2521 $optionParams = array(
2522 'option_group_id' => $gId,
2523 'label' => 'Test Card',
2524 'name' => 'Test Card',
2525 'value' => '6',
2526 'weight' => '6',
2527 'is_active' => 1,
2528 );
2529 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
2530 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
2531 $financialParams = array(
2532 'entity_table' => 'civicrm_option_value',
2533 'entity_id' => $optionValue['id'],
2534 'account_relationship' => $relationTypeId,
2535 'financial_account_id' => 7,
2536 );
2537 CRM_Financial_BAO_FinancialTypeAccount::add($financialParams, CRM_Core_DAO::$_nullArray);
2538 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
2539 return $optionValue['values'][$optionValue['id']]['value'];
2540 }
2541
2542 /**
2543 * @param array $params
2544 * @param $context
2545 */
2546 public function _checkFinancialRecords($params, $context) {
2547 $entityParams = array(
2548 'entity_id' => $params['id'],
2549 'entity_table' => 'civicrm_contribution',
2550 );
2551 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $params['id']));
2552 $this->assertEquals($contribution['total_amount'] - $contribution['fee_amount'], $contribution['net_amount']);
2553 if ($context == 'pending') {
2554 $trxn = CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams);
2555 $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
2556 return;
2557 }
2558 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
2559 $trxnParams = array(
2560 'id' => $trxn['financial_trxn_id'],
2561 );
2562 if ($context != 'online' && $context != 'payLater') {
2563 $compareParams = array(
2564 'to_financial_account_id' => 6,
2565 'total_amount' => 100,
2566 'status_id' => 1,
2567 );
2568 }
2569 if ($context == 'feeAmount') {
2570 $compareParams['fee_amount'] = 50;
2571 }
2572 elseif ($context == 'online') {
2573 $compareParams = array(
2574 'to_financial_account_id' => 12,
2575 'total_amount' => 100,
2576 'status_id' => 1,
2577 );
2578 }
2579 elseif ($context == 'payLater') {
2580 $compareParams = array(
2581 'to_financial_account_id' => 7,
2582 'total_amount' => 100,
2583 'status_id' => 2,
2584 );
2585 }
2586 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
2587 $entityParams = array(
2588 'financial_trxn_id' => $trxn['financial_trxn_id'],
2589 'entity_table' => 'civicrm_financial_item',
2590 );
2591 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
2592 $fitemParams = array(
2593 'id' => $entityTrxn['entity_id'],
2594 );
2595 $compareParams = array(
2596 'amount' => 100,
2597 'status_id' => 1,
2598 'financial_account_id' => 1,
2599 );
2600 if ($context == 'payLater') {
2601 $compareParams = array(
2602 'amount' => 100,
2603 'status_id' => 3,
2604 'financial_account_id' => 1,
2605 );
2606 }
2607 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
2608 if ($context == 'feeAmount') {
2609 $maxParams = array(
2610 'entity_id' => $params['id'],
2611 'entity_table' => 'civicrm_contribution',
2612 );
2613 $maxTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($maxParams, TRUE));
2614 $trxnParams = array(
2615 'id' => $maxTrxn['financial_trxn_id'],
2616 );
2617 $compareParams = array(
2618 'to_financial_account_id' => 5,
2619 'from_financial_account_id' => 6,
2620 'total_amount' => 50,
2621 'status_id' => 1,
2622 );
2623 $trxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($params['id'], 'DESC');
2624 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
2625 $fitemParams = array(
2626 'entity_id' => $trxnId['financialTrxnId'],
2627 'entity_table' => 'civicrm_financial_trxn',
2628 );
2629 $compareParams = array(
2630 'amount' => 50,
2631 'status_id' => 1,
2632 'financial_account_id' => 5,
2633 );
2634 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
2635 }
2636 }
2637
2638 /**
2639 * Set up the basic recurring contribution for tests.
2640 *
2641 * @param array $generalParams
2642 * Parameters that can be merged into the recurring AND the contribution.
2643 *
2644 * @param array $recurParams
2645 * Parameters to merge into the recur only.
2646 *
2647 * @return array|int
2648 */
2649 protected function setUpRecurringContribution($generalParams = array(), $recurParams = array()) {
2650 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
2651 'contact_id' => $this->_individualId,
2652 'installments' => '12',
2653 'frequency_interval' => '1',
2654 'amount' => '100',
2655 'contribution_status_id' => 1,
2656 'start_date' => '2012-01-01 00:00:00',
2657 'currency' => 'USD',
2658 'frequency_unit' => 'month',
2659 'payment_processor_id' => $this->paymentProcessorID,
2660 ), $generalParams, $recurParams));
2661 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2662 $this->_params,
2663 array(
2664 'contribution_recur_id' => $contributionRecur['id'],
2665 ), $generalParams)
2666 );
2667 return $originalContribution;
2668 }
2669
2670 /**
2671 * Set up a repeat transaction.
2672 *
2673 * @param array $recurParams
2674 *
2675 * @return array
2676 */
2677 protected function setUpRepeatTransaction($recurParams = array()) {
2678 $paymentProcessorID = $this->paymentProcessorCreate();
2679 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
2680 'contact_id' => $this->_individualId,
2681 'installments' => '12',
2682 'frequency_interval' => '1',
2683 'amount' => '500',
2684 'contribution_status_id' => 1,
2685 'start_date' => '2012-01-01 00:00:00',
2686 'currency' => 'USD',
2687 'frequency_unit' => 'month',
2688 'payment_processor_id' => $paymentProcessorID,
2689 ), $recurParams));
2690 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2691 $this->_params,
2692 array('contribution_recur_id' => $contributionRecur['id']))
2693 );
2694 return $originalContribution;
2695 }
2696
2697 }