Add test for api money, fix net_amount calc
[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 $contributionPage = $this->createPriceSetWithPage('membership');
2948 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', array(
2949 'name' => 'Grace',
2950 'return' => 'id')
2951 );
2952 $this->setUpPendingContribution($this->_ids['price_field_value'][0]);
2953 $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
2954 // Case 1: Assert that Membership Signup Activity is created on Pending to Completed Contribution via backoffice
2955 $activity = $this->callAPISuccess('Activity', 'get', array(
2956 'activity_type_id' => 'Membership Signup',
2957 'source_record_id' => $this->_ids['membership'],
2958 'status_id' => 'Scheduled',
2959 ));
2960 $this->assertEquals(1, $activity['count']);
2961
2962 // change pending contribution to completed
2963 $form = new CRM_Contribute_Form_Contribution();
2964 $error = FALSE;
2965 $form->_params = array(
2966 'id' => $this->_ids['contribution'],
2967 'total_amount' => 20,
2968 'net_amount' => 20,
2969 'fee_amount' => 0,
2970 'financial_type_id' => 1,
2971 'contact_id' => $this->_individualId,
2972 'contribution_status_id' => 1,
2973 'billing_middle_name' => '',
2974 'billing_last_name' => 'Adams',
2975 'billing_street_address-5' => '790L Lincoln St S',
2976 'billing_city-5' => 'Maryknoll',
2977 'billing_state_province_id-5' => 1031,
2978 'billing_postal_code-5' => 10545,
2979 'billing_country_id-5' => 1228,
2980 'frequency_interval' => 1,
2981 'frequency_unit' => 'month',
2982 'installments' => '',
2983 'hidden_AdditionalDetail' => 1,
2984 'hidden_Premium' => 1,
2985 'from_email_address' => '"civi45" <civi45@civicrm.com>',
2986 'receipt_date' => '',
2987 'receipt_date_time' => '',
2988 'payment_processor_id' => $this->paymentProcessorID,
2989 'currency' => 'USD',
2990 'contribution_page_id' => $this->_ids['contribution_page'],
2991 'contribution_mode' => 'membership',
2992 'source' => 'Membership Signup and Renewal',
2993 );
2994 try {
2995 $form->testSubmit($form->_params, CRM_Core_Action::UPDATE);
2996 }
2997 catch (Civi\Payment\Exception\PaymentProcessorException $e) {
2998 $error = TRUE;
2999 }
3000 // Case 2: After successful payment for Pending backoffice there are three activities created
3001 // 2.a Update status of existing Scheduled Membership Signup (created in step 1) to Completed
3002 $activity = $this->callAPISuccess('Activity', 'get', array(
3003 'activity_type_id' => 'Membership Signup',
3004 'source_record_id' => $this->_ids['membership'],
3005 'status_id' => 'Completed',
3006 ));
3007 $this->assertEquals(1, $activity['count']);
3008 // 2.b Contribution activity created to record successful payment
3009 $activity = $this->callAPISuccess('Activity', 'get', array(
3010 'activity_type_id' => 'Contribution',
3011 'source_record_id' => $this->_ids['contribution'],
3012 'status_id' => 'Completed',
3013 ));
3014 $this->assertEquals(1, $activity['count']);
3015
3016 // 2.c 'Change membership type' activity created to record Membership status change from Grace to Current
3017 $activity = $this->callAPISuccess('Activity', 'get', array(
3018 'activity_type_id' => 'Change Membership Status',
3019 'source_record_id' => $this->_ids['membership'],
3020 'status_id' => 'Completed',
3021 ));
3022 $this->assertEquals(1, $activity['count']);
3023 $this->assertEquals('Status changed from Grace to Current', $activity['values'][$activity['id']]['subject']);
3024
3025 //Create another pending contribution for renewal
3026 $contribution = $this->callAPISuccess('contribution', 'create', array(
3027 'domain_id' => 1,
3028 'contact_id' => $this->_ids['contact'],
3029 'receive_date' => date('Ymd'),
3030 'total_amount' => 20.00,
3031 'financial_type_id' => 1,
3032 'payment_instrument_id' => 'Credit Card',
3033 'non_deductible_amount' => 10.00,
3034 'trxn_id' => 'rdhfi88',
3035 'invoice_id' => 'dofhiewuyr',
3036 'source' => 'SSF',
3037 'contribution_status_id' => 2,
3038 'contribution_page_id' => $this->_ids['contribution_page'],
3039 'api.membership_payment.create' => array('membership_id' => $this->_ids['membership']),
3040 ));
3041
3042 $this->callAPISuccess('line_item', 'create', array(
3043 'entity_id' => $contribution['id'],
3044 'entity_table' => 'civicrm_contribution',
3045 'contribution_id' => $contribution['id'],
3046 'price_field_id' => $this->_ids['price_field'][0],
3047 'qty' => 1,
3048 'unit_price' => 20,
3049 'line_total' => 20,
3050 'financial_type_id' => 1,
3051 'price_field_value_id' => $this->_ids['price_field_value'][0],
3052 ));
3053
3054 //Update it to Failed.
3055 $form->_params['id'] = $contribution['id'];
3056 $form->_params['contribution_status_id'] = 4;
3057 try {
3058 $form->testSubmit($form->_params, CRM_Core_Action::UPDATE);
3059 }
3060 catch (Civi\Payment\Exception\PaymentProcessorException $e) {
3061 $error = TRUE;
3062 }
3063 //Existing membership should not get updated to expired.
3064 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
3065 $this->assertNotEquals($membership['status_id'], 4);
3066 }
3067
3068 /**
3069 * Test membership is renewed when transaction completed.
3070 */
3071 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
3072 $this->createPriceSetWithPage('membership');
3073 $this->setUpPendingContribution($this->_ids['price_field_value'][1]);
3074 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
3075 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
3076 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
3077 $this->cleanUpAfterPriceSets();
3078 }
3079
3080 public function cleanUpAfterPriceSets() {
3081 $this->quickCleanUpFinancialEntities();
3082 $this->contactDelete($this->_ids['contact']);
3083 }
3084
3085 /**
3086 * Set up a pending transaction with a specific price field id.
3087 *
3088 * @param int $priceFieldValueID
3089 */
3090 public function setUpPendingContribution($priceFieldValueID, $contriParams = array()) {
3091 $contactID = $this->individualCreate();
3092 $membership = $this->callAPISuccess('membership', 'create', array(
3093 'contact_id' => $contactID,
3094 'membership_type_id' => $this->_ids['membership_type'],
3095 'start_date' => 'yesterday - 1 year',
3096 'end_date' => 'yesterday',
3097 'join_date' => 'yesterday - 1 year',
3098 ));
3099 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(array(
3100 'domain_id' => 1,
3101 'contact_id' => $contactID,
3102 'receive_date' => date('Ymd'),
3103 'total_amount' => 20.00,
3104 'financial_type_id' => 1,
3105 'payment_instrument_id' => 'Credit Card',
3106 'non_deductible_amount' => 10.00,
3107 'trxn_id' => 'jdhfi' . rand(1, 100),
3108 'invoice_id' => 'djfhiew' . rand(5, 100),
3109 'source' => 'SSF',
3110 'contribution_status_id' => 2,
3111 'contribution_page_id' => $this->_ids['contribution_page'],
3112 'api.membership_payment.create' => array('membership_id' => $membership['id']),
3113 ), $contriParams));
3114
3115 $this->callAPISuccess('line_item', 'create', array(
3116 'entity_id' => $contribution['id'],
3117 'entity_table' => 'civicrm_contribution',
3118 'contribution_id' => $contribution['id'],
3119 'price_field_id' => $this->_ids['price_field'][0],
3120 'qty' => 1,
3121 'unit_price' => 20,
3122 'line_total' => 20,
3123 'financial_type_id' => 1,
3124 'price_field_value_id' => $priceFieldValueID,
3125 ));
3126 $this->_ids['contact'] = $contactID;
3127 $this->_ids['contribution'] = $contribution['id'];
3128 $this->_ids['membership'] = $membership['id'];
3129 }
3130
3131 /**
3132 * Test sending a mail via the API.
3133 */
3134 public function testSendMail() {
3135 $mut = new CiviMailUtils($this, TRUE);
3136 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
3137 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3138 'id' => $contribution['id'],
3139 'receipt_from_email' => 'api@civicrm.org',
3140 )
3141 );
3142 $mut->checkMailLog(array(
3143 '$ 100.00',
3144 'Contribution Information',
3145 'Please print this confirmation for your records',
3146 ), array(
3147 'Event',
3148 )
3149 );
3150
3151 $this->checkCreditCardDetails($mut, $contribution['id']);
3152 $mut->stop();
3153 }
3154
3155 /**
3156 * Test sending a mail via the API.
3157 * This simulates webform_civicrm using pay later contribution page
3158 */
3159 public function testSendconfirmationPayLater() {
3160 $mut = new CiviMailUtils($this, TRUE);
3161
3162 // Create contribution page
3163 $pageParams = array(
3164 'title' => 'Webform Contributions',
3165 'financial_type_id' => 1,
3166 'contribution_type_id' => 1,
3167 'is_confirm_enabled' => 1,
3168 'is_pay_later' => 1,
3169 'pay_later_text' => 'I will send payment by cheque',
3170 'pay_later_receipt' => 'Send your cheque payable to "CiviCRM LLC" to the office',
3171 );
3172 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $pageParams);
3173
3174 // Create pay later contribution
3175 $contribParams = array(
3176 'contact_id' => $this->_individualId,
3177 'financial_type_id' => 1,
3178 'is_pay_later' => 1,
3179 'contribution_status_id' => 2,
3180 'contribution_page_id' => $contributionPage['id'],
3181 'total_amount' => '10.00',
3182 );
3183 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
3184
3185 // Create line item
3186 $lineItemParams = array(
3187 'contribution_id' => $contribution['id'],
3188 'entity_id' => $contribution['id'],
3189 'entity_table' => 'civicrm_contribution',
3190 'label' => 'My lineitem label',
3191 'qty' => 1,
3192 'unit_price' => "10.00",
3193 'line_total' => "10.00",
3194 );
3195 $lineItem = $this->callAPISuccess('lineItem', 'create', $lineItemParams);
3196
3197 // Create email
3198 try {
3199 civicrm_api3('contribution', 'sendconfirmation', array(
3200 'id' => $contribution['id'],
3201 'receipt_from_email' => 'api@civicrm.org',
3202 )
3203 );
3204 }
3205 catch (Exception $e) {
3206 // Need to figure out how to stop this some other day
3207 // We don't care about the Payment Processor because this is Pay Later
3208 // The point of this test is to check we get the pay_later version of the mail
3209 if ($e->getMessage() != "Undefined variable: CRM16923AnUnreliableMethodHasBeenUserToDeterminePaymentProcessorFromContributionPage") {
3210 throw $e;
3211 }
3212 }
3213
3214 // Retrieve mail & check it has the pay_later_receipt info
3215 $mut->getMostRecentEmail('raw');
3216 $mut->checkMailLog(array(
3217 (string) $contribParams['total_amount'],
3218 $pageParams['pay_later_receipt'],
3219 ), array(
3220 'Event',
3221 )
3222 );
3223 $mut->stop();
3224 }
3225
3226
3227 /**
3228 * Check credit card details in sent mail via API
3229 *
3230 * @param $mut obj CiviMailUtils instance
3231 * @param int $contributionID Contribution ID
3232 *
3233 */
3234 public function checkCreditCardDetails($mut, $contributionID) {
3235 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
3236 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3237 'id' => $contributionID,
3238 'receipt_from_email' => 'api@civicrm.org',
3239 'payment_processor_id' => $this->paymentProcessorID,
3240 )
3241 );
3242 $mut->checkMailLog(array(
3243 'Credit Card Information', // credit card header
3244 'Billing Name and Address', // billing header
3245 'anthony_anderson@civicrm.org', // billing name
3246 ), array(
3247 'Event',
3248 )
3249 );
3250 }
3251
3252 /**
3253 * Test sending a mail via the API.
3254 */
3255 public function testSendMailEvent() {
3256 $mut = new CiviMailUtils($this, TRUE);
3257 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
3258 $event = $this->eventCreate(array(
3259 'is_email_confirm' => 1,
3260 'confirm_from_email' => 'test@civicrm.org',
3261 ));
3262 $this->_eventID = $event['id'];
3263 $participantParams = array(
3264 'contact_id' => $this->_individualId,
3265 'event_id' => $this->_eventID,
3266 'status_id' => 1,
3267 'role_id' => 1,
3268 // to ensure it matches later on
3269 'register_date' => '2007-07-21 00:00:00',
3270 'source' => 'Online Event Registration: API Testing',
3271
3272 );
3273 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
3274 $this->callAPISuccess('participant_payment', 'create', array(
3275 'participant_id' => $participant['id'],
3276 'contribution_id' => $contribution['id'],
3277 ));
3278 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3279 'id' => $contribution['id'],
3280 'receipt_from_email' => 'api@civicrm.org',
3281 )
3282 );
3283
3284 $mut->checkMailLog(array(
3285 'Annual CiviCRM meet',
3286 'Event',
3287 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
3288 ), array()
3289 );
3290 $mut->stop();
3291 }
3292
3293 /**
3294 * This function does a GET & compares the result against the $params.
3295 *
3296 * Use as a double check on Creates.
3297 *
3298 * @param array $params
3299 * @param int $id
3300 * @param bool $delete
3301 */
3302 public function contributionGetnCheck($params, $id, $delete = TRUE) {
3303
3304 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
3305 'id' => $id,
3306
3307 ));
3308
3309 if ($delete) {
3310 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
3311 }
3312 $this->assertAPISuccess($contribution, 0);
3313 $values = $contribution['values'][$contribution['id']];
3314 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
3315 // this is not returned in id format
3316 unset($params['payment_instrument_id']);
3317 $params['contribution_source'] = $params['source'];
3318 unset($params['source']);
3319 foreach ($params as $key => $value) {
3320 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
3321 }
3322 }
3323
3324 /**
3325 * Create a pending contribution & linked pending pledge record.
3326 */
3327 public function createPendingPledgeContribution() {
3328
3329 $pledgeID = $this->pledgeCreate(array('contact_id' => $this->_individualId, 'installments' => 1, 'amount' => 500));
3330 $this->_ids['pledge'] = $pledgeID;
3331 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params, array(
3332 'contribution_status_id' => 'Pending',
3333 'total_amount' => 500,
3334 ))
3335 );
3336 $paymentID = $this->callAPISuccessGetValue('PledgePayment', array(
3337 'options' => array('limit' => 1),
3338 'return' => 'id',
3339 ));
3340 $this->callAPISuccess('PledgePayment', 'create', array(
3341 'id' => $paymentID,
3342 'contribution_id' =>
3343 $contribution['id'],
3344 'status_id' => 'Pending',
3345 'scheduled_amount' => 500,
3346 ));
3347
3348 return $contribution['id'];
3349 }
3350
3351 /**
3352 * Create a pending contribution & linked pending participant record (along with an event).
3353 */
3354 public function createPendingParticipantContribution() {
3355 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'));
3356 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6, 'contact_id' => $this->_individualId));
3357 $this->_ids['participant'] = $participantID;
3358 $params = array_merge($this->_params, array('contact_id' => $this->_individualId, 'contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
3359 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3360 $this->callAPISuccess('participant_payment', 'create', array(
3361 'contribution_id' => $contribution['id'],
3362 'participant_id' => $participantID,
3363 ));
3364 $this->callAPISuccess('line_item', 'get', array(
3365 'entity_id' => $contribution['id'],
3366 'entity_table' => 'civicrm_contribution',
3367 'api.line_item.create' => array(
3368 'entity_id' => $participantID,
3369 'entity_table' => 'civicrm_participant',
3370 ),
3371 ));
3372 return $contribution['id'];
3373 }
3374
3375 /**
3376 * Get financial transaction amount.
3377 *
3378 * @param int $contId
3379 *
3380 * @return null|string
3381 */
3382 public function _getFinancialTrxnAmount($contId) {
3383 $query = "SELECT
3384 SUM( ft.total_amount ) AS total
3385 FROM civicrm_financial_trxn AS ft
3386 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
3387 WHERE ceft.entity_table = 'civicrm_contribution'
3388 AND ceft.entity_id = {$contId}";
3389
3390 $result = CRM_Core_DAO::singleValueQuery($query);
3391 return $result;
3392 }
3393
3394 /**
3395 * @param int $contId
3396 *
3397 * @return null|string
3398 */
3399 public function _getFinancialItemAmount($contId) {
3400 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
3401 $query = "SELECT
3402 SUM(amount)
3403 FROM civicrm_financial_item
3404 WHERE entity_table = 'civicrm_line_item'
3405 AND entity_id = {$lineItem}";
3406 $result = CRM_Core_DAO::singleValueQuery($query);
3407 return $result;
3408 }
3409
3410 /**
3411 * @param int $contId
3412 * @param $context
3413 */
3414 public function _checkFinancialItem($contId, $context) {
3415 if ($context != 'paylater') {
3416 $params = array(
3417 'entity_id' => $contId,
3418 'entity_table' => 'civicrm_contribution',
3419 );
3420 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($params, TRUE));
3421 $entityParams = array(
3422 'financial_trxn_id' => $trxn['financial_trxn_id'],
3423 'entity_table' => 'civicrm_financial_item',
3424 );
3425 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
3426 $params = array(
3427 'id' => $entityTrxn['entity_id'],
3428 );
3429 }
3430 if ($context == 'paylater') {
3431 $lineItems = CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution');
3432 foreach ($lineItems as $key => $item) {
3433 $params = array(
3434 'entity_id' => $key,
3435 'entity_table' => 'civicrm_line_item',
3436 );
3437 $compareParams = array('status_id' => 1);
3438 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3439 }
3440 }
3441 elseif ($context == 'refund') {
3442 $compareParams = array(
3443 'status_id' => 1,
3444 'financial_account_id' => 1,
3445 'amount' => -100,
3446 );
3447 }
3448 elseif ($context == 'cancelPending') {
3449 $compareParams = array(
3450 'status_id' => 3,
3451 'financial_account_id' => 1,
3452 'amount' => -100,
3453 );
3454 }
3455 elseif ($context == 'changeFinancial') {
3456 $lineKey = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
3457 $params = array(
3458 'entity_id' => $lineKey,
3459 'amount' => -100,
3460 );
3461 $compareParams = array(
3462 'financial_account_id' => 1,
3463 );
3464 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3465 $params = array(
3466 'financial_account_id' => 3,
3467 'entity_id' => $lineKey,
3468 );
3469 $compareParams = array(
3470 'amount' => 100,
3471 );
3472 }
3473 if ($context != 'paylater') {
3474 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3475 }
3476 }
3477
3478 /**
3479 * Check correct financial transaction entries were created for the change in payment instrument.
3480 *
3481 * @param int $contributionID
3482 * @param int $originalInstrumentID
3483 * @param int $newInstrumentID
3484 */
3485 public function checkFinancialTrxnPaymentInstrumentChange($contributionID, $originalInstrumentID, $newInstrumentID, $amount = 100) {
3486
3487 $entityFinancialTrxns = $this->getFinancialTransactionsForContribution($contributionID);
3488
3489 $originalTrxnParams = array(
3490 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($originalInstrumentID),
3491 'payment_instrument_id' => $originalInstrumentID,
3492 'amount' => $amount,
3493 'status_id' => 1,
3494 );
3495
3496 $reversalTrxnParams = array(
3497 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($originalInstrumentID),
3498 'payment_instrument_id' => $originalInstrumentID,
3499 'amount' => -$amount,
3500 'status_id' => 1,
3501 );
3502
3503 $newTrxnParams = array(
3504 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($newInstrumentID),
3505 'payment_instrument_id' => $newInstrumentID,
3506 'amount' => $amount,
3507 'status_id' => 1,
3508 );
3509
3510 foreach (array($originalTrxnParams, $reversalTrxnParams, $newTrxnParams) as $index => $transaction) {
3511 $entityFinancialTrxn = $entityFinancialTrxns[$index];
3512 $this->assertEquals($entityFinancialTrxn['amount'], $transaction['amount']);
3513
3514 $financialTrxn = $this->callAPISuccessGetSingle('FinancialTrxn', array(
3515 'id' => $entityFinancialTrxn['financial_trxn_id'],
3516 ));
3517 $this->assertEquals($transaction['status_id'], $financialTrxn['status_id']);
3518 $this->assertEquals($transaction['amount'], $financialTrxn['total_amount']);
3519 $this->assertEquals($transaction['amount'], $financialTrxn['net_amount']);
3520 $this->assertEquals(0, $financialTrxn['fee_amount']);
3521 $this->assertEquals($transaction['payment_instrument_id'], $financialTrxn['payment_instrument_id']);
3522 $this->assertEquals($transaction['to_financial_account_id'], $financialTrxn['to_financial_account_id']);
3523
3524 // Generic checks.
3525 $this->assertEquals(1, $financialTrxn['is_payment']);
3526 $this->assertEquals('USD', $financialTrxn['currency']);
3527 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($financialTrxn['trxn_date'])));
3528 }
3529 }
3530
3531 /**
3532 * Check financial transaction.
3533 *
3534 * @todo break this down into sensible functions - most calls to it only use a few lines out of the big if.
3535 *
3536 * @param array $contribution
3537 * @param string $context
3538 * @param int $instrumentId
3539 * @param array $extraParams
3540 */
3541 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL, $extraParams = array()) {
3542 $financialTrxns = $this->getFinancialTransactionsForContribution($contribution['id']);
3543 $trxn = array_pop($financialTrxns);
3544
3545 $params = array(
3546 'id' => $trxn['financial_trxn_id'],
3547 );
3548 if ($context == 'payLater') {
3549 $compareParams = array(
3550 'status_id' => 1,
3551 'from_financial_account_id' => CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($contribution['financial_type_id'], 'Accounts Receivable Account is'),
3552 );
3553 }
3554 elseif ($context == 'refund') {
3555 $compareParams = array(
3556 'to_financial_account_id' => 6,
3557 'total_amount' => -100,
3558 'status_id' => 7,
3559 'trxn_date' => '2015-01-01 09:00:00',
3560 'trxn_id' => 'the refund',
3561 );
3562 }
3563 elseif ($context == 'cancelPending') {
3564 $compareParams = array(
3565 'to_financial_account_id' => 7,
3566 'total_amount' => -100,
3567 'status_id' => 3,
3568 );
3569 }
3570 elseif ($context == 'changeFinancial' || $context == 'paymentInstrument') {
3571 // @todo checkFinancialTrxnPaymentInstrumentChange instead for paymentInstrument.
3572 // It does the same thing with greater readability.
3573 // @todo remove handling for
3574
3575 $entityParams = array(
3576 'entity_id' => $contribution['id'],
3577 'entity_table' => 'civicrm_contribution',
3578 'amount' => -100,
3579 );
3580 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
3581 $trxnParams1 = array(
3582 'id' => $trxn['financial_trxn_id'],
3583 );
3584 if (empty($extraParams)) {
3585 $compareParams = array(
3586 'total_amount' => -100,
3587 'status_id' => 1,
3588 );
3589 }
3590 else {
3591 $compareParams = array(
3592 'total_amount' => 100,
3593 'status_id' => 1,
3594 );
3595 }
3596 if ($context == 'paymentInstrument') {
3597 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($instrumentId);
3598 $compareParams['payment_instrument_id'] = $instrumentId;
3599 }
3600 else {
3601 $compareParams['to_financial_account_id'] = 12;
3602 }
3603 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, array_merge($compareParams, $extraParams));
3604 $compareParams['total_amount'] = 100;
3605 }
3606
3607 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, array_merge($compareParams, $extraParams));
3608 }
3609
3610 /**
3611 * @return mixed
3612 */
3613 public function _addPaymentInstrument() {
3614 $gId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
3615 $optionParams = array(
3616 'option_group_id' => $gId,
3617 'label' => 'Test Card',
3618 'name' => 'Test Card',
3619 'value' => '6',
3620 'weight' => '6',
3621 'is_active' => 1,
3622 );
3623 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
3624 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
3625 $financialParams = array(
3626 'entity_table' => 'civicrm_option_value',
3627 'entity_id' => $optionValue['id'],
3628 'account_relationship' => $relationTypeId,
3629 'financial_account_id' => 7,
3630 );
3631 CRM_Financial_BAO_FinancialTypeAccount::add($financialParams, CRM_Core_DAO::$_nullArray);
3632 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
3633 return $optionValue['values'][$optionValue['id']]['value'];
3634 }
3635
3636 public function _deletedAddedPaymentInstrument() {
3637 $result = $this->callAPISuccess('OptionValue', 'get', array(
3638 'option_group_id' => 'payment_instrument',
3639 'name' => 'Test Card',
3640 'value' => '6',
3641 'is_active' => 1,
3642 ));
3643 if ($id = CRM_Utils_Array::value('id', $result)) {
3644 $this->callAPISuccess('OptionValue', 'delete', array('id' => $id));
3645 }
3646 }
3647
3648 /**
3649 * Set up the basic recurring contribution for tests.
3650 *
3651 * @param array $generalParams
3652 * Parameters that can be merged into the recurring AND the contribution.
3653 *
3654 * @param array $recurParams
3655 * Parameters to merge into the recur only.
3656 *
3657 * @return array|int
3658 */
3659 protected function setUpRecurringContribution($generalParams = array(), $recurParams = array()) {
3660 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3661 'contact_id' => $this->_individualId,
3662 'installments' => '12',
3663 'frequency_interval' => '1',
3664 'amount' => '100',
3665 'contribution_status_id' => 1,
3666 'start_date' => '2012-01-01 00:00:00',
3667 'currency' => 'USD',
3668 'frequency_unit' => 'month',
3669 'payment_processor_id' => $this->paymentProcessorID,
3670 ), $generalParams, $recurParams));
3671 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3672 $this->_params,
3673 array(
3674 'contribution_recur_id' => $contributionRecur['id'],
3675 ), $generalParams)
3676 );
3677 return $originalContribution;
3678 }
3679
3680 /**
3681 * Set up a basic auto-renew membership for tests.
3682 *
3683 * @param array $generalParams
3684 * Parameters that can be merged into the recurring AND the contribution.
3685 *
3686 * @param array $recurParams
3687 * Parameters to merge into the recur only.
3688 *
3689 * @return array|int
3690 */
3691 protected function setUpAutoRenewMembership($generalParams = array(), $recurParams = array()) {
3692 $newContact = $this->callAPISuccess('Contact', 'create', array(
3693 'contact_type' => 'Individual',
3694 'sort_name' => 'McTesterson, Testy',
3695 'display_name' => 'Testy McTesterson',
3696 'preferred_language' => 'en_US',
3697 'preferred_mail_format' => 'Both',
3698 'first_name' => 'Testy',
3699 'last_name' => 'McTesterson',
3700 'contact_is_deleted' => '0',
3701 'email_id' => '4',
3702 'email' => 'tmctesterson@example.com',
3703 'on_hold' => '0',
3704 ));
3705 $membershipType = $this->callAPISuccess('MembershipType', 'create', array(
3706 'domain_id' => "Default Domain Name",
3707 'member_of_contact_id' => 1,
3708 'financial_type_id' => "Member Dues",
3709 'duration_unit' => "month",
3710 'duration_interval' => 1,
3711 'period_type' => 'rolling',
3712 'name' => "Standard Member",
3713 'minimum_fee' => 100,
3714 ));
3715 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3716 'contact_id' => $newContact['id'],
3717 'installments' => '12',
3718 'frequency_interval' => '1',
3719 'amount' => '100',
3720 'contribution_status_id' => 1,
3721 'start_date' => '2012-01-01 00:00:00',
3722 'currency' => 'USD',
3723 'frequency_unit' => 'month',
3724 'payment_processor_id' => $this->paymentProcessorID,
3725 ), $generalParams, $recurParams));
3726
3727 $membership = $this->callAPISuccess('membership', 'create', array(
3728 'contact_id' => $newContact['id'],
3729 'contribution_recur_id' => $contributionRecur['id'],
3730 'financial_type_id' => "Member Dues",
3731 'membership_type_id' => $membershipType['id'],
3732 'num_terms' => 1,
3733 'skipLineItem' => TRUE,
3734 ));
3735
3736 CRM_Price_BAO_LineItem::getLineItemArray($this->_params, NULL, 'membership', $membershipType['id']);
3737 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3738 $this->_params,
3739 array(
3740 'contact_id' => $newContact['id'],
3741 'contribution_recur_id' => $contributionRecur['id'],
3742 'financial_type_id' => "Member Dues",
3743 'contribution_status_id' => 1,
3744 'invoice_id' => uniqid(),
3745 ), $generalParams)
3746 );
3747 $lineItem = $this->callAPISuccess('LineItem', 'getsingle', array());
3748 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
3749 $membership = $this->callAPISuccess('Membership', 'getsingle', array('id' => $lineItem['entity_id']));
3750 $this->callAPISuccess('LineItem', 'getsingle', array());
3751 $this->callAPISuccessGetCount('MembershipPayment', array('membership_id' => $membership['id']), 1);
3752
3753 return array($originalContribution, $membership);
3754 }
3755 /**
3756 * Set up a repeat transaction.
3757 *
3758 * @param array $recurParams
3759 *
3760 * @return array
3761 */
3762 protected function setUpRepeatTransaction($recurParams = array(), $flag, $contributionParams = array()) {
3763 $paymentProcessorID = $this->paymentProcessorCreate();
3764 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3765 'contact_id' => $this->_individualId,
3766 'installments' => '12',
3767 'frequency_interval' => '1',
3768 'amount' => '500',
3769 'contribution_status_id' => 1,
3770 'start_date' => '2012-01-01 00:00:00',
3771 'currency' => 'USD',
3772 'frequency_unit' => 'month',
3773 'payment_processor_id' => $paymentProcessorID,
3774 ), $recurParams));
3775
3776 $originalContribution = '';
3777 if ($flag == 'multiple') {
3778 // CRM-19309 create a contribution + also add in line_items (plural):
3779 $params = array_merge($this->_params, $contributionParams);
3780 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3781 $params,
3782 array(
3783 'contribution_recur_id' => $contributionRecur['id'],
3784 'skipLineItem' => 1,
3785 'api.line_item.create' => array(
3786 array(
3787 'price_field_id' => 1,
3788 'qty' => 2,
3789 'line_total' => '20',
3790 'unit_price' => '10',
3791 'financial_type_id' => 1,
3792 ),
3793 array(
3794 'price_field_id' => 1,
3795 'qty' => 1,
3796 'line_total' => '80',
3797 'unit_price' => '80',
3798 'financial_type_id' => 2,
3799 ),
3800 ),
3801 )
3802 )
3803 );
3804 }
3805 elseif ($flag == 'single') {
3806 $params = array_merge($this->_params, array('contribution_recur_id' => $contributionRecur['id']));
3807 $params = array_merge($params, $contributionParams);
3808 $originalContribution = $this->callAPISuccess('contribution', 'create', $params);
3809 }
3810 $originalContribution['payment_processor_id'] = $paymentProcessorID;
3811 return $originalContribution;
3812 }
3813
3814 /**
3815 * Common set up routine.
3816 *
3817 * @return array
3818 */
3819 protected function setUpForCompleteTransaction() {
3820 $this->mut = new CiviMailUtils($this, TRUE);
3821 $this->createLoggedInUser();
3822 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
3823 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3824 return $contribution;
3825 }
3826
3827 /**
3828 * Test repeat contribution uses the Payment Processor' payment_instrument setting.
3829 */
3830 public function testRepeatTransactionWithNonCreditCardDefault() {
3831 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
3832 'contact_id' => $this->_individualId,
3833 'installments' => '12',
3834 'frequency_interval' => '1',
3835 'amount' => '100',
3836 'contribution_status_id' => 1,
3837 'start_date' => '2012-01-01 00:00:00',
3838 'currency' => 'USD',
3839 'frequency_unit' => 'month',
3840 'payment_processor_id' => $this->paymentProcessorID,
3841 ));
3842 $contribution1 = $this->callAPISuccess('contribution', 'create', array_merge(
3843 $this->_params,
3844 array('contribution_recur_id' => $contributionRecur['id'], 'payment_instrument_id' => 2))
3845 );
3846 $paymentInstruments = CRM_Contribute_PseudoConstant::paymentInstrument('name');
3847 $contribution2 = $this->callAPISuccess('contribution', 'repeattransaction', array(
3848 'contribution_status_id' => 'Completed',
3849 'trxn_id' => uniqid(),
3850 'original_contribution_id' => $contribution1,
3851 ));
3852 $this->assertEquals(array_search('Debit Card', $paymentInstruments), $contribution2['values'][$contribution2['id']]['payment_instrument_id']);
3853 $this->quickCleanUpFinancialEntities();
3854 }
3855
3856 /**
3857 * CRM-20008 Tests repeattransaction creates pending membership.
3858 */
3859 public function testRepeatTransactionPendingMembership() {
3860 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
3861 $this->callAPISuccess('membership', 'create', array(
3862 'id' => $membership['id'],
3863 'end_date' => 'yesterday',
3864 'status_id' => 'Expired',
3865 ));
3866 $repeatedContribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
3867 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
3868 'contribution_status_id' => 'Pending',
3869 'trxn_id' => uniqid(),
3870 ));
3871 $membershipStatusId = $this->callAPISuccess('membership', 'getvalue', array(
3872 'id' => $membership['id'],
3873 'return' => 'status_id',
3874 ));
3875
3876 // Let's see if the membership payments got created while we're at it.
3877 $membershipPayments = $this->callAPISuccess('MembershipPayment', 'get', array(
3878 'memberhip_id' => $membership['id'],
3879 ));
3880 $this->assertEquals(2, $membershipPayments['count']);
3881
3882 $this->assertEquals('Expired', CRM_Core_PseudoConstant::getLabel('CRM_Member_BAO_Membership', 'status_id', $membershipStatusId));
3883 $this->callAPISuccess('Contribution', 'completetransaction', array('id' => $repeatedContribution['id']));
3884 $membership = $this->callAPISuccessGetSingle('membership', array(
3885 'id' => $membership['id'],
3886 'return' => 'status_id, end_date',
3887 ));
3888 $this->assertEquals('New', CRM_Core_PseudoConstant::getLabel('CRM_Member_BAO_Membership', 'status_id', $membership['status_id']));
3889 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 month')), $membership['end_date']);
3890
3891 $this->quickCleanUpFinancialEntities();
3892 $this->contactDelete($originalContribution['values'][1]['contact_id']);
3893 }
3894
3895 /**
3896 * Test sending a mail via the API.
3897 */
3898 public function testSendMailWithAPISetFromDetails() {
3899 $mut = new CiviMailUtils($this, TRUE);
3900 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
3901 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3902 'id' => $contribution['id'],
3903 'receipt_from_email' => 'api@civicrm.org',
3904 'receipt_from_name' => 'CiviCRM LLC',
3905 ));
3906 $mut->checkMailLog(array(
3907 'From: CiviCRM LLC <api@civicrm.org>',
3908 'Contribution Information',
3909 'Please print this confirmation for your records',
3910 ), array(
3911 'Event',
3912 )
3913 );
3914 $mut->stop();
3915 }
3916
3917 /**
3918 * Test sending a mail via the API.
3919 */
3920 public function testSendMailWithNoFromSetFallToDomain() {
3921 $this->createLoggedInUser();
3922 $mut = new CiviMailUtils($this, TRUE);
3923 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
3924 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3925 'id' => $contribution['id'],
3926 ));
3927 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => 1));
3928 $mut->checkMailLog(array(
3929 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>',
3930 'Contribution Information',
3931 'Please print this confirmation for your records',
3932 ), array(
3933 'Event',
3934 )
3935 );
3936 $mut->stop();
3937 }
3938
3939 /**
3940 * Test sending a mail via the API.
3941 */
3942 public function testSendMailWithRepeatTransactionAPIFalltoDomain() {
3943 $this->createLoggedInUser();
3944 $mut = new CiviMailUtils($this, TRUE);
3945 $contribution = $this->setUpRepeatTransaction(array(), 'single');
3946 $this->callAPISuccess('contribution', 'repeattransaction', array(
3947 'contribution_status_id' => 'Completed',
3948 'trxn_id' => uniqid(),
3949 'original_contribution_id' => $contribution,
3950 ));
3951 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => 1));
3952 $mut->checkMailLog(array(
3953 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>',
3954 'Contribution Information',
3955 'Please print this confirmation for your records',
3956 ), array(
3957 'Event',
3958 )
3959 );
3960 $mut->stop();
3961 }
3962
3963 /**
3964 * Test sending a mail via the API.
3965 */
3966 public function testSendMailWithRepeatTransactionAPIFalltoContributionPage() {
3967 $mut = new CiviMailUtils($this, TRUE);
3968 $contributionPage = $this->contributionPageCreate(array('receipt_from_name' => 'CiviCRM LLC', 'receipt_from_email' => 'contributionpage@civicrm.org', 'is_email_receipt' => 1));
3969 $paymentProcessorID = $this->paymentProcessorCreate();
3970 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
3971 'contact_id' => $this->_individualId,
3972 'installments' => '12',
3973 'frequency_interval' => '1',
3974 'amount' => '500',
3975 'contribution_status_id' => 1,
3976 'start_date' => '2012-01-01 00:00:00',
3977 'currency' => 'USD',
3978 'frequency_unit' => 'month',
3979 'payment_processor_id' => $paymentProcessorID,
3980 ));
3981 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3982 $this->_params,
3983 array(
3984 'contribution_recur_id' => $contributionRecur['id'],
3985 'contribution_page_id' => $contributionPage['id']))
3986 );
3987 $this->callAPISuccess('contribution', 'repeattransaction', array(
3988 'contribution_status_id' => 'Completed',
3989 'trxn_id' => uniqid(),
3990 'original_contribution_id' => $originalContribution,
3991 )
3992 );
3993 $mut->checkMailLog(array(
3994 'From: CiviCRM LLC <contributionpage@civicrm.org>',
3995 'Contribution Information',
3996 'Please print this confirmation for your records',
3997 ), array(
3998 'Event',
3999 )
4000 );
4001 $mut->stop();
4002 }
4003
4004 /**
4005 * Test sending a mail via the API.
4006 */
4007 public function testSendMailWithRepeatTransactionAPIFalltoSystemFromNoDefaultFrom() {
4008 $mut = new CiviMailUtils($this, TRUE);
4009 $originalContribution = $contribution = $this->setUpRepeatTransaction(array(), 'single');
4010 $fromEmail = $this->CallAPISuccess('optionValue', 'get', array('is_default' => 1, 'option_group_id' => 'from_email_address', 'sequential' => 1));
4011 foreach ($fromEmail['values'] as $from) {
4012 $this->callAPISuccess('optionValue', 'create', array('is_default' => 0, 'id' => $from['id']));
4013 }
4014 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => CRM_Core_Config::domainID()));
4015 $this->callAPISuccess('contribution', 'repeattransaction', array(
4016 'contribution_status_id' => 'Completed',
4017 'trxn_id' => uniqid(),
4018 'original_contribution_id' => $originalContribution,
4019 )
4020 );
4021 $mut->checkMailLog(array(
4022 'From: ' . $domain['name'] . ' <' . $domain['domain_email'] . '>',
4023 'Contribution Information',
4024 'Please print this confirmation for your records',
4025 ), array(
4026 'Event',
4027 )
4028 );
4029 $mut->stop();
4030 }
4031
4032 /**
4033 * Create a Contribution Page with is_email_receipt = TRUE.
4034 *
4035 * @param array $params
4036 * Params to overwrite with.
4037 *
4038 * @return array|int
4039 */
4040 protected function createReceiptableContributionPage($params = array()) {
4041 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array_merge(array(
4042 'receipt_from_name' => 'Mickey Mouse',
4043 'receipt_from_email' => 'mickey@mouse.com',
4044 'title' => "Test Contribution Page",
4045 'financial_type_id' => 1,
4046 'currency' => 'CAD',
4047 'is_monetary' => TRUE,
4048 'is_email_receipt' => TRUE,
4049 ), $params));
4050 return $contributionPage;
4051 }
4052
4053 /**
4054 * function to test card_type and pan truncation.
4055 */
4056 public function testCardTypeAndPanTruncation() {
4057 $creditCardTypeIDs = array_flip(CRM_Financial_DAO_FinancialTrxn::buildOptions('card_type_id'));
4058 $contactId = $this->individualCreate();
4059 $params = array(
4060 'contact_id' => $contactId,
4061 'receive_date' => '2016-01-20',
4062 'total_amount' => 100,
4063 'financial_type_id' => 1,
4064 'payment_instrument' => 'Credit Card',
4065 'card_type_id' => $creditCardTypeIDs['Visa'],
4066 'pan_truncation' => 4567,
4067 );
4068 $contribution = $this->callAPISuccess('contribution', 'create', $params);
4069 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
4070 $financialTrxn = $this->callAPISuccessGetSingle(
4071 'FinancialTrxn',
4072 array(
4073 'id' => $lastFinancialTrxnId['financialTrxnId'],
4074 'return' => array('card_type_id', 'pan_truncation'),
4075 )
4076 );
4077 $this->assertEquals(CRM_Utils_Array::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Visa']);
4078 $this->assertEquals(CRM_Utils_Array::value('pan_truncation', $financialTrxn), 4567);
4079 $params = array(
4080 'id' => $contribution['id'],
4081 'pan_truncation' => 2345,
4082 'card_type_id' => $creditCardTypeIDs['Amex'],
4083 );
4084 $contribution = $this->callAPISuccess('contribution', 'create', $params);
4085 $financialTrxn = $this->callAPISuccessGetSingle(
4086 'FinancialTrxn',
4087 array(
4088 'id' => $lastFinancialTrxnId['financialTrxnId'],
4089 'return' => array('card_type_id', 'pan_truncation'),
4090 )
4091 );
4092 $this->assertEquals(CRM_Utils_Array::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Amex']);
4093 $this->assertEquals(CRM_Utils_Array::value('pan_truncation', $financialTrxn), 2345);
4094 }
4095
4096 /**
4097 * Test repeat contribution uses non default currency
4098 * @see https://issues.civicrm.org/jira/projects/CRM/issues/CRM-20678
4099 */
4100 public function testRepeatTransactionWithDifferenceCurrency() {
4101 $originalContribution = $this->setUpRepeatTransaction(array('currency' => 'AUD'), 'single', array('currency' => 'AUD'));
4102 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
4103 'original_contribution_id' => $originalContribution['id'],
4104 'contribution_status_id' => 'Completed',
4105 'trxn_id' => uniqid(),
4106 ));
4107 $this->assertEquals('AUD', $contribution['values'][$contribution['id']]['currency']);
4108 }
4109
4110 /**
4111 * Get the financial items for the contribution.
4112 *
4113 * @param int $contributionID
4114 *
4115 * @return array
4116 * Array of associated financial items.
4117 */
4118 protected function getFinancialTransactionsForContribution($contributionID) {
4119 $trxnParams = array(
4120 'entity_id' => $contributionID,
4121 'entity_table' => 'civicrm_contribution',
4122 );
4123 // @todo the following function has naming errors & has a weird signature & appears to
4124 // only be called from test classes. Move into test suite & maybe just use api
4125 // from this function.
4126 return array_merge(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($trxnParams, FALSE, array()));
4127 }
4128
4129 }