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