Merge pull request #8365 from saurabhbatra96/CRM-18537
[civicrm-core.git] / tests / phpunit / api / v3 / ContributionTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Test APIv3 civicrm_contribute_* functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contribution
33 * @group headless
34 */
35 class api_v3_ContributionTest extends CiviUnitTestCase {
36
37 /**
38 * Assume empty database with just civicrm_data.
39 */
40 protected $_individualId;
41 protected $_contribution;
42 protected $_financialTypeId = 1;
43 protected $_apiversion;
44 protected $_entity = 'Contribution';
45 public $debug = 0;
46 protected $_params;
47 protected $_ids = array();
48 protected $_pageParams = array();
49 /**
50 * Payment processor ID (dummy processor).
51 *
52 * @var int
53 */
54 protected $paymentProcessorID;
55
56 /**
57 * Parameters to create payment processor.
58 *
59 * @var array
60 */
61 protected $_processorParams = array();
62
63 /**
64 * ID of created event.
65 *
66 * @var int
67 */
68 protected $_eventID;
69
70 /**
71 * @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 * Create test with unique field name on source.
545 */
546 public function testCreateContributionSource() {
547
548 $params = array(
549 'contact_id' => $this->_individualId,
550 'receive_date' => date('Ymd'),
551 'total_amount' => 100.00,
552 'financial_type_id' => $this->_financialTypeId,
553 'payment_instrument_id' => 1,
554 'non_deductible_amount' => 10.00,
555 'fee_amount' => 50.00,
556 'net_amount' => 90.00,
557 'trxn_id' => 12345,
558 'invoice_id' => 67890,
559 'contribution_source' => 'SSF',
560 'contribution_status_id' => 1,
561 );
562
563 $contribution = $this->callAPISuccess('contribution', 'create', $params);
564 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
565 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
566 }
567
568 /**
569 * Create test with unique field name on source.
570 */
571 public function testCreateDefaultNow() {
572
573 $params = $this->_params;
574 unset($params['receive_date']);
575
576 $contribution = $this->callAPISuccess('contribution', 'create', $params);
577 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
578 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date'])));
579 }
580
581 /**
582 * Create test with unique field name on source.
583 */
584 public function testCreateContributionSourceInvalidContact() {
585
586 $params = array(
587 'contact_id' => 999,
588 'receive_date' => date('Ymd'),
589 'total_amount' => 100.00,
590 'financial_type_id' => $this->_financialTypeId,
591 'payment_instrument_id' => 1,
592 'non_deductible_amount' => 10.00,
593 'fee_amount' => 50.00,
594 'net_amount' => 90.00,
595 'trxn_id' => 12345,
596 'invoice_id' => 67890,
597 'contribution_source' => 'SSF',
598 'contribution_status_id' => 1,
599 );
600
601 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
602 }
603
604 public function testCreateContributionSourceInvalidContContact() {
605
606 $params = array(
607 'contribution_contact_id' => 999,
608 'receive_date' => date('Ymd'),
609 'total_amount' => 100.00,
610 'financial_type_id' => $this->_financialTypeId,
611 'payment_instrument_id' => 1,
612 'non_deductible_amount' => 10.00,
613 'fee_amount' => 50.00,
614 'net_amount' => 90.00,
615 'trxn_id' => 12345,
616 'invoice_id' => 67890,
617 'contribution_source' => 'SSF',
618 'contribution_status_id' => 1,
619 );
620
621 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
622 }
623
624 /**
625 * Test note created correctly.
626 */
627 public function testCreateContributionWithNote() {
628 $description = "Demonstrates creating contribution with Note Entity.";
629 $subfile = "ContributionCreateWithNote";
630 $params = array(
631 'contact_id' => $this->_individualId,
632 'receive_date' => '2012-01-01',
633 'total_amount' => 100.00,
634 'financial_type_id' => $this->_financialTypeId,
635 'payment_instrument_id' => 1,
636 'non_deductible_amount' => 10.00,
637 'fee_amount' => 50.00,
638 'net_amount' => 90.00,
639 'trxn_id' => 12345,
640 'invoice_id' => 67890,
641 'source' => 'SSF',
642 'contribution_status_id' => 1,
643 'note' => 'my contribution note',
644 );
645
646 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
647 $result = $this->callAPISuccess('note', 'get', array(
648 'entity_table' => 'civicrm_contribution',
649 'entity_id' => $contribution['id'],
650 'sequential' => 1,
651 ));
652 $this->assertEquals('my contribution note', $result['values'][0]['note']);
653 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
654 }
655
656 public function testCreateContributionWithNoteUniqueNameAliases() {
657 $params = array(
658 'contact_id' => $this->_individualId,
659 'receive_date' => '2012-01-01',
660 'total_amount' => 100.00,
661 'financial_type_id' => $this->_financialTypeId,
662 'payment_instrument_id' => 1,
663 'non_deductible_amount' => 10.00,
664 'fee_amount' => 50.00,
665 'net_amount' => 90.00,
666 'trxn_id' => 12345,
667 'invoice_id' => 67890,
668 'source' => 'SSF',
669 'contribution_status_id' => 1,
670 'contribution_note' => 'my contribution note',
671 );
672
673 $contribution = $this->callAPISuccess('contribution', 'create', $params);
674 $result = $this->callAPISuccess('note', 'get', array(
675 'entity_table' => 'civicrm_contribution',
676 'entity_id' => $contribution['id'],
677 'sequential' => 1,
678 ));
679 $this->assertEquals('my contribution note', $result['values'][0]['note']);
680 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
681 }
682
683 /**
684 * This is the test for creating soft credits.
685 */
686 public function testCreateContributionWithSoftCredit() {
687 $description = "Demonstrates creating contribution with SoftCredit.";
688 $subfile = "ContributionCreateWithSoftCredit";
689 $contact2 = $this->callAPISuccess('Contact', 'create', array(
690 'display_name' => 'superman',
691 'contact_type' => 'Individual',
692 ));
693 $softParams = array(
694 'contact_id' => $contact2['id'],
695 'amount' => 50,
696 'soft_credit_type_id' => 3,
697 );
698
699 $params = $this->_params + array('soft_credit' => array(1 => $softParams));
700 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
701 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
702
703 $this->assertEquals($softParams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
704 $this->assertEquals($softParams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
705 $this->assertEquals($softParams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']);
706
707 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
708 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
709 }
710
711 public function testCreateContributionWithSoftCreditDefaults() {
712 $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type.";
713 $subfile = "ContributionCreateWithSoftCreditDefaults";
714 $contact2 = $this->callAPISuccess('Contact', 'create', array(
715 'display_name' => 'superman',
716 'contact_type' => 'Individual',
717 ));
718 $params = $this->_params + array(
719 'soft_credit_to' => $contact2['id'],
720 );
721 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
722 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
723
724 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
725 // Default soft credit amount = contribution.total_amount
726 $this->assertEquals($this->_params['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
727 $this->assertEquals(CRM_Core_OptionGroup::getDefaultValue("soft_credit_type"), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
728
729 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
730 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
731 }
732
733 public function testCreateContributionWithHonoreeContact() {
734 $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id.";
735 $subfile = "ContributionCreateWithHonoreeContact";
736 $contact2 = $this->callAPISuccess('Contact', 'create', array(
737 'display_name' => 'superman',
738 'contact_type' => 'Individual',
739 ));
740 $params = $this->_params + array(
741 'honor_contact_id' => $contact2['id'],
742 );
743 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
744 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
745
746 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
747 // Default soft credit amount = contribution.total_amount
748 // Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type
749 $this->assertEquals($this->_params['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
750 $this->assertEquals(CRM_Core_OptionGroup::getValue('soft_credit_type', 'in_honor_of', 'name'), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
751
752 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
753 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
754 }
755
756 /**
757 * Test using example code.
758 */
759 public function testContributionCreateExample() {
760 //make sure at least on page exists since there is a truncate in tear down
761 $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
762 require_once 'api/v3/examples/Contribution/Create.php';
763 $result = contribution_create_example();
764 $id = $result['id'];
765 $expectedResult = contribution_create_expectedresult();
766 $this->checkArrayEquals($expectedResult, $result);
767 $this->contributionDelete($id);
768 }
769
770 /**
771 * Function tests that additional financial records are created when fee amount is recorded.
772 */
773 public function testCreateContributionWithFee() {
774 $params = array(
775 'contact_id' => $this->_individualId,
776 'receive_date' => '20120511',
777 'total_amount' => 100.00,
778 'fee_amount' => 50,
779 'financial_type_id' => 1,
780 'trxn_id' => 12345,
781 'invoice_id' => 67890,
782 'source' => 'SSF',
783 'contribution_status_id' => 1,
784 );
785
786 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
787 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
788 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
789 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 50.00);
790 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 50.00);
791 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
792 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
793 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
794 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
795 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
796
797 $lineItems = $this->callAPISuccess('line_item', 'get', array(
798
799 'entity_id' => $contribution['id'],
800 'entity_table' => 'civicrm_contribution',
801 'sequential' => 1,
802 ));
803 $this->assertEquals(1, $lineItems['count']);
804 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
805 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
806 $lineItems = $this->callAPISuccess('line_item', 'get', array(
807
808 'entity_id' => $contribution['id'],
809 'contribution_id' => $contribution['id'],
810 'entity_table' => 'civicrm_contribution',
811 'sequential' => 1,
812 ));
813 $this->assertEquals(1, $lineItems['count']);
814 $this->_checkFinancialRecords($contribution, 'feeAmount');
815 }
816
817
818 /**
819 * Function tests that additional financial records are created when online contribution is created.
820 */
821 public function testCreateContributionOnline() {
822 CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
823 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
824 $this->assertAPISuccess($contributionPage);
825 $params = array(
826 'contact_id' => $this->_individualId,
827 'receive_date' => '20120511',
828 'total_amount' => 100.00,
829 'financial_type_id' => 1,
830 'contribution_page_id' => $contributionPage['id'],
831 'payment_processor' => 1,
832 'trxn_id' => 12345,
833 'invoice_id' => 67890,
834 'source' => 'SSF',
835 'contribution_status_id' => 1,
836
837 );
838
839 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
840 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
841 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
842 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
843 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
844 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
845 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
846 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
847 $this->_checkFinancialRecords($contribution, 'online');
848 }
849
850 /**
851 * Check handling of financial type.
852 *
853 * In the interests of removing financial type / contribution type checks from
854 * legacy format function lets test that the api is doing this for us
855 */
856 public function testCreateInvalidFinancialType() {
857 $params = $this->_params;
858 $params['financial_type_id'] = 99999;
859 $this->callAPIFailure($this->_entity, 'create', $params, "'99999' is not a valid option for field financial_type_id");
860 }
861
862 /**
863 * Check handling of financial type.
864 *
865 * In the interests of removing financial type / contribution type checks from
866 * legacy format function lets test that the api is doing this for us
867 */
868 public function testValidNamedFinancialType() {
869 $params = $this->_params;
870 $params['financial_type_id'] = 'Donation';
871 $this->callAPISuccess($this->_entity, 'create', $params);
872 }
873
874 /**
875 * Tests that additional financial records are created.
876 *
877 * Checks when online contribution with pay later option is created
878 */
879 public function testCreateContributionPayLaterOnline() {
880 CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
881 $this->_pageParams['is_pay_later'] = 1;
882 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
883 $this->assertAPISuccess($contributionPage);
884 $params = array(
885 'contact_id' => $this->_individualId,
886 'receive_date' => '20120511',
887 'total_amount' => 100.00,
888 'financial_type_id' => 1,
889 'contribution_page_id' => $contributionPage['id'],
890 'trxn_id' => 12345,
891 'is_pay_later' => 1,
892 'invoice_id' => 67890,
893 'source' => 'SSF',
894 'contribution_status_id' => 2,
895
896 );
897
898 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
899 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
900 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
901 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
902 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
903 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
904 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
905 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
906 $this->_checkFinancialRecords($contribution, 'payLater');
907 }
908
909 /**
910 * Function tests that additional financial records are created for online contribution with pending option.
911 */
912 public function testCreateContributionPendingOnline() {
913 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
914 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
915 $this->assertAPISuccess($contributionPage);
916 $params = array(
917 'contact_id' => $this->_individualId,
918 'receive_date' => '20120511',
919 'total_amount' => 100.00,
920 'financial_type_id' => 1,
921 'contribution_page_id' => $contributionPage['id'],
922 'trxn_id' => 12345,
923 'invoice_id' => 67890,
924 'source' => 'SSF',
925 'contribution_status_id' => 2,
926 );
927
928 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
929 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
930 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
931 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
932 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
933 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
934 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
935 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
936 $this->_checkFinancialRecords($contribution, 'pending');
937 }
938
939 /**
940 * Test that BAO defaults work.
941 */
942 public function testCreateBAODefaults() {
943 unset($this->_params['contribution_source_id'], $this->_params['payment_instrument_id']);
944 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
945 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
946 'id' => $contribution['id'],
947 'api.contribution.delete' => 1,
948 ));
949 $this->assertEquals(1, $contribution['contribution_status_id']);
950 $this->assertEquals('Check', $contribution['payment_instrument']);
951 }
952
953 /**
954 * Function tests that line items, financial records are updated when contribution amount is changed.
955 */
956 public function testCreateUpdateContributionChangeTotal() {
957 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
958 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
959
960 'entity_id' => $contribution['id'],
961 'entity_table' => 'civicrm_contribution',
962 'sequential' => 1,
963 'return' => 'line_total',
964 ));
965 $this->assertEquals('100.00', $lineItems);
966 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
967 // Financial trxn SUM = 100 + 5 (fee)
968 $this->assertEquals('105.00', $trxnAmount);
969 $newParams = array(
970
971 'id' => $contribution['id'],
972 'total_amount' => '125',
973 );
974 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
975
976 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
977
978 'entity_id' => $contribution['id'],
979 'entity_table' => 'civicrm_contribution',
980 'sequential' => 1,
981 'return' => 'line_total',
982 ));
983
984 $this->assertEquals('125.00', $lineItems);
985 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
986
987 // Financial trxn SUM = 125 + 5 (fee).
988 $this->assertEquals('130.00', $trxnAmount);
989 $this->assertEquals('125.00', $this->_getFinancialItemAmount($contribution['id']));
990 }
991
992 /**
993 * Function tests that line items, financial records are updated when pay later contribution is received.
994 */
995 public function testCreateUpdateContributionPayLater() {
996 $contribParams = array(
997 'contact_id' => $this->_individualId,
998 'receive_date' => '2012-01-01',
999 'total_amount' => 100.00,
1000 'financial_type_id' => $this->_financialTypeId,
1001 'payment_instrument_id' => 1,
1002 'contribution_status_id' => 2,
1003 'is_pay_later' => 1,
1004
1005 );
1006 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1007
1008 $newParams = array_merge($contribParams, array(
1009 'id' => $contribution['id'],
1010 'contribution_status_id' => 1,
1011 )
1012 );
1013 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1014 $contribution = $contribution['values'][$contribution['id']];
1015 $this->assertEquals($contribution['contribution_status_id'], '1');
1016 $this->_checkFinancialItem($contribution['id'], 'paylater');
1017 $this->_checkFinancialTrxn($contribution, 'payLater');
1018 }
1019
1020 /**
1021 * Function tests that financial records are updated when Payment Instrument is changed.
1022 */
1023 public function testCreateUpdateContributionPaymentInstrument() {
1024 $instrumentId = $this->_addPaymentInstrument();
1025 $contribParams = array(
1026 'contact_id' => $this->_individualId,
1027 'total_amount' => 100.00,
1028 'financial_type_id' => $this->_financialTypeId,
1029 'payment_instrument_id' => 4,
1030 'contribution_status_id' => 1,
1031
1032 );
1033 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1034
1035 $newParams = array_merge($contribParams, array(
1036 'id' => $contribution['id'],
1037 'payment_instrument_id' => $instrumentId,
1038 )
1039 );
1040 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1041 $this->assertAPISuccess($contribution);
1042 $this->_checkFinancialTrxn($contribution, 'paymentInstrument', $instrumentId);
1043 }
1044
1045 /**
1046 * Function tests that financial records are added when Contribution is Refunded.
1047 */
1048 public function testCreateUpdateContributionRefund() {
1049 $contributionParams = array(
1050 'contact_id' => $this->_individualId,
1051 'receive_date' => '2012-01-01',
1052 'total_amount' => 100.00,
1053 'financial_type_id' => $this->_financialTypeId,
1054 'payment_instrument_id' => 4,
1055 'contribution_status_id' => 1,
1056 'trxn_id' => 'original_payment',
1057 );
1058 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1059 $newParams = array_merge($contributionParams, array(
1060 'id' => $contribution['id'],
1061 'contribution_status_id' => 'Refunded',
1062 'cancel_date' => '2015-01-01 09:00',
1063 'refund_trxn_id' => 'the refund',
1064 )
1065 );
1066
1067 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1068 $this->_checkFinancialTrxn($contribution, 'refund');
1069 $this->_checkFinancialItem($contribution['id'], 'refund');
1070 $this->assertEquals('original_payment', $this->callAPISuccessGetValue('Contribution', array(
1071 'id' => $contribution['id'],
1072 'return' => 'trxn_id',
1073 )));
1074 }
1075
1076 /**
1077 * Refund a contribution for a financial type with a contra account.
1078 *
1079 * CRM-17951 the contra account is a financial account with a relationship to a
1080 * financial type. It is not always configured but should be reflected
1081 * in the financial_trxn & financial_item table if it is.
1082 */
1083 public function testCreateUpdateChargebackContributionDefaultAccount() {
1084 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
1085 $this->callAPISuccess('Contribution', 'create', array(
1086 'id' => $contribution['id'],
1087 'contribution_status_id' => 'Chargeback',
1088 ));
1089 $this->callAPISuccessGetSingle('Contribution', array('contribution_status_id' => 'Chargeback'));
1090
1091 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1092 'contribution_id' => $contribution['id'],
1093 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1094 ));
1095 $this->assertEquals(1, $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1096 $this->callAPISuccessGetSingle('FinancialTrxn', array(
1097 'total_amount' => -100,
1098 'status_id' => 'Chargeback',
1099 'to_financial_account_id' => 6,
1100 ));
1101 }
1102
1103 /**
1104 * Refund a contribution for a financial type with a contra account.
1105 *
1106 * CRM-17951 the contra account is a financial account with a relationship to a
1107 * financial type. It is not always configured but should be reflected
1108 * in the financial_trxn & financial_item table if it is.
1109 */
1110 public function testCreateUpdateChargebackContributionCustomAccount() {
1111 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
1112 'name' => 'Chargeback Account',
1113 'is_active' => TRUE,
1114 ));
1115
1116 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', array(
1117 'entity_id' => $this->_financialTypeId,
1118 'entity_table' => 'civicrm_financial_type',
1119 'account_relationship' => 'Chargeback Account is',
1120 'financial_account_id' => 'Chargeback Account',
1121 ));
1122
1123 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
1124 $this->callAPISuccess('Contribution', 'create', array(
1125 'id' => $contribution['id'],
1126 'contribution_status_id' => 'Chargeback',
1127 ));
1128 $this->callAPISuccessGetSingle('Contribution', array('contribution_status_id' => 'Chargeback'));
1129
1130 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1131 'contribution_id' => $contribution['id'],
1132 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1133 ));
1134 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1135
1136 $this->callAPISuccess('Contribution', 'delete', array('id' => $contribution['id']));
1137 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialAccount['id']));
1138 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
1139 }
1140
1141 /**
1142 * Refund a contribution for a financial type with a contra account.
1143 *
1144 * CRM-17951 the contra account is a financial account with a relationship to a
1145 * financial type. It is not always configured but should be reflected
1146 * in the financial_trxn & financial_item table if it is.
1147 */
1148 public function testCreateUpdateRefundContributionConfiguredContraAccount() {
1149 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
1150 'name' => 'Refund Account',
1151 'is_active' => TRUE,
1152 ));
1153
1154 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', array(
1155 'entity_id' => $this->_financialTypeId,
1156 'entity_table' => 'civicrm_financial_type',
1157 'account_relationship' => 'Credit/Contra Revenue Account is',
1158 'financial_account_id' => 'Refund Account',
1159 ));
1160
1161 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
1162 $this->callAPISuccess('Contribution', 'create', array(
1163 'id' => $contribution['id'],
1164 'contribution_status_id' => 'Refunded',
1165 ));
1166
1167 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1168 'contribution_id' => $contribution['id'],
1169 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1170 ));
1171 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1172
1173 $this->callAPISuccess('Contribution', 'delete', array('id' => $contribution['id']));
1174 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialAccount['id']));
1175 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
1176 }
1177
1178 /**
1179 * Function tests that trxn_id is set when passed in.
1180 *
1181 * Here we ensure that the civicrm_financial_trxn.trxn_id & the civicrm_contribution.trxn_id are set
1182 * when trxn_id is passed in.
1183 */
1184 public function testCreateUpdateContributionRefundTrxnIDPassedIn() {
1185 $contributionParams = array(
1186 'contact_id' => $this->_individualId,
1187 'receive_date' => '2012-01-01',
1188 'total_amount' => 100.00,
1189 'financial_type_id' => $this->_financialTypeId,
1190 'payment_instrument_id' => 4,
1191 'contribution_status_id' => 1,
1192 'trxn_id' => 'original_payment',
1193 );
1194 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1195 $newParams = array_merge($contributionParams, array(
1196 'id' => $contribution['id'],
1197 'contribution_status_id' => 'Refunded',
1198 'cancel_date' => '2015-01-01 09:00',
1199 'trxn_id' => 'the refund',
1200 )
1201 );
1202
1203 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1204 $this->_checkFinancialTrxn($contribution, 'refund');
1205 $this->_checkFinancialItem($contribution['id'], 'refund');
1206 $this->assertEquals('the refund', $this->callAPISuccessGetValue('Contribution', array(
1207 'id' => $contribution['id'],
1208 'return' => 'trxn_id',
1209 )));
1210 }
1211
1212 /**
1213 * Function tests that trxn_id is set when passed in.
1214 *
1215 * Here we ensure that the civicrm_contribution.trxn_id is set
1216 * when trxn_id is passed in but if refund_trxn_id is different then that
1217 * is kept for the refund transaction.
1218 */
1219 public function testCreateUpdateContributionRefundRefundAndTrxnIDPassedIn() {
1220 $contributionParams = array(
1221 'contact_id' => $this->_individualId,
1222 'receive_date' => '2012-01-01',
1223 'total_amount' => 100.00,
1224 'financial_type_id' => $this->_financialTypeId,
1225 'payment_instrument_id' => 4,
1226 'contribution_status_id' => 1,
1227 'trxn_id' => 'original_payment',
1228 );
1229 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1230 $newParams = array_merge($contributionParams, array(
1231 'id' => $contribution['id'],
1232 'contribution_status_id' => 'Refunded',
1233 'cancel_date' => '2015-01-01 09:00',
1234 'trxn_id' => 'cont id',
1235 'refund_trxn_id' => 'the refund',
1236 )
1237 );
1238
1239 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1240 $this->_checkFinancialTrxn($contribution, 'refund');
1241 $this->_checkFinancialItem($contribution['id'], 'refund');
1242 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1243 'id' => $contribution['id'],
1244 'return' => 'trxn_id',
1245 )));
1246 }
1247
1248 /**
1249 * Function tests that refund_trxn_id is set when passed in empty.
1250 *
1251 * Here we ensure that the civicrm_contribution.trxn_id is set
1252 * when trxn_id is passed in but if refund_trxn_id isset but empty then that
1253 * is kept for the refund transaction.
1254 */
1255 public function testCreateUpdateContributionRefundRefundNullTrxnIDPassedIn() {
1256 $contributionParams = array(
1257 'contact_id' => $this->_individualId,
1258 'receive_date' => '2012-01-01',
1259 'total_amount' => 100.00,
1260 'financial_type_id' => $this->_financialTypeId,
1261 'payment_instrument_id' => 4,
1262 'contribution_status_id' => 1,
1263 'trxn_id' => 'original_payment',
1264 );
1265 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1266 $newParams = array_merge($contributionParams, array(
1267 'id' => $contribution['id'],
1268 'contribution_status_id' => 'Refunded',
1269 'cancel_date' => '2015-01-01 09:00',
1270 'trxn_id' => 'cont id',
1271 'refund_trxn_id' => '',
1272 )
1273 );
1274
1275 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1276 $this->_checkFinancialTrxn($contribution, 'refund', NULL, array('trxn_id' => NULL));
1277 $this->_checkFinancialItem($contribution['id'], 'refund');
1278 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1279 'id' => $contribution['id'],
1280 'return' => 'trxn_id',
1281 )));
1282 }
1283
1284 /**
1285 * Function tests invalid contribution status change.
1286 */
1287 public function testCreateUpdateContributionInValidStatusChange() {
1288 $contribParams = array(
1289 'contact_id' => 1,
1290 'receive_date' => '2012-01-01',
1291 'total_amount' => 100.00,
1292 'financial_type_id' => 1,
1293 'payment_instrument_id' => 1,
1294 'contribution_status_id' => 1,
1295 );
1296 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1297 $newParams = array_merge($contribParams, array(
1298 'id' => $contribution['id'],
1299 'contribution_status_id' => 2,
1300 )
1301 );
1302 $this->callAPIFailure('contribution', 'create', $newParams, ts('Cannot change contribution status from Completed to Pending.'));
1303
1304 }
1305
1306 /**
1307 * Function tests that financial records are added when Pending Contribution is Canceled.
1308 */
1309 public function testCreateUpdateContributionCancelPending() {
1310 $contribParams = array(
1311 'contact_id' => $this->_individualId,
1312 'receive_date' => '2012-01-01',
1313 'total_amount' => 100.00,
1314 'financial_type_id' => $this->_financialTypeId,
1315 'payment_instrument_id' => 1,
1316 'contribution_status_id' => 2,
1317 'is_pay_later' => 1,
1318
1319 );
1320 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1321 $newParams = array_merge($contribParams, array(
1322 'id' => $contribution['id'],
1323 'contribution_status_id' => 3,
1324 )
1325 );
1326 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1327 $this->_checkFinancialTrxn($contribution, 'cancelPending');
1328 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1329 }
1330
1331 /**
1332 * Function tests that financial records are added when Financial Type is Changed.
1333 */
1334 public function testCreateUpdateContributionChangeFinancialType() {
1335 $contribParams = array(
1336 'contact_id' => $this->_individualId,
1337 'receive_date' => '2012-01-01',
1338 'total_amount' => 100.00,
1339 'financial_type_id' => 1,
1340 'payment_instrument_id' => 1,
1341 'contribution_status_id' => 1,
1342
1343 );
1344 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1345 $newParams = array_merge($contribParams, array(
1346 'id' => $contribution['id'],
1347 'financial_type_id' => 3,
1348 )
1349 );
1350 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1351 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1352 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1353 }
1354
1355 /**
1356 * Test that update does not change status id CRM-15105.
1357 */
1358 public function testCreateUpdateWithoutChangingPendingStatus() {
1359 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params, array('contribution_status_id' => 2)));
1360 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
1361 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1362 'id' => $contribution['id'],
1363 'api.contribution.delete' => 1,
1364 ));
1365 $this->assertEquals(2, $contribution['contribution_status_id']);
1366 }
1367
1368 /**
1369 * Test Updating a Contribution.
1370 *
1371 * CHANGE: we require the API to do an incremental update
1372 */
1373 public function testCreateUpdateContribution() {
1374
1375 $contributionID = $this->contributionCreate(array(
1376 'contact_id' => $this->_individualId,
1377 'trxn_id' => 212355,
1378 'financial_type_id' => $this->_financialTypeId,
1379 'invoice_id' => 'old_invoice',
1380 ));
1381 $old_params = array(
1382 'contribution_id' => $contributionID,
1383 );
1384 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1385 $this->assertEquals($original['id'], $contributionID);
1386 //set up list of old params, verify
1387
1388 //This should not be required on update:
1389 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1390 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1391 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1392 $old_source = $original['values'][$contributionID]['contribution_source'];
1393
1394 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1395 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1396
1397 //check against values in CiviUnitTestCase::createContribution()
1398 $this->assertEquals($old_contact_id, $this->_individualId);
1399 $this->assertEquals($old_fee_amount, 5.00);
1400 $this->assertEquals($old_source, 'SSF');
1401 $this->assertEquals($old_trxn_id, 212355);
1402 $this->assertEquals($old_invoice_id, 'old_invoice');
1403 $params = array(
1404 'id' => $contributionID,
1405 'contact_id' => $this->_individualId,
1406 'total_amount' => 110.00,
1407 'financial_type_id' => $this->_financialTypeId,
1408 'non_deductible_amount' => 10.00,
1409 'net_amount' => 100.00,
1410 'contribution_status_id' => 1,
1411 'note' => 'Donating for Nobel Cause',
1412
1413 );
1414
1415 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1416
1417 $new_params = array(
1418 'contribution_id' => $contribution['id'],
1419
1420 );
1421 $contribution = $this->callAPISuccess('contribution', 'get', $new_params);
1422
1423 $this->assertEquals($contribution['values'][$contributionID]['contact_id'], $this->_individualId);
1424 $this->assertEquals($contribution['values'][$contributionID]['total_amount'], 110.00);
1425 $this->assertEquals($contribution['values'][$contributionID]['financial_type_id'], $this->_financialTypeId);
1426 $this->assertEquals($contribution['values'][$contributionID]['instrument_id'], $old_payment_instrument);
1427 $this->assertEquals($contribution['values'][$contributionID]['non_deductible_amount'], 10.00);
1428 $this->assertEquals($contribution['values'][$contributionID]['fee_amount'], $old_fee_amount);
1429 $this->assertEquals($contribution['values'][$contributionID]['net_amount'], 100.00);
1430 $this->assertEquals($contribution['values'][$contributionID]['trxn_id'], $old_trxn_id);
1431 $this->assertEquals($contribution['values'][$contributionID]['invoice_id'], $old_invoice_id);
1432 $this->assertEquals($contribution['values'][$contributionID]['contribution_source'], $old_source);
1433 $this->assertEquals($contribution['values'][$contributionID]['contribution_status'], 'Completed');
1434 $params = array(
1435 'contribution_id' => $contributionID,
1436
1437 );
1438 $result = $this->callAPISuccess('contribution', 'delete', $params);
1439 $this->assertAPISuccess($result);
1440 }
1441
1442 ///////////////// civicrm_contribution_delete methods
1443
1444 /**
1445 * Attempt (but fail) to delete a contribution without parameters.
1446 */
1447 public function testDeleteEmptyParamsContribution() {
1448 $params = array();
1449 $this->callAPIFailure('contribution', 'delete', $params);
1450 }
1451
1452 public function testDeleteParamsNotArrayContribution() {
1453 $params = 'contribution_id= 1';
1454 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1455 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1456 }
1457
1458 public function testDeleteWrongParamContribution() {
1459 $params = array(
1460 'contribution_source' => 'SSF',
1461
1462 );
1463 $this->callAPIFailure('contribution', 'delete', $params);
1464 }
1465
1466 public function testDeleteContribution() {
1467 $contributionID = $this->contributionCreate(array(
1468 'contact_id' => $this->_individualId,
1469 'financial_type_id' => $this->_financialTypeId,
1470 ));
1471 $params = array(
1472 'id' => $contributionID,
1473 );
1474 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__, __FILE__);
1475 }
1476
1477 /**
1478 * Test civicrm_contribution_search with empty params.
1479 *
1480 * All available contributions expected.
1481 */
1482 public function testSearchEmptyParams() {
1483 $params = array();
1484
1485 $p = array(
1486 'contact_id' => $this->_individualId,
1487 'receive_date' => date('Ymd'),
1488 'total_amount' => 100.00,
1489 'financial_type_id' => $this->_financialTypeId,
1490 'non_deductible_amount' => 10.00,
1491 'fee_amount' => 5.00,
1492 'net_amount' => 95.00,
1493 'trxn_id' => 23456,
1494 'invoice_id' => 78910,
1495 'source' => 'SSF',
1496 'contribution_status_id' => 1,
1497
1498 );
1499 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1500
1501 $result = $this->callAPISuccess('contribution', 'get', $params);
1502 // We're taking the first element.
1503 $res = $result['values'][$contribution['id']];
1504
1505 $this->assertEquals($p['contact_id'], $res['contact_id']);
1506 $this->assertEquals($p['total_amount'], $res['total_amount']);
1507 $this->assertEquals($p['financial_type_id'], $res['financial_type_id']);
1508 $this->assertEquals($p['net_amount'], $res['net_amount']);
1509 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1510 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1511 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1512 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1513 $this->assertEquals($p['source'], $res['contribution_source']);
1514 // contribution_status_id = 1 => Completed
1515 $this->assertEquals('Completed', $res['contribution_status']);
1516
1517 $this->contributionDelete($contribution['id']);
1518 }
1519
1520 /**
1521 * Test civicrm_contribution_search. Success expected.
1522 */
1523 public function testSearch() {
1524 $p1 = array(
1525 'contact_id' => $this->_individualId,
1526 'receive_date' => date('Ymd'),
1527 'total_amount' => 100.00,
1528 'financial_type_id' => $this->_financialTypeId,
1529 'non_deductible_amount' => 10.00,
1530 'contribution_status_id' => 1,
1531
1532 );
1533 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1534
1535 $p2 = array(
1536 'contact_id' => $this->_individualId,
1537 'receive_date' => date('Ymd'),
1538 'total_amount' => 200.00,
1539 'financial_type_id' => $this->_financialTypeId,
1540 'non_deductible_amount' => 20.00,
1541 'trxn_id' => 5454565,
1542 'invoice_id' => 1212124,
1543 'fee_amount' => 50.00,
1544 'net_amount' => 60.00,
1545 'contribution_status_id' => 2,
1546
1547 );
1548 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1549
1550 $params = array(
1551 'contribution_id' => $contribution2['id'],
1552
1553 );
1554 $result = $this->callAPISuccess('contribution', 'get', $params);
1555 $res = $result['values'][$contribution2['id']];
1556
1557 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1558 $this->assertEquals($p2['total_amount'], $res['total_amount']);
1559 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id']);
1560 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1561 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1562 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1563 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1564 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
1565 // contribution_status_id = 2 => Pending
1566 $this->assertEquals('Pending', $res['contribution_status']);
1567
1568 $this->contributionDelete($contribution1['id']);
1569 $this->contributionDelete($contribution2['id']);
1570 }
1571
1572 /**
1573 * Test completing a transaction via the API.
1574 *
1575 * Note that we are creating a logged in user because email goes out from
1576 * that person
1577 */
1578 public function testCompleteTransaction() {
1579 $mut = new CiviMailUtils($this, TRUE);
1580 $this->swapMessageTemplateForTestTemplate();
1581 $this->createLoggedInUser();
1582 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1583 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1584 $this->callAPISuccess('contribution', 'completetransaction', array(
1585 'id' => $contribution['id'],
1586 ));
1587 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
1588 $this->assertEquals('SSF', $contribution['contribution_source']);
1589 $this->assertEquals('Completed', $contribution['contribution_status']);
1590 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
1591 $mut->checkMailLog(array(
1592 'email:::anthony_anderson@civicrm.org',
1593 'is_monetary:::1',
1594 'amount:::100.00',
1595 'currency:::USD',
1596 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])),
1597 "receipt_date:::\n",
1598 'contributeMode:::notify',
1599 'title:::Contribution',
1600 'displayName:::Mr. Anthony Anderson II',
1601 ));
1602 $mut->stop();
1603 $this->revertTemplateToReservedTemplate();
1604 }
1605
1606 /**
1607 * Test to ensure mail is sent on chosing pay later
1608 */
1609 public function testpayLater() {
1610 $mut = new CiviMailUtils($this, TRUE);
1611 $this->swapMessageTemplateForTestTemplate();
1612 $this->createLoggedInUser();
1613
1614 // create contribution page first
1615 $contributionPageParams = array(
1616 'title' => 'Help Support CiviCRM!',
1617 'financial_type_id' => 1,
1618 'is_monetary' => TRUE,
1619 'is_pay_later' => 1,
1620 'is_quick_config' => TRUE,
1621 'pay_later_text' => 'I will send payment by check',
1622 'pay_later_receipt' => 'This is a pay later reciept',
1623 'is_allow_other_amount' => 1,
1624 'min_amount' => 10.00,
1625 'max_amount' => 10000.00,
1626 'goal_amount' => 100000.00,
1627 'is_email_receipt' => 1,
1628 'is_active' => 1,
1629 'amount_block_is_active' => 1,
1630 'currency' => 'USD',
1631 'is_billing_required' => 0,
1632 );
1633 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', $contributionPageParams);
1634
1635 // submit form values
1636 $priceSet = $this->callAPISuccess('price_set', 'getsingle', array('name' => 'default_contribution_amount'));
1637 $params = array(
1638 'id' => $contributionPageResult['id'],
1639 'contact_id' => $this->_individualId,
1640 'email-5' => 'anthony_anderson@civicrm.org',
1641 'payment_processor_id' => 0,
1642 'amount' => 100.00,
1643 'tax_amount' => '',
1644 'currencyID' => 'USD',
1645 'is_pay_later' => 1,
1646 'invoiceID' => 'f28e1ddc86f8c4a0ff5bcf46393e4bc8',
1647 'is_quick_config' => 1,
1648 'description' => 'Online Contribution: Help Support CiviCRM!',
1649 'price_set_id' => $priceSet['id'],
1650 );
1651 $this->callAPISuccess('contribution_page', 'submit', $params);
1652
1653 $mut->checkMailLog(array(
1654 'is_pay_later:::1',
1655 'email:::anthony_anderson@civicrm.org',
1656 'pay_later_receipt:::' . $contributionPageParams['pay_later_receipt'],
1657 'displayName:::Mr. Anthony Anderson II',
1658 'contributionPageId:::' . $contributionPageResult['id'],
1659 'title:::' . $contributionPageParams['title'],
1660 ));
1661 $mut->stop();
1662 $this->revertTemplateToReservedTemplate();
1663 }
1664
1665
1666 /**
1667 * Test to check whether contact billing address is used when no contribution address
1668 */
1669 public function testBillingAddress() {
1670 $mut = new CiviMailUtils($this, TRUE);
1671 $this->swapMessageTemplateForTestTemplate();
1672 $this->createLoggedInUser();
1673
1674 //Scenario 1: When Contact don't have any address
1675 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1676 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1677 $this->callAPISuccess('contribution', 'completetransaction', array(
1678 'id' => $contribution['id'],
1679 ));
1680 $mut->checkMailLog(array(
1681 'address:::',
1682 ));
1683
1684 // Scenario 2: Contribution using address
1685 $address = $this->callAPISuccess('address', 'create', array(
1686 'street_address' => 'contribution billing st',
1687 'location_type_id' => 2,
1688 'contact_id' => $this->_params['contact_id'],
1689 ));
1690 $params = array_merge($this->_params, array(
1691 'contribution_status_id' => 2,
1692 'address_id' => $address['id'],
1693 )
1694 );
1695 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1696 $this->callAPISuccess('contribution', 'completetransaction', array(
1697 'id' => $contribution['id'],
1698 ));
1699 $mut->checkMailLog(array(
1700 'address:::contribution billing st',
1701 ));
1702
1703 // Scenario 3: Contribution wtth no address but contact has a billing address
1704 $this->callAPISuccess('address', 'create', array(
1705 'id' => $address['id'],
1706 'street_address' => 'is billing st',
1707 'contact_id' => $this->_params['contact_id'],
1708 ));
1709 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1710 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1711 $this->callAPISuccess('contribution', 'completetransaction', array(
1712 'id' => $contribution['id'],
1713 ));
1714 $mut->checkMailLog(array(
1715 'address:::is billing st',
1716 ));
1717
1718 $mut->stop();
1719 $this->revertTemplateToReservedTemplate();
1720 }
1721
1722 /**
1723 * Test completing a transaction via the API.
1724 *
1725 * Note that we are creating a logged in user because email goes out from
1726 * that person
1727 */
1728 public function testCompleteTransactionFeeAmount() {
1729 $this->createLoggedInUser();
1730 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1731 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1732 $this->callAPISuccess('contribution', 'completetransaction', array(
1733 'id' => $contribution['id'],
1734 'fee_amount' => '.56',
1735 'trxn_id' => '7778888',
1736 ));
1737 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'], 'sequential' => 1));
1738 $this->assertEquals('Completed', $contribution['contribution_status']);
1739 $this->assertEquals('7778888', $contribution['trxn_id']);
1740 $this->assertEquals('.56', $contribution['fee_amount']);
1741 $this->assertEquals('99.44', $contribution['net_amount']);
1742 }
1743
1744 /**
1745 * Test repeat contribution successfully creates line items.
1746 */
1747 public function testRepeatTransaction() {
1748 $originalContribution = $this->setUpRepeatTransaction();
1749
1750 $this->callAPISuccess('contribution', 'repeattransaction', array(
1751 'original_contribution_id' => $originalContribution['id'],
1752 'contribution_status_id' => 'Completed',
1753 'trxn_id' => uniqid(),
1754 ));
1755 $lineItemParams = array(
1756 'entity_id' => $originalContribution['id'],
1757 'sequential' => 1,
1758 'return' => array(
1759 'entity_table',
1760 'qty',
1761 'unit_price',
1762 'line_total',
1763 'label',
1764 'financial_type_id',
1765 'deductible_amount',
1766 'price_field_value_id',
1767 'price_field_id',
1768 ),
1769 );
1770 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1771 'entity_id' => $originalContribution['id'],
1772 )));
1773 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1774 'entity_id' => $originalContribution['id'] + 1,
1775 )));
1776 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
1777 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1778 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
1779
1780 $this->quickCleanUpFinancialEntities();
1781 }
1782
1783 /**
1784 * Test repeat contribution successfully creates line items.
1785 */
1786 public function testRepeatTransactionIsTest() {
1787 $this->_params['is_test'] = 1;
1788 $originalContribution = $this->setUpRepeatTransaction(array('is_test' => 1));
1789
1790 $this->callAPISuccess('contribution', 'repeattransaction', array(
1791 'original_contribution_id' => $originalContribution['id'],
1792 'contribution_status_id' => 'Completed',
1793 'trxn_id' => uniqid(),
1794 ));
1795 $this->callAPISuccessGetCount('Contribution', array('contribution_test' => 1), 2);
1796 }
1797
1798 /**
1799 * Test repeat contribution passed in status.
1800 */
1801 public function testRepeatTransactionPassedInStatus() {
1802 $originalContribution = $this->setUpRepeatTransaction();
1803
1804 $this->callAPISuccess('contribution', 'repeattransaction', array(
1805 'original_contribution_id' => $originalContribution['id'],
1806 'contribution_status_id' => 'Pending',
1807 'trxn_id' => uniqid(),
1808 ));
1809 $this->callAPISuccessGetCount('Contribution', array('contribution_status_id' => 2), 1);
1810 }
1811
1812 /**
1813 * Test repeat contribution accepts recur_id instead of original_contribution_id.
1814 */
1815 public function testRepeatTransactionAcceptRecurID() {
1816 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1817 'contact_id' => $this->_individualId,
1818 'installments' => '12',
1819 'frequency_interval' => '1',
1820 'amount' => '100',
1821 'contribution_status_id' => 1,
1822 'start_date' => '2012-01-01 00:00:00',
1823 'currency' => 'USD',
1824 'frequency_unit' => 'month',
1825 'payment_processor_id' => $this->paymentProcessorID,
1826 ));
1827 $this->callAPISuccess('contribution', 'create', array_merge(
1828 $this->_params,
1829 array('contribution_recur_id' => $contributionRecur['id']))
1830 );
1831
1832 $this->callAPISuccess('contribution', 'repeattransaction', array(
1833 'contribution_recur_id' => $contributionRecur['id'],
1834 'contribution_status_id' => 'Completed',
1835 'trxn_id' => uniqid(),
1836 ));
1837
1838 $this->quickCleanUpFinancialEntities();
1839 }
1840
1841 /**
1842 * CRM-16397 test appropriate action if total amount has changed for single line items.
1843 */
1844 public function testRepeatTransactionAlteredAmount() {
1845 $paymentProcessorID = $this->paymentProcessorCreate();
1846 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1847 'contact_id' => $this->_individualId,
1848 'installments' => '12',
1849 'frequency_interval' => '1',
1850 'amount' => '500',
1851 'contribution_status_id' => 1,
1852 'start_date' => '2012-01-01 00:00:00',
1853 'currency' => 'USD',
1854 'frequency_unit' => 'month',
1855 'payment_processor_id' => $paymentProcessorID,
1856 ));
1857 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
1858 $this->_params,
1859 array(
1860 'contribution_recur_id' => $contributionRecur['id'],
1861 ))
1862 );
1863
1864 $this->callAPISuccess('contribution', 'repeattransaction', array(
1865 'original_contribution_id' => $originalContribution['id'],
1866 'contribution_status_id' => 'Completed',
1867 'trxn_id' => uniqid(),
1868 'total_amount' => '400',
1869 'fee_amount' => 50,
1870 ));
1871 $lineItemParams = array(
1872 'entity_id' => $originalContribution['id'],
1873 'sequential' => 1,
1874 'return' => array(
1875 'entity_table',
1876 'qty',
1877 'unit_price',
1878 'line_total',
1879 'label',
1880 'financial_type_id',
1881 'deductible_amount',
1882 'price_field_value_id',
1883 'price_field_id',
1884 ),
1885 );
1886 $this->callAPISuccessGetSingle('contribution', array(
1887 'total_amount' => 400,
1888 'fee_amount' => 50,
1889 'net_amount' => 350,
1890 ));
1891 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1892 'entity_id' => $originalContribution['id'],
1893 )));
1894 $expectedLineItem = array_merge(
1895 $lineItem1['values'][0], array(
1896 'line_total' => '400.00',
1897 'unit_price' => '400.00',
1898 )
1899 );
1900
1901 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1902 'entity_id' => $originalContribution['id'] + 1,
1903 )));
1904 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
1905 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1906 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
1907 }
1908
1909 /**
1910 * CRM-17718 test appropriate action if financial type has changed for single line items.
1911 */
1912 public function testRepeatTransactionPassedInFinancialType() {
1913 $originalContribution = $this->setUpRecurringContribution();
1914
1915 $this->callAPISuccess('contribution', 'repeattransaction', array(
1916 'original_contribution_id' => $originalContribution['id'],
1917 'contribution_status_id' => 'Completed',
1918 'trxn_id' => uniqid(),
1919 'financial_type_id' => 2,
1920 ));
1921 $lineItemParams = array(
1922 'entity_id' => $originalContribution['id'],
1923 'sequential' => 1,
1924 'return' => array(
1925 'entity_table',
1926 'qty',
1927 'unit_price',
1928 'line_total',
1929 'label',
1930 'financial_type_id',
1931 'deductible_amount',
1932 'price_field_value_id',
1933 'price_field_id',
1934 ),
1935 );
1936
1937 $this->callAPISuccessGetSingle('contribution', array(
1938 'total_amount' => 100,
1939 'financial_type_id' => 2,
1940 ));
1941 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1942 'entity_id' => $originalContribution['id'],
1943 )));
1944 $expectedLineItem = array_merge(
1945 $lineItem1['values'][0], array(
1946 'line_total' => '100.00',
1947 'unit_price' => '100.00',
1948 'financial_type_id' => 2,
1949 )
1950 );
1951
1952 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1953 'entity_id' => $originalContribution['id'] + 1,
1954 )));
1955 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
1956 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1957 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
1958 }
1959
1960 /**
1961 * CRM-17718 test appropriate action if financial type has changed for single line items.
1962 */
1963 public function testRepeatTransactionUpdatedFinancialType() {
1964 $originalContribution = $this->setUpRecurringContribution(array(), array('financial_type_id' => 2));
1965
1966 $this->callAPISuccess('contribution', 'repeattransaction', array(
1967 'contribution_recur_id' => $originalContribution['id'],
1968 'contribution_status_id' => 'Completed',
1969 'trxn_id' => uniqid(),
1970 ));
1971 $lineItemParams = array(
1972 'entity_id' => $originalContribution['id'],
1973 'sequential' => 1,
1974 'return' => array(
1975 'entity_table',
1976 'qty',
1977 'unit_price',
1978 'line_total',
1979 'label',
1980 'financial_type_id',
1981 'deductible_amount',
1982 'price_field_value_id',
1983 'price_field_id',
1984 ),
1985 );
1986
1987 $this->callAPISuccessGetSingle('contribution', array(
1988 'total_amount' => 100,
1989 'financial_type_id' => 2,
1990 ));
1991 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1992 'entity_id' => $originalContribution['id'],
1993 )));
1994 $expectedLineItem = array_merge(
1995 $lineItem1['values'][0], array(
1996 'line_total' => '100.00',
1997 'unit_price' => '100.00',
1998 'financial_type_id' => 2,
1999 )
2000 );
2001
2002 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2003 'entity_id' => $originalContribution['id'] + 1,
2004 )));
2005 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2006 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2007 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2008 }
2009
2010 /**
2011 * CRM-16397 test appropriate action if campaign has been passed in.
2012 */
2013 public function testRepeatTransactionPassedInCampaign() {
2014 $paymentProcessorID = $this->paymentProcessorCreate();
2015 $campaignID = $this->campaignCreate();
2016 $campaignID2 = $this->campaignCreate();
2017 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2018 'contact_id' => $this->_individualId,
2019 'installments' => '12',
2020 'frequency_interval' => '1',
2021 'amount' => '100',
2022 'contribution_status_id' => 1,
2023 'start_date' => '2012-01-01 00:00:00',
2024 'currency' => 'USD',
2025 'frequency_unit' => 'month',
2026 'payment_processor_id' => $paymentProcessorID,
2027 ));
2028 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2029 $this->_params,
2030 array(
2031 'contribution_recur_id' => $contributionRecur['id'],
2032 'campaign_id' => $campaignID,
2033 ))
2034 );
2035
2036 $this->callAPISuccess('contribution', 'repeattransaction', array(
2037 'original_contribution_id' => $originalContribution['id'],
2038 'contribution_status_id' => 'Completed',
2039 'trxn_id' => uniqid(),
2040 'campaign_id' => $campaignID2,
2041 ));
2042
2043 $this->callAPISuccessGetSingle('contribution', array(
2044 'total_amount' => 100,
2045 'campaign_id' => $campaignID2,
2046 ));
2047 }
2048
2049 /**
2050 * CRM-17718 campaign stored on contribution recur gets priority.
2051 *
2052 * This reflects the fact we permit people to update them.
2053 */
2054 public function testRepeatTransactionUpdatedCampaign() {
2055 $paymentProcessorID = $this->paymentProcessorCreate();
2056 $campaignID = $this->campaignCreate();
2057 $campaignID2 = $this->campaignCreate();
2058 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2059 'contact_id' => $this->_individualId,
2060 'installments' => '12',
2061 'frequency_interval' => '1',
2062 'amount' => '100',
2063 'contribution_status_id' => 1,
2064 'start_date' => '2012-01-01 00:00:00',
2065 'currency' => 'USD',
2066 'frequency_unit' => 'month',
2067 'payment_processor_id' => $paymentProcessorID,
2068 'campaign_id' => $campaignID,
2069 ));
2070 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2071 $this->_params,
2072 array(
2073 'contribution_recur_id' => $contributionRecur['id'],
2074 'campaign_id' => $campaignID2,
2075 ))
2076 );
2077
2078 $this->callAPISuccess('contribution', 'repeattransaction', array(
2079 'original_contribution_id' => $originalContribution['id'],
2080 'contribution_status_id' => 'Completed',
2081 'trxn_id' => uniqid(),
2082 ));
2083
2084 $this->callAPISuccessGetSingle('contribution', array(
2085 'total_amount' => 100,
2086 'campaign_id' => $campaignID,
2087 ));
2088 }
2089
2090 /**
2091 * Test completing a transaction does not 'mess' with net amount (CRM-15960).
2092 */
2093 public function testCompleteTransactionNetAmountOK() {
2094 $this->createLoggedInUser();
2095 $params = array_merge($this->_params, array('contribution_status_id' => 2));
2096 unset($params['net_amount']);
2097 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2098 $this->callAPISuccess('contribution', 'completetransaction', array(
2099 'id' => $contribution['id'],
2100 ));
2101 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
2102 $this->assertEquals('Completed', $contribution['contribution_status']);
2103 $this->assertTrue(($contribution['total_amount'] - $contribution['net_amount']) == $contribution['fee_amount']);
2104 }
2105
2106 /**
2107 * CRM-14151 - Test completing a transaction via the API.
2108 */
2109 public function testCompleteTransactionWithReceiptDateSet() {
2110 $this->swapMessageTemplateForTestTemplate();
2111 $mut = new CiviMailUtils($this, TRUE);
2112 $this->createLoggedInUser();
2113 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
2114 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2115 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id'], 'trxn_date' => date('Y-m-d')));
2116 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
2117 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
2118 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['values'][0]['receive_date'])));
2119 $mut->checkMailLog(array(
2120 'Receipt - Contribution',
2121 'receipt_date:::' . date('Ymd'),
2122 ));
2123 $mut->stop();
2124 $this->revertTemplateToReservedTemplate();
2125 }
2126
2127
2128 /**
2129 * Complete the transaction using the template with all the possible.
2130 */
2131 public function testCompleteTransactionWithTestTemplate() {
2132 $this->swapMessageTemplateForTestTemplate();
2133 $contribution = $this->setUpForCompleteTransaction();
2134 $this->callAPISuccess('contribution', 'completetransaction', array(
2135 'id' => $contribution['id'],
2136 'trxn_date' => date('2011-04-09'),
2137 'trxn_id' => 'kazam',
2138 ));
2139 $receive_date = $this->callAPISuccess('Contribution', 'getvalue', array('id' => $contribution['id'], 'return' => 'receive_date'));
2140 $this->mut->checkMailLog(array(
2141 'email:::anthony_anderson@civicrm.org',
2142 'is_monetary:::1',
2143 'amount:::100.00',
2144 'currency:::USD',
2145 'receive_date:::' . date('Ymd', strtotime($receive_date)),
2146 'receipt_date:::' . date('Ymd'),
2147 'contributeMode:::notify',
2148 'title:::Contribution',
2149 'displayName:::Mr. Anthony Anderson II',
2150 'trxn_id:::kazam',
2151 'contactID:::' . $this->_params['contact_id'],
2152 'contributionID:::' . $contribution['id'],
2153 'financialTypeId:::1',
2154 'financialTypeName:::Donation',
2155 ));
2156 $this->mut->stop();
2157 $this->revertTemplateToReservedTemplate();
2158 }
2159
2160 /**
2161 * Complete the transaction using the template with all the possible.
2162 */
2163 public function testCompleteTransactionContributionPageFromAddress() {
2164 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array(
2165 'receipt_from_name' => 'Mickey Mouse',
2166 'receipt_from_email' => 'mickey@mouse.com',
2167 'title' => "Test Contribution Page",
2168 'financial_type_id' => 1,
2169 'currency' => 'NZD',
2170 'goal_amount' => 50,
2171 'is_pay_later' => 1,
2172 'is_monetary' => TRUE,
2173 'is_email_receipt' => TRUE,
2174 ));
2175 $this->_params['contribution_page_id'] = $contributionPage['id'];
2176 $contribution = $this->setUpForCompleteTransaction();
2177 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id']));
2178 $this->mut->checkMailLog(array(
2179 'mickey@mouse.com',
2180 'Mickey Mouse <',
2181 ));
2182 $this->mut->stop();
2183 }
2184
2185 /**
2186 * Test completing first transaction in a recurring series.
2187 *
2188 * The status should be set to 'in progress' and the next scheduled payment date calculated.
2189 */
2190 public function testCompleteTransactionSetStatusToInProgress() {
2191 $paymentProcessorID = $this->paymentProcessorCreate();
2192 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2193 'contact_id' => $this->_individualId,
2194 'installments' => '12',
2195 'frequency_interval' => '1',
2196 'amount' => '500',
2197 'contribution_status_id' => 'Pending',
2198 'start_date' => '2012-01-01 00:00:00',
2199 'currency' => 'USD',
2200 'frequency_unit' => 'month',
2201 'payment_processor_id' => $paymentProcessorID,
2202 ));
2203 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(
2204 $this->_params,
2205 array(
2206 'contribution_recur_id' => $contributionRecur['id'],
2207 'contribution_status_id' => 'Pending',
2208 ))
2209 );
2210 $this->callAPISuccess('Contribution', 'completetransaction', array('id' => $contribution));
2211 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array(
2212 'id' => $contributionRecur['id'],
2213 'return' => array('next_sched_contribution_date', 'contribution_status_id'),
2214 ));
2215 $this->assertEquals(5, $contributionRecur['contribution_status_id']);
2216 $this->assertEquals(date('Y-m-d 00:00:00', strtotime('+1 month')), $contributionRecur['next_sched_contribution_date']);
2217 }
2218
2219 /**
2220 * Test completing a pledge with the completeTransaction api..
2221 *
2222 * Note that we are creating a logged in user because email goes out from
2223 * that person.
2224 */
2225 public function testCompleteTransactionUpdatePledgePayment() {
2226 $this->swapMessageTemplateForTestTemplate();
2227 $mut = new CiviMailUtils($this, TRUE);
2228 $mut->clearMessages();
2229 $this->createLoggedInUser();
2230 $contributionID = $this->createPendingPledgeContribution();
2231 $this->callAPISuccess('contribution', 'completetransaction', array(
2232 'id' => $contributionID,
2233 'trxn_date' => '1 Feb 2013',
2234 ));
2235 $pledge = $this->callAPISuccessGetSingle('Pledge', array(
2236 'id' => $this->_ids['pledge'],
2237 ));
2238 $this->assertEquals('Completed', $pledge['pledge_status']);
2239
2240 $status = $this->callAPISuccessGetValue('PledgePayment', array(
2241 'pledge_id' => $this->_ids['pledge'],
2242 'return' => 'status_id',
2243 ));
2244 $this->assertEquals(1, $status);
2245 $mut->checkMailLog(array(
2246 'amount:::500.00',
2247 'receive_date:::20130201000000',
2248 "receipt_date:::\n",
2249 ));
2250 $mut->stop();
2251 $this->revertTemplateToReservedTemplate();
2252 }
2253
2254 /**
2255 * Test completing a transaction with an event via the API.
2256 *
2257 * Note that we are creating a logged in user because email goes out from
2258 * that person
2259 */
2260 public function testCompleteTransactionWithParticipantRecord() {
2261 $mut = new CiviMailUtils($this, TRUE);
2262 $mut->clearMessages();
2263 $this->createLoggedInUser();
2264 $contributionID = $this->createPendingParticipantContribution();
2265 $this->callAPISuccess('contribution', 'completetransaction', array(
2266 'id' => $contributionID,
2267 )
2268 );
2269 $participantStatus = $this->callAPISuccessGetValue('participant', array(
2270 'id' => $this->_ids['participant'],
2271 'return' => 'participant_status_id',
2272 ));
2273 $this->assertEquals(1, $participantStatus);
2274 $mut->checkMailLog(array(
2275 'Annual CiviCRM meet',
2276 'Event',
2277 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
2278 ));
2279 $mut->stop();
2280 }
2281
2282 /**
2283 * Test membership is renewed when transaction completed.
2284 */
2285 public function testCompleteTransactionMembershipPriceSet() {
2286 $this->createPriceSetWithPage('membership');
2287 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', array(
2288 'name' => 'Grace',
2289 'return' => 'id')
2290 );
2291 $this->setUpPendingContribution($this->_ids['price_field_value'][0]);
2292 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
2293 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
2294 'membership_id' => $this->_ids['membership'],
2295 ));
2296 $this->assertEquals(1, $logs['count']);
2297 $this->assertEquals($stateOfGrace, $membership['status_id']);
2298 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
2299 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
2300 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
2301 $this->callAPISuccessGetSingle('LineItem', array(
2302 'entity_id' => $this->_ids['membership'],
2303 'entity_table' => 'civicrm_membership',
2304 ));
2305 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
2306 'membership_id' => $this->_ids['membership'],
2307 ));
2308 $this->assertEquals(2, $logs['count']);
2309 $this->assertNotEquals($stateOfGrace, $logs['values'][2]['status_id']);
2310 $this->cleanUpAfterPriceSets();
2311 }
2312
2313 /**
2314 * Test membership is renewed when transaction completed.
2315 */
2316 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
2317 $this->createPriceSetWithPage('membership');
2318 $this->setUpPendingContribution($this->_ids['price_field_value'][1]);
2319 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
2320 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
2321 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
2322 $this->cleanUpAfterPriceSets();
2323 }
2324
2325 public function cleanUpAfterPriceSets() {
2326 $this->quickCleanUpFinancialEntities();
2327 $this->contactDelete($this->_ids['contact']);
2328 }
2329
2330
2331 /**
2332 * Create price set with contribution test for test setup.
2333 *
2334 * This could be merged with 4.5 function setup in api_v3_ContributionPageTest::setUpContributionPage
2335 * on parent class at some point (fn is not in 4.4).
2336 *
2337 * @param $entity
2338 * @param array $params
2339 */
2340 public function createPriceSetWithPage($entity, $params = array()) {
2341 $membershipTypeID = $this->membershipTypeCreate();
2342 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', array(
2343 'title' => "Test Contribution Page",
2344 'financial_type_id' => 1,
2345 'currency' => 'NZD',
2346 'goal_amount' => 50,
2347 'is_pay_later' => 1,
2348 'is_monetary' => TRUE,
2349 'is_email_receipt' => FALSE,
2350 ));
2351 $priceSet = $this->callAPISuccess('price_set', 'create', array(
2352 'is_quick_config' => 0,
2353 'extends' => 'CiviMember',
2354 'financial_type_id' => 1,
2355 'title' => 'my Page',
2356 ));
2357 $priceSetID = $priceSet['id'];
2358
2359 CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
2360 $priceField = $this->callAPISuccess('price_field', 'create', array(
2361 'price_set_id' => $priceSetID,
2362 'label' => 'Goat Breed',
2363 'html_type' => 'Radio',
2364 ));
2365 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
2366 'price_set_id' => $priceSetID,
2367 'price_field_id' => $priceField['id'],
2368 'label' => 'Long Haired Goat',
2369 'amount' => 20,
2370 'financial_type_id' => 'Donation',
2371 'membership_type_id' => $membershipTypeID,
2372 'membership_num_terms' => 1,
2373 )
2374 );
2375 $this->_ids['price_field_value'] = array($priceFieldValue['id']);
2376 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
2377 'price_set_id' => $priceSetID,
2378 'price_field_id' => $priceField['id'],
2379 'label' => 'Shoe-eating Goat',
2380 'amount' => 10,
2381 'financial_type_id' => 'Donation',
2382 'membership_type_id' => $membershipTypeID,
2383 'membership_num_terms' => 2,
2384 )
2385 );
2386 $this->_ids['price_field_value'][] = $priceFieldValue['id'];
2387 $this->_ids['price_set'] = $priceSetID;
2388 $this->_ids['contribution_page'] = $contributionPageResult['id'];
2389 $this->_ids['price_field'] = array($priceField['id']);
2390
2391 $this->_ids['membership_type'] = $membershipTypeID;
2392 }
2393
2394 /**
2395 * Set up a pending transaction with a specific price field id.
2396 *
2397 * @param int $priceFieldValueID
2398 */
2399 public function setUpPendingContribution($priceFieldValueID) {
2400 $contactID = $this->individualCreate();
2401 $membership = $this->callAPISuccess('membership', 'create', array(
2402 'contact_id' => $contactID,
2403 'membership_type_id' => $this->_ids['membership_type'],
2404 'start_date' => 'yesterday - 1 year',
2405 'end_date' => 'yesterday',
2406 'join_date' => 'yesterday - 1 year',
2407 ));
2408 $contribution = $this->callAPISuccess('contribution', 'create', array(
2409 'domain_id' => 1,
2410 'contact_id' => $contactID,
2411 'receive_date' => date('Ymd'),
2412 'total_amount' => 100.00,
2413 'financial_type_id' => 1,
2414 'payment_instrument_id' => 'Credit Card',
2415 'non_deductible_amount' => 10.00,
2416 'trxn_id' => 'jdhfi88',
2417 'invoice_id' => 'djfhiewuyr',
2418 'source' => 'SSF',
2419 'contribution_status_id' => 2,
2420 'contribution_page_id' => $this->_ids['contribution_page'],
2421 'api.membership_payment.create' => array('membership_id' => $membership['id']),
2422 ));
2423
2424 $this->callAPISuccess('line_item', 'create', array(
2425 'entity_id' => $contribution['id'],
2426 'entity_table' => 'civicrm_contribution',
2427 'contribution_id' => $contribution['id'],
2428 'price_field_id' => $this->_ids['price_field'][0],
2429 'qty' => 1,
2430 'unit_price' => 20,
2431 'line_total' => 20,
2432 'financial_type_id' => 1,
2433 'price_field_value_id' => $priceFieldValueID,
2434 ));
2435 $this->_ids['contact'] = $contactID;
2436 $this->_ids['contribution'] = $contribution['id'];
2437 $this->_ids['membership'] = $membership['id'];
2438 }
2439
2440 /**
2441 * Test sending a mail via the API.
2442 */
2443 public function testSendMail() {
2444 $mut = new CiviMailUtils($this, TRUE);
2445 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
2446 $this->callAPISuccess('contribution', 'sendconfirmation', array(
2447 'id' => $contribution['id'],
2448 'receipt_from_email' => 'api@civicrm.org',
2449 )
2450 );
2451 $mut->checkMailLog(array(
2452 '$ 100.00',
2453 'Contribution Information',
2454 'Please print this confirmation for your records',
2455 ), array(
2456 'Event',
2457 )
2458 );
2459 $mut->stop();
2460 }
2461
2462 /**
2463 * Test sending a mail via the API.
2464 */
2465 public function testSendMailEvent() {
2466 $mut = new CiviMailUtils($this, TRUE);
2467 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
2468 $event = $this->eventCreate(array(
2469 'is_email_confirm' => 1,
2470 'confirm_from_email' => 'test@civicrm.org',
2471 ));
2472 $this->_eventID = $event['id'];
2473 $participantParams = array(
2474 'contact_id' => $this->_individualId,
2475 'event_id' => $this->_eventID,
2476 'status_id' => 1,
2477 'role_id' => 1,
2478 // to ensure it matches later on
2479 'register_date' => '2007-07-21 00:00:00',
2480 'source' => 'Online Event Registration: API Testing',
2481
2482 );
2483 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
2484 $this->callAPISuccess('participant_payment', 'create', array(
2485 'participant_id' => $participant['id'],
2486 'contribution_id' => $contribution['id'],
2487 ));
2488 $this->callAPISuccess('contribution', 'sendconfirmation', array(
2489 'id' => $contribution['id'],
2490 'receipt_from_email' => 'api@civicrm.org',
2491 )
2492 );
2493
2494 $mut->checkMailLog(array(
2495 'Annual CiviCRM meet',
2496 'Event',
2497 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
2498 ), array()
2499 );
2500 $mut->stop();
2501 }
2502
2503 /**
2504 * This function does a GET & compares the result against the $params.
2505 *
2506 * Use as a double check on Creates.
2507 *
2508 * @param array $params
2509 * @param int $id
2510 * @param bool $delete
2511 */
2512 public function contributionGetnCheck($params, $id, $delete = TRUE) {
2513
2514 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
2515 'id' => $id,
2516
2517 ));
2518
2519 if ($delete) {
2520 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
2521 }
2522 $this->assertAPISuccess($contribution, 0);
2523 $values = $contribution['values'][$contribution['id']];
2524 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
2525 // this is not returned in id format
2526 unset($params['payment_instrument_id']);
2527 $params['contribution_source'] = $params['source'];
2528 unset($params['source']);
2529 foreach ($params as $key => $value) {
2530 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
2531 }
2532 }
2533
2534 /**
2535 * Create a pending contribution & linked pending pledge record.
2536 */
2537 public function createPendingPledgeContribution() {
2538
2539 $pledgeID = $this->pledgeCreate(array('contact_id' => $this->_individualId, 'installments' => 1, 'amount' => 500));
2540 $this->_ids['pledge'] = $pledgeID;
2541 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params, array(
2542 'contribution_status_id' => 'Pending',
2543 'total_amount' => 500,
2544 ))
2545 );
2546 $paymentID = $this->callAPISuccessGetValue('PledgePayment', array(
2547 'options' => array('limit' => 1),
2548 'return' => 'id',
2549 ));
2550 $this->callAPISuccess('PledgePayment', 'create', array(
2551 'id' => $paymentID,
2552 'contribution_id' =>
2553 $contribution['id'],
2554 'status_id' => 'Pending',
2555 'scheduled_amount' => 500,
2556 ));
2557
2558 return $contribution['id'];
2559 }
2560
2561 /**
2562 * Create a pending contribution & linked pending participant record (along with an event).
2563 */
2564 public function createPendingParticipantContribution() {
2565 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'));
2566 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6));
2567 $this->_ids['participant'] = $participantID;
2568 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
2569 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2570 $this->callAPISuccess('participant_payment', 'create', array(
2571 'contribution_id' => $contribution['id'],
2572 'participant_id' => $participantID,
2573 ));
2574 $this->callAPISuccess('line_item', 'get', array(
2575 'entity_id' => $contribution['id'],
2576 'entity_table' => 'civicrm_contribution',
2577 'api.line_item.create' => array(
2578 'entity_id' => $participantID,
2579 'entity_table' => 'civicrm_participant',
2580 ),
2581 ));
2582 return $contribution['id'];
2583 }
2584
2585 /**
2586 * Get financial transaction amount.
2587 *
2588 * @param int $contId
2589 *
2590 * @return null|string
2591 */
2592 public function _getFinancialTrxnAmount($contId) {
2593 $query = "SELECT
2594 SUM( ft.total_amount ) AS total
2595 FROM civicrm_financial_trxn AS ft
2596 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
2597 WHERE ceft.entity_table = 'civicrm_contribution'
2598 AND ceft.entity_id = {$contId}";
2599
2600 $result = CRM_Core_DAO::singleValueQuery($query);
2601 return $result;
2602 }
2603
2604 /**
2605 * @param int $contId
2606 *
2607 * @return null|string
2608 */
2609 public function _getFinancialItemAmount($contId) {
2610 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
2611 $query = "SELECT
2612 SUM(amount)
2613 FROM civicrm_financial_item
2614 WHERE entity_table = 'civicrm_line_item'
2615 AND entity_id = {$lineItem}";
2616 $result = CRM_Core_DAO::singleValueQuery($query);
2617 return $result;
2618 }
2619
2620 /**
2621 * @param int $contId
2622 * @param $context
2623 */
2624 public function _checkFinancialItem($contId, $context) {
2625 if ($context != 'paylater') {
2626 $params = array(
2627 'entity_id' => $contId,
2628 'entity_table' => 'civicrm_contribution',
2629 );
2630 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($params, TRUE));
2631 $entityParams = array(
2632 'financial_trxn_id' => $trxn['financial_trxn_id'],
2633 'entity_table' => 'civicrm_financial_item',
2634 );
2635 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
2636 $params = array(
2637 'id' => $entityTrxn['entity_id'],
2638 );
2639 }
2640 if ($context == 'paylater') {
2641 $lineItems = CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution');
2642 foreach ($lineItems as $key => $item) {
2643 $params = array(
2644 'entity_id' => $key,
2645 'entity_table' => 'civicrm_line_item',
2646 );
2647 $compareParams = array('status_id' => 1);
2648 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
2649 }
2650 }
2651 elseif ($context == 'refund') {
2652 $compareParams = array(
2653 'status_id' => 1,
2654 'financial_account_id' => 1,
2655 'amount' => -100,
2656 );
2657 }
2658 elseif ($context == 'cancelPending') {
2659 $compareParams = array(
2660 'status_id' => 3,
2661 'financial_account_id' => 1,
2662 'amount' => -100,
2663 );
2664 }
2665 elseif ($context == 'changeFinancial') {
2666 $lineKey = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
2667 $params = array(
2668 'entity_id' => $lineKey,
2669 'amount' => -100,
2670 );
2671 $compareParams = array(
2672 'financial_account_id' => 1,
2673 );
2674 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
2675 $params = array(
2676 'financial_account_id' => 3,
2677 'entity_id' => $lineKey,
2678 );
2679 $compareParams = array(
2680 'amount' => 100,
2681 );
2682 }
2683 if ($context != 'paylater') {
2684 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
2685 }
2686 }
2687
2688 /**
2689 * Check financial transaction.
2690 *
2691 * @todo break this down into sensible functions - most calls to it only use a few lines out of the big if.
2692 *
2693 * @param array $contribution
2694 * @param string $context
2695 * @param int $instrumentId
2696 * @param array $extraParams
2697 */
2698 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL, $extraParams = array()) {
2699 $trxnParams = array(
2700 'entity_id' => $contribution['id'],
2701 'entity_table' => 'civicrm_contribution',
2702 );
2703 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($trxnParams, TRUE));
2704 $params = array(
2705 'id' => $trxn['financial_trxn_id'],
2706 );
2707 if ($context == 'payLater') {
2708 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
2709 $compareParams = array(
2710 'status_id' => 1,
2711 'from_financial_account_id' => CRM_Contribute_PseudoConstant::financialAccountType($contribution['financial_type_id'], $relationTypeId),
2712 );
2713 }
2714 elseif ($context == 'refund') {
2715 $compareParams = array(
2716 'to_financial_account_id' => 6,
2717 'total_amount' => -100,
2718 'status_id' => 7,
2719 'trxn_date' => '2015-01-01 09:00:00',
2720 'trxn_id' => 'the refund',
2721 );
2722 }
2723 elseif ($context == 'cancelPending') {
2724 $compareParams = array(
2725 'to_financial_account_id' => 7,
2726 'total_amount' => -100,
2727 'status_id' => 3,
2728 );
2729 }
2730 elseif ($context == 'changeFinancial' || $context == 'paymentInstrument') {
2731 $entityParams = array(
2732 'entity_id' => $contribution['id'],
2733 'entity_table' => 'civicrm_contribution',
2734 'amount' => -100,
2735 );
2736 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
2737 $trxnParams1 = array(
2738 'id' => $trxn['financial_trxn_id'],
2739 );
2740 $compareParams = array(
2741 'total_amount' => -100,
2742 'status_id' => 1,
2743 );
2744 if ($context == 'paymentInstrument') {
2745 $compareParams += array(
2746 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount(4),
2747 'payment_instrument_id' => 4,
2748 );
2749 }
2750 else {
2751 $compareParams['to_financial_account_id'] = 12;
2752 }
2753 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, array_merge($compareParams, $extraParams));
2754 $compareParams['total_amount'] = 100;
2755 if ($context == 'paymentInstrument') {
2756 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($instrumentId);
2757 $compareParams['payment_instrument_id'] = $instrumentId;
2758 }
2759 else {
2760 $compareParams['to_financial_account_id'] = 12;
2761 }
2762 }
2763
2764 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, array_merge($compareParams, $extraParams));
2765 }
2766
2767 /**
2768 * @return mixed
2769 */
2770 public function _addPaymentInstrument() {
2771 $gId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
2772 $optionParams = array(
2773 'option_group_id' => $gId,
2774 'label' => 'Test Card',
2775 'name' => 'Test Card',
2776 'value' => '6',
2777 'weight' => '6',
2778 'is_active' => 1,
2779 );
2780 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
2781 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
2782 $financialParams = array(
2783 'entity_table' => 'civicrm_option_value',
2784 'entity_id' => $optionValue['id'],
2785 'account_relationship' => $relationTypeId,
2786 'financial_account_id' => 7,
2787 );
2788 CRM_Financial_BAO_FinancialTypeAccount::add($financialParams, CRM_Core_DAO::$_nullArray);
2789 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
2790 return $optionValue['values'][$optionValue['id']]['value'];
2791 }
2792
2793 /**
2794 * @param array $params
2795 * @param $context
2796 */
2797 public function _checkFinancialRecords($params, $context) {
2798 $entityParams = array(
2799 'entity_id' => $params['id'],
2800 'entity_table' => 'civicrm_contribution',
2801 );
2802 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $params['id']));
2803 $this->assertEquals($contribution['total_amount'] - $contribution['fee_amount'], $contribution['net_amount']);
2804 if ($context == 'pending') {
2805 $trxn = CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams);
2806 $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
2807 return;
2808 }
2809 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
2810 $trxnParams = array(
2811 'id' => $trxn['financial_trxn_id'],
2812 );
2813 if ($context != 'online' && $context != 'payLater') {
2814 $compareParams = array(
2815 'to_financial_account_id' => 6,
2816 'total_amount' => 100,
2817 'status_id' => 1,
2818 );
2819 }
2820 if ($context == 'feeAmount') {
2821 $compareParams['fee_amount'] = 50;
2822 }
2823 elseif ($context == 'online') {
2824 $compareParams = array(
2825 'to_financial_account_id' => 12,
2826 'total_amount' => 100,
2827 'status_id' => 1,
2828 );
2829 }
2830 elseif ($context == 'payLater') {
2831 $compareParams = array(
2832 'to_financial_account_id' => 7,
2833 'total_amount' => 100,
2834 'status_id' => 2,
2835 );
2836 }
2837 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
2838 $entityParams = array(
2839 'financial_trxn_id' => $trxn['financial_trxn_id'],
2840 'entity_table' => 'civicrm_financial_item',
2841 );
2842 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
2843 $fitemParams = array(
2844 'id' => $entityTrxn['entity_id'],
2845 );
2846 $compareParams = array(
2847 'amount' => 100,
2848 'status_id' => 1,
2849 'financial_account_id' => 1,
2850 );
2851 if ($context == 'payLater') {
2852 $compareParams = array(
2853 'amount' => 100,
2854 'status_id' => 3,
2855 'financial_account_id' => 1,
2856 );
2857 }
2858 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
2859 if ($context == 'feeAmount') {
2860 $maxParams = array(
2861 'entity_id' => $params['id'],
2862 'entity_table' => 'civicrm_contribution',
2863 );
2864 $maxTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($maxParams, TRUE));
2865 $trxnParams = array(
2866 'id' => $maxTrxn['financial_trxn_id'],
2867 );
2868 $compareParams = array(
2869 'to_financial_account_id' => 5,
2870 'from_financial_account_id' => 6,
2871 'total_amount' => 50,
2872 'status_id' => 1,
2873 );
2874 $trxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($params['id'], 'DESC');
2875 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
2876 $fitemParams = array(
2877 'entity_id' => $trxnId['financialTrxnId'],
2878 'entity_table' => 'civicrm_financial_trxn',
2879 );
2880 $compareParams = array(
2881 'amount' => 50,
2882 'status_id' => 1,
2883 'financial_account_id' => 5,
2884 );
2885 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
2886 }
2887 }
2888
2889 /**
2890 * Set up the basic recurring contribution for tests.
2891 *
2892 * @param array $generalParams
2893 * Parameters that can be merged into the recurring AND the contribution.
2894 *
2895 * @param array $recurParams
2896 * Parameters to merge into the recur only.
2897 *
2898 * @return array|int
2899 */
2900 protected function setUpRecurringContribution($generalParams = array(), $recurParams = array()) {
2901 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
2902 'contact_id' => $this->_individualId,
2903 'installments' => '12',
2904 'frequency_interval' => '1',
2905 'amount' => '100',
2906 'contribution_status_id' => 1,
2907 'start_date' => '2012-01-01 00:00:00',
2908 'currency' => 'USD',
2909 'frequency_unit' => 'month',
2910 'payment_processor_id' => $this->paymentProcessorID,
2911 ), $generalParams, $recurParams));
2912 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2913 $this->_params,
2914 array(
2915 'contribution_recur_id' => $contributionRecur['id'],
2916 ), $generalParams)
2917 );
2918 return $originalContribution;
2919 }
2920
2921 /**
2922 * Set up a repeat transaction.
2923 *
2924 * @param array $recurParams
2925 *
2926 * @return array
2927 */
2928 protected function setUpRepeatTransaction($recurParams = array()) {
2929 $paymentProcessorID = $this->paymentProcessorCreate();
2930 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
2931 'contact_id' => $this->_individualId,
2932 'installments' => '12',
2933 'frequency_interval' => '1',
2934 'amount' => '500',
2935 'contribution_status_id' => 1,
2936 'start_date' => '2012-01-01 00:00:00',
2937 'currency' => 'USD',
2938 'frequency_unit' => 'month',
2939 'payment_processor_id' => $paymentProcessorID,
2940 ), $recurParams));
2941 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2942 $this->_params,
2943 array('contribution_recur_id' => $contributionRecur['id']))
2944 );
2945 return $originalContribution;
2946 }
2947
2948 /**
2949 * Common set up routine.
2950 *
2951 * @return array
2952 */
2953 protected function setUpForCompleteTransaction() {
2954 $this->mut = new CiviMailUtils($this, TRUE);
2955 $this->createLoggedInUser();
2956 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
2957 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2958 return $contribution;
2959 }
2960
2961 }