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