Merge pull request #11071 from mukeshcompucorp/HW-380-civicase-settings-page
[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
251 // update contribution with invoice number
252 $params = array_merge($params, array(
253 'id' => $contributionID,
254 'invoice_number' => CRM_Utils_Array::value('invoice_prefix', Civi::settings()->get('contribution_invoice_settings')) . "" . $contributionID,
255 ));
256 $contributionID = $this->contributionCreate($params);
257
258 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $contributionID));
259 $this->assertEquals('bouncer', $contribution['check_number']);
260 $this->assertEquals('bouncer', $contribution['contribution_check_number']);
261
262 $fields = CRM_Contribute_BAO_Contribution::fields();
263 // Re-add these 2 to the fields to check. They were locked in but the metadata changed so we
264 // need to specify them.
265 $fields['address_id'] = $fields['contribution_address_id'];
266 $fields['check_number'] = $fields['contribution_check_number'];
267
268 $fieldsLockedIn = array(
269 'contribution_id', 'contribution_contact_id', 'financial_type_id', 'contribution_page_id',
270 'payment_instrument_id', 'receive_date', 'non_deductible_amount', 'total_amount',
271 'fee_amount', 'net_amount', 'trxn_id', 'invoice_id', 'currency', 'cancel_date', 'cancel_reason',
272 'receipt_date', 'thankyou_date', 'contribution_source', 'amount_level', 'contribution_recur_id',
273 'is_test', 'is_pay_later', 'contribution_status_id', 'address_id', 'check_number', 'contribution_campaign_id',
274 'creditnote_id', 'tax_amount', 'revenue_recognition_date', 'decoy',
275 );
276 $missingFields = array_diff($fieldsLockedIn, array_keys($fields));
277 // If any of the locked in fields disappear from the $fields array we need to make sure it is still
278 // covered as the test contract now guarantees them in the return array.
279 $this->assertEquals($missingFields, array(29 => 'decoy'), 'A field which was covered by the test contract has changed.');
280 foreach ($fields as $fieldName => $fieldSpec) {
281 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $contributionID, 'return' => $fieldName));
282 $returnField = $fieldName;
283 if ($returnField == 'contribution_contact_id') {
284 $returnField = 'contact_id';
285 }
286 $this->assertTrue((!empty($contribution[$returnField]) || $contribution[$returnField] === "0"), $returnField);
287 }
288 }
289
290 /**
291 * We need to ensure previous tested behaviour still works as part of the api contract.
292 */
293 public function testGetContributionLegacyBehaviour() {
294 $p = array(
295 'contact_id' => $this->_individualId,
296 'receive_date' => '2010-01-20',
297 'total_amount' => 100.00,
298 'contribution_type_id' => $this->_financialTypeId,
299 'non_deductible_amount' => 10.00,
300 'fee_amount' => 5.00,
301 'net_amount' => 95.00,
302 'trxn_id' => 23456,
303 'invoice_id' => 78910,
304 'source' => 'SSF',
305 'contribution_status_id' => 1,
306 );
307 $this->_contribution = $this->callAPISuccess('Contribution', 'create', $p);
308
309 $params = array(
310 'contribution_id' => $this->_contribution['id'],
311 );
312 $contribution = $this->callAPISuccess('contribution', 'get', $params);
313 $financialParams['id'] = $this->_financialTypeId;
314 $default = NULL;
315 CRM_Financial_BAO_FinancialType::retrieve($financialParams, $default);
316
317 $this->assertEquals(1, $contribution['count']);
318 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
319 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->_financialTypeId);
320 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_type_id'], $this->_financialTypeId);
321 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
322 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
323 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
324 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
325 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
326 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
327 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
328 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
329
330 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
331 $p['trxn_id'] = '3847';
332 $p['invoice_id'] = '3847';
333
334 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
335
336 // now we have 2 - test getcount
337 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
338 $this->assertEquals(2, $contribution);
339 //test id only format
340 $contribution = $this->callAPISuccess('contribution', 'get', array(
341 'id' => $this->_contribution['id'],
342 'format.only_id' => 1,
343 ));
344 $this->assertEquals($this->_contribution['id'], $contribution, print_r($contribution, TRUE));
345 //test id only format
346 $contribution = $this->callAPISuccess('contribution', 'get', array(
347 'id' => $contribution2['id'],
348 'format.only_id' => 1,
349 ));
350 $this->assertEquals($contribution2['id'], $contribution);
351 $contribution = $this->callAPISuccess('contribution', 'get', array(
352 'id' => $this->_contribution['id'],
353 ));
354 //test id as field
355 $this->assertEquals(1, $contribution['count']);
356 // $this->assertEquals($this->_contribution['id'], $contribution['id'] ) ;
357 //test get by contact id works
358 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId));
359
360 $this->assertEquals(2, $contribution['count']);
361 $this->callAPISuccess('Contribution', 'Delete', array(
362 'id' => $this->_contribution['id'],
363 ));
364 $this->callAPISuccess('Contribution', 'Delete', array(
365 'id' => $contribution2['id'],
366 ));
367 }
368
369 /**
370 * Create an contribution_id=FALSE and financial_type_id=Donation.
371 */
372 public function testCreateEmptyContributionIDUseDonation() {
373 $params = array(
374 'contribution_id' => FALSE,
375 'contact_id' => 1,
376 'total_amount' => 1,
377 'check_permissions' => FALSE,
378 'financial_type_id' => 'Donation',
379 );
380 $this->callAPISuccess('contribution', 'create', $params);
381 }
382
383 /**
384 * Check with complete array + custom field.
385 *
386 * Note that the test is written on purpose without any
387 * variables specific to participant so it can be replicated into other entities
388 * and / or moved to the automated test suite
389 */
390 public function testCreateWithCustom() {
391 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
392
393 $params = $this->_params;
394 $params['custom_' . $ids['custom_field_id']] = "custom string";
395
396 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
397 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
398 $check = $this->callAPISuccess($this->_entity, 'get', array(
399 'return.custom_' . $ids['custom_field_id'] => 1,
400 'id' => $result['id'],
401 ));
402 $this->customFieldDelete($ids['custom_field_id']);
403 $this->customGroupDelete($ids['custom_group_id']);
404 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']]);
405 }
406
407 /**
408 * Check with complete array + custom field.
409 *
410 * Note that the test is written on purpose without any
411 * variables specific to participant so it can be replicated into other entities
412 * and / or moved to the automated test suite
413 */
414 public function testCreateGetFieldsWithCustom() {
415 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
416 $idsContact = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTest.php');
417 $result = $this->callAPISuccess('Contribution', 'getfields', array());
418 $this->assertArrayHasKey('custom_' . $ids['custom_field_id'], $result['values']);
419 $this->assertArrayNotHasKey('custom_' . $idsContact['custom_field_id'], $result['values']);
420 $this->customFieldDelete($ids['custom_field_id']);
421 $this->customGroupDelete($ids['custom_group_id']);
422 $this->customFieldDelete($idsContact['custom_field_id']);
423 $this->customGroupDelete($idsContact['custom_group_id']);
424 }
425
426 public function testCreateContributionNoLineItems() {
427
428 $params = array(
429 'contact_id' => $this->_individualId,
430 'receive_date' => '20120511',
431 'total_amount' => 100.00,
432 'financial_type_id' => $this->_financialTypeId,
433 'payment_instrument_id' => 1,
434 'non_deductible_amount' => 10.00,
435 'fee_amount' => 50.00,
436 'net_amount' => 90.00,
437 'trxn_id' => 12345,
438 'invoice_id' => 67890,
439 'source' => 'SSF',
440 'contribution_status_id' => 1,
441 'skipLineItem' => 1,
442 );
443
444 $contribution = $this->callAPISuccess('contribution', 'create', $params);
445 $lineItems = $this->callAPISuccess('line_item', 'get', array(
446 'entity_id' => $contribution['id'],
447 'entity_table' => 'civicrm_contribution',
448 'sequential' => 1,
449 ));
450 $this->assertEquals(0, $lineItems['count']);
451 }
452
453 /**
454 * Test checks that passing in line items suppresses the create mechanism.
455 */
456 public function testCreateContributionChainedLineItems() {
457 $params = array(
458 'contact_id' => $this->_individualId,
459 'receive_date' => '20120511',
460 'total_amount' => 100.00,
461 'financial_type_id' => $this->_financialTypeId,
462 'payment_instrument_id' => 1,
463 'non_deductible_amount' => 10.00,
464 'fee_amount' => 50.00,
465 'net_amount' => 90.00,
466 'trxn_id' => 12345,
467 'invoice_id' => 67890,
468 'source' => 'SSF',
469 'contribution_status_id' => 1,
470 'skipLineItem' => 1,
471 'api.line_item.create' => array(
472 array(
473 'price_field_id' => 1,
474 'qty' => 2,
475 'line_total' => '20',
476 'unit_price' => '10',
477 ),
478 array(
479 'price_field_id' => 1,
480 'qty' => 1,
481 'line_total' => '80',
482 'unit_price' => '80',
483 ),
484 ),
485 );
486
487 $description = "Create Contribution with Nested Line Items.";
488 $subfile = "CreateWithNestedLineItems";
489 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
490
491 $lineItems = $this->callAPISuccess('line_item', 'get', array(
492 'entity_id' => $contribution['id'],
493 'contribution_id' => $contribution['id'],
494 'entity_table' => 'civicrm_contribution',
495 'sequential' => 1,
496 ));
497 $this->assertEquals(2, $lineItems['count']);
498 }
499
500 public function testCreateContributionOffline() {
501 $params = array(
502 'contact_id' => $this->_individualId,
503 'receive_date' => '20120511',
504 'total_amount' => 100.00,
505 'financial_type_id' => 1,
506 'trxn_id' => 12345,
507 'invoice_id' => 67890,
508 'source' => 'SSF',
509 'contribution_status_id' => 1,
510 );
511
512 $contribution = $this->callAPISuccess('contribution', 'create', $params);
513 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
514 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
515 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
516 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
517 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
518 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
519 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
520 $lineItems = $this->callAPISuccess('line_item', 'get', array(
521 'entity_id' => $contribution['id'],
522 'contribution_id' => $contribution['id'],
523 'entity_table' => 'civicrm_contribution',
524 'sequential' => 1,
525 ));
526 $this->assertEquals(1, $lineItems['count']);
527 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
528 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
529 $this->_checkFinancialRecords($contribution, 'offline');
530 $this->contributionGetnCheck($params, $contribution['id']);
531 }
532
533 /**
534 * Test create with valid payment instrument.
535 */
536 public function testCreateContributionWithPaymentInstrument() {
537 $params = $this->_params + array('payment_instrument' => 'EFT');
538 $contribution = $this->callAPISuccess('contribution', 'create', $params);
539 $contribution = $this->callAPISuccess('contribution', 'get', array(
540 'sequential' => 1,
541 'id' => $contribution['id'],
542 ));
543 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
544 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
545
546 $this->callAPISuccess('contribution', 'create', array(
547 'id' => $contribution['id'],
548 'payment_instrument' => 'Credit Card',
549 ));
550 $contribution = $this->callAPISuccess('contribution', 'get', array(
551 'sequential' => 1,
552 'id' => $contribution['id'],
553 ));
554 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
555 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
556 }
557
558 public function testGetContributionByPaymentInstrument() {
559 $params = $this->_params + array('payment_instrument' => 'EFT');
560 $params2 = $this->_params + array('payment_instrument' => 'Cash');
561 $this->callAPISuccess('contribution', 'create', $params);
562 $this->callAPISuccess('contribution', 'create', $params2);
563 $contribution = $this->callAPISuccess('contribution', 'get', array(
564 'sequential' => 1,
565 'contribution_payment_instrument' => 'Cash',
566 ));
567 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
568 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
569 $this->assertEquals(1, $contribution['count']);
570 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'payment_instrument' => 'Cash'));
571 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
572 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
573 $this->assertEquals(1, $contribution['count']);
574 $contribution = $this->callAPISuccess('contribution', 'get', array(
575 'sequential' => 1,
576 'payment_instrument_id' => 5,
577 ));
578 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
579 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
580 $this->assertEquals(1, $contribution['count']);
581 $contribution = $this->callAPISuccess('contribution', 'get', array(
582 'sequential' => 1,
583 'payment_instrument' => 'EFT',
584 ));
585 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
586 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
587 $this->assertEquals(1, $contribution['count']);
588 $contribution = $this->callAPISuccess('contribution', 'create', array(
589 'id' => $contribution['id'],
590 'payment_instrument' => 'Credit Card',
591 ));
592 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'id' => $contribution['id']));
593 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
594 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
595 $this->assertEquals(1, $contribution['count']);
596 }
597
598 /**
599 * CRM-16227 introduces invoice_id as a parameter.
600 */
601 public function testGetContributionByInvoice() {
602 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, array('invoice_id' => 'curly')));
603 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params), array('invoice_id' => 'churlish'));
604 $this->callAPISuccessGetCount('Contribution', array(), 2);
605 $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => 'curly'));
606 // The following don't work. They are the format we are trying to introduce but although the form uses this format
607 // CRM_Contact_BAO_Query::convertFormValues puts them into the other format & the where only supports that.
608 // ideally the where clause would support this format (as it does on contact_BAO_Query) and those lines would
609 // come out of convertFormValues
610 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('LIKE' => '%ish%')));
611 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('NOT IN' => array('curly'))));
612 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('LIKE' => '%ly%')), 2);
613 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('IN' => array('curly', 'churlish'))),
614 // 2);
615 }
616
617 /**
618 * Check the credit note retrieval is case insensitive.
619 */
620 public function testGetCreditNoteCaseInsensitive() {
621 $this->contributionCreate(array('contact_id' => $this->_individualId));
622 $this->contributionCreate(array('creditnote_id' => 'cN1234', 'contact_id' => $this->_individualId, 'invoice_id' => rand(), 'trxn_id' => rand()));
623 $contribution = $this->callAPISuccess('Contribution', 'getsingle', array('creditnote_id' => 'CN1234'));
624 $this->assertEquals($contribution['creditnote_id'], 'cN1234');
625 }
626
627 /**
628 * Test retrieval by total_amount works.
629 *
630 * @throws Exception
631 */
632 public function testGetContributionByTotalAmount() {
633 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, array('total_amount' => '5')));
634 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, array('total_amount' => '10')));
635 $this->callAPISuccessGetCount('Contribution', array('total_amount' => 10), 1);
636 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('>' => 6)), 1);
637 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('>' => 0)), 2);
638 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('>' => -5)), 2);
639 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('<' => 0)), 0);
640 $this->callAPISuccessGetCount('Contribution', array(), 2);
641 }
642
643 /**
644 * Create test with unique field name on source.
645 */
646 public function testCreateContributionSource() {
647
648 $params = array(
649 'contact_id' => $this->_individualId,
650 'receive_date' => date('Ymd'),
651 'total_amount' => 100.00,
652 'financial_type_id' => $this->_financialTypeId,
653 'payment_instrument_id' => 1,
654 'non_deductible_amount' => 10.00,
655 'fee_amount' => 50.00,
656 'net_amount' => 90.00,
657 'trxn_id' => 12345,
658 'invoice_id' => 67890,
659 'contribution_source' => 'SSF',
660 'contribution_status_id' => 1,
661 );
662
663 $contribution = $this->callAPISuccess('contribution', 'create', $params);
664 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
665 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
666 }
667
668 /**
669 * Create test with unique field name on source.
670 */
671 public function testCreateDefaultNow() {
672
673 $params = $this->_params;
674 unset($params['receive_date']);
675
676 $contribution = $this->callAPISuccess('contribution', 'create', $params);
677 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
678 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date'])));
679 }
680
681 /**
682 * Create test with unique field name on source.
683 */
684 public function testCreateContributionSourceInvalidContact() {
685
686 $params = array(
687 'contact_id' => 999,
688 'receive_date' => date('Ymd'),
689 'total_amount' => 100.00,
690 'financial_type_id' => $this->_financialTypeId,
691 'payment_instrument_id' => 1,
692 'non_deductible_amount' => 10.00,
693 'fee_amount' => 50.00,
694 'net_amount' => 90.00,
695 'trxn_id' => 12345,
696 'invoice_id' => 67890,
697 'contribution_source' => 'SSF',
698 'contribution_status_id' => 1,
699 );
700
701 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
702 }
703
704 public function testCreateContributionSourceInvalidContContact() {
705
706 $params = array(
707 'contribution_contact_id' => 999,
708 'receive_date' => date('Ymd'),
709 'total_amount' => 100.00,
710 'financial_type_id' => $this->_financialTypeId,
711 'payment_instrument_id' => 1,
712 'non_deductible_amount' => 10.00,
713 'fee_amount' => 50.00,
714 'net_amount' => 90.00,
715 'trxn_id' => 12345,
716 'invoice_id' => 67890,
717 'contribution_source' => 'SSF',
718 'contribution_status_id' => 1,
719 );
720
721 $this->callAPIFailure('contribution', 'create', $params);
722 }
723
724 /**
725 * Test note created correctly.
726 */
727 public function testCreateContributionWithNote() {
728 $description = "Demonstrates creating contribution with Note Entity.";
729 $subfile = "ContributionCreateWithNote";
730 $params = array(
731 'contact_id' => $this->_individualId,
732 'receive_date' => '2012-01-01',
733 'total_amount' => 100.00,
734 'financial_type_id' => $this->_financialTypeId,
735 'payment_instrument_id' => 1,
736 'non_deductible_amount' => 10.00,
737 'fee_amount' => 50.00,
738 'net_amount' => 90.00,
739 'trxn_id' => 12345,
740 'invoice_id' => 67890,
741 'source' => 'SSF',
742 'contribution_status_id' => 1,
743 'note' => 'my contribution note',
744 );
745
746 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
747 $result = $this->callAPISuccess('note', 'get', array(
748 'entity_table' => 'civicrm_contribution',
749 'entity_id' => $contribution['id'],
750 'sequential' => 1,
751 ));
752 $this->assertEquals('my contribution note', $result['values'][0]['note']);
753 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
754 }
755
756 public function testCreateContributionWithNoteUniqueNameAliases() {
757 $params = array(
758 'contact_id' => $this->_individualId,
759 'receive_date' => '2012-01-01',
760 'total_amount' => 100.00,
761 'financial_type_id' => $this->_financialTypeId,
762 'payment_instrument_id' => 1,
763 'non_deductible_amount' => 10.00,
764 'fee_amount' => 50.00,
765 'net_amount' => 90.00,
766 'trxn_id' => 12345,
767 'invoice_id' => 67890,
768 'source' => 'SSF',
769 'contribution_status_id' => 1,
770 'contribution_note' => 'my contribution note',
771 );
772
773 $contribution = $this->callAPISuccess('contribution', 'create', $params);
774 $result = $this->callAPISuccess('note', 'get', array(
775 'entity_table' => 'civicrm_contribution',
776 'entity_id' => $contribution['id'],
777 'sequential' => 1,
778 ));
779 $this->assertEquals('my contribution note', $result['values'][0]['note']);
780 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
781 }
782
783 /**
784 * This is the test for creating soft credits.
785 */
786 public function testCreateContributionWithSoftCredit() {
787 $description = "Demonstrates creating contribution with SoftCredit.";
788 $subfile = "ContributionCreateWithSoftCredit";
789 $contact2 = $this->callAPISuccess('Contact', 'create', array(
790 'display_name' => 'superman',
791 'contact_type' => 'Individual',
792 ));
793 $softParams = array(
794 'contact_id' => $contact2['id'],
795 'amount' => 50,
796 'soft_credit_type_id' => 3,
797 );
798
799 $params = $this->_params + array('soft_credit' => array(1 => $softParams));
800 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
801 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
802
803 $this->assertEquals($softParams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
804 $this->assertEquals($softParams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
805 $this->assertEquals($softParams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']);
806
807 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
808 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
809 }
810
811 public function testCreateContributionWithSoftCreditDefaults() {
812 $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type.";
813 $subfile = "ContributionCreateWithSoftCreditDefaults";
814 $contact2 = $this->callAPISuccess('Contact', 'create', array(
815 'display_name' => 'superman',
816 'contact_type' => 'Individual',
817 ));
818 $params = $this->_params + array(
819 'soft_credit_to' => $contact2['id'],
820 );
821 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
822 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
823
824 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
825 // Default soft credit amount = contribution.total_amount
826 $this->assertEquals($this->_params['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
827 $this->assertEquals(CRM_Core_OptionGroup::getDefaultValue("soft_credit_type"), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
828
829 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
830 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
831 }
832
833 /**
834 * Test creating contribution with Soft Credit by passing in honor_contact_id.
835 */
836 public function testCreateContributionWithHonoreeContact() {
837 $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id.";
838 $subfile = "ContributionCreateWithHonoreeContact";
839 $contact2 = $this->callAPISuccess('Contact', 'create', array(
840 'display_name' => 'superman',
841 'contact_type' => 'Individual',
842 ));
843 $params = $this->_params + array(
844 'honor_contact_id' => $contact2['id'],
845 );
846 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
847 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
848
849 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
850 // Default soft credit amount = contribution.total_amount
851 // Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type
852 $this->assertEquals($this->_params['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
853 $softCreditValueTypeID = $result['values'][0]['soft_credit'][1]['soft_credit_type'];
854 $this->assertEquals('in_honor_of', CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', $softCreditValueTypeID));
855
856 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
857 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
858 }
859
860 /**
861 * Test using example code.
862 */
863 public function testContributionCreateExample() {
864 //make sure at least on page exists since there is a truncate in tear down
865 $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
866 require_once 'api/v3/examples/Contribution/Create.php';
867 $result = contribution_create_example();
868 $id = $result['id'];
869 $expectedResult = contribution_create_expectedresult();
870 $this->checkArrayEquals($expectedResult, $result);
871 $this->contributionDelete($id);
872 }
873
874 /**
875 * Function tests that additional financial records are created when fee amount is recorded.
876 */
877 public function testCreateContributionWithFee() {
878 $params = array(
879 'contact_id' => $this->_individualId,
880 'receive_date' => '20120511',
881 'total_amount' => 100.00,
882 'fee_amount' => 50,
883 'financial_type_id' => 1,
884 'trxn_id' => 12345,
885 'invoice_id' => 67890,
886 'source' => 'SSF',
887 'contribution_status_id' => 1,
888 );
889
890 $contribution = $this->callAPISuccess('contribution', 'create', $params);
891 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
892 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
893 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 50.00);
894 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 50.00);
895 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
896 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
897 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
898 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
899 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
900
901 $lineItems = $this->callAPISuccess('line_item', 'get', array(
902
903 'entity_id' => $contribution['id'],
904 'entity_table' => 'civicrm_contribution',
905 'sequential' => 1,
906 ));
907 $this->assertEquals(1, $lineItems['count']);
908 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
909 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
910 $lineItems = $this->callAPISuccess('line_item', 'get', array(
911
912 'entity_id' => $contribution['id'],
913 'contribution_id' => $contribution['id'],
914 'entity_table' => 'civicrm_contribution',
915 'sequential' => 1,
916 ));
917 $this->assertEquals(1, $lineItems['count']);
918 $this->_checkFinancialRecords($contribution, 'feeAmount');
919 }
920
921
922 /**
923 * Function tests that additional financial records are created when online contribution is created.
924 */
925 public function testCreateContributionOnline() {
926 CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
927 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
928 $this->assertAPISuccess($contributionPage);
929 $params = array(
930 'contact_id' => $this->_individualId,
931 'receive_date' => '20120511',
932 'total_amount' => 100.00,
933 'financial_type_id' => 1,
934 'contribution_page_id' => $contributionPage['id'],
935 'payment_processor' => $this->paymentProcessorID,
936 'trxn_id' => 12345,
937 'invoice_id' => 67890,
938 'source' => 'SSF',
939 'contribution_status_id' => 1,
940
941 );
942
943 $contribution = $this->callAPISuccess('contribution', 'create', $params);
944 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
945 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
946 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
947 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
948 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
949 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
950 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
951 $contribution['payment_instrument_id'] = $this->callAPISuccessGetValue('PaymentProcessor', array(
952 'id' => $this->paymentProcessorID,
953 'return' => 'payment_instrument_id',
954 ));
955 $this->_checkFinancialRecords($contribution, 'online');
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 testCreateInvalidFinancialType() {
965 $params = $this->_params;
966 $params['financial_type_id'] = 99999;
967 $this->callAPIFailure($this->_entity, 'create', $params, "'99999' is not a valid option for field financial_type_id");
968 }
969
970 /**
971 * Check handling of financial type.
972 *
973 * In the interests of removing financial type / contribution type checks from
974 * legacy format function lets test that the api is doing this for us
975 */
976 public function testValidNamedFinancialType() {
977 $params = $this->_params;
978 $params['financial_type_id'] = 'Donation';
979 $this->callAPISuccess($this->_entity, 'create', $params);
980 }
981
982 /**
983 * Tests that additional financial records are created.
984 *
985 * Checks when online contribution with pay later option is created
986 */
987 public function testCreateContributionPayLaterOnline() {
988 CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
989 $this->_pageParams['is_pay_later'] = 1;
990 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
991 $this->assertAPISuccess($contributionPage);
992 $params = array(
993 'contact_id' => $this->_individualId,
994 'receive_date' => '20120511',
995 'total_amount' => 100.00,
996 'financial_type_id' => 1,
997 'contribution_page_id' => $contributionPage['id'],
998 'trxn_id' => 12345,
999 'is_pay_later' => 1,
1000 'invoice_id' => 67890,
1001 'source' => 'SSF',
1002 'contribution_status_id' => 2,
1003
1004 );
1005
1006 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
1007 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
1008 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
1009 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
1010 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
1011 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
1012 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
1013 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
1014 $this->_checkFinancialRecords($contribution, 'payLater');
1015 }
1016
1017 /**
1018 * Function tests that additional financial records are created for online contribution with pending option.
1019 */
1020 public function testCreateContributionPendingOnline() {
1021 CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
1022 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
1023 $this->assertAPISuccess($contributionPage);
1024 $params = array(
1025 'contact_id' => $this->_individualId,
1026 'receive_date' => '20120511',
1027 'total_amount' => 100.00,
1028 'financial_type_id' => 1,
1029 'contribution_page_id' => $contributionPage['id'],
1030 'trxn_id' => 12345,
1031 'invoice_id' => 67890,
1032 'source' => 'SSF',
1033 'contribution_status_id' => 2,
1034 );
1035
1036 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1037 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
1038 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
1039 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
1040 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
1041 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
1042 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
1043 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
1044 $this->_checkFinancialRecords($contribution, 'pending');
1045 }
1046
1047 /**
1048 * Test that BAO defaults work.
1049 */
1050 public function testCreateBAODefaults() {
1051 unset($this->_params['contribution_source_id'], $this->_params['payment_instrument_id']);
1052 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
1053 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1054 'id' => $contribution['id'],
1055 'api.contribution.delete' => 1,
1056 ));
1057 $this->assertEquals(1, $contribution['contribution_status_id']);
1058 $this->assertEquals('Check', $contribution['payment_instrument']);
1059 }
1060
1061 /**
1062 * Function tests that line items, financial records are updated when contribution amount is changed.
1063 */
1064 public function testCreateUpdateContributionChangeTotal() {
1065 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
1066 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
1067
1068 'entity_id' => $contribution['id'],
1069 'entity_table' => 'civicrm_contribution',
1070 'sequential' => 1,
1071 'return' => 'line_total',
1072 ));
1073 $this->assertEquals('100.00', $lineItems);
1074 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
1075 // Financial trxn SUM = 100 + 5 (fee)
1076 $this->assertEquals('105.00', $trxnAmount);
1077 $newParams = array(
1078
1079 'id' => $contribution['id'],
1080 'total_amount' => '125',
1081 );
1082 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1083
1084 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
1085
1086 'entity_id' => $contribution['id'],
1087 'entity_table' => 'civicrm_contribution',
1088 'sequential' => 1,
1089 'return' => 'line_total',
1090 ));
1091
1092 $this->assertEquals('125.00', $lineItems);
1093 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
1094
1095 // Financial trxn SUM = 125 + 5 (fee).
1096 $this->assertEquals('130.00', $trxnAmount);
1097 $this->assertEquals('125.00', $this->_getFinancialItemAmount($contribution['id']));
1098 }
1099
1100 /**
1101 * Function tests that line items, financial records are updated when pay later contribution is received.
1102 */
1103 public function testCreateUpdateContributionPayLater() {
1104 $contribParams = array(
1105 'contact_id' => $this->_individualId,
1106 'receive_date' => '2012-01-01',
1107 'total_amount' => 100.00,
1108 'financial_type_id' => $this->_financialTypeId,
1109 'payment_instrument_id' => 1,
1110 'contribution_status_id' => 2,
1111 'is_pay_later' => 1,
1112
1113 );
1114 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1115
1116 $newParams = array_merge($contribParams, array(
1117 'id' => $contribution['id'],
1118 'contribution_status_id' => 1,
1119 )
1120 );
1121 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1122 $contribution = $contribution['values'][$contribution['id']];
1123 $this->assertEquals($contribution['contribution_status_id'], '1');
1124 $this->_checkFinancialItem($contribution['id'], 'paylater');
1125 $this->_checkFinancialTrxn($contribution, 'payLater');
1126 }
1127
1128 /**
1129 * Function tests that financial records are updated when Payment Instrument is changed.
1130 */
1131 public function testCreateUpdateContributionPaymentInstrument() {
1132 $instrumentId = $this->_addPaymentInstrument();
1133 $contribParams = array(
1134 'contact_id' => $this->_individualId,
1135 'total_amount' => 100.00,
1136 'financial_type_id' => $this->_financialTypeId,
1137 'payment_instrument_id' => 4,
1138 'contribution_status_id' => 1,
1139
1140 );
1141 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1142
1143 $newParams = array_merge($contribParams, array(
1144 'id' => $contribution['id'],
1145 'payment_instrument_id' => $instrumentId,
1146 )
1147 );
1148 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1149 $this->assertAPISuccess($contribution);
1150 $this->checkFinancialTrxnPaymentInstrumentChange($contribution['id'], 4, $instrumentId);
1151
1152 // cleanup - delete created payment instrument
1153 $this->_deletedAddedPaymentInstrument();
1154 }
1155
1156 /**
1157 * Function tests that financial records are updated when Payment Instrument is changed when amount is negative.
1158 */
1159 public function testCreateUpdateNegativeContributionPaymentInstrument() {
1160 $instrumentId = $this->_addPaymentInstrument();
1161 $contribParams = array(
1162 'contact_id' => $this->_individualId,
1163 'total_amount' => -100.00,
1164 'financial_type_id' => $this->_financialTypeId,
1165 'payment_instrument_id' => 4,
1166 'contribution_status_id' => 1,
1167
1168 );
1169 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1170
1171 $newParams = array_merge($contribParams, array(
1172 'id' => $contribution['id'],
1173 'payment_instrument_id' => $instrumentId,
1174 )
1175 );
1176 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1177 $this->assertAPISuccess($contribution);
1178 $this->checkFinancialTrxnPaymentInstrumentChange($contribution['id'], 4, $instrumentId, -100);
1179
1180 // cleanup - delete created payment instrument
1181 $this->_deletedAddedPaymentInstrument();
1182 }
1183
1184 /**
1185 * Function tests that financial records are added when Contribution is Refunded.
1186 */
1187 public function testCreateUpdateContributionRefund() {
1188 $contributionParams = array(
1189 'contact_id' => $this->_individualId,
1190 'receive_date' => '2012-01-01',
1191 'total_amount' => 100.00,
1192 'financial_type_id' => $this->_financialTypeId,
1193 'payment_instrument_id' => 4,
1194 'contribution_status_id' => 1,
1195 'trxn_id' => 'original_payment',
1196 );
1197 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1198 $newParams = array_merge($contributionParams, array(
1199 'id' => $contribution['id'],
1200 'contribution_status_id' => 'Refunded',
1201 'cancel_date' => '2015-01-01 09:00',
1202 'refund_trxn_id' => 'the refund',
1203 )
1204 );
1205
1206 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1207 $this->_checkFinancialTrxn($contribution, 'refund');
1208 $this->_checkFinancialItem($contribution['id'], 'refund');
1209 $this->assertEquals('original_payment', $this->callAPISuccessGetValue('Contribution', array(
1210 'id' => $contribution['id'],
1211 'return' => 'trxn_id',
1212 )));
1213 }
1214
1215 /**
1216 * Refund a contribution for a financial type with a contra account.
1217 *
1218 * CRM-17951 the contra account is a financial account with a relationship to a
1219 * financial type. It is not always configured but should be reflected
1220 * in the financial_trxn & financial_item table if it is.
1221 */
1222 public function testCreateUpdateChargebackContributionDefaultAccount() {
1223 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
1224 $this->callAPISuccess('Contribution', 'create', array(
1225 'id' => $contribution['id'],
1226 'contribution_status_id' => 'Chargeback',
1227 ));
1228 $this->callAPISuccessGetSingle('Contribution', array('contribution_status_id' => 'Chargeback'));
1229
1230 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1231 'contribution_id' => $contribution['id'],
1232 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1233 ));
1234 $this->assertEquals(1, $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1235 $this->callAPISuccessGetSingle('FinancialTrxn', array(
1236 'total_amount' => -100,
1237 'status_id' => 'Chargeback',
1238 'to_financial_account_id' => 6,
1239 ));
1240 }
1241
1242 /**
1243 * Refund a contribution for a financial type with a contra account.
1244 *
1245 * CRM-17951 the contra account is a financial account with a relationship to a
1246 * financial type. It is not always configured but should be reflected
1247 * in the financial_trxn & financial_item table if it is.
1248 */
1249 public function testCreateUpdateChargebackContributionCustomAccount() {
1250 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
1251 'name' => 'Chargeback Account',
1252 'is_active' => TRUE,
1253 ));
1254
1255 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', array(
1256 'entity_id' => $this->_financialTypeId,
1257 'entity_table' => 'civicrm_financial_type',
1258 'account_relationship' => 'Chargeback Account is',
1259 'financial_account_id' => 'Chargeback Account',
1260 ));
1261
1262 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
1263 $this->callAPISuccess('Contribution', 'create', array(
1264 'id' => $contribution['id'],
1265 'contribution_status_id' => 'Chargeback',
1266 ));
1267 $this->callAPISuccessGetSingle('Contribution', array('contribution_status_id' => 'Chargeback'));
1268
1269 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1270 'contribution_id' => $contribution['id'],
1271 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1272 ));
1273 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1274
1275 $this->callAPISuccess('Contribution', 'delete', array('id' => $contribution['id']));
1276 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialAccount['id']));
1277 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
1278 }
1279
1280 /**
1281 * Refund a contribution for a financial type with a contra account.
1282 *
1283 * CRM-17951 the contra account is a financial account with a relationship to a
1284 * financial type. It is not always configured but should be reflected
1285 * in the financial_trxn & financial_item table if it is.
1286 */
1287 public function testCreateUpdateRefundContributionConfiguredContraAccount() {
1288 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
1289 'name' => 'Refund Account',
1290 'is_active' => TRUE,
1291 ));
1292
1293 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', array(
1294 'entity_id' => $this->_financialTypeId,
1295 'entity_table' => 'civicrm_financial_type',
1296 'account_relationship' => 'Credit/Contra Revenue Account is',
1297 'financial_account_id' => 'Refund Account',
1298 ));
1299
1300 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
1301 $this->callAPISuccess('Contribution', 'create', array(
1302 'id' => $contribution['id'],
1303 'contribution_status_id' => 'Refunded',
1304 ));
1305
1306 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1307 'contribution_id' => $contribution['id'],
1308 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1309 ));
1310 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1311
1312 $this->callAPISuccess('Contribution', 'delete', array('id' => $contribution['id']));
1313 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialAccount['id']));
1314 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
1315 }
1316
1317 /**
1318 * Function tests that trxn_id is set when passed in.
1319 *
1320 * Here we ensure that the civicrm_financial_trxn.trxn_id & the civicrm_contribution.trxn_id are set
1321 * when trxn_id is passed in.
1322 */
1323 public function testCreateUpdateContributionRefundTrxnIDPassedIn() {
1324 $contributionParams = array(
1325 'contact_id' => $this->_individualId,
1326 'receive_date' => '2012-01-01',
1327 'total_amount' => 100.00,
1328 'financial_type_id' => $this->_financialTypeId,
1329 'payment_instrument_id' => 4,
1330 'contribution_status_id' => 1,
1331 'trxn_id' => 'original_payment',
1332 );
1333 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1334 $newParams = array_merge($contributionParams, array(
1335 'id' => $contribution['id'],
1336 'contribution_status_id' => 'Refunded',
1337 'cancel_date' => '2015-01-01 09:00',
1338 'trxn_id' => 'the refund',
1339 )
1340 );
1341
1342 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1343 $this->_checkFinancialTrxn($contribution, 'refund');
1344 $this->_checkFinancialItem($contribution['id'], 'refund');
1345 $this->assertEquals('the refund', $this->callAPISuccessGetValue('Contribution', array(
1346 'id' => $contribution['id'],
1347 'return' => 'trxn_id',
1348 )));
1349 }
1350
1351 /**
1352 * Function tests that trxn_id is set when passed in.
1353 *
1354 * Here we ensure that the civicrm_contribution.trxn_id is set
1355 * when trxn_id is passed in but if refund_trxn_id is different then that
1356 * is kept for the refund transaction.
1357 */
1358 public function testCreateUpdateContributionRefundRefundAndTrxnIDPassedIn() {
1359 $contributionParams = array(
1360 'contact_id' => $this->_individualId,
1361 'receive_date' => '2012-01-01',
1362 'total_amount' => 100.00,
1363 'financial_type_id' => $this->_financialTypeId,
1364 'payment_instrument_id' => 4,
1365 'contribution_status_id' => 1,
1366 'trxn_id' => 'original_payment',
1367 );
1368 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1369 $newParams = array_merge($contributionParams, array(
1370 'id' => $contribution['id'],
1371 'contribution_status_id' => 'Refunded',
1372 'cancel_date' => '2015-01-01 09:00',
1373 'trxn_id' => 'cont id',
1374 'refund_trxn_id' => 'the refund',
1375 )
1376 );
1377
1378 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1379 $this->_checkFinancialTrxn($contribution, 'refund');
1380 $this->_checkFinancialItem($contribution['id'], 'refund');
1381 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1382 'id' => $contribution['id'],
1383 'return' => 'trxn_id',
1384 )));
1385 }
1386
1387 /**
1388 * Function tests that refund_trxn_id is set when passed in empty.
1389 *
1390 * Here we ensure that the civicrm_contribution.trxn_id is set
1391 * when trxn_id is passed in but if refund_trxn_id isset but empty then that
1392 * is kept for the refund transaction.
1393 */
1394 public function testCreateUpdateContributionRefundRefundNullTrxnIDPassedIn() {
1395 $contributionParams = array(
1396 'contact_id' => $this->_individualId,
1397 'receive_date' => '2012-01-01',
1398 'total_amount' => 100.00,
1399 'financial_type_id' => $this->_financialTypeId,
1400 'payment_instrument_id' => 4,
1401 'contribution_status_id' => 1,
1402 'trxn_id' => 'original_payment',
1403 );
1404 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1405 $newParams = array_merge($contributionParams, array(
1406 'id' => $contribution['id'],
1407 'contribution_status_id' => 'Refunded',
1408 'cancel_date' => '2015-01-01 09:00',
1409 'trxn_id' => 'cont id',
1410 'refund_trxn_id' => '',
1411 )
1412 );
1413
1414 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1415 $this->_checkFinancialTrxn($contribution, 'refund', NULL, array('trxn_id' => NULL));
1416 $this->_checkFinancialItem($contribution['id'], 'refund');
1417 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1418 'id' => $contribution['id'],
1419 'return' => 'trxn_id',
1420 )));
1421 }
1422
1423 /**
1424 * Function tests invalid contribution status change.
1425 */
1426 public function testCreateUpdateContributionInValidStatusChange() {
1427 $contribParams = array(
1428 'contact_id' => 1,
1429 'receive_date' => '2012-01-01',
1430 'total_amount' => 100.00,
1431 'financial_type_id' => 1,
1432 'payment_instrument_id' => 1,
1433 'contribution_status_id' => 1,
1434 );
1435 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1436 $newParams = array_merge($contribParams, array(
1437 'id' => $contribution['id'],
1438 'contribution_status_id' => 2,
1439 )
1440 );
1441 $this->callAPIFailure('contribution', 'create', $newParams, ts('Cannot change contribution status from Completed to Pending.'));
1442
1443 }
1444
1445 /**
1446 * Function tests that financial records are added when Pending Contribution is Canceled.
1447 */
1448 public function testCreateUpdateContributionCancelPending() {
1449 $contribParams = array(
1450 'contact_id' => $this->_individualId,
1451 'receive_date' => '2012-01-01',
1452 'total_amount' => 100.00,
1453 'financial_type_id' => $this->_financialTypeId,
1454 'payment_instrument_id' => 1,
1455 'contribution_status_id' => 2,
1456 'is_pay_later' => 1,
1457
1458 );
1459 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1460 $newParams = array_merge($contribParams, array(
1461 'id' => $contribution['id'],
1462 'contribution_status_id' => 3,
1463 'cancel_date' => '2012-02-02 09:00',
1464 )
1465 );
1466 //Check if trxn_date is same as cancel_date.
1467 $checkTrxnDate = array(
1468 'trxn_date' => '2012-02-02 09:00:00',
1469 );
1470 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1471 $this->_checkFinancialTrxn($contribution, 'cancelPending', NULL, $checkTrxnDate);
1472 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1473 }
1474
1475 /**
1476 * Function tests that financial records are added when Financial Type is Changed.
1477 */
1478 public function testCreateUpdateContributionChangeFinancialType() {
1479 $contribParams = array(
1480 'contact_id' => $this->_individualId,
1481 'receive_date' => '2012-01-01',
1482 'total_amount' => 100.00,
1483 'financial_type_id' => 1,
1484 'payment_instrument_id' => 1,
1485 'contribution_status_id' => 1,
1486
1487 );
1488 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1489 $newParams = array_merge($contribParams, array(
1490 'id' => $contribution['id'],
1491 'financial_type_id' => 3,
1492 )
1493 );
1494 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1495 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1496 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1497 }
1498
1499 /**
1500 * Test that update does not change status id CRM-15105.
1501 */
1502 public function testCreateUpdateWithoutChangingPendingStatus() {
1503 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params, array('contribution_status_id' => 2)));
1504 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
1505 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1506 'id' => $contribution['id'],
1507 'api.contribution.delete' => 1,
1508 ));
1509 $this->assertEquals(2, $contribution['contribution_status_id']);
1510 }
1511
1512 /**
1513 * Test Updating a Contribution.
1514 *
1515 * CHANGE: we require the API to do an incremental update
1516 */
1517 public function testCreateUpdateContribution() {
1518 $contributionID = $this->contributionCreate(array(
1519 'contact_id' => $this->_individualId,
1520 'trxn_id' => 212355,
1521 'financial_type_id' => $this->_financialTypeId,
1522 'invoice_id' => 'old_invoice',
1523 ));
1524 $old_params = array(
1525 'contribution_id' => $contributionID,
1526 );
1527 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1528 $this->assertEquals($original['id'], $contributionID);
1529 //set up list of old params, verify
1530
1531 //This should not be required on update:
1532 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1533 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1534 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1535 $old_source = $original['values'][$contributionID]['contribution_source'];
1536
1537 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1538 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1539
1540 //check against values in CiviUnitTestCase::createContribution()
1541 $this->assertEquals($old_contact_id, $this->_individualId);
1542 $this->assertEquals($old_fee_amount, 5.00);
1543 $this->assertEquals($old_source, 'SSF');
1544 $this->assertEquals($old_trxn_id, 212355);
1545 $this->assertEquals($old_invoice_id, 'old_invoice');
1546 $params = array(
1547 'id' => $contributionID,
1548 'contact_id' => $this->_individualId,
1549 'total_amount' => 105.00,
1550 'fee_amount' => 7.00,
1551 'financial_type_id' => $this->_financialTypeId,
1552 'non_deductible_amount' => 22.00,
1553 'contribution_status_id' => 1,
1554 'note' => 'Donating for Noble Cause',
1555 );
1556
1557 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1558
1559 $new_params = array(
1560 'contribution_id' => $contribution['id'],
1561
1562 );
1563 $contribution = $this->callAPISuccessGetSingle('contribution', $new_params);
1564
1565 $this->assertEquals($contribution['contact_id'], $this->_individualId);
1566 $this->assertEquals($contribution['total_amount'], 105.00);
1567 $this->assertEquals($contribution['financial_type_id'], $this->_financialTypeId);
1568 $this->assertEquals($contribution['financial_type'], 'Donation');
1569 $this->assertEquals($contribution['instrument_id'], $old_payment_instrument);
1570 $this->assertEquals($contribution['non_deductible_amount'], 22.00);
1571 $this->assertEquals($contribution['fee_amount'], 7.00);
1572 $this->assertEquals($contribution['trxn_id'], $old_trxn_id);
1573 $this->assertEquals($contribution['invoice_id'], $old_invoice_id);
1574 $this->assertEquals($contribution['contribution_source'], $old_source);
1575 $this->assertEquals($contribution['contribution_status'], 'Completed');
1576
1577 $this->assertEquals($contribution['net_amount'], $contribution['total_amount'] - $contribution['fee_amount']);
1578
1579 $params = array(
1580 'contribution_id' => $contributionID,
1581
1582 );
1583 $result = $this->callAPISuccess('contribution', 'delete', $params);
1584 $this->assertAPISuccess($result);
1585 }
1586
1587 /**
1588 * Attempt (but fail) to delete a contribution without parameters.
1589 */
1590 public function testDeleteEmptyParamsContribution() {
1591 $params = array();
1592 $this->callAPIFailure('contribution', 'delete', $params);
1593 }
1594
1595 public function testDeleteParamsNotArrayContribution() {
1596 $params = 'contribution_id= 1';
1597 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1598 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1599 }
1600
1601 public function testDeleteWrongParamContribution() {
1602 $params = array(
1603 'contribution_source' => 'SSF',
1604
1605 );
1606 $this->callAPIFailure('contribution', 'delete', $params);
1607 }
1608
1609 public function testDeleteContribution() {
1610 $contributionID = $this->contributionCreate(array(
1611 'contact_id' => $this->_individualId,
1612 'financial_type_id' => $this->_financialTypeId,
1613 ));
1614 $params = array(
1615 'id' => $contributionID,
1616 );
1617 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__, __FILE__);
1618 }
1619
1620 /**
1621 * Test civicrm_contribution_search with empty params.
1622 *
1623 * All available contributions expected.
1624 */
1625 public function testSearchEmptyParams() {
1626 $params = array();
1627
1628 $p = array(
1629 'contact_id' => $this->_individualId,
1630 'receive_date' => date('Ymd'),
1631 'total_amount' => 100.00,
1632 'financial_type_id' => $this->_financialTypeId,
1633 'non_deductible_amount' => 10.00,
1634 'fee_amount' => 5.00,
1635 'net_amount' => 95.00,
1636 'trxn_id' => 23456,
1637 'invoice_id' => 78910,
1638 'source' => 'SSF',
1639 'contribution_status_id' => 1,
1640
1641 );
1642 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1643
1644 $result = $this->callAPISuccess('contribution', 'get', $params);
1645 // We're taking the first element.
1646 $res = $result['values'][$contribution['id']];
1647
1648 $this->assertEquals($p['contact_id'], $res['contact_id']);
1649 $this->assertEquals($p['total_amount'], $res['total_amount']);
1650 $this->assertEquals($p['financial_type_id'], $res['financial_type_id']);
1651 $this->assertEquals($p['net_amount'], $res['net_amount']);
1652 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1653 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1654 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1655 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1656 $this->assertEquals($p['source'], $res['contribution_source']);
1657 // contribution_status_id = 1 => Completed
1658 $this->assertEquals('Completed', $res['contribution_status']);
1659
1660 $this->contributionDelete($contribution['id']);
1661 }
1662
1663 /**
1664 * Test civicrm_contribution_search. Success expected.
1665 */
1666 public function testSearch() {
1667 $p1 = array(
1668 'contact_id' => $this->_individualId,
1669 'receive_date' => date('Ymd'),
1670 'total_amount' => 100.00,
1671 'financial_type_id' => $this->_financialTypeId,
1672 'non_deductible_amount' => 10.00,
1673 'contribution_status_id' => 1,
1674
1675 );
1676 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1677
1678 $p2 = array(
1679 'contact_id' => $this->_individualId,
1680 'receive_date' => date('Ymd'),
1681 'total_amount' => 200.00,
1682 'financial_type_id' => $this->_financialTypeId,
1683 'non_deductible_amount' => 20.00,
1684 'trxn_id' => 5454565,
1685 'invoice_id' => 1212124,
1686 'fee_amount' => 50.00,
1687 'net_amount' => 60.00,
1688 'contribution_status_id' => 2,
1689
1690 );
1691 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1692
1693 $params = array(
1694 'contribution_id' => $contribution2['id'],
1695
1696 );
1697 $result = $this->callAPISuccess('contribution', 'get', $params);
1698 $res = $result['values'][$contribution2['id']];
1699
1700 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1701 $this->assertEquals($p2['total_amount'], $res['total_amount']);
1702 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id']);
1703 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1704 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1705 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1706 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1707 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
1708 // contribution_status_id = 2 => Pending
1709 $this->assertEquals('Pending', $res['contribution_status']);
1710
1711 $this->contributionDelete($contribution1['id']);
1712 $this->contributionDelete($contribution2['id']);
1713 }
1714
1715 /**
1716 * Test completing a transaction via the API.
1717 *
1718 * Note that we are creating a logged in user because email goes out from
1719 * that person
1720 */
1721 public function testCompleteTransaction() {
1722 $mut = new CiviMailUtils($this, TRUE);
1723 $this->swapMessageTemplateForTestTemplate();
1724 $this->createLoggedInUser();
1725 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1726 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1727 $this->callAPISuccess('contribution', 'completetransaction', array(
1728 'id' => $contribution['id'],
1729 ));
1730 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
1731 $this->assertEquals('SSF', $contribution['contribution_source']);
1732 $this->assertEquals('Completed', $contribution['contribution_status']);
1733 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
1734 $mut->checkMailLog(array(
1735 'email:::anthony_anderson@civicrm.org',
1736 'is_monetary:::1',
1737 'amount:::100.00',
1738 'currency:::USD',
1739 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])),
1740 "receipt_date:::\n",
1741 'contributeMode:::notify',
1742 'title:::Contribution',
1743 'displayName:::Mr. Anthony Anderson II',
1744 'contributionStatus:::Completed',
1745 ));
1746 $mut->stop();
1747 $this->revertTemplateToReservedTemplate();
1748 }
1749
1750 /**
1751 * Test completing a transaction via the API with a non-USD transaction.
1752 */
1753 public function testCompleteTransactionEuro() {
1754 $mut = new CiviMailUtils($this, TRUE);
1755 $this->swapMessageTemplateForTestTemplate();
1756 $this->createLoggedInUser();
1757 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'currency' => 'EUR'));
1758 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1759
1760 $this->callAPISuccess('contribution', 'completetransaction', array(
1761 'id' => $contribution['id'],
1762 ));
1763
1764 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
1765 $this->assertEquals('SSF', $contribution['contribution_source']);
1766 $this->assertEquals('Completed', $contribution['contribution_status']);
1767 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
1768
1769 $entityFinancialTransactions = $this->getFinancialTransactionsForContribution($contribution['id']);
1770 $entityFinancialTransaction = reset($entityFinancialTransactions);
1771 $financialTrxn = $this->callAPISuccessGetSingle('FinancialTrxn', array('id' => $entityFinancialTransaction['financial_trxn_id']));
1772 $this->assertEquals('EUR', $financialTrxn['currency']);
1773
1774 $mut->checkMailLog(array(
1775 'email:::anthony_anderson@civicrm.org',
1776 'is_monetary:::1',
1777 'amount:::100.00',
1778 'currency:::EUR',
1779 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])),
1780 "receipt_date:::\n",
1781 'contributeMode:::notify',
1782 'title:::Contribution',
1783 'displayName:::Mr. Anthony Anderson II',
1784 'contributionStatus:::Completed',
1785 ));
1786 $mut->stop();
1787 $this->revertTemplateToReservedTemplate();
1788 }
1789
1790 /**
1791 * Test to ensure mail is sent on chosing pay later
1792 */
1793 public function testpayLater() {
1794 $mut = new CiviMailUtils($this, TRUE);
1795 $this->swapMessageTemplateForTestTemplate();
1796 $this->createLoggedInUser();
1797
1798 // create contribution page first
1799 $contributionPageParams = array(
1800 'title' => 'Help Support CiviCRM!',
1801 'financial_type_id' => 1,
1802 'is_monetary' => TRUE,
1803 'is_pay_later' => 1,
1804 'is_quick_config' => TRUE,
1805 'pay_later_text' => 'I will send payment by check',
1806 'pay_later_receipt' => 'This is a pay later reciept',
1807 'is_allow_other_amount' => 1,
1808 'min_amount' => 10.00,
1809 'max_amount' => 10000.00,
1810 'goal_amount' => 100000.00,
1811 'is_email_receipt' => 1,
1812 'is_active' => 1,
1813 'amount_block_is_active' => 1,
1814 'currency' => 'USD',
1815 'is_billing_required' => 0,
1816 );
1817 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', $contributionPageParams);
1818
1819 // submit form values
1820 $priceSet = $this->callAPISuccess('price_set', 'getsingle', array('name' => 'default_contribution_amount'));
1821 $params = array(
1822 'id' => $contributionPageResult['id'],
1823 'contact_id' => $this->_individualId,
1824 'email-5' => 'anthony_anderson@civicrm.org',
1825 'payment_processor_id' => 0,
1826 'amount' => 100.00,
1827 'tax_amount' => '',
1828 'currencyID' => 'USD',
1829 'is_pay_later' => 1,
1830 'invoiceID' => 'f28e1ddc86f8c4a0ff5bcf46393e4bc8',
1831 'is_quick_config' => 1,
1832 'description' => 'Online Contribution: Help Support CiviCRM!',
1833 'price_set_id' => $priceSet['id'],
1834 );
1835 $this->callAPISuccess('contribution_page', 'submit', $params);
1836
1837 $mut->checkMailLog(array(
1838 'is_pay_later:::1',
1839 'email:::anthony_anderson@civicrm.org',
1840 'pay_later_receipt:::' . $contributionPageParams['pay_later_receipt'],
1841 'displayName:::Mr. Anthony Anderson II',
1842 'contributionPageId:::' . $contributionPageResult['id'],
1843 'title:::' . $contributionPageParams['title'],
1844 'amount:::' . $params['amount'],
1845 ));
1846 $mut->stop();
1847 $this->revertTemplateToReservedTemplate();
1848 }
1849
1850 /**
1851 * Test to check whether contact billing address is used when no contribution address
1852 */
1853 public function testBillingAddress() {
1854 $mut = new CiviMailUtils($this, TRUE);
1855 $this->swapMessageTemplateForTestTemplate();
1856 $this->createLoggedInUser();
1857
1858 //Scenario 1: When Contact don't have any address
1859 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1860 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1861 $this->callAPISuccess('contribution', 'completetransaction', array(
1862 'id' => $contribution['id'],
1863 ));
1864 $mut->checkMailLog(array(
1865 'address:::',
1866 ));
1867
1868 // Scenario 2: Contribution using address
1869 $address = $this->callAPISuccess('address', 'create', array(
1870 'street_address' => 'contribution billing st',
1871 'location_type_id' => 2,
1872 'contact_id' => $this->_params['contact_id'],
1873 ));
1874 $params = array_merge($this->_params, array(
1875 'contribution_status_id' => 2,
1876 'address_id' => $address['id'],
1877 )
1878 );
1879 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1880 $this->callAPISuccess('contribution', 'completetransaction', array(
1881 'id' => $contribution['id'],
1882 ));
1883 $mut->checkMailLog(array(
1884 'address:::contribution billing st',
1885 ));
1886
1887 // Scenario 3: Contribution wtth no address but contact has a billing address
1888 $this->callAPISuccess('address', 'create', array(
1889 'id' => $address['id'],
1890 'street_address' => 'is billing st',
1891 'contact_id' => $this->_params['contact_id'],
1892 ));
1893 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1894 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1895 $this->callAPISuccess('contribution', 'completetransaction', array(
1896 'id' => $contribution['id'],
1897 ));
1898 $mut->checkMailLog(array(
1899 'address:::is billing st',
1900 ));
1901
1902 $mut->stop();
1903 $this->revertTemplateToReservedTemplate();
1904 }
1905
1906 /**
1907 * Test completing a transaction via the API.
1908 *
1909 * Note that we are creating a logged in user because email goes out from
1910 * that person
1911 */
1912 public function testCompleteTransactionFeeAmount() {
1913 $this->createLoggedInUser();
1914 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1915 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1916 $this->callAPISuccess('contribution', 'completetransaction', array(
1917 'id' => $contribution['id'],
1918 'fee_amount' => '.56',
1919 'trxn_id' => '7778888',
1920 ));
1921 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'], 'sequential' => 1));
1922 $this->assertEquals('Completed', $contribution['contribution_status']);
1923 $this->assertEquals('7778888', $contribution['trxn_id']);
1924 $this->assertEquals('.56', $contribution['fee_amount']);
1925 $this->assertEquals('99.44', $contribution['net_amount']);
1926 }
1927
1928 /**
1929 * CRM-19126 Add test to verify when complete transaction is called tax amount is not changed
1930 */
1931 public function testCheckTaxAmount() {
1932 $contact = $this->createLoggedInUser();
1933 $financialType = $this->callAPISuccess('financial_type', 'create', array(
1934 'name' => 'Test taxable financial Type',
1935 'is_reserved' => 0,
1936 'is_active' => 1,
1937 ));
1938 $financialAccount = $this->callAPISuccess('financial_account', 'create', array(
1939 'name' => 'Test Tax financial account ',
1940 'contact_id' => $contact,
1941 'financial_account_type_id' => 2,
1942 'is_tax' => 1,
1943 'tax_rate' => 5.00,
1944 'is_reserved' => 0,
1945 'is_active' => 1,
1946 'is_default' => 0,
1947 ));
1948 $financialTypeId = $financialType['id'];
1949 $financialAccountId = $financialAccount['id'];
1950 $financialAccountParams = array(
1951 'entity_table' => 'civicrm_financial_type',
1952 'entity_id' => $financialTypeId,
1953 'account_relationship' => 10,
1954 'financial_account_id' => $financialAccountId,
1955 );
1956 CRM_Financial_BAO_FinancialTypeAccount::add($financialAccountParams);
1957
1958 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'financial_type_id' => $financialTypeId));
1959 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1960 $contribution1 = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'return' => 'tax_amount', 'sequential' => 1));
1961 $this->callAPISuccess('contribution', 'completetransaction', array(
1962 'id' => $contribution['id'],
1963 'trxn_id' => '777788888',
1964 'fee_amount' => '6.00',
1965 ));
1966 $contribution2 = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'return' => array('tax_amount', 'fee_amount', 'net_amount'), 'sequential' => 1));
1967 $this->assertEquals($contribution1['values'][0]['tax_amount'], $contribution2['values'][0]['tax_amount']);
1968 $this->assertEquals('6.00', $contribution2['values'][0]['fee_amount']);
1969 $this->assertEquals('99.00', $contribution2['values'][0]['net_amount']);
1970 }
1971
1972 /**
1973 * Test repeat contribution successfully creates line item.
1974 */
1975 public function testRepeatTransaction() {
1976 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'single');
1977 $this->callAPISuccess('contribution', 'repeattransaction', array(
1978 'original_contribution_id' => $originalContribution['id'],
1979 'contribution_status_id' => 'Completed',
1980 'trxn_id' => uniqid(),
1981 ));
1982 $lineItemParams = array(
1983 'entity_id' => $originalContribution['id'],
1984 'sequential' => 1,
1985 'return' => array(
1986 'entity_table',
1987 'qty',
1988 'unit_price',
1989 'line_total',
1990 'label',
1991 'financial_type_id',
1992 'deductible_amount',
1993 'price_field_value_id',
1994 'price_field_id',
1995 ),
1996 );
1997 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1998 'entity_id' => $originalContribution['id'],
1999 )));
2000 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2001 'entity_id' => $originalContribution['id'] + 1,
2002 )));
2003 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
2004 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2005 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
2006 $this->_checkFinancialRecords(array(
2007 'id' => $originalContribution['id'] + 1,
2008 'payment_instrument_id' => $this->callAPISuccessGetValue('PaymentProcessor', array(
2009 'id' => $originalContribution['payment_processor_id'],
2010 'return' => 'payment_instrument_id',
2011 )),
2012 ), 'online');
2013 $this->quickCleanUpFinancialEntities();
2014 }
2015
2016 /**
2017 * Test repeat contribution successfully creates line items (plural).
2018 */
2019 public function testRepeatTransactionLineItems() {
2020 // CRM-19309
2021 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'multiple');
2022 $this->callAPISuccess('contribution', 'repeattransaction', array(
2023 'original_contribution_id' => $originalContribution['id'],
2024 'contribution_status_id' => 'Completed',
2025 'trxn_id' => uniqid(),
2026 ));
2027
2028 $lineItemParams = array(
2029 'entity_id' => $originalContribution['id'],
2030 'sequential' => 1,
2031 'return' => array(
2032 'entity_table',
2033 'qty',
2034 'unit_price',
2035 'line_total',
2036 'label',
2037 'financial_type_id',
2038 'deductible_amount',
2039 'price_field_value_id',
2040 'price_field_id',
2041 ),
2042 );
2043 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2044 'entity_id' => $originalContribution['id'],
2045 )));
2046 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2047 'entity_id' => $originalContribution['id'] + 1,
2048 )));
2049
2050 // unset id and entity_id for all of them to be able to compare the lineItems:
2051 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
2052 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2053 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
2054
2055 unset($lineItem1['values'][1]['id'], $lineItem1['values'][1]['entity_id']);
2056 unset($lineItem2['values'][1]['id'], $lineItem2['values'][1]['entity_id']);
2057 $this->assertEquals($lineItem1['values'][1], $lineItem2['values'][1]);
2058
2059 // CRM-19309 so in future we also want to:
2060 // check that financial_line_items have been created for entity_id 3 and 4;
2061
2062 $this->callAPISuccessGetCount('FinancialItem', array('description' => 'Sales Tax', 'amount' => 0), 0);
2063 $this->quickCleanUpFinancialEntities();
2064 }
2065
2066 /**
2067 * Test repeat contribution successfully creates is_test transaction.
2068 */
2069 public function testRepeatTransactionIsTest() {
2070 $this->_params['is_test'] = 1;
2071 $originalContribution = $this->setUpRepeatTransaction(array('is_test' => 1), 'single');
2072
2073 $this->callAPISuccess('contribution', 'repeattransaction', array(
2074 'original_contribution_id' => $originalContribution['id'],
2075 'contribution_status_id' => 'Completed',
2076 'trxn_id' => uniqid(),
2077 ));
2078 $this->callAPISuccessGetCount('Contribution', array('contribution_test' => 1), 2);
2079 }
2080
2081 /**
2082 * Test repeat contribution passed in status.
2083 */
2084 public function testRepeatTransactionPassedInStatus() {
2085 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'single');
2086
2087 $this->callAPISuccess('contribution', 'repeattransaction', array(
2088 'original_contribution_id' => $originalContribution['id'],
2089 'contribution_status_id' => 'Pending',
2090 'trxn_id' => uniqid(),
2091 ));
2092 $this->callAPISuccessGetCount('Contribution', array('contribution_status_id' => 2), 1);
2093 }
2094
2095 /**
2096 * Test repeat contribution accepts recur_id instead of original_contribution_id.
2097 */
2098 public function testRepeatTransactionAcceptRecurID() {
2099 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2100 'contact_id' => $this->_individualId,
2101 'installments' => '12',
2102 'frequency_interval' => '1',
2103 'amount' => '100',
2104 'contribution_status_id' => 1,
2105 'start_date' => '2012-01-01 00:00:00',
2106 'currency' => 'USD',
2107 'frequency_unit' => 'month',
2108 'payment_processor_id' => $this->paymentProcessorID,
2109 ));
2110 $this->callAPISuccess('contribution', 'create', array_merge(
2111 $this->_params,
2112 array('contribution_recur_id' => $contributionRecur['id']))
2113 );
2114
2115 $this->callAPISuccess('contribution', 'repeattransaction', array(
2116 'contribution_recur_id' => $contributionRecur['id'],
2117 'contribution_status_id' => 'Completed',
2118 'trxn_id' => uniqid(),
2119 ));
2120
2121 $this->quickCleanUpFinancialEntities();
2122 }
2123
2124 /**
2125 * CRM-19873 Test repattransaction if contribution_recur_id is a test.
2126 */
2127 public function testRepeatTransactionTestRecurId() {
2128 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2129 'contact_id' => $this->_individualId,
2130 'frequency_interval' => '1',
2131 'amount' => '1.00',
2132 'contribution_status_id' => 1,
2133 'start_date' => '2017-01-01 00:00:00',
2134 'currency' => 'USD',
2135 'frequency_unit' => 'month',
2136 'payment_processor_id' => $this->paymentProcessorID,
2137 'is_test' => 1,
2138 ));
2139 $this->callAPISuccess('contribution', 'create', array_merge(
2140 $this->_params,
2141 array(
2142 'contribution_recur_id' => $contributionRecur['id'],
2143 'is_test' => 1,
2144 ))
2145 );
2146
2147 $repeatedContribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
2148 'contribution_recur_id' => $contributionRecur['id'],
2149 'contribution_status_id' => 'Completed',
2150 'trxn_id' => uniqid(),
2151 ));
2152
2153 $this->assertEquals($contributionRecur['values'][1]['is_test'], $repeatedContribution['values'][2]['is_test']);
2154 $this->quickCleanUpFinancialEntities();
2155 }
2156 /**
2157 * CRM-19945 Tests repeattransaction is using a completed contribution for the template.
2158 *
2159 * ( Tests membership is renewed after repeattransaction. )
2160 */
2161 public function testRepeatTransactionUsesCompleted() {
2162 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
2163
2164 $this->callAPISuccess('contribution', 'create', array(
2165 'contact_id' => $originalContribution['values'][1]['contact_id'],
2166 'financial_type_id' => $originalContribution['values'][1]['financial_type_id'],
2167 'total_amount' => $originalContribution['values'][1]['total_amount'],
2168 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2169 'contribution_status_id' => "Failed",
2170 ));
2171
2172 $this->callAPISuccess('membership', 'create', array(
2173 'id' => $membership['id'],
2174 'end_date' => 'yesterday',
2175 'status_id' => 4,
2176 ));
2177
2178 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
2179 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2180 'contribution_status_id' => 'Completed',
2181 'trxn_id' => 'bobsled',
2182 ));
2183
2184 $membershipStatusId = $this->callAPISuccess('membership', 'getvalue', array(
2185 'id' => $membership['id'],
2186 'return' => 'status_id',
2187 ));
2188
2189 $this->assertEquals('New', CRM_Core_PseudoConstant::getLabel('CRM_Member_BAO_Membership', 'status_id', $membershipStatusId));
2190
2191 $lineItem = $this->callAPISuccessGetSingle('LineItem', array('contribution_id' => $contribution['id']));
2192 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
2193 $this->callAPISuccessGetCount('MembershipPayment', array('membership_id' => $membership['id']));
2194 $this->quickCleanUpFinancialEntities();
2195 $this->contactDelete($originalContribution['values'][1]['contact_id']);
2196 }
2197
2198 /**
2199 * CRM-16397 test appropriate action if total amount has changed for single line items.
2200 */
2201 public function testRepeatTransactionAlteredAmount() {
2202 $paymentProcessorID = $this->paymentProcessorCreate();
2203 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2204 'contact_id' => $this->_individualId,
2205 'installments' => '12',
2206 'frequency_interval' => '1',
2207 'amount' => '500',
2208 'contribution_status_id' => 1,
2209 'start_date' => '2012-01-01 00:00:00',
2210 'currency' => 'USD',
2211 'frequency_unit' => 'month',
2212 'payment_processor_id' => $paymentProcessorID,
2213 ));
2214 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2215 $this->_params,
2216 array(
2217 'contribution_recur_id' => $contributionRecur['id'],
2218 ))
2219 );
2220
2221 $this->callAPISuccess('contribution', 'repeattransaction', array(
2222 'original_contribution_id' => $originalContribution['id'],
2223 'contribution_status_id' => 'Completed',
2224 'trxn_id' => uniqid(),
2225 'total_amount' => '400',
2226 'fee_amount' => 50,
2227 ));
2228
2229 $lineItemParams = array(
2230 'entity_id' => $originalContribution['id'],
2231 'sequential' => 1,
2232 'return' => array(
2233 'entity_table',
2234 'qty',
2235 'unit_price',
2236 'line_total',
2237 'label',
2238 'financial_type_id',
2239 'deductible_amount',
2240 'price_field_value_id',
2241 'price_field_id',
2242 ),
2243 );
2244 $this->callAPISuccessGetSingle('contribution', array(
2245 'total_amount' => 400,
2246 'fee_amount' => 50,
2247 'net_amount' => 350,
2248 ));
2249 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2250 'entity_id' => $originalContribution['id'],
2251 )));
2252 $expectedLineItem = array_merge(
2253 $lineItem1['values'][0], array(
2254 'line_total' => '400.00',
2255 'unit_price' => '400.00',
2256 )
2257 );
2258
2259 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2260 'entity_id' => $originalContribution['id'] + 1,
2261 )));
2262
2263 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2264 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2265 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2266 }
2267
2268 /**
2269 * CRM-17718 test appropriate action if financial type has changed for single line items.
2270 */
2271 public function testRepeatTransactionPassedInFinancialType() {
2272 $originalContribution = $this->setUpRecurringContribution();
2273
2274 $this->callAPISuccess('contribution', 'repeattransaction', array(
2275 'original_contribution_id' => $originalContribution['id'],
2276 'contribution_status_id' => 'Completed',
2277 'trxn_id' => uniqid(),
2278 'financial_type_id' => 2,
2279 ));
2280 $lineItemParams = array(
2281 'entity_id' => $originalContribution['id'],
2282 'sequential' => 1,
2283 'return' => array(
2284 'entity_table',
2285 'qty',
2286 'unit_price',
2287 'line_total',
2288 'label',
2289 'financial_type_id',
2290 'deductible_amount',
2291 'price_field_value_id',
2292 'price_field_id',
2293 ),
2294 );
2295
2296 $this->callAPISuccessGetSingle('contribution', array(
2297 'total_amount' => 100,
2298 'financial_type_id' => 2,
2299 ));
2300 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2301 'entity_id' => $originalContribution['id'],
2302 )));
2303 $expectedLineItem = array_merge(
2304 $lineItem1['values'][0], array(
2305 'line_total' => '100.00',
2306 'unit_price' => '100.00',
2307 'financial_type_id' => 2,
2308 'contribution_type_id' => 2,
2309 )
2310 );
2311 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2312 'entity_id' => $originalContribution['id'] + 1,
2313 )));
2314 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2315 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2316 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2317 }
2318
2319 /**
2320 * CRM-17718 test appropriate action if financial type has changed for single line items.
2321 */
2322 public function testRepeatTransactionUpdatedFinancialType() {
2323 $originalContribution = $this->setUpRecurringContribution(array(), array('financial_type_id' => 2));
2324
2325 $this->callAPISuccess('contribution', 'repeattransaction', array(
2326 'contribution_recur_id' => $originalContribution['id'],
2327 'contribution_status_id' => 'Completed',
2328 'trxn_id' => uniqid(),
2329 ));
2330 $lineItemParams = array(
2331 'entity_id' => $originalContribution['id'],
2332 'sequential' => 1,
2333 'return' => array(
2334 'entity_table',
2335 'qty',
2336 'unit_price',
2337 'line_total',
2338 'label',
2339 'financial_type_id',
2340 'deductible_amount',
2341 'price_field_value_id',
2342 'price_field_id',
2343 ),
2344 );
2345
2346 $this->callAPISuccessGetSingle('contribution', array(
2347 'total_amount' => 100,
2348 'financial_type_id' => 2,
2349 ));
2350 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2351 'entity_id' => $originalContribution['id'],
2352 )));
2353 $expectedLineItem = array_merge(
2354 $lineItem1['values'][0], array(
2355 'line_total' => '100.00',
2356 'unit_price' => '100.00',
2357 'financial_type_id' => 2,
2358 'contribution_type_id' => 2,
2359 )
2360 );
2361
2362 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2363 'entity_id' => $originalContribution['id'] + 1,
2364 )));
2365 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2366 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2367 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2368 }
2369
2370 /**
2371 * CRM-16397 test appropriate action if campaign has been passed in.
2372 */
2373 public function testRepeatTransactionPassedInCampaign() {
2374 $paymentProcessorID = $this->paymentProcessorCreate();
2375 $campaignID = $this->campaignCreate();
2376 $campaignID2 = $this->campaignCreate();
2377 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2378 'contact_id' => $this->_individualId,
2379 'installments' => '12',
2380 'frequency_interval' => '1',
2381 'amount' => '100',
2382 'contribution_status_id' => 1,
2383 'start_date' => '2012-01-01 00:00:00',
2384 'currency' => 'USD',
2385 'frequency_unit' => 'month',
2386 'payment_processor_id' => $paymentProcessorID,
2387 ));
2388 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2389 $this->_params,
2390 array(
2391 'contribution_recur_id' => $contributionRecur['id'],
2392 'campaign_id' => $campaignID,
2393 ))
2394 );
2395
2396 $this->callAPISuccess('contribution', 'repeattransaction', array(
2397 'original_contribution_id' => $originalContribution['id'],
2398 'contribution_status_id' => 'Completed',
2399 'trxn_id' => uniqid(),
2400 'campaign_id' => $campaignID2,
2401 ));
2402
2403 $this->callAPISuccessGetSingle('contribution', array(
2404 'total_amount' => 100,
2405 'campaign_id' => $campaignID2,
2406 ));
2407 }
2408
2409 /**
2410 * CRM-17718 campaign stored on contribution recur gets priority.
2411 *
2412 * This reflects the fact we permit people to update them.
2413 */
2414 public function testRepeatTransactionUpdatedCampaign() {
2415 $paymentProcessorID = $this->paymentProcessorCreate();
2416 $campaignID = $this->campaignCreate();
2417 $campaignID2 = $this->campaignCreate();
2418 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2419 'contact_id' => $this->_individualId,
2420 'installments' => '12',
2421 'frequency_interval' => '1',
2422 'amount' => '100',
2423 'contribution_status_id' => 1,
2424 'start_date' => '2012-01-01 00:00:00',
2425 'currency' => 'USD',
2426 'frequency_unit' => 'month',
2427 'payment_processor_id' => $paymentProcessorID,
2428 'campaign_id' => $campaignID,
2429 ));
2430 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2431 $this->_params,
2432 array(
2433 'contribution_recur_id' => $contributionRecur['id'],
2434 'campaign_id' => $campaignID2,
2435 ))
2436 );
2437
2438 $this->callAPISuccess('contribution', 'repeattransaction', array(
2439 'original_contribution_id' => $originalContribution['id'],
2440 'contribution_status_id' => 'Completed',
2441 'trxn_id' => uniqid(),
2442 ));
2443
2444 $this->callAPISuccessGetSingle('contribution', array(
2445 'total_amount' => 100,
2446 'campaign_id' => $campaignID,
2447 ));
2448 }
2449
2450 /**
2451 * Test completing a transaction does not 'mess' with net amount (CRM-15960).
2452 */
2453 public function testCompleteTransactionNetAmountOK() {
2454 $this->createLoggedInUser();
2455 $params = array_merge($this->_params, array('contribution_status_id' => 2));
2456 unset($params['net_amount']);
2457 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2458 $this->callAPISuccess('contribution', 'completetransaction', array(
2459 'id' => $contribution['id'],
2460 ));
2461 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
2462 $this->assertEquals('Completed', $contribution['contribution_status']);
2463 $this->assertTrue(($contribution['total_amount'] - $contribution['net_amount']) == $contribution['fee_amount']);
2464 }
2465
2466 /**
2467 * CRM-14151 - Test completing a transaction via the API.
2468 */
2469 public function testCompleteTransactionWithReceiptDateSet() {
2470 $this->swapMessageTemplateForTestTemplate();
2471 $mut = new CiviMailUtils($this, TRUE);
2472 $this->createLoggedInUser();
2473 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
2474 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2475 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id'], 'trxn_date' => date('Y-m-d')));
2476 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
2477 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
2478 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['values'][0]['receive_date'])));
2479 $mut->checkMailLog(array(
2480 'Receipt - Contribution',
2481 'receipt_date:::' . date('Ymd'),
2482 ));
2483 $mut->stop();
2484 $this->revertTemplateToReservedTemplate();
2485 }
2486
2487 /**
2488 * CRM-1960 - Test to ensure that completetransaction respects the is_email_receipt setting
2489 */
2490 public function testCompleteTransactionWithEmailReceiptInput() {
2491 $contributionPage = $this->createReceiptableContributionPage();
2492
2493 $this->_params['contribution_page_id'] = $contributionPage['id'];
2494 $params = array_merge($this->_params, array('contribution_status_id' => 2));
2495 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2496 // Complete the transaction overriding is_email_receipt to = FALSE
2497 $this->callAPISuccess('contribution', 'completetransaction', array(
2498 'id' => $contribution['id'],
2499 'trxn_date' => date('2011-04-09'),
2500 'trxn_id' => 'kazam',
2501 'is_email_receipt' => 0,
2502 ));
2503 // Check if a receipt was issued
2504 $receipt_date = $this->callAPISuccess('Contribution', 'getvalue', array('id' => $contribution['id'], 'return' => 'receipt_date'));
2505 $this->assertEquals('', $receipt_date);
2506 }
2507
2508 /**
2509 * Test that $is_recur is assigned to the receipt.
2510 */
2511 public function testCompleteTransactionForRecurring() {
2512
2513 $this->swapMessageTemplateForTestTemplate();
2514 $recurring = $this->setUpRecurringContribution();
2515 $contributionPage = $this->createReceiptableContributionPage(array('is_recur' => TRUE, 'recur_frequency_unit' => 'month', 'recur_interval' => 1));
2516
2517 $this->_params['contribution_page_id'] = $contributionPage['id'];
2518 $this->_params['contribution_recur_id'] = $recurring['id'];
2519
2520 $contribution = $this->setUpForCompleteTransaction();
2521
2522 $this->callAPISuccess('contribution', 'completetransaction', array(
2523 'id' => $contribution['id'],
2524 'trxn_date' => date('2011-04-09'),
2525 'trxn_id' => 'kazam',
2526 'is_email_receipt' => 1,
2527 ));
2528
2529 $this->mut->checkMailLog(array(
2530 'is_recur:::1',
2531 'cancelSubscriptionUrl:::' . CIVICRM_UF_BASEURL,
2532 ));
2533 $this->mut->stop();
2534 $this->revertTemplateToReservedTemplate();
2535 }
2536 /**
2537 * CRM-19710 - Test to ensure that completetransaction respects the input for is_email_receipt setting.
2538 *
2539 * If passed in it will override the default from contribution page.
2540 */
2541 public function testCompleteTransactionWithEmailReceiptInputTrue() {
2542 $mut = new CiviMailUtils($this, TRUE);
2543 $this->createLoggedInUser();
2544 // Create a Contribution Page with is_email_receipt = FALSE
2545 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array(
2546 'receipt_from_name' => 'Mickey Mouse',
2547 'receipt_from_email' => 'mickey@mouse.com',
2548 'title' => "Test Contribution Page",
2549 'financial_type_id' => 1,
2550 'currency' => 'CAD',
2551 'is_monetary' => TRUE,
2552 'is_email_receipt' => 0,
2553 ));
2554 $this->_params['contribution_page_id'] = $contributionPage['id'];
2555 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
2556 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2557 // Complete the transaction overriding is_email_receipt to = TRUE
2558 $this->callAPISuccess('contribution', 'completetransaction', array(
2559 'id' => $contribution['id'],
2560 'is_email_receipt' => 1,
2561 ));
2562 $mut->checkMailLog(array(
2563 'Please print this receipt for your records.',
2564 ));
2565 $mut->stop();
2566 }
2567
2568 /**
2569 * Complete the transaction using the template with all the possible.
2570 */
2571 public function testCompleteTransactionWithTestTemplate() {
2572 $this->swapMessageTemplateForTestTemplate();
2573 $contribution = $this->setUpForCompleteTransaction();
2574 $this->callAPISuccess('contribution', 'completetransaction', array(
2575 'id' => $contribution['id'],
2576 'trxn_date' => date('2011-04-09'),
2577 'trxn_id' => 'kazam',
2578 ));
2579 $receive_date = $this->callAPISuccess('Contribution', 'getvalue', array('id' => $contribution['id'], 'return' => 'receive_date'));
2580 $this->mut->checkMailLog(array(
2581 'email:::anthony_anderson@civicrm.org',
2582 'is_monetary:::1',
2583 'amount:::100.00',
2584 'currency:::USD',
2585 'receive_date:::' . date('Ymd', strtotime($receive_date)),
2586 'receipt_date:::' . date('Ymd'),
2587 'contributeMode:::notify',
2588 'title:::Contribution',
2589 'displayName:::Mr. Anthony Anderson II',
2590 'trxn_id:::kazam',
2591 'contactID:::' . $this->_params['contact_id'],
2592 'contributionID:::' . $contribution['id'],
2593 'financialTypeId:::1',
2594 'financialTypeName:::Donation',
2595 ));
2596 $this->mut->stop();
2597 $this->revertTemplateToReservedTemplate();
2598 }
2599
2600 /**
2601 * Complete the transaction using the template with all the possible.
2602 */
2603 public function testCompleteTransactionContributionPageFromAddress() {
2604 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array(
2605 'receipt_from_name' => 'Mickey Mouse',
2606 'receipt_from_email' => 'mickey@mouse.com',
2607 'title' => "Test Contribution Page",
2608 'financial_type_id' => 1,
2609 'currency' => 'NZD',
2610 'goal_amount' => 50,
2611 'is_pay_later' => 1,
2612 'is_monetary' => TRUE,
2613 'is_email_receipt' => TRUE,
2614 ));
2615 $this->_params['contribution_page_id'] = $contributionPage['id'];
2616 $contribution = $this->setUpForCompleteTransaction();
2617 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id']));
2618 $this->mut->checkMailLog(array(
2619 'mickey@mouse.com',
2620 'Mickey Mouse <',
2621 ));
2622 $this->mut->stop();
2623 }
2624
2625 /**
2626 * Test completing first transaction in a recurring series.
2627 *
2628 * The status should be set to 'in progress' and the next scheduled payment date calculated.
2629 *
2630 * @dataProvider getScheduledDateData
2631 *
2632 * @param array $dataSet
2633 *
2634 * @throws \Exception
2635 */
2636 public function testCompleteTransactionSetStatusToInProgress($dataSet) {
2637 $paymentProcessorID = $this->paymentProcessorCreate();
2638 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
2639 'contact_id' => $this->_individualId,
2640 'installments' => '2',
2641 'frequency_interval' => '1',
2642 'amount' => '500',
2643 'contribution_status_id' => 'Pending',
2644 'start_date' => '2012-01-01 00:00:00',
2645 'currency' => 'USD',
2646 'frequency_unit' => 'month',
2647 'payment_processor_id' => $paymentProcessorID,
2648 ), $dataSet['data']));
2649 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(
2650 $this->_params,
2651 array(
2652 'contribution_recur_id' => $contributionRecur['id'],
2653 'contribution_status_id' => 'Pending',
2654 'receive_date' => $dataSet['receive_date'],
2655 ))
2656 );
2657 $this->callAPISuccess('Contribution', 'completetransaction', array(
2658 'id' => $contribution,
2659 'receive_date' => $dataSet['receive_date'],
2660 ));
2661 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array(
2662 'id' => $contributionRecur['id'],
2663 'return' => array('next_sched_contribution_date', 'contribution_status_id'),
2664 ));
2665 $this->assertEquals(5, $contributionRecur['contribution_status_id']);
2666 $this->assertEquals($dataSet['expected'], $contributionRecur['next_sched_contribution_date']);
2667 $this->callAPISuccess('Contribution', 'create', array_merge(
2668 $this->_params,
2669 array(
2670 'contribution_recur_id' => $contributionRecur['id'],
2671 'contribution_status_id' => 'Completed',
2672 )
2673 ));
2674 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array(
2675 'id' => $contributionRecur['id'],
2676 'return' => array('contribution_status_id'),
2677 ));
2678 $this->assertEquals(1, $contributionRecur['contribution_status_id']);
2679 }
2680
2681 /**
2682 * Get dates for testing.
2683 *
2684 * @return array
2685 */
2686 public function getScheduledDateData() {
2687 $result = array();
2688 $result[]['2016-08-31-1-month'] = array(
2689 'data' => array(
2690 'start_date' => '2016-08-31',
2691 'frequency_interval' => 1,
2692 'frequency_unit' => 'month',
2693 ),
2694 'receive_date' => '2016-08-31',
2695 'expected' => '2016-10-01 00:00:00',
2696 );
2697 $result[]['2012-01-01-1-month'] = array(
2698 'data' => array(
2699 'start_date' => '2012-01-01',
2700 'frequency_interval' => 1,
2701 'frequency_unit' => 'month',
2702 ),
2703 'receive_date' => '2012-01-01',
2704 'expected' => '2012-02-01 00:00:00',
2705 );
2706 $result[]['2012-01-01-1-month'] = array(
2707 'data' => array(
2708 'start_date' => '2012-01-01',
2709 'frequency_interval' => 1,
2710 'frequency_unit' => 'month',
2711 ),
2712 'receive_date' => '2012-02-29',
2713 'expected' => '2012-03-29 00:00:00',
2714 );
2715 $result['receive_date_includes_time']['2012-01-01-1-month'] = array(
2716 'data' => array(
2717 'start_date' => '2012-01-01',
2718 'frequency_interval' => 1,
2719 'frequency_unit' => 'month',
2720 'next_sched_contribution_date' => '2012-02-29',
2721 ),
2722 'receive_date' => '2012-02-29 16:00:00',
2723 'expected' => '2012-03-29 00:00:00',
2724 );
2725 return $result;
2726 }
2727
2728 /**
2729 * Test completing a pledge with the completeTransaction api..
2730 *
2731 * Note that we are creating a logged in user because email goes out from
2732 * that person.
2733 */
2734 public function testCompleteTransactionUpdatePledgePayment() {
2735 $this->swapMessageTemplateForTestTemplate();
2736 $mut = new CiviMailUtils($this, TRUE);
2737 $mut->clearMessages();
2738 $this->createLoggedInUser();
2739 $contributionID = $this->createPendingPledgeContribution();
2740 $this->callAPISuccess('contribution', 'completetransaction', array(
2741 'id' => $contributionID,
2742 'trxn_date' => '1 Feb 2013',
2743 ));
2744 $pledge = $this->callAPISuccessGetSingle('Pledge', array(
2745 'id' => $this->_ids['pledge'],
2746 ));
2747 $this->assertEquals('Completed', $pledge['pledge_status']);
2748
2749 $status = $this->callAPISuccessGetValue('PledgePayment', array(
2750 'pledge_id' => $this->_ids['pledge'],
2751 'return' => 'status_id',
2752 ));
2753 $this->assertEquals(1, $status);
2754 $mut->checkMailLog(array(
2755 'amount:::500.00',
2756 'receive_date:::20130201000000',
2757 "receipt_date:::\n",
2758 ));
2759 $mut->stop();
2760 $this->revertTemplateToReservedTemplate();
2761 }
2762
2763 /**
2764 * Test completing a transaction with an event via the API.
2765 *
2766 * Note that we are creating a logged in user because email goes out from
2767 * that person
2768 */
2769 public function testCompleteTransactionWithParticipantRecord() {
2770 $mut = new CiviMailUtils($this, TRUE);
2771 $mut->clearMessages();
2772 $this->_individualId = $this->createLoggedInUser();
2773 $contributionID = $this->createPendingParticipantContribution();
2774 $this->callAPISuccess('contribution', 'completetransaction', array(
2775 'id' => $contributionID,
2776 )
2777 );
2778 $participantStatus = $this->callAPISuccessGetValue('participant', array(
2779 'id' => $this->_ids['participant'],
2780 'return' => 'participant_status_id',
2781 ));
2782 $this->assertEquals(1, $participantStatus);
2783
2784 //Assert only three activities are created.
2785 $activities = CRM_Activity_BAO_Activity::getContactActivity($this->_individualId);
2786 $this->assertEquals(3, count($activities));
2787 $activityNames = array_count_values(CRM_Utils_Array::collect('activity_name', $activities));
2788 // record two activities before and after completing payment for Event registration
2789 $this->assertEquals(2, $activityNames['Event Registration']);
2790 // update the original 'Contribution' activity created after completing payment
2791 $this->assertEquals(1, $activityNames['Contribution']);
2792
2793 $mut->checkMailLog(array(
2794 'Annual CiviCRM meet',
2795 'Event',
2796 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
2797 ));
2798 $mut->stop();
2799 }
2800
2801 /**
2802 * Test membership is renewed when transaction completed.
2803 */
2804 public function testCompleteTransactionMembershipPriceSet() {
2805 $this->createPriceSetWithPage('membership');
2806 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', array(
2807 'name' => 'Grace',
2808 'return' => 'id')
2809 );
2810 $this->setUpPendingContribution($this->_ids['price_field_value'][0]);
2811 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
2812 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
2813 'membership_id' => $this->_ids['membership'],
2814 ));
2815 $this->assertEquals(1, $logs['count']);
2816 $this->assertEquals($stateOfGrace, $membership['status_id']);
2817 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
2818 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
2819 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
2820 $this->callAPISuccessGetSingle('LineItem', array(
2821 'entity_id' => $this->_ids['membership'],
2822 'entity_table' => 'civicrm_membership',
2823 ));
2824 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
2825 'membership_id' => $this->_ids['membership'],
2826 ));
2827 $this->assertEquals(2, $logs['count']);
2828 $this->assertNotEquals($stateOfGrace, $logs['values'][2]['status_id']);
2829 //Assert only three activities are created.
2830 $activities = CRM_Activity_BAO_Activity::getContactActivity($this->_ids['contact']);
2831 $this->assertEquals(3, count($activities));
2832 $activityNames = array_flip(CRM_Utils_Array::collect('activity_name', $activities));
2833 $this->assertArrayHasKey('Contribution', $activityNames);
2834 $this->assertArrayHasKey('Membership Signup', $activityNames);
2835 $this->assertArrayHasKey('Change Membership Status', $activityNames);
2836 $this->cleanUpAfterPriceSets();
2837 }
2838
2839 /**
2840 * Test if renewal activity is create after changing Pending contribution to Completed via offline
2841 */
2842 public function testPendingToCompleteContribution() {
2843 $contributionPage = $this->createPriceSetWithPage('membership');
2844 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', array(
2845 'name' => 'Grace',
2846 'return' => 'id')
2847 );
2848 $this->setUpPendingContribution($this->_ids['price_field_value'][0]);
2849 $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
2850 // Case 1: Assert that Membership Signup Activity is created on Pending to Completed Contribution via backoffice
2851 $activity = $this->callAPISuccess('Activity', 'get', array(
2852 'activity_type_id' => 'Membership Signup',
2853 'source_record_id' => $this->_ids['membership'],
2854 'status_id' => 'Scheduled',
2855 ));
2856 $this->assertEquals(1, $activity['count']);
2857
2858 // change pending contribution to completed
2859 $form = new CRM_Contribute_Form_Contribution();
2860 $error = FALSE;
2861 $form->_params = array(
2862 'id' => $this->_ids['contribution'],
2863 'total_amount' => 20,
2864 'net_amount' => 20,
2865 'fee_amount' => 0,
2866 'financial_type_id' => 1,
2867 'receive_date' => '04/21/2015',
2868 'receive_date_time' => '11:27PM',
2869 'contact_id' => $this->_individualId,
2870 'contribution_status_id' => 1,
2871 'billing_middle_name' => '',
2872 'billing_last_name' => 'Adams',
2873 'billing_street_address-5' => '790L Lincoln St S',
2874 'billing_city-5' => 'Maryknoll',
2875 'billing_state_province_id-5' => 1031,
2876 'billing_postal_code-5' => 10545,
2877 'billing_country_id-5' => 1228,
2878 'frequency_interval' => 1,
2879 'frequency_unit' => 'month',
2880 'installments' => '',
2881 'hidden_AdditionalDetail' => 1,
2882 'hidden_Premium' => 1,
2883 'from_email_address' => '"civi45" <civi45@civicrm.com>',
2884 'receipt_date' => '',
2885 'receipt_date_time' => '',
2886 'payment_processor_id' => $this->paymentProcessorID,
2887 'currency' => 'USD',
2888 'contribution_page_id' => $this->_ids['contribution_page'],
2889 'contribution_mode' => 'membership',
2890 'source' => 'Membership Signup and Renewal',
2891 );
2892 try {
2893 $form->testSubmit($form->_params, CRM_Core_Action::UPDATE);
2894 }
2895 catch (Civi\Payment\Exception\PaymentProcessorException $e) {
2896 $error = TRUE;
2897 }
2898 // Case 2: After successful payment for Pending backoffice there are three activities created
2899 // 2.a Update status of existing Scheduled Membership Signup (created in step 1) to Completed
2900 $activity = $this->callAPISuccess('Activity', 'get', array(
2901 'activity_type_id' => 'Membership Signup',
2902 'source_record_id' => $this->_ids['membership'],
2903 'status_id' => 'Completed',
2904 ));
2905 $this->assertEquals(1, $activity['count']);
2906 // 2.b Contribution activity created to record successful payment
2907 $activity = $this->callAPISuccess('Activity', 'get', array(
2908 'activity_type_id' => 'Contribution',
2909 'source_record_id' => $this->_ids['contribution'],
2910 'status_id' => 'Completed',
2911 ));
2912 $this->assertEquals(1, $activity['count']);
2913
2914 // 2.c 'Change membership type' activity created to record Membership status change from Grace to Current
2915 $activity = $this->callAPISuccess('Activity', 'get', array(
2916 'activity_type_id' => 'Change Membership Status',
2917 'source_record_id' => $this->_ids['membership'],
2918 'status_id' => 'Completed',
2919 ));
2920 $this->assertEquals(1, $activity['count']);
2921 $this->assertEquals('Status changed from Grace to Current', $activity['values'][$activity['id']]['subject']);
2922
2923 //Create another pending contribution for renewal
2924 $contribution = $this->callAPISuccess('contribution', 'create', array(
2925 'domain_id' => 1,
2926 'contact_id' => $this->_ids['contact'],
2927 'receive_date' => date('Ymd'),
2928 'total_amount' => 20.00,
2929 'financial_type_id' => 1,
2930 'payment_instrument_id' => 'Credit Card',
2931 'non_deductible_amount' => 10.00,
2932 'trxn_id' => 'rdhfi88',
2933 'invoice_id' => 'dofhiewuyr',
2934 'source' => 'SSF',
2935 'contribution_status_id' => 2,
2936 'contribution_page_id' => $this->_ids['contribution_page'],
2937 'api.membership_payment.create' => array('membership_id' => $this->_ids['membership']),
2938 ));
2939
2940 $this->callAPISuccess('line_item', 'create', array(
2941 'entity_id' => $contribution['id'],
2942 'entity_table' => 'civicrm_contribution',
2943 'contribution_id' => $contribution['id'],
2944 'price_field_id' => $this->_ids['price_field'][0],
2945 'qty' => 1,
2946 'unit_price' => 20,
2947 'line_total' => 20,
2948 'financial_type_id' => 1,
2949 'price_field_value_id' => $this->_ids['price_field_value'][0],
2950 ));
2951
2952 //Update it to Failed.
2953 $form->_params['id'] = $contribution['id'];
2954 $form->_params['contribution_status_id'] = 4;
2955 try {
2956 $form->testSubmit($form->_params, CRM_Core_Action::UPDATE);
2957 }
2958 catch (Civi\Payment\Exception\PaymentProcessorException $e) {
2959 $error = TRUE;
2960 }
2961 //Existing membership should not get updated to expired.
2962 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
2963 $this->assertNotEquals($membership['status_id'], 4);
2964 }
2965
2966 /**
2967 * Test membership is renewed when transaction completed.
2968 */
2969 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
2970 $this->createPriceSetWithPage('membership');
2971 $this->setUpPendingContribution($this->_ids['price_field_value'][1]);
2972 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
2973 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
2974 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
2975 $this->cleanUpAfterPriceSets();
2976 }
2977
2978 public function cleanUpAfterPriceSets() {
2979 $this->quickCleanUpFinancialEntities();
2980 $this->contactDelete($this->_ids['contact']);
2981 }
2982
2983 /**
2984 * Set up a pending transaction with a specific price field id.
2985 *
2986 * @param int $priceFieldValueID
2987 */
2988 public function setUpPendingContribution($priceFieldValueID, $contriParams = array()) {
2989 $contactID = $this->individualCreate();
2990 $membership = $this->callAPISuccess('membership', 'create', array(
2991 'contact_id' => $contactID,
2992 'membership_type_id' => $this->_ids['membership_type'],
2993 'start_date' => 'yesterday - 1 year',
2994 'end_date' => 'yesterday',
2995 'join_date' => 'yesterday - 1 year',
2996 ));
2997 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(array(
2998 'domain_id' => 1,
2999 'contact_id' => $contactID,
3000 'receive_date' => date('Ymd'),
3001 'total_amount' => 20.00,
3002 'financial_type_id' => 1,
3003 'payment_instrument_id' => 'Credit Card',
3004 'non_deductible_amount' => 10.00,
3005 'trxn_id' => 'jdhfi' . rand(1, 100),
3006 'invoice_id' => 'djfhiew' . rand(5, 100),
3007 'source' => 'SSF',
3008 'contribution_status_id' => 2,
3009 'contribution_page_id' => $this->_ids['contribution_page'],
3010 'api.membership_payment.create' => array('membership_id' => $membership['id']),
3011 ), $contriParams));
3012
3013 $this->callAPISuccess('line_item', 'create', array(
3014 'entity_id' => $contribution['id'],
3015 'entity_table' => 'civicrm_contribution',
3016 'contribution_id' => $contribution['id'],
3017 'price_field_id' => $this->_ids['price_field'][0],
3018 'qty' => 1,
3019 'unit_price' => 20,
3020 'line_total' => 20,
3021 'financial_type_id' => 1,
3022 'price_field_value_id' => $priceFieldValueID,
3023 ));
3024 $this->_ids['contact'] = $contactID;
3025 $this->_ids['contribution'] = $contribution['id'];
3026 $this->_ids['membership'] = $membership['id'];
3027 }
3028
3029 /**
3030 * Test sending a mail via the API.
3031 */
3032 public function testSendMail() {
3033 $mut = new CiviMailUtils($this, TRUE);
3034 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
3035 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3036 'id' => $contribution['id'],
3037 'receipt_from_email' => 'api@civicrm.org',
3038 )
3039 );
3040 $mut->checkMailLog(array(
3041 '$ 100.00',
3042 'Contribution Information',
3043 'Please print this confirmation for your records',
3044 ), array(
3045 'Event',
3046 )
3047 );
3048
3049 $this->checkCreditCardDetails($mut, $contribution['id']);
3050 $mut->stop();
3051 }
3052
3053 /**
3054 * Test sending a mail via the API.
3055 * This simulates webform_civicrm using pay later contribution page
3056 */
3057 public function testSendconfirmationPayLater() {
3058 $mut = new CiviMailUtils($this, TRUE);
3059
3060 // Create contribution page
3061 $pageParams = array(
3062 'title' => 'Webform Contributions',
3063 'financial_type_id' => 1,
3064 'contribution_type_id' => 1,
3065 'is_confirm_enabled' => 1,
3066 'is_pay_later' => 1,
3067 'pay_later_text' => 'I will send payment by cheque',
3068 'pay_later_receipt' => 'Send your cheque payable to "CiviCRM LLC" to the office',
3069 );
3070 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $pageParams);
3071
3072 // Create pay later contribution
3073 $contribParams = array(
3074 'contact_id' => $this->_individualId,
3075 'financial_type_id' => 1,
3076 'is_pay_later' => 1,
3077 'contribution_status_id' => 2,
3078 'contribution_page_id' => $contributionPage['id'],
3079 'total_amount' => '10.00',
3080 );
3081 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
3082
3083 // Create line item
3084 $lineItemParams = array(
3085 'contribution_id' => $contribution['id'],
3086 'entity_id' => $contribution['id'],
3087 'entity_table' => 'civicrm_contribution',
3088 'label' => 'My lineitem label',
3089 'qty' => 1,
3090 'unit_price' => "10.00",
3091 'line_total' => "10.00",
3092 );
3093 $lineItem = $this->callAPISuccess('lineItem', 'create', $lineItemParams);
3094
3095 // Create email
3096 try {
3097 civicrm_api3('contribution', 'sendconfirmation', array(
3098 'id' => $contribution['id'],
3099 'receipt_from_email' => 'api@civicrm.org',
3100 )
3101 );
3102 }
3103 catch (Exception $e) {
3104 // Need to figure out how to stop this some other day
3105 // We don't care about the Payment Processor because this is Pay Later
3106 // The point of this test is to check we get the pay_later version of the mail
3107 if ($e->getMessage() != "Undefined variable: CRM16923AnUnreliableMethodHasBeenUserToDeterminePaymentProcessorFromContributionPage") {
3108 throw $e;
3109 }
3110 }
3111
3112 // Retrieve mail & check it has the pay_later_receipt info
3113 $mut->getMostRecentEmail('raw');
3114 $mut->checkMailLog(array(
3115 (string) $contribParams['total_amount'],
3116 $pageParams['pay_later_receipt'],
3117 ), array(
3118 'Event',
3119 )
3120 );
3121 $mut->stop();
3122 }
3123
3124
3125 /**
3126 * Check credit card details in sent mail via API
3127 *
3128 * @param $mut obj CiviMailUtils instance
3129 * @param int $contributionID Contribution ID
3130 *
3131 */
3132 public function checkCreditCardDetails($mut, $contributionID) {
3133 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
3134 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3135 'id' => $contributionID,
3136 'receipt_from_email' => 'api@civicrm.org',
3137 'payment_processor_id' => $this->paymentProcessorID,
3138 )
3139 );
3140 $mut->checkMailLog(array(
3141 'Credit Card Information', // credit card header
3142 'Billing Name and Address', // billing header
3143 'anthony_anderson@civicrm.org', // billing name
3144 ), array(
3145 'Event',
3146 )
3147 );
3148 }
3149
3150 /**
3151 * Test sending a mail via the API.
3152 */
3153 public function testSendMailEvent() {
3154 $mut = new CiviMailUtils($this, TRUE);
3155 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
3156 $event = $this->eventCreate(array(
3157 'is_email_confirm' => 1,
3158 'confirm_from_email' => 'test@civicrm.org',
3159 ));
3160 $this->_eventID = $event['id'];
3161 $participantParams = array(
3162 'contact_id' => $this->_individualId,
3163 'event_id' => $this->_eventID,
3164 'status_id' => 1,
3165 'role_id' => 1,
3166 // to ensure it matches later on
3167 'register_date' => '2007-07-21 00:00:00',
3168 'source' => 'Online Event Registration: API Testing',
3169
3170 );
3171 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
3172 $this->callAPISuccess('participant_payment', 'create', array(
3173 'participant_id' => $participant['id'],
3174 'contribution_id' => $contribution['id'],
3175 ));
3176 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3177 'id' => $contribution['id'],
3178 'receipt_from_email' => 'api@civicrm.org',
3179 )
3180 );
3181
3182 $mut->checkMailLog(array(
3183 'Annual CiviCRM meet',
3184 'Event',
3185 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
3186 ), array()
3187 );
3188 $mut->stop();
3189 }
3190
3191 /**
3192 * This function does a GET & compares the result against the $params.
3193 *
3194 * Use as a double check on Creates.
3195 *
3196 * @param array $params
3197 * @param int $id
3198 * @param bool $delete
3199 */
3200 public function contributionGetnCheck($params, $id, $delete = TRUE) {
3201
3202 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
3203 'id' => $id,
3204
3205 ));
3206
3207 if ($delete) {
3208 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
3209 }
3210 $this->assertAPISuccess($contribution, 0);
3211 $values = $contribution['values'][$contribution['id']];
3212 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
3213 // this is not returned in id format
3214 unset($params['payment_instrument_id']);
3215 $params['contribution_source'] = $params['source'];
3216 unset($params['source']);
3217 foreach ($params as $key => $value) {
3218 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
3219 }
3220 }
3221
3222 /**
3223 * Create a pending contribution & linked pending pledge record.
3224 */
3225 public function createPendingPledgeContribution() {
3226
3227 $pledgeID = $this->pledgeCreate(array('contact_id' => $this->_individualId, 'installments' => 1, 'amount' => 500));
3228 $this->_ids['pledge'] = $pledgeID;
3229 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params, array(
3230 'contribution_status_id' => 'Pending',
3231 'total_amount' => 500,
3232 ))
3233 );
3234 $paymentID = $this->callAPISuccessGetValue('PledgePayment', array(
3235 'options' => array('limit' => 1),
3236 'return' => 'id',
3237 ));
3238 $this->callAPISuccess('PledgePayment', 'create', array(
3239 'id' => $paymentID,
3240 'contribution_id' =>
3241 $contribution['id'],
3242 'status_id' => 'Pending',
3243 'scheduled_amount' => 500,
3244 ));
3245
3246 return $contribution['id'];
3247 }
3248
3249 /**
3250 * Create a pending contribution & linked pending participant record (along with an event).
3251 */
3252 public function createPendingParticipantContribution() {
3253 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'));
3254 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6, 'contact_id' => $this->_individualId));
3255 $this->_ids['participant'] = $participantID;
3256 $params = array_merge($this->_params, array('contact_id' => $this->_individualId, 'contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
3257 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3258 $this->callAPISuccess('participant_payment', 'create', array(
3259 'contribution_id' => $contribution['id'],
3260 'participant_id' => $participantID,
3261 ));
3262 $this->callAPISuccess('line_item', 'get', array(
3263 'entity_id' => $contribution['id'],
3264 'entity_table' => 'civicrm_contribution',
3265 'api.line_item.create' => array(
3266 'entity_id' => $participantID,
3267 'entity_table' => 'civicrm_participant',
3268 ),
3269 ));
3270 return $contribution['id'];
3271 }
3272
3273 /**
3274 * Get financial transaction amount.
3275 *
3276 * @param int $contId
3277 *
3278 * @return null|string
3279 */
3280 public function _getFinancialTrxnAmount($contId) {
3281 $query = "SELECT
3282 SUM( ft.total_amount ) AS total
3283 FROM civicrm_financial_trxn AS ft
3284 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
3285 WHERE ceft.entity_table = 'civicrm_contribution'
3286 AND ceft.entity_id = {$contId}";
3287
3288 $result = CRM_Core_DAO::singleValueQuery($query);
3289 return $result;
3290 }
3291
3292 /**
3293 * @param int $contId
3294 *
3295 * @return null|string
3296 */
3297 public function _getFinancialItemAmount($contId) {
3298 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
3299 $query = "SELECT
3300 SUM(amount)
3301 FROM civicrm_financial_item
3302 WHERE entity_table = 'civicrm_line_item'
3303 AND entity_id = {$lineItem}";
3304 $result = CRM_Core_DAO::singleValueQuery($query);
3305 return $result;
3306 }
3307
3308 /**
3309 * @param int $contId
3310 * @param $context
3311 */
3312 public function _checkFinancialItem($contId, $context) {
3313 if ($context != 'paylater') {
3314 $params = array(
3315 'entity_id' => $contId,
3316 'entity_table' => 'civicrm_contribution',
3317 );
3318 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($params, TRUE));
3319 $entityParams = array(
3320 'financial_trxn_id' => $trxn['financial_trxn_id'],
3321 'entity_table' => 'civicrm_financial_item',
3322 );
3323 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
3324 $params = array(
3325 'id' => $entityTrxn['entity_id'],
3326 );
3327 }
3328 if ($context == 'paylater') {
3329 $lineItems = CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution');
3330 foreach ($lineItems as $key => $item) {
3331 $params = array(
3332 'entity_id' => $key,
3333 'entity_table' => 'civicrm_line_item',
3334 );
3335 $compareParams = array('status_id' => 1);
3336 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3337 }
3338 }
3339 elseif ($context == 'refund') {
3340 $compareParams = array(
3341 'status_id' => 1,
3342 'financial_account_id' => 1,
3343 'amount' => -100,
3344 );
3345 }
3346 elseif ($context == 'cancelPending') {
3347 $compareParams = array(
3348 'status_id' => 3,
3349 'financial_account_id' => 1,
3350 'amount' => -100,
3351 );
3352 }
3353 elseif ($context == 'changeFinancial') {
3354 $lineKey = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
3355 $params = array(
3356 'entity_id' => $lineKey,
3357 'amount' => -100,
3358 );
3359 $compareParams = array(
3360 'financial_account_id' => 1,
3361 );
3362 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3363 $params = array(
3364 'financial_account_id' => 3,
3365 'entity_id' => $lineKey,
3366 );
3367 $compareParams = array(
3368 'amount' => 100,
3369 );
3370 }
3371 if ($context != 'paylater') {
3372 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3373 }
3374 }
3375
3376 /**
3377 * Check correct financial transaction entries were created for the change in payment instrument.
3378 *
3379 * @param int $contributionID
3380 * @param int $originalInstrumentID
3381 * @param int $newInstrumentID
3382 */
3383 public function checkFinancialTrxnPaymentInstrumentChange($contributionID, $originalInstrumentID, $newInstrumentID, $amount = 100) {
3384
3385 $entityFinancialTrxns = $this->getFinancialTransactionsForContribution($contributionID);
3386
3387 $originalTrxnParams = array(
3388 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($originalInstrumentID),
3389 'payment_instrument_id' => $originalInstrumentID,
3390 'amount' => $amount,
3391 'status_id' => 1,
3392 );
3393
3394 $reversalTrxnParams = array(
3395 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($originalInstrumentID),
3396 'payment_instrument_id' => $originalInstrumentID,
3397 'amount' => -$amount,
3398 'status_id' => 1,
3399 );
3400
3401 $newTrxnParams = array(
3402 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($newInstrumentID),
3403 'payment_instrument_id' => $newInstrumentID,
3404 'amount' => $amount,
3405 'status_id' => 1,
3406 );
3407
3408 foreach (array($originalTrxnParams, $reversalTrxnParams, $newTrxnParams) as $index => $transaction) {
3409 $entityFinancialTrxn = $entityFinancialTrxns[$index];
3410 $this->assertEquals($entityFinancialTrxn['amount'], $transaction['amount']);
3411
3412 $financialTrxn = $this->callAPISuccessGetSingle('FinancialTrxn', array(
3413 'id' => $entityFinancialTrxn['financial_trxn_id'],
3414 ));
3415 $this->assertEquals($transaction['status_id'], $financialTrxn['status_id']);
3416 $this->assertEquals($transaction['amount'], $financialTrxn['total_amount']);
3417 $this->assertEquals($transaction['amount'], $financialTrxn['net_amount']);
3418 $this->assertEquals(0, $financialTrxn['fee_amount']);
3419 $this->assertEquals($transaction['payment_instrument_id'], $financialTrxn['payment_instrument_id']);
3420 $this->assertEquals($transaction['to_financial_account_id'], $financialTrxn['to_financial_account_id']);
3421
3422 // Generic checks.
3423 $this->assertEquals(1, $financialTrxn['is_payment']);
3424 $this->assertEquals('USD', $financialTrxn['currency']);
3425 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($financialTrxn['trxn_date'])));
3426 }
3427 }
3428
3429 /**
3430 * Check financial transaction.
3431 *
3432 * @todo break this down into sensible functions - most calls to it only use a few lines out of the big if.
3433 *
3434 * @param array $contribution
3435 * @param string $context
3436 * @param int $instrumentId
3437 * @param array $extraParams
3438 */
3439 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL, $extraParams = array()) {
3440 $financialTrxns = $this->getFinancialTransactionsForContribution($contribution['id']);
3441 $trxn = array_pop($financialTrxns);
3442
3443 $params = array(
3444 'id' => $trxn['financial_trxn_id'],
3445 );
3446 if ($context == 'payLater') {
3447 $compareParams = array(
3448 'status_id' => 1,
3449 'from_financial_account_id' => CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($contribution['financial_type_id'], 'Accounts Receivable Account is'),
3450 );
3451 }
3452 elseif ($context == 'refund') {
3453 $compareParams = array(
3454 'to_financial_account_id' => 6,
3455 'total_amount' => -100,
3456 'status_id' => 7,
3457 'trxn_date' => '2015-01-01 09:00:00',
3458 'trxn_id' => 'the refund',
3459 );
3460 }
3461 elseif ($context == 'cancelPending') {
3462 $compareParams = array(
3463 'to_financial_account_id' => 7,
3464 'total_amount' => -100,
3465 'status_id' => 3,
3466 );
3467 }
3468 elseif ($context == 'changeFinancial' || $context == 'paymentInstrument') {
3469 // @todo checkFinancialTrxnPaymentInstrumentChange instead for paymentInstrument.
3470 // It does the same thing with greater readability.
3471 // @todo remove handling for
3472
3473 $entityParams = array(
3474 'entity_id' => $contribution['id'],
3475 'entity_table' => 'civicrm_contribution',
3476 'amount' => -100,
3477 );
3478 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
3479 $trxnParams1 = array(
3480 'id' => $trxn['financial_trxn_id'],
3481 );
3482 if (empty($extraParams)) {
3483 $compareParams = array(
3484 'total_amount' => -100,
3485 'status_id' => 1,
3486 );
3487 }
3488 else {
3489 $compareParams = array(
3490 'total_amount' => 100,
3491 'status_id' => 1,
3492 );
3493 }
3494 if ($context == 'paymentInstrument') {
3495 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($instrumentId);
3496 $compareParams['payment_instrument_id'] = $instrumentId;
3497 }
3498 else {
3499 $compareParams['to_financial_account_id'] = 12;
3500 }
3501 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, array_merge($compareParams, $extraParams));
3502 $compareParams['total_amount'] = 100;
3503 }
3504
3505 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, array_merge($compareParams, $extraParams));
3506 }
3507
3508 /**
3509 * @return mixed
3510 */
3511 public function _addPaymentInstrument() {
3512 $gId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
3513 $optionParams = array(
3514 'option_group_id' => $gId,
3515 'label' => 'Test Card',
3516 'name' => 'Test Card',
3517 'value' => '6',
3518 'weight' => '6',
3519 'is_active' => 1,
3520 );
3521 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
3522 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
3523 $financialParams = array(
3524 'entity_table' => 'civicrm_option_value',
3525 'entity_id' => $optionValue['id'],
3526 'account_relationship' => $relationTypeId,
3527 'financial_account_id' => 7,
3528 );
3529 CRM_Financial_BAO_FinancialTypeAccount::add($financialParams, CRM_Core_DAO::$_nullArray);
3530 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
3531 return $optionValue['values'][$optionValue['id']]['value'];
3532 }
3533
3534 public function _deletedAddedPaymentInstrument() {
3535 $result = $this->callAPISuccess('OptionValue', 'get', array(
3536 'option_group_id' => 'payment_instrument',
3537 'name' => 'Test Card',
3538 'value' => '6',
3539 'is_active' => 1,
3540 ));
3541 if ($id = CRM_Utils_Array::value('id', $result)) {
3542 $this->callAPISuccess('OptionValue', 'delete', array('id' => $id));
3543 }
3544 }
3545
3546 /**
3547 * Set up the basic recurring contribution for tests.
3548 *
3549 * @param array $generalParams
3550 * Parameters that can be merged into the recurring AND the contribution.
3551 *
3552 * @param array $recurParams
3553 * Parameters to merge into the recur only.
3554 *
3555 * @return array|int
3556 */
3557 protected function setUpRecurringContribution($generalParams = array(), $recurParams = array()) {
3558 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3559 'contact_id' => $this->_individualId,
3560 'installments' => '12',
3561 'frequency_interval' => '1',
3562 'amount' => '100',
3563 'contribution_status_id' => 1,
3564 'start_date' => '2012-01-01 00:00:00',
3565 'currency' => 'USD',
3566 'frequency_unit' => 'month',
3567 'payment_processor_id' => $this->paymentProcessorID,
3568 ), $generalParams, $recurParams));
3569 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3570 $this->_params,
3571 array(
3572 'contribution_recur_id' => $contributionRecur['id'],
3573 ), $generalParams)
3574 );
3575 return $originalContribution;
3576 }
3577
3578 /**
3579 * Set up a basic auto-renew membership for tests.
3580 *
3581 * @param array $generalParams
3582 * Parameters that can be merged into the recurring AND the contribution.
3583 *
3584 * @param array $recurParams
3585 * Parameters to merge into the recur only.
3586 *
3587 * @return array|int
3588 */
3589 protected function setUpAutoRenewMembership($generalParams = array(), $recurParams = array()) {
3590 $newContact = $this->callAPISuccess('Contact', 'create', array(
3591 'contact_type' => 'Individual',
3592 'sort_name' => 'McTesterson, Testy',
3593 'display_name' => 'Testy McTesterson',
3594 'preferred_language' => 'en_US',
3595 'preferred_mail_format' => 'Both',
3596 'first_name' => 'Testy',
3597 'last_name' => 'McTesterson',
3598 'contact_is_deleted' => '0',
3599 'email_id' => '4',
3600 'email' => 'tmctesterson@example.com',
3601 'on_hold' => '0',
3602 ));
3603 $membershipType = $this->callAPISuccess('MembershipType', 'create', array(
3604 'domain_id' => "Default Domain Name",
3605 'member_of_contact_id' => 1,
3606 'financial_type_id' => "Member Dues",
3607 'duration_unit' => "month",
3608 'duration_interval' => 1,
3609 'period_type' => 'rolling',
3610 'name' => "Standard Member",
3611 'minimum_fee' => 100,
3612 ));
3613 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3614 'contact_id' => $newContact['id'],
3615 'installments' => '12',
3616 'frequency_interval' => '1',
3617 'amount' => '100',
3618 'contribution_status_id' => 1,
3619 'start_date' => '2012-01-01 00:00:00',
3620 'currency' => 'USD',
3621 'frequency_unit' => 'month',
3622 'payment_processor_id' => $this->paymentProcessorID,
3623 ), $generalParams, $recurParams));
3624
3625 $membership = $this->callAPISuccess('membership', 'create', array(
3626 'contact_id' => $newContact['id'],
3627 'contribution_recur_id' => $contributionRecur['id'],
3628 'financial_type_id' => "Member Dues",
3629 'membership_type_id' => $membershipType['id'],
3630 'num_terms' => 1,
3631 'skipLineItem' => TRUE,
3632 ));
3633
3634 CRM_Price_BAO_LineItem::getLineItemArray($this->_params, NULL, 'membership', $membershipType['id']);
3635 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3636 $this->_params,
3637 array(
3638 'contact_id' => $newContact['id'],
3639 'contribution_recur_id' => $contributionRecur['id'],
3640 'financial_type_id' => "Member Dues",
3641 'contribution_status_id' => 1,
3642 'invoice_id' => uniqid(),
3643 ), $generalParams)
3644 );
3645 $lineItem = $this->callAPISuccess('LineItem', 'getsingle', array());
3646 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
3647 $membership = $this->callAPISuccess('Membership', 'getsingle', array('id' => $lineItem['entity_id']));
3648 $this->callAPISuccess('LineItem', 'getsingle', array());
3649 $this->callAPISuccessGetCount('MembershipPayment', array('membership_id' => $membership['id']), 1);
3650
3651 return array($originalContribution, $membership);
3652 }
3653 /**
3654 * Set up a repeat transaction.
3655 *
3656 * @param array $recurParams
3657 *
3658 * @return array
3659 */
3660 protected function setUpRepeatTransaction($recurParams = array(), $flag, $contributionParams = array()) {
3661 $paymentProcessorID = $this->paymentProcessorCreate();
3662 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3663 'contact_id' => $this->_individualId,
3664 'installments' => '12',
3665 'frequency_interval' => '1',
3666 'amount' => '500',
3667 'contribution_status_id' => 1,
3668 'start_date' => '2012-01-01 00:00:00',
3669 'currency' => 'USD',
3670 'frequency_unit' => 'month',
3671 'payment_processor_id' => $paymentProcessorID,
3672 ), $recurParams));
3673
3674 $originalContribution = '';
3675 if ($flag == 'multiple') {
3676 // CRM-19309 create a contribution + also add in line_items (plural):
3677 $params = array_merge($this->_params, $contributionParams);
3678 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3679 $params,
3680 array(
3681 'contribution_recur_id' => $contributionRecur['id'],
3682 'skipLineItem' => 1,
3683 'api.line_item.create' => array(
3684 array(
3685 'price_field_id' => 1,
3686 'qty' => 2,
3687 'line_total' => '20',
3688 'unit_price' => '10',
3689 'financial_type_id' => 1,
3690 ),
3691 array(
3692 'price_field_id' => 1,
3693 'qty' => 1,
3694 'line_total' => '80',
3695 'unit_price' => '80',
3696 'financial_type_id' => 2,
3697 ),
3698 ),
3699 )
3700 )
3701 );
3702 }
3703 elseif ($flag == 'single') {
3704 $params = array_merge($this->_params, array('contribution_recur_id' => $contributionRecur['id']));
3705 $params = array_merge($params, $contributionParams);
3706 $originalContribution = $this->callAPISuccess('contribution', 'create', $params);
3707 }
3708 $originalContribution['payment_processor_id'] = $paymentProcessorID;
3709 return $originalContribution;
3710 }
3711
3712 /**
3713 * Common set up routine.
3714 *
3715 * @return array
3716 */
3717 protected function setUpForCompleteTransaction() {
3718 $this->mut = new CiviMailUtils($this, TRUE);
3719 $this->createLoggedInUser();
3720 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
3721 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3722 return $contribution;
3723 }
3724
3725 /**
3726 * Test repeat contribution uses the Payment Processor' payment_instrument setting.
3727 */
3728 public function testRepeatTransactionWithNonCreditCardDefault() {
3729 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
3730 'contact_id' => $this->_individualId,
3731 'installments' => '12',
3732 'frequency_interval' => '1',
3733 'amount' => '100',
3734 'contribution_status_id' => 1,
3735 'start_date' => '2012-01-01 00:00:00',
3736 'currency' => 'USD',
3737 'frequency_unit' => 'month',
3738 'payment_processor_id' => $this->paymentProcessorID,
3739 ));
3740 $contribution1 = $this->callAPISuccess('contribution', 'create', array_merge(
3741 $this->_params,
3742 array('contribution_recur_id' => $contributionRecur['id'], 'payment_instrument_id' => 2))
3743 );
3744 $paymentInstruments = CRM_Contribute_PseudoConstant::paymentInstrument('name');
3745 $contribution2 = $this->callAPISuccess('contribution', 'repeattransaction', array(
3746 'contribution_status_id' => 'Completed',
3747 'trxn_id' => uniqid(),
3748 'original_contribution_id' => $contribution1,
3749 ));
3750 $this->assertEquals(array_search('Debit Card', $paymentInstruments), $contribution2['values'][$contribution2['id']]['payment_instrument_id']);
3751 $this->quickCleanUpFinancialEntities();
3752 }
3753
3754 /**
3755 * CRM-20008 Tests repeattransaction creates pending membership.
3756 */
3757 public function testRepeatTransactionPendingMembership() {
3758 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
3759 $this->callAPISuccess('membership', 'create', array(
3760 'id' => $membership['id'],
3761 'end_date' => 'yesterday',
3762 'status_id' => 'Expired',
3763 ));
3764 $repeatedContribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
3765 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
3766 'contribution_status_id' => 'Pending',
3767 'trxn_id' => uniqid(),
3768 ));
3769 $membershipStatusId = $this->callAPISuccess('membership', 'getvalue', array(
3770 'id' => $membership['id'],
3771 'return' => 'status_id',
3772 ));
3773
3774 // Let's see if the membership payments got created while we're at it.
3775 $membershipPayments = $this->callAPISuccess('MembershipPayment', 'get', array(
3776 'memberhip_id' => $membership['id'],
3777 ));
3778 $this->assertEquals(2, $membershipPayments['count']);
3779
3780 $this->assertEquals('Expired', CRM_Core_PseudoConstant::getLabel('CRM_Member_BAO_Membership', 'status_id', $membershipStatusId));
3781 $this->callAPISuccess('Contribution', 'completetransaction', array('id' => $repeatedContribution['id']));
3782 $membership = $this->callAPISuccessGetSingle('membership', array(
3783 'id' => $membership['id'],
3784 'return' => 'status_id, end_date',
3785 ));
3786 $this->assertEquals('New', CRM_Core_PseudoConstant::getLabel('CRM_Member_BAO_Membership', 'status_id', $membership['status_id']));
3787 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 month')), $membership['end_date']);
3788
3789 $this->quickCleanUpFinancialEntities();
3790 $this->contactDelete($originalContribution['values'][1]['contact_id']);
3791 }
3792
3793 /**
3794 * Test sending a mail via the API.
3795 */
3796 public function testSendMailWithAPISetFromDetails() {
3797 $mut = new CiviMailUtils($this, TRUE);
3798 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
3799 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3800 'id' => $contribution['id'],
3801 'receipt_from_email' => 'api@civicrm.org',
3802 'receipt_from_name' => 'CiviCRM LLC',
3803 ));
3804 $mut->checkMailLog(array(
3805 'From: CiviCRM LLC <api@civicrm.org>',
3806 'Contribution Information',
3807 'Please print this confirmation for your records',
3808 ), array(
3809 'Event',
3810 )
3811 );
3812 $mut->stop();
3813 }
3814
3815 /**
3816 * Test sending a mail via the API.
3817 */
3818 public function testSendMailWithNoFromSetFallToDomain() {
3819 $this->createLoggedInUser();
3820 $mut = new CiviMailUtils($this, TRUE);
3821 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
3822 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3823 'id' => $contribution['id'],
3824 ));
3825 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => 1));
3826 $mut->checkMailLog(array(
3827 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>',
3828 'Contribution Information',
3829 'Please print this confirmation for your records',
3830 ), array(
3831 'Event',
3832 )
3833 );
3834 $mut->stop();
3835 }
3836
3837 /**
3838 * Test sending a mail via the API.
3839 */
3840 public function testSendMailWithRepeatTransactionAPIFalltoDomain() {
3841 $this->createLoggedInUser();
3842 $mut = new CiviMailUtils($this, TRUE);
3843 $contribution = $this->setUpRepeatTransaction(array(), 'single');
3844 $this->callAPISuccess('contribution', 'repeattransaction', array(
3845 'contribution_status_id' => 'Completed',
3846 'trxn_id' => uniqid(),
3847 'original_contribution_id' => $contribution,
3848 ));
3849 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => 1));
3850 $mut->checkMailLog(array(
3851 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>',
3852 'Contribution Information',
3853 'Please print this confirmation for your records',
3854 ), array(
3855 'Event',
3856 )
3857 );
3858 $mut->stop();
3859 }
3860
3861 /**
3862 * Test sending a mail via the API.
3863 */
3864 public function testSendMailWithRepeatTransactionAPIFalltoContributionPage() {
3865 $mut = new CiviMailUtils($this, TRUE);
3866 $contributionPage = $this->contributionPageCreate(array('receipt_from_name' => 'CiviCRM LLC', 'receipt_from_email' => 'contributionpage@civicrm.org', 'is_email_receipt' => 1));
3867 $paymentProcessorID = $this->paymentProcessorCreate();
3868 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
3869 'contact_id' => $this->_individualId,
3870 'installments' => '12',
3871 'frequency_interval' => '1',
3872 'amount' => '500',
3873 'contribution_status_id' => 1,
3874 'start_date' => '2012-01-01 00:00:00',
3875 'currency' => 'USD',
3876 'frequency_unit' => 'month',
3877 'payment_processor_id' => $paymentProcessorID,
3878 ));
3879 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3880 $this->_params,
3881 array(
3882 'contribution_recur_id' => $contributionRecur['id'],
3883 'contribution_page_id' => $contributionPage['id']))
3884 );
3885 $this->callAPISuccess('contribution', 'repeattransaction', array(
3886 'contribution_status_id' => 'Completed',
3887 'trxn_id' => uniqid(),
3888 'original_contribution_id' => $originalContribution,
3889 )
3890 );
3891 $mut->checkMailLog(array(
3892 'From: CiviCRM LLC <contributionpage@civicrm.org>',
3893 'Contribution Information',
3894 'Please print this confirmation for your records',
3895 ), array(
3896 'Event',
3897 )
3898 );
3899 $mut->stop();
3900 }
3901
3902 /**
3903 * Test sending a mail via the API.
3904 */
3905 public function testSendMailWithRepeatTransactionAPIFalltoSystemFromNoDefaultFrom() {
3906 $mut = new CiviMailUtils($this, TRUE);
3907 $originalContribution = $contribution = $this->setUpRepeatTransaction(array(), 'single');
3908 $fromEmail = $this->CallAPISuccess('optionValue', 'get', array('is_default' => 1, 'option_group_id' => 'from_email_address', 'sequential' => 1));
3909 foreach ($fromEmail['values'] as $from) {
3910 $this->callAPISuccess('optionValue', 'create', array('is_default' => 0, 'id' => $from['id']));
3911 }
3912 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => CRM_Core_Config::domainID()));
3913 $this->callAPISuccess('contribution', 'repeattransaction', array(
3914 'contribution_status_id' => 'Completed',
3915 'trxn_id' => uniqid(),
3916 'original_contribution_id' => $originalContribution,
3917 )
3918 );
3919 $mut->checkMailLog(array(
3920 'From: ' . $domain['name'] . ' <' . $domain['domain_email'] . '>',
3921 'Contribution Information',
3922 'Please print this confirmation for your records',
3923 ), array(
3924 'Event',
3925 )
3926 );
3927 $mut->stop();
3928 }
3929
3930 /**
3931 * Create a Contribution Page with is_email_receipt = TRUE.
3932 *
3933 * @param array $params
3934 * Params to overwrite with.
3935 *
3936 * @return array|int
3937 */
3938 protected function createReceiptableContributionPage($params = array()) {
3939 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array_merge(array(
3940 'receipt_from_name' => 'Mickey Mouse',
3941 'receipt_from_email' => 'mickey@mouse.com',
3942 'title' => "Test Contribution Page",
3943 'financial_type_id' => 1,
3944 'currency' => 'CAD',
3945 'is_monetary' => TRUE,
3946 'is_email_receipt' => TRUE,
3947 ), $params));
3948 return $contributionPage;
3949 }
3950
3951 /**
3952 * function to test card_type and pan truncation.
3953 */
3954 public function testCardTypeAndPanTruncation() {
3955 $creditCardTypeIDs = array_flip(CRM_Financial_DAO_FinancialTrxn::buildOptions('card_type_id'));
3956 $contactId = $this->individualCreate();
3957 $params = array(
3958 'contact_id' => $contactId,
3959 'receive_date' => '2016-01-20',
3960 'total_amount' => 100,
3961 'financial_type_id' => 1,
3962 'payment_instrument' => 'Credit Card',
3963 'card_type_id' => $creditCardTypeIDs['Visa'],
3964 'pan_truncation' => 4567,
3965 );
3966 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3967 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
3968 $financialTrxn = $this->callAPISuccessGetSingle(
3969 'FinancialTrxn',
3970 array(
3971 'id' => $lastFinancialTrxnId['financialTrxnId'],
3972 'return' => array('card_type_id', 'pan_truncation'),
3973 )
3974 );
3975 $this->assertEquals(CRM_Utils_Array::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Visa']);
3976 $this->assertEquals(CRM_Utils_Array::value('pan_truncation', $financialTrxn), 4567);
3977 $params = array(
3978 'id' => $contribution['id'],
3979 'pan_truncation' => 2345,
3980 'card_type_id' => $creditCardTypeIDs['Amex'],
3981 );
3982 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3983 $financialTrxn = $this->callAPISuccessGetSingle(
3984 'FinancialTrxn',
3985 array(
3986 'id' => $lastFinancialTrxnId['financialTrxnId'],
3987 'return' => array('card_type_id', 'pan_truncation'),
3988 )
3989 );
3990 $this->assertEquals(CRM_Utils_Array::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Amex']);
3991 $this->assertEquals(CRM_Utils_Array::value('pan_truncation', $financialTrxn), 2345);
3992 }
3993
3994 /**
3995 * Test repeat contribution uses non default currency
3996 * @see https://issues.civicrm.org/jira/projects/CRM/issues/CRM-20678
3997 */
3998 public function testRepeatTransactionWithDifferenceCurrency() {
3999 $originalContribution = $this->setUpRepeatTransaction(array('currency' => 'AUD'), 'single', array('currency' => 'AUD'));
4000 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
4001 'original_contribution_id' => $originalContribution['id'],
4002 'contribution_status_id' => 'Completed',
4003 'trxn_id' => uniqid(),
4004 ));
4005 $this->assertEquals('AUD', $contribution['values'][$contribution['id']]['currency']);
4006 }
4007
4008 /**
4009 * Get the financial items for the contribution.
4010 *
4011 * @param int $contributionID
4012 *
4013 * @return array
4014 * Array of associated financial items.
4015 */
4016 protected function getFinancialTransactionsForContribution($contributionID) {
4017 $trxnParams = array(
4018 'entity_id' => $contributionID,
4019 'entity_table' => 'civicrm_contribution',
4020 );
4021 // @todo the following function has naming errors & has a weird signature & appears to
4022 // only be called from test classes. Move into test suite & maybe just use api
4023 // from this function.
4024 return array_merge(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($trxnParams, FALSE, array()));
4025 }
4026
4027 }