Merge pull request #9690 from jitendrapurohit/CRM-19887
[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 /**
2099 * CRM-16397 test appropriate action if total amount has changed for single line items.
2100 */
2101 public function testRepeatTransactionAlteredAmount() {
2102 $paymentProcessorID = $this->paymentProcessorCreate();
2103 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2104 'contact_id' => $this->_individualId,
2105 'installments' => '12',
2106 'frequency_interval' => '1',
2107 'amount' => '500',
2108 'contribution_status_id' => 1,
2109 'start_date' => '2012-01-01 00:00:00',
2110 'currency' => 'USD',
2111 'frequency_unit' => 'month',
2112 'payment_processor_id' => $paymentProcessorID,
2113 ));
2114 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2115 $this->_params,
2116 array(
2117 'contribution_recur_id' => $contributionRecur['id'],
2118 ))
2119 );
2120
2121 $this->callAPISuccess('contribution', 'repeattransaction', array(
2122 'original_contribution_id' => $originalContribution['id'],
2123 'contribution_status_id' => 'Completed',
2124 'trxn_id' => uniqid(),
2125 'total_amount' => '400',
2126 'fee_amount' => 50,
2127 ));
2128
2129 $lineItemParams = array(
2130 'entity_id' => $originalContribution['id'],
2131 'sequential' => 1,
2132 'return' => array(
2133 'entity_table',
2134 'qty',
2135 'unit_price',
2136 'line_total',
2137 'label',
2138 'financial_type_id',
2139 'deductible_amount',
2140 'price_field_value_id',
2141 'price_field_id',
2142 ),
2143 );
2144 $this->callAPISuccessGetSingle('contribution', array(
2145 'total_amount' => 400,
2146 'fee_amount' => 50,
2147 'net_amount' => 350,
2148 ));
2149 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2150 'entity_id' => $originalContribution['id'],
2151 )));
2152 $expectedLineItem = array_merge(
2153 $lineItem1['values'][0], array(
2154 'line_total' => '400.00',
2155 'unit_price' => '400.00',
2156 )
2157 );
2158
2159 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2160 'entity_id' => $originalContribution['id'] + 1,
2161 )));
2162
2163 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2164 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2165 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2166 }
2167
2168 /**
2169 * CRM-17718 test appropriate action if financial type has changed for single line items.
2170 */
2171 public function testRepeatTransactionPassedInFinancialType() {
2172 $originalContribution = $this->setUpRecurringContribution();
2173
2174 $this->callAPISuccess('contribution', 'repeattransaction', array(
2175 'original_contribution_id' => $originalContribution['id'],
2176 'contribution_status_id' => 'Completed',
2177 'trxn_id' => uniqid(),
2178 'financial_type_id' => 2,
2179 ));
2180 $lineItemParams = array(
2181 'entity_id' => $originalContribution['id'],
2182 'sequential' => 1,
2183 'return' => array(
2184 'entity_table',
2185 'qty',
2186 'unit_price',
2187 'line_total',
2188 'label',
2189 'financial_type_id',
2190 'deductible_amount',
2191 'price_field_value_id',
2192 'price_field_id',
2193 ),
2194 );
2195
2196 $this->callAPISuccessGetSingle('contribution', array(
2197 'total_amount' => 100,
2198 'financial_type_id' => 2,
2199 ));
2200 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2201 'entity_id' => $originalContribution['id'],
2202 )));
2203 $expectedLineItem = array_merge(
2204 $lineItem1['values'][0], array(
2205 'line_total' => '100.00',
2206 'unit_price' => '100.00',
2207 'financial_type_id' => 2,
2208 'contribution_type_id' => 2,
2209 )
2210 );
2211 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2212 'entity_id' => $originalContribution['id'] + 1,
2213 )));
2214 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2215 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2216 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2217 }
2218
2219 /**
2220 * CRM-17718 test appropriate action if financial type has changed for single line items.
2221 */
2222 public function testRepeatTransactionUpdatedFinancialType() {
2223 $originalContribution = $this->setUpRecurringContribution(array(), array('financial_type_id' => 2));
2224
2225 $this->callAPISuccess('contribution', 'repeattransaction', array(
2226 'contribution_recur_id' => $originalContribution['id'],
2227 'contribution_status_id' => 'Completed',
2228 'trxn_id' => uniqid(),
2229 ));
2230 $lineItemParams = array(
2231 'entity_id' => $originalContribution['id'],
2232 'sequential' => 1,
2233 'return' => array(
2234 'entity_table',
2235 'qty',
2236 'unit_price',
2237 'line_total',
2238 'label',
2239 'financial_type_id',
2240 'deductible_amount',
2241 'price_field_value_id',
2242 'price_field_id',
2243 ),
2244 );
2245
2246 $this->callAPISuccessGetSingle('contribution', array(
2247 'total_amount' => 100,
2248 'financial_type_id' => 2,
2249 ));
2250 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2251 'entity_id' => $originalContribution['id'],
2252 )));
2253 $expectedLineItem = array_merge(
2254 $lineItem1['values'][0], array(
2255 'line_total' => '100.00',
2256 'unit_price' => '100.00',
2257 'financial_type_id' => 2,
2258 'contribution_type_id' => 2,
2259 )
2260 );
2261
2262 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2263 'entity_id' => $originalContribution['id'] + 1,
2264 )));
2265 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2266 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2267 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2268 }
2269
2270 /**
2271 * CRM-16397 test appropriate action if campaign has been passed in.
2272 */
2273 public function testRepeatTransactionPassedInCampaign() {
2274 $paymentProcessorID = $this->paymentProcessorCreate();
2275 $campaignID = $this->campaignCreate();
2276 $campaignID2 = $this->campaignCreate();
2277 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2278 'contact_id' => $this->_individualId,
2279 'installments' => '12',
2280 'frequency_interval' => '1',
2281 'amount' => '100',
2282 'contribution_status_id' => 1,
2283 'start_date' => '2012-01-01 00:00:00',
2284 'currency' => 'USD',
2285 'frequency_unit' => 'month',
2286 'payment_processor_id' => $paymentProcessorID,
2287 ));
2288 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2289 $this->_params,
2290 array(
2291 'contribution_recur_id' => $contributionRecur['id'],
2292 'campaign_id' => $campaignID,
2293 ))
2294 );
2295
2296 $this->callAPISuccess('contribution', 'repeattransaction', array(
2297 'original_contribution_id' => $originalContribution['id'],
2298 'contribution_status_id' => 'Completed',
2299 'trxn_id' => uniqid(),
2300 'campaign_id' => $campaignID2,
2301 ));
2302
2303 $this->callAPISuccessGetSingle('contribution', array(
2304 'total_amount' => 100,
2305 'campaign_id' => $campaignID2,
2306 ));
2307 }
2308
2309 /**
2310 * CRM-17718 campaign stored on contribution recur gets priority.
2311 *
2312 * This reflects the fact we permit people to update them.
2313 */
2314 public function testRepeatTransactionUpdatedCampaign() {
2315 $paymentProcessorID = $this->paymentProcessorCreate();
2316 $campaignID = $this->campaignCreate();
2317 $campaignID2 = $this->campaignCreate();
2318 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2319 'contact_id' => $this->_individualId,
2320 'installments' => '12',
2321 'frequency_interval' => '1',
2322 'amount' => '100',
2323 'contribution_status_id' => 1,
2324 'start_date' => '2012-01-01 00:00:00',
2325 'currency' => 'USD',
2326 'frequency_unit' => 'month',
2327 'payment_processor_id' => $paymentProcessorID,
2328 'campaign_id' => $campaignID,
2329 ));
2330 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2331 $this->_params,
2332 array(
2333 'contribution_recur_id' => $contributionRecur['id'],
2334 'campaign_id' => $campaignID2,
2335 ))
2336 );
2337
2338 $this->callAPISuccess('contribution', 'repeattransaction', array(
2339 'original_contribution_id' => $originalContribution['id'],
2340 'contribution_status_id' => 'Completed',
2341 'trxn_id' => uniqid(),
2342 ));
2343
2344 $this->callAPISuccessGetSingle('contribution', array(
2345 'total_amount' => 100,
2346 'campaign_id' => $campaignID,
2347 ));
2348 }
2349
2350 /**
2351 * Test completing a transaction does not 'mess' with net amount (CRM-15960).
2352 */
2353 public function testCompleteTransactionNetAmountOK() {
2354 $this->createLoggedInUser();
2355 $params = array_merge($this->_params, array('contribution_status_id' => 2));
2356 unset($params['net_amount']);
2357 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2358 $this->callAPISuccess('contribution', 'completetransaction', array(
2359 'id' => $contribution['id'],
2360 ));
2361 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
2362 $this->assertEquals('Completed', $contribution['contribution_status']);
2363 $this->assertTrue(($contribution['total_amount'] - $contribution['net_amount']) == $contribution['fee_amount']);
2364 }
2365
2366 /**
2367 * CRM-14151 - Test completing a transaction via the API.
2368 */
2369 public function testCompleteTransactionWithReceiptDateSet() {
2370 $this->swapMessageTemplateForTestTemplate();
2371 $mut = new CiviMailUtils($this, TRUE);
2372 $this->createLoggedInUser();
2373 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
2374 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2375 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id'], 'trxn_date' => date('Y-m-d')));
2376 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
2377 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
2378 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['values'][0]['receive_date'])));
2379 $mut->checkMailLog(array(
2380 'Receipt - Contribution',
2381 'receipt_date:::' . date('Ymd'),
2382 ));
2383 $mut->stop();
2384 $this->revertTemplateToReservedTemplate();
2385 }
2386
2387 /**
2388 * CRM-1960 - Test to ensure that completetransaction respects the is_email_receipt setting
2389 */
2390 public function testCompleteTransactionWithEmailReceiptInput() {
2391 // Create a Contribution Page with is_email_receipt = TRUE
2392 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array(
2393 'receipt_from_name' => 'Mickey Mouse',
2394 'receipt_from_email' => 'mickey@mouse.com',
2395 'title' => "Test Contribution Page",
2396 'financial_type_id' => 1,
2397 'currency' => 'CAD',
2398 'is_monetary' => TRUE,
2399 'is_email_receipt' => TRUE,
2400 ));
2401 $this->_params['contribution_page_id'] = $contributionPage['id'];
2402 $params = array_merge($this->_params, array('contribution_status_id' => 2));
2403 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2404 // Complete the transaction overriding is_email_receipt to = FALSE
2405 $this->callAPISuccess('contribution', 'completetransaction', array(
2406 'id' => $contribution['id'],
2407 'trxn_date' => date('2011-04-09'),
2408 'trxn_id' => 'kazam',
2409 'is_email_receipt' => 0,
2410 ));
2411 // Check if a receipt was issued
2412 $receipt_date = $this->callAPISuccess('Contribution', 'getvalue', array('id' => $contribution['id'], 'return' => 'receipt_date'));
2413 $this->assertEquals('', $receipt_date);
2414 }
2415
2416 /**
2417 * Complete the transaction using the template with all the possible.
2418 */
2419 public function testCompleteTransactionWithTestTemplate() {
2420 $this->swapMessageTemplateForTestTemplate();
2421 $contribution = $this->setUpForCompleteTransaction();
2422 $this->callAPISuccess('contribution', 'completetransaction', array(
2423 'id' => $contribution['id'],
2424 'trxn_date' => date('2011-04-09'),
2425 'trxn_id' => 'kazam',
2426 ));
2427 $receive_date = $this->callAPISuccess('Contribution', 'getvalue', array('id' => $contribution['id'], 'return' => 'receive_date'));
2428 $this->mut->checkMailLog(array(
2429 'email:::anthony_anderson@civicrm.org',
2430 'is_monetary:::1',
2431 'amount:::100.00',
2432 'currency:::USD',
2433 'receive_date:::' . date('Ymd', strtotime($receive_date)),
2434 'receipt_date:::' . date('Ymd'),
2435 'contributeMode:::notify',
2436 'title:::Contribution',
2437 'displayName:::Mr. Anthony Anderson II',
2438 'trxn_id:::kazam',
2439 'contactID:::' . $this->_params['contact_id'],
2440 'contributionID:::' . $contribution['id'],
2441 'financialTypeId:::1',
2442 'financialTypeName:::Donation',
2443 ));
2444 $this->mut->stop();
2445 $this->revertTemplateToReservedTemplate();
2446 }
2447
2448 /**
2449 * Complete the transaction using the template with all the possible.
2450 */
2451 public function testCompleteTransactionContributionPageFromAddress() {
2452 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array(
2453 'receipt_from_name' => 'Mickey Mouse',
2454 'receipt_from_email' => 'mickey@mouse.com',
2455 'title' => "Test Contribution Page",
2456 'financial_type_id' => 1,
2457 'currency' => 'NZD',
2458 'goal_amount' => 50,
2459 'is_pay_later' => 1,
2460 'is_monetary' => TRUE,
2461 'is_email_receipt' => TRUE,
2462 ));
2463 $this->_params['contribution_page_id'] = $contributionPage['id'];
2464 $contribution = $this->setUpForCompleteTransaction();
2465 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id']));
2466 $this->mut->checkMailLog(array(
2467 'mickey@mouse.com',
2468 'Mickey Mouse <',
2469 ));
2470 $this->mut->stop();
2471 }
2472
2473 /**
2474 * Test completing first transaction in a recurring series.
2475 *
2476 * The status should be set to 'in progress' and the next scheduled payment date calculated.
2477 */
2478 public function testCompleteTransactionSetStatusToInProgress() {
2479 $paymentProcessorID = $this->paymentProcessorCreate();
2480 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2481 'contact_id' => $this->_individualId,
2482 'installments' => '12',
2483 'frequency_interval' => '1',
2484 'amount' => '500',
2485 'contribution_status_id' => 'Pending',
2486 'start_date' => '2012-01-01 00:00:00',
2487 'currency' => 'USD',
2488 'frequency_unit' => 'month',
2489 'payment_processor_id' => $paymentProcessorID,
2490 ));
2491 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(
2492 $this->_params,
2493 array(
2494 'contribution_recur_id' => $contributionRecur['id'],
2495 'contribution_status_id' => 'Pending',
2496 ))
2497 );
2498 $this->callAPISuccess('Contribution', 'completetransaction', array('id' => $contribution));
2499 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array(
2500 'id' => $contributionRecur['id'],
2501 'return' => array('next_sched_contribution_date', 'contribution_status_id'),
2502 ));
2503 $this->assertEquals(5, $contributionRecur['contribution_status_id']);
2504 $this->assertEquals(date('Y-m-d 00:00:00', strtotime('+1 month')), $contributionRecur['next_sched_contribution_date']);
2505 }
2506
2507 /**
2508 * Test completing a pledge with the completeTransaction api..
2509 *
2510 * Note that we are creating a logged in user because email goes out from
2511 * that person.
2512 */
2513 public function testCompleteTransactionUpdatePledgePayment() {
2514 $this->swapMessageTemplateForTestTemplate();
2515 $mut = new CiviMailUtils($this, TRUE);
2516 $mut->clearMessages();
2517 $this->createLoggedInUser();
2518 $contributionID = $this->createPendingPledgeContribution();
2519 $this->callAPISuccess('contribution', 'completetransaction', array(
2520 'id' => $contributionID,
2521 'trxn_date' => '1 Feb 2013',
2522 ));
2523 $pledge = $this->callAPISuccessGetSingle('Pledge', array(
2524 'id' => $this->_ids['pledge'],
2525 ));
2526 $this->assertEquals('Completed', $pledge['pledge_status']);
2527
2528 $status = $this->callAPISuccessGetValue('PledgePayment', array(
2529 'pledge_id' => $this->_ids['pledge'],
2530 'return' => 'status_id',
2531 ));
2532 $this->assertEquals(1, $status);
2533 $mut->checkMailLog(array(
2534 'amount:::500.00',
2535 'receive_date:::20130201000000',
2536 "receipt_date:::\n",
2537 ));
2538 $mut->stop();
2539 $this->revertTemplateToReservedTemplate();
2540 }
2541
2542 /**
2543 * Test completing a transaction with an event via the API.
2544 *
2545 * Note that we are creating a logged in user because email goes out from
2546 * that person
2547 */
2548 public function testCompleteTransactionWithParticipantRecord() {
2549 $mut = new CiviMailUtils($this, TRUE);
2550 $mut->clearMessages();
2551 $this->createLoggedInUser();
2552 $contributionID = $this->createPendingParticipantContribution();
2553 $this->callAPISuccess('contribution', 'completetransaction', array(
2554 'id' => $contributionID,
2555 )
2556 );
2557 $participantStatus = $this->callAPISuccessGetValue('participant', array(
2558 'id' => $this->_ids['participant'],
2559 'return' => 'participant_status_id',
2560 ));
2561 $this->assertEquals(1, $participantStatus);
2562 $mut->checkMailLog(array(
2563 'Annual CiviCRM meet',
2564 'Event',
2565 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
2566 ));
2567 $mut->stop();
2568 }
2569
2570 /**
2571 * Test membership is renewed when transaction completed.
2572 */
2573 public function testCompleteTransactionMembershipPriceSet() {
2574 $this->createPriceSetWithPage('membership');
2575 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', array(
2576 'name' => 'Grace',
2577 'return' => 'id')
2578 );
2579 $this->setUpPendingContribution($this->_ids['price_field_value'][0]);
2580 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
2581 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
2582 'membership_id' => $this->_ids['membership'],
2583 ));
2584 $this->assertEquals(1, $logs['count']);
2585 $this->assertEquals($stateOfGrace, $membership['status_id']);
2586 $contribution = $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
2587 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
2588 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
2589 $this->callAPISuccessGetSingle('LineItem', array(
2590 'entity_id' => $this->_ids['membership'],
2591 'entity_table' => 'civicrm_membership',
2592 ));
2593 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
2594 'membership_id' => $this->_ids['membership'],
2595 ));
2596 //CRM-19600: Ensure that 'Membership Renewal' activity is created after successful membership regsitration
2597 $activity = $this->callAPISuccess('Activity', 'get', array(
2598 'activity_type_id' => 'Membership Renewal',
2599 'source_record_id' => $contribution['id'],
2600 ));
2601 $this->assertEquals(1, $activity['count']);
2602 $this->assertEquals(2, $logs['count']);
2603 $this->assertNotEquals($stateOfGrace, $logs['values'][2]['status_id']);
2604 $this->cleanUpAfterPriceSets();
2605 }
2606
2607 /**
2608 * Test if renewal activity is create after changing Pending contribution to Completed via offline
2609 */
2610 public function testPendingToCompleteContribution() {
2611 $contributionPage = $this->createPriceSetWithPage('membership');
2612 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', array(
2613 'name' => 'Grace',
2614 'return' => 'id')
2615 );
2616 $this->setUpPendingContribution($this->_ids['price_field_value'][0]);
2617 $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
2618
2619 // change pending contribution to completed
2620 $form = new CRM_Contribute_Form_Contribution();
2621 $error = FALSE;
2622 $form->_params = array(
2623 'id' => $this->_ids['contribution'],
2624 'total_amount' => 20,
2625 'net_amount' => 20,
2626 'fee_amount' => 0,
2627 'financial_type_id' => 1,
2628 'receive_date' => '04/21/2015',
2629 'receive_date_time' => '11:27PM',
2630 'contact_id' => $this->_individualId,
2631 'contribution_status_id' => 1,
2632 'billing_middle_name' => '',
2633 'billing_last_name' => 'Adams',
2634 'billing_street_address-5' => '790L Lincoln St S',
2635 'billing_city-5' => 'Maryknoll',
2636 'billing_state_province_id-5' => 1031,
2637 'billing_postal_code-5' => 10545,
2638 'billing_country_id-5' => 1228,
2639 'frequency_interval' => 1,
2640 'frequency_unit' => 'month',
2641 'installments' => '',
2642 'hidden_AdditionalDetail' => 1,
2643 'hidden_Premium' => 1,
2644 'from_email_address' => '"civi45" <civi45@civicrm.com>',
2645 'receipt_date' => '',
2646 'receipt_date_time' => '',
2647 'payment_processor_id' => $this->paymentProcessorID,
2648 'currency' => 'USD',
2649 'contribution_page_id' => $this->_ids['contribution_page'],
2650 'contribution_mode' => 'membership',
2651 'source' => 'Membership Signup and Renewal',
2652 );
2653 try {
2654 $form->testSubmit($form->_params, CRM_Core_Action::UPDATE);
2655 }
2656 catch (Civi\Payment\Exception\PaymentProcessorException $e) {
2657 $error = TRUE;
2658 }
2659 $activity = $this->callAPISuccess('Activity', 'get', array(
2660 'activity_type_id' => 'Membership Renewal',
2661 'source_record_id' => $this->_ids['contribution'],
2662 ));
2663 $this->assertEquals(1, $activity['count']);
2664 }
2665
2666 /**
2667 * Test membership is renewed when transaction completed.
2668 */
2669 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
2670 $this->createPriceSetWithPage('membership');
2671 $this->setUpPendingContribution($this->_ids['price_field_value'][1]);
2672 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
2673 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
2674 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
2675 $this->cleanUpAfterPriceSets();
2676 }
2677
2678 public function cleanUpAfterPriceSets() {
2679 $this->quickCleanUpFinancialEntities();
2680 $this->contactDelete($this->_ids['contact']);
2681 }
2682
2683 /**
2684 * Set up a pending transaction with a specific price field id.
2685 *
2686 * @param int $priceFieldValueID
2687 */
2688 public function setUpPendingContribution($priceFieldValueID) {
2689 $contactID = $this->individualCreate();
2690 $membership = $this->callAPISuccess('membership', 'create', array(
2691 'contact_id' => $contactID,
2692 'membership_type_id' => $this->_ids['membership_type'],
2693 'start_date' => 'yesterday - 1 year',
2694 'end_date' => 'yesterday',
2695 'join_date' => 'yesterday - 1 year',
2696 ));
2697 $contribution = $this->callAPISuccess('contribution', 'create', array(
2698 'domain_id' => 1,
2699 'contact_id' => $contactID,
2700 'receive_date' => date('Ymd'),
2701 'total_amount' => 20.00,
2702 'financial_type_id' => 1,
2703 'payment_instrument_id' => 'Credit Card',
2704 'non_deductible_amount' => 10.00,
2705 'trxn_id' => 'jdhfi88',
2706 'invoice_id' => 'djfhiewuyr',
2707 'source' => 'SSF',
2708 'contribution_status_id' => 2,
2709 'contribution_page_id' => $this->_ids['contribution_page'],
2710 'api.membership_payment.create' => array('membership_id' => $membership['id']),
2711 ));
2712
2713 $this->callAPISuccess('line_item', 'create', array(
2714 'entity_id' => $contribution['id'],
2715 'entity_table' => 'civicrm_contribution',
2716 'contribution_id' => $contribution['id'],
2717 'price_field_id' => $this->_ids['price_field'][0],
2718 'qty' => 1,
2719 'unit_price' => 20,
2720 'line_total' => 20,
2721 'financial_type_id' => 1,
2722 'price_field_value_id' => $priceFieldValueID,
2723 ));
2724 $this->_ids['contact'] = $contactID;
2725 $this->_ids['contribution'] = $contribution['id'];
2726 $this->_ids['membership'] = $membership['id'];
2727 }
2728
2729 /**
2730 * Test sending a mail via the API.
2731 */
2732 public function testSendMail() {
2733 $mut = new CiviMailUtils($this, TRUE);
2734 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
2735 $this->callAPISuccess('contribution', 'sendconfirmation', array(
2736 'id' => $contribution['id'],
2737 'receipt_from_email' => 'api@civicrm.org',
2738 )
2739 );
2740 $mut->checkMailLog(array(
2741 '$ 100.00',
2742 'Contribution Information',
2743 'Please print this confirmation for your records',
2744 ), array(
2745 'Event',
2746 )
2747 );
2748
2749 $this->checkCreditCardDetails($mut, $contribution['id']);
2750 $mut->stop();
2751 }
2752
2753 /**
2754 * Check credit card details in sent mail via API
2755 *
2756 * @param $mut obj CiviMailUtils instance
2757 * @param int $contributionID Contribution ID
2758 *
2759 */
2760 public function checkCreditCardDetails($mut, $contributionID) {
2761 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
2762 $this->callAPISuccess('contribution', 'sendconfirmation', array(
2763 'id' => $contributionID,
2764 'receipt_from_email' => 'api@civicrm.org',
2765 'payment_processor_id' => $this->paymentProcessorID,
2766 )
2767 );
2768 $mut->checkMailLog(array(
2769 'Credit Card Information', // credit card header
2770 'Billing Name and Address', // billing header
2771 'anthony_anderson@civicrm.org', // billing name
2772 ), array(
2773 'Event',
2774 )
2775 );
2776 }
2777
2778 /**
2779 * Test sending a mail via the API.
2780 */
2781 public function testSendMailEvent() {
2782 $mut = new CiviMailUtils($this, TRUE);
2783 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
2784 $event = $this->eventCreate(array(
2785 'is_email_confirm' => 1,
2786 'confirm_from_email' => 'test@civicrm.org',
2787 ));
2788 $this->_eventID = $event['id'];
2789 $participantParams = array(
2790 'contact_id' => $this->_individualId,
2791 'event_id' => $this->_eventID,
2792 'status_id' => 1,
2793 'role_id' => 1,
2794 // to ensure it matches later on
2795 'register_date' => '2007-07-21 00:00:00',
2796 'source' => 'Online Event Registration: API Testing',
2797
2798 );
2799 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
2800 $this->callAPISuccess('participant_payment', 'create', array(
2801 'participant_id' => $participant['id'],
2802 'contribution_id' => $contribution['id'],
2803 ));
2804 $this->callAPISuccess('contribution', 'sendconfirmation', array(
2805 'id' => $contribution['id'],
2806 'receipt_from_email' => 'api@civicrm.org',
2807 )
2808 );
2809
2810 $mut->checkMailLog(array(
2811 'Annual CiviCRM meet',
2812 'Event',
2813 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
2814 ), array()
2815 );
2816 $mut->stop();
2817 }
2818
2819 /**
2820 * This function does a GET & compares the result against the $params.
2821 *
2822 * Use as a double check on Creates.
2823 *
2824 * @param array $params
2825 * @param int $id
2826 * @param bool $delete
2827 */
2828 public function contributionGetnCheck($params, $id, $delete = TRUE) {
2829
2830 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
2831 'id' => $id,
2832
2833 ));
2834
2835 if ($delete) {
2836 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
2837 }
2838 $this->assertAPISuccess($contribution, 0);
2839 $values = $contribution['values'][$contribution['id']];
2840 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
2841 // this is not returned in id format
2842 unset($params['payment_instrument_id']);
2843 $params['contribution_source'] = $params['source'];
2844 unset($params['source']);
2845 foreach ($params as $key => $value) {
2846 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
2847 }
2848 }
2849
2850 /**
2851 * Create a pending contribution & linked pending pledge record.
2852 */
2853 public function createPendingPledgeContribution() {
2854
2855 $pledgeID = $this->pledgeCreate(array('contact_id' => $this->_individualId, 'installments' => 1, 'amount' => 500));
2856 $this->_ids['pledge'] = $pledgeID;
2857 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params, array(
2858 'contribution_status_id' => 'Pending',
2859 'total_amount' => 500,
2860 ))
2861 );
2862 $paymentID = $this->callAPISuccessGetValue('PledgePayment', array(
2863 'options' => array('limit' => 1),
2864 'return' => 'id',
2865 ));
2866 $this->callAPISuccess('PledgePayment', 'create', array(
2867 'id' => $paymentID,
2868 'contribution_id' =>
2869 $contribution['id'],
2870 'status_id' => 'Pending',
2871 'scheduled_amount' => 500,
2872 ));
2873
2874 return $contribution['id'];
2875 }
2876
2877 /**
2878 * Create a pending contribution & linked pending participant record (along with an event).
2879 */
2880 public function createPendingParticipantContribution() {
2881 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'));
2882 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6));
2883 $this->_ids['participant'] = $participantID;
2884 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
2885 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2886 $this->callAPISuccess('participant_payment', 'create', array(
2887 'contribution_id' => $contribution['id'],
2888 'participant_id' => $participantID,
2889 ));
2890 $this->callAPISuccess('line_item', 'get', array(
2891 'entity_id' => $contribution['id'],
2892 'entity_table' => 'civicrm_contribution',
2893 'api.line_item.create' => array(
2894 'entity_id' => $participantID,
2895 'entity_table' => 'civicrm_participant',
2896 ),
2897 ));
2898 return $contribution['id'];
2899 }
2900
2901 /**
2902 * Get financial transaction amount.
2903 *
2904 * @param int $contId
2905 *
2906 * @return null|string
2907 */
2908 public function _getFinancialTrxnAmount($contId) {
2909 $query = "SELECT
2910 SUM( ft.total_amount ) AS total
2911 FROM civicrm_financial_trxn AS ft
2912 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
2913 WHERE ceft.entity_table = 'civicrm_contribution'
2914 AND ceft.entity_id = {$contId}";
2915
2916 $result = CRM_Core_DAO::singleValueQuery($query);
2917 return $result;
2918 }
2919
2920 /**
2921 * @param int $contId
2922 *
2923 * @return null|string
2924 */
2925 public function _getFinancialItemAmount($contId) {
2926 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
2927 $query = "SELECT
2928 SUM(amount)
2929 FROM civicrm_financial_item
2930 WHERE entity_table = 'civicrm_line_item'
2931 AND entity_id = {$lineItem}";
2932 $result = CRM_Core_DAO::singleValueQuery($query);
2933 return $result;
2934 }
2935
2936 /**
2937 * @param int $contId
2938 * @param $context
2939 */
2940 public function _checkFinancialItem($contId, $context) {
2941 if ($context != 'paylater') {
2942 $params = array(
2943 'entity_id' => $contId,
2944 'entity_table' => 'civicrm_contribution',
2945 );
2946 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($params, TRUE));
2947 $entityParams = array(
2948 'financial_trxn_id' => $trxn['financial_trxn_id'],
2949 'entity_table' => 'civicrm_financial_item',
2950 );
2951 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
2952 $params = array(
2953 'id' => $entityTrxn['entity_id'],
2954 );
2955 }
2956 if ($context == 'paylater') {
2957 $lineItems = CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution');
2958 foreach ($lineItems as $key => $item) {
2959 $params = array(
2960 'entity_id' => $key,
2961 'entity_table' => 'civicrm_line_item',
2962 );
2963 $compareParams = array('status_id' => 1);
2964 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
2965 }
2966 }
2967 elseif ($context == 'refund') {
2968 $compareParams = array(
2969 'status_id' => 1,
2970 'financial_account_id' => 1,
2971 'amount' => -100,
2972 );
2973 }
2974 elseif ($context == 'cancelPending') {
2975 $compareParams = array(
2976 'status_id' => 3,
2977 'financial_account_id' => 1,
2978 'amount' => -100,
2979 );
2980 }
2981 elseif ($context == 'changeFinancial') {
2982 $lineKey = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
2983 $params = array(
2984 'entity_id' => $lineKey,
2985 'amount' => -100,
2986 );
2987 $compareParams = array(
2988 'financial_account_id' => 1,
2989 );
2990 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
2991 $params = array(
2992 'financial_account_id' => 3,
2993 'entity_id' => $lineKey,
2994 );
2995 $compareParams = array(
2996 'amount' => 100,
2997 );
2998 }
2999 if ($context != 'paylater') {
3000 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3001 }
3002 }
3003
3004 /**
3005 * Check financial transaction.
3006 *
3007 * @todo break this down into sensible functions - most calls to it only use a few lines out of the big if.
3008 *
3009 * @param array $contribution
3010 * @param string $context
3011 * @param int $instrumentId
3012 * @param array $extraParams
3013 */
3014 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL, $extraParams = array()) {
3015 $trxnParams = array(
3016 'entity_id' => $contribution['id'],
3017 'entity_table' => 'civicrm_contribution',
3018 );
3019 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($trxnParams, TRUE));
3020 $params = array(
3021 'id' => $trxn['financial_trxn_id'],
3022 );
3023 if ($context == 'payLater') {
3024 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
3025 $compareParams = array(
3026 'status_id' => 1,
3027 'from_financial_account_id' => CRM_Contribute_PseudoConstant::financialAccountType($contribution['financial_type_id'], $relationTypeId),
3028 );
3029 }
3030 elseif ($context == 'refund') {
3031 $compareParams = array(
3032 'to_financial_account_id' => 6,
3033 'total_amount' => -100,
3034 'status_id' => 7,
3035 'trxn_date' => '2015-01-01 09:00:00',
3036 'trxn_id' => 'the refund',
3037 );
3038 }
3039 elseif ($context == 'cancelPending') {
3040 $compareParams = array(
3041 'to_financial_account_id' => 7,
3042 'total_amount' => -100,
3043 'status_id' => 3,
3044 );
3045 }
3046 elseif ($context == 'changeFinancial' || $context == 'paymentInstrument') {
3047 $entityParams = array(
3048 'entity_id' => $contribution['id'],
3049 'entity_table' => 'civicrm_contribution',
3050 'amount' => -100,
3051 );
3052 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
3053 $trxnParams1 = array(
3054 'id' => $trxn['financial_trxn_id'],
3055 );
3056 if (empty($extraParams)) {
3057 $compareParams = array(
3058 'total_amount' => -100,
3059 'status_id' => 1,
3060 );
3061 }
3062 else {
3063 $compareParams = array(
3064 'total_amount' => 100,
3065 'status_id' => 1,
3066 );
3067 }
3068 if ($context == 'paymentInstrument') {
3069 $compareParams += array(
3070 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount(4),
3071 'payment_instrument_id' => 4,
3072 );
3073 }
3074 else {
3075 $compareParams['to_financial_account_id'] = 12;
3076 }
3077 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, array_merge($compareParams, $extraParams));
3078 $compareParams['total_amount'] = 100;
3079 if ($context == 'paymentInstrument') {
3080 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($instrumentId);
3081 $compareParams['payment_instrument_id'] = $instrumentId;
3082 }
3083 else {
3084 $compareParams['to_financial_account_id'] = 12;
3085 }
3086 }
3087
3088 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, array_merge($compareParams, $extraParams));
3089 }
3090
3091 /**
3092 * @return mixed
3093 */
3094 public function _addPaymentInstrument() {
3095 $gId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
3096 $optionParams = array(
3097 'option_group_id' => $gId,
3098 'label' => 'Test Card',
3099 'name' => 'Test Card',
3100 'value' => '6',
3101 'weight' => '6',
3102 'is_active' => 1,
3103 );
3104 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
3105 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
3106 $financialParams = array(
3107 'entity_table' => 'civicrm_option_value',
3108 'entity_id' => $optionValue['id'],
3109 'account_relationship' => $relationTypeId,
3110 'financial_account_id' => 7,
3111 );
3112 CRM_Financial_BAO_FinancialTypeAccount::add($financialParams, CRM_Core_DAO::$_nullArray);
3113 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
3114 return $optionValue['values'][$optionValue['id']]['value'];
3115 }
3116
3117 /**
3118 * Set up the basic recurring contribution for tests.
3119 *
3120 * @param array $generalParams
3121 * Parameters that can be merged into the recurring AND the contribution.
3122 *
3123 * @param array $recurParams
3124 * Parameters to merge into the recur only.
3125 *
3126 * @return array|int
3127 */
3128 protected function setUpRecurringContribution($generalParams = array(), $recurParams = array()) {
3129 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3130 'contact_id' => $this->_individualId,
3131 'installments' => '12',
3132 'frequency_interval' => '1',
3133 'amount' => '100',
3134 'contribution_status_id' => 1,
3135 'start_date' => '2012-01-01 00:00:00',
3136 'currency' => 'USD',
3137 'frequency_unit' => 'month',
3138 'payment_processor_id' => $this->paymentProcessorID,
3139 ), $generalParams, $recurParams));
3140 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3141 $this->_params,
3142 array(
3143 'contribution_recur_id' => $contributionRecur['id'],
3144 ), $generalParams)
3145 );
3146 return $originalContribution;
3147 }
3148
3149 /**
3150 * Set up a repeat transaction.
3151 *
3152 * @param array $recurParams
3153 *
3154 * @return array
3155 */
3156 protected function setUpRepeatTransaction($recurParams = array(), $flag) {
3157 $paymentProcessorID = $this->paymentProcessorCreate();
3158 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3159 'contact_id' => $this->_individualId,
3160 'installments' => '12',
3161 'frequency_interval' => '1',
3162 'amount' => '500',
3163 'contribution_status_id' => 1,
3164 'start_date' => '2012-01-01 00:00:00',
3165 'currency' => 'USD',
3166 'frequency_unit' => 'month',
3167 'payment_processor_id' => $paymentProcessorID,
3168 ), $recurParams));
3169
3170 $originalContribution = '';
3171 if ($flag == 'multiple') {
3172 // CRM-19309 create a contribution + also add in line_items (plural):
3173 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3174 $this->_params,
3175 array(
3176 'contribution_recur_id' => $contributionRecur['id'],
3177 'skipLineItem' => 1,
3178 'api.line_item.create' => array(
3179 array(
3180 'price_field_id' => 1,
3181 'qty' => 2,
3182 'line_total' => '20',
3183 'unit_price' => '10',
3184 'financial_type_id' => 1,
3185 ),
3186 array(
3187 'price_field_id' => 1,
3188 'qty' => 1,
3189 'line_total' => '80',
3190 'unit_price' => '80',
3191 'financial_type_id' => 2,
3192 ),
3193 ),
3194 )
3195 )
3196 );
3197 }
3198 elseif ($flag == 'single') {
3199 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3200 $this->_params,
3201 array('contribution_recur_id' => $contributionRecur['id']))
3202 );
3203 }
3204 $originalContribution['payment_processor_id'] = $paymentProcessorID;
3205 return $originalContribution;
3206 }
3207
3208 /**
3209 * Common set up routine.
3210 *
3211 * @return array
3212 */
3213 protected function setUpForCompleteTransaction() {
3214 $this->mut = new CiviMailUtils($this, TRUE);
3215 $this->createLoggedInUser();
3216 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
3217 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3218 return $contribution;
3219 }
3220
3221 }