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