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