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