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