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