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