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