| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 11 | | under the terms of the GNU Affero General Public License | |
| 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 13 | | | |
| 14 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 17 | | See the GNU Affero General Public License for more details. | |
| 18 | | | |
| 19 | | You should have received a copy of the GNU Affero General Public | |
| 20 | | License and the CiviCRM Licensing Exception along | |
| 21 | | with this program; if not, contact CiviCRM LLC | |
| 22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 25 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * Test APIv3 civicrm_contribute_* functions |
| 30 | * |
| 31 | * @package CiviCRM_APIv3 |
| 32 | * @subpackage API_Contribution |
| 33 | * @group headless |
| 34 | */ |
| 35 | class api_v3_PaymentTest extends CiviUnitTestCase { |
| 36 | |
| 37 | /** |
| 38 | * Assume empty database with just civicrm_data. |
| 39 | */ |
| 40 | protected $_individualId; |
| 41 | protected $_financialTypeId = 1; |
| 42 | protected $_apiversion; |
| 43 | public $debug = 0; |
| 44 | |
| 45 | /** |
| 46 | * Setup function. |
| 47 | */ |
| 48 | public function setUp() { |
| 49 | parent::setUp(); |
| 50 | |
| 51 | $this->_apiversion = 3; |
| 52 | $this->_individualId = $this->individualCreate(); |
| 53 | CRM_Core_Config::singleton()->userPermissionClass->permissions = array(); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Clean up after each test. |
| 58 | */ |
| 59 | public function tearDown() { |
| 60 | $this->quickCleanUpFinancialEntities(); |
| 61 | $this->quickCleanup(array('civicrm_uf_match')); |
| 62 | unset(CRM_Core_Config::singleton()->userPermissionClass->permissions); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Test Get Payment api. |
| 67 | */ |
| 68 | public function testGetPayment() { |
| 69 | $p = array( |
| 70 | 'contact_id' => $this->_individualId, |
| 71 | 'receive_date' => '2010-01-20', |
| 72 | 'total_amount' => 100.00, |
| 73 | 'financial_type_id' => $this->_financialTypeId, |
| 74 | 'trxn_id' => 23456, |
| 75 | 'contribution_status_id' => 1, |
| 76 | ); |
| 77 | $contribution = $this->callAPISuccess('contribution', 'create', $p); |
| 78 | |
| 79 | $params = array( |
| 80 | 'contribution_id' => $contribution['id'], |
| 81 | 'check_permissions' => TRUE, |
| 82 | ); |
| 83 | CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM', 'administer CiviCRM'); |
| 84 | $payment = $this->callAPIFailure('payment', 'get', $params, 'API permission check failed for Payment/get call; insufficient permission: require access CiviCRM and access CiviContribute'); |
| 85 | |
| 86 | array_push(CRM_Core_Config::singleton()->userPermissionClass->permissions, 'access CiviContribute'); |
| 87 | $payment = $this->callAPISuccess('payment', 'get', $params); |
| 88 | |
| 89 | $payment = $this->callAPIAndDocument('payment', 'get', $params, __FUNCTION__, __FILE__); |
| 90 | $this->assertEquals(1, $payment['count']); |
| 91 | |
| 92 | $expectedResult = array( |
| 93 | $contribution['id'] => array( |
| 94 | 'total_amount' => 100, |
| 95 | 'trxn_id' => 23456, |
| 96 | 'trxn_date' => '2010-01-20 00:00:00', |
| 97 | 'contribution_id' => $contribution['id'], |
| 98 | 'is_payment' => 1, |
| 99 | ), |
| 100 | ); |
| 101 | $this->checkPaymentResult($payment, $expectedResult); |
| 102 | $this->callAPISuccess('Contribution', 'Delete', array( |
| 103 | 'id' => $contribution['id'], |
| 104 | )); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Test create payment api with no line item in params |
| 109 | */ |
| 110 | public function testCreatePaymentNoLineItems() { |
| 111 | list($lineItems, $contribution) = $this->createParticipantWithContribution(); |
| 112 | |
| 113 | //Create partial payment |
| 114 | $params = array( |
| 115 | 'contribution_id' => $contribution['id'], |
| 116 | 'total_amount' => 50, |
| 117 | ); |
| 118 | $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__); |
| 119 | $expectedResult = array( |
| 120 | $payment['id'] => array( |
| 121 | 'from_financial_account_id' => 7, |
| 122 | 'to_financial_account_id' => 6, |
| 123 | 'total_amount' => 50, |
| 124 | 'status_id' => 1, |
| 125 | 'is_payment' => 1, |
| 126 | ), |
| 127 | ); |
| 128 | $this->checkPaymentResult($payment, $expectedResult); |
| 129 | |
| 130 | // Check entity financial trxn created properly |
| 131 | $params = array( |
| 132 | 'entity_id' => $contribution['id'], |
| 133 | 'entity_table' => 'civicrm_contribution', |
| 134 | 'financial_trxn_id' => $payment['id'], |
| 135 | ); |
| 136 | |
| 137 | $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params); |
| 138 | |
| 139 | $this->assertEquals($eft['values'][$eft['id']]['amount'], 50); |
| 140 | |
| 141 | $params = array( |
| 142 | 'entity_table' => 'civicrm_financial_item', |
| 143 | 'financial_trxn_id' => $payment['id'], |
| 144 | ); |
| 145 | $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params); |
| 146 | $amounts = array(33.33, 16.67); |
| 147 | foreach ($eft['values'] as $value) { |
| 148 | $this->assertEquals($value['amount'], array_pop($amounts)); |
| 149 | } |
| 150 | |
| 151 | // Now create payment to complete total amount of contribution |
| 152 | $params = array( |
| 153 | 'contribution_id' => $contribution['id'], |
| 154 | 'total_amount' => 100, |
| 155 | ); |
| 156 | $payment = $this->callAPISuccess('payment', 'create', $params); |
| 157 | $expectedResult = array( |
| 158 | $payment['id'] => array( |
| 159 | 'from_financial_account_id' => 7, |
| 160 | 'to_financial_account_id' => 6, |
| 161 | 'total_amount' => 100, |
| 162 | 'status_id' => 1, |
| 163 | 'is_payment' => 1, |
| 164 | ), |
| 165 | ); |
| 166 | $this->checkPaymentResult($payment, $expectedResult); |
| 167 | $params = array( |
| 168 | 'entity_table' => 'civicrm_financial_item', |
| 169 | 'financial_trxn_id' => $payment['id'], |
| 170 | ); |
| 171 | $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params); |
| 172 | $amounts = array(66.67, 33.33); |
| 173 | foreach ($eft['values'] as $value) { |
| 174 | $this->assertEquals($value['amount'], array_pop($amounts)); |
| 175 | } |
| 176 | // Check contribution for completed status |
| 177 | $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'])); |
| 178 | |
| 179 | $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed'); |
| 180 | $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 300.00); |
| 181 | $paymentParticipant = array( |
| 182 | 'contribution_id' => $contribution['id'], |
| 183 | ); |
| 184 | $participantPayment = $this->callAPISuccess('ParticipantPayment', 'getsingle', $paymentParticipant); |
| 185 | $participant = $this->callAPISuccess('participant', 'get', array('id' => $participantPayment['participant_id'])); |
| 186 | $this->assertEquals($participant['values'][$participant['id']]['participant_status'], 'Registered'); |
| 187 | $this->callAPISuccess('Contribution', 'Delete', array( |
| 188 | 'id' => $contribution['id'], |
| 189 | )); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Function to assert db values |
| 194 | */ |
| 195 | public function checkPaymentResult($payment, $expectedResult) { |
| 196 | foreach ($expectedResult[$payment['id']] as $key => $value) { |
| 197 | $this->assertEquals($payment['values'][$payment['id']][$key], $value); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Test create payment api with line item in params |
| 203 | */ |
| 204 | public function testCreatePaymentLineItems() { |
| 205 | list($lineItems, $contribution) = $this->createParticipantWithContribution(); |
| 206 | $lineItems = $this->callAPISuccess('LineItem', 'get', array('contribution_id' => $contribution['id'])); |
| 207 | |
| 208 | //Create partial payment by passing line item array is params |
| 209 | $params = array( |
| 210 | 'contribution_id' => $contribution['id'], |
| 211 | 'total_amount' => 50, |
| 212 | ); |
| 213 | $amounts = array(40, 10); |
| 214 | foreach ($lineItems['values'] as $id => $ignore) { |
| 215 | $params['line_item'][] = array($id => array_pop($amounts)); |
| 216 | } |
| 217 | $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__, 'Payment with line item', 'CreatePaymentWithLineItems'); |
| 218 | $expectedResult = array( |
| 219 | $payment['id'] => array( |
| 220 | 'from_financial_account_id' => 7, |
| 221 | 'to_financial_account_id' => 6, |
| 222 | 'total_amount' => 50, |
| 223 | 'status_id' => 1, |
| 224 | 'is_payment' => 1, |
| 225 | ), |
| 226 | ); |
| 227 | $this->checkPaymentResult($payment, $expectedResult); |
| 228 | |
| 229 | // Check entity financial trxn created properly |
| 230 | $params = array( |
| 231 | 'entity_id' => $contribution['id'], |
| 232 | 'entity_table' => 'civicrm_contribution', |
| 233 | 'financial_trxn_id' => $payment['id'], |
| 234 | ); |
| 235 | |
| 236 | $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params); |
| 237 | |
| 238 | $this->assertEquals($eft['values'][$eft['id']]['amount'], 50); |
| 239 | |
| 240 | $params = array( |
| 241 | 'entity_table' => 'civicrm_financial_item', |
| 242 | 'financial_trxn_id' => $payment['id'], |
| 243 | ); |
| 244 | $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params); |
| 245 | $amounts = array(40, 10); |
| 246 | foreach ($eft['values'] as $value) { |
| 247 | $this->assertEquals($value['amount'], array_pop($amounts)); |
| 248 | } |
| 249 | |
| 250 | // Now create payment to complete total amount of contribution |
| 251 | $params = array( |
| 252 | 'contribution_id' => $contribution['id'], |
| 253 | 'total_amount' => 100, |
| 254 | ); |
| 255 | $amounts = array(80, 20); |
| 256 | foreach ($lineItems['values'] as $id => $ignore) { |
| 257 | $params['line_item'][] = array($id => array_pop($amounts)); |
| 258 | } |
| 259 | $payment = $this->callAPISuccess('payment', 'create', $params); |
| 260 | $expectedResult = array( |
| 261 | $payment['id'] => array( |
| 262 | 'from_financial_account_id' => 7, |
| 263 | 'to_financial_account_id' => 6, |
| 264 | 'total_amount' => 100, |
| 265 | 'status_id' => 1, |
| 266 | 'is_payment' => 1, |
| 267 | ), |
| 268 | ); |
| 269 | $this->checkPaymentResult($payment, $expectedResult); |
| 270 | $params = array( |
| 271 | 'entity_table' => 'civicrm_financial_item', |
| 272 | 'financial_trxn_id' => $payment['id'], |
| 273 | ); |
| 274 | $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params); |
| 275 | $amounts = array(80, 20); |
| 276 | foreach ($eft['values'] as $value) { |
| 277 | $this->assertEquals($value['amount'], array_pop($amounts)); |
| 278 | } |
| 279 | // Check contribution for completed status |
| 280 | $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'])); |
| 281 | |
| 282 | $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed'); |
| 283 | $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 300.00); |
| 284 | $paymentParticipant = array( |
| 285 | 'contribution_id' => $contribution['id'], |
| 286 | ); |
| 287 | $participantPayment = $this->callAPISuccess('ParticipantPayment', 'getsingle', $paymentParticipant); |
| 288 | $participant = $this->callAPISuccess('participant', 'get', array('id' => $participantPayment['participant_id'])); |
| 289 | $this->assertEquals($participant['values'][$participant['id']]['participant_status'], 'Registered'); |
| 290 | $this->callAPISuccess('Contribution', 'Delete', array( |
| 291 | 'id' => $contribution['id'], |
| 292 | )); |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Test cancel payment api |
| 297 | */ |
| 298 | public function testCancelPayment() { |
| 299 | CRM_Core_Config::singleton()->userPermissionClass->permissions = array('administer CiviCRM', 'access CiviContribute'); |
| 300 | list($lineItems, $contribution) = $this->createParticipantWithContribution(); |
| 301 | |
| 302 | $params = array( |
| 303 | 'contribution_id' => $contribution['id'], |
| 304 | ); |
| 305 | |
| 306 | $payment = $this->callAPISuccess('payment', 'get', $params); |
| 307 | $this->assertEquals(1, $payment['count']); |
| 308 | |
| 309 | $cancelParams = array( |
| 310 | 'id' => $payment['id'], |
| 311 | 'check_permissions' => TRUE, |
| 312 | ); |
| 313 | $payment = $this->callAPIFailure('payment', 'cancel', $cancelParams, 'API permission check failed for Payment/cancel call; insufficient permission: require access CiviCRM and access CiviContribute and edit contributions'); |
| 314 | |
| 315 | array_push(CRM_Core_Config::singleton()->userPermissionClass->permissions, 'access CiviCRM', 'edit contributions'); |
| 316 | |
| 317 | $this->callAPIAndDocument('payment', 'cancel', $cancelParams, __FUNCTION__, __FILE__); |
| 318 | |
| 319 | $payment = $this->callAPISuccess('payment', 'get', $params); |
| 320 | $this->assertEquals(2, $payment['count']); |
| 321 | $amounts = array(-150.00, 150.00); |
| 322 | foreach ($payment['values'] as $value) { |
| 323 | $this->assertEquals($value['total_amount'], array_pop($amounts), 'Mismatch total amount'); |
| 324 | } |
| 325 | |
| 326 | $this->callAPISuccess('Contribution', 'Delete', array( |
| 327 | 'id' => $contribution['id'], |
| 328 | )); |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Test delete payment api |
| 333 | */ |
| 334 | public function testDeletePayment() { |
| 335 | CRM_Core_Config::singleton()->userPermissionClass->permissions = array('administer CiviCRM', 'access CiviContribute'); |
| 336 | list($lineItems, $contribution) = $this->createParticipantWithContribution(); |
| 337 | |
| 338 | $params = array( |
| 339 | 'contribution_id' => $contribution['id'], |
| 340 | ); |
| 341 | |
| 342 | $payment = $this->callAPISuccess('payment', 'get', $params); |
| 343 | $this->assertEquals(1, $payment['count']); |
| 344 | |
| 345 | $deleteParams = array( |
| 346 | 'id' => $payment['id'], |
| 347 | 'check_permissions' => TRUE, |
| 348 | ); |
| 349 | $payment = $this->callAPIFailure('payment', 'delete', $deleteParams, 'API permission check failed for Payment/delete call; insufficient permission: require access CiviCRM and access CiviContribute and delete in CiviContribute'); |
| 350 | |
| 351 | array_push(CRM_Core_Config::singleton()->userPermissionClass->permissions, 'access CiviCRM', 'delete in CiviContribute'); |
| 352 | $this->callAPIAndDocument('payment', 'delete', $deleteParams, __FUNCTION__, __FILE__); |
| 353 | |
| 354 | $payment = $this->callAPISuccess('payment', 'get', $params); |
| 355 | $this->assertEquals(0, $payment['count']); |
| 356 | |
| 357 | $this->callAPISuccess('Contribution', 'Delete', array( |
| 358 | 'id' => $contribution['id'], |
| 359 | )); |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Test update payment api |
| 364 | */ |
| 365 | public function testUpdatePayment() { |
| 366 | CRM_Core_Config::singleton()->userPermissionClass->permissions = array('administer CiviCRM', 'access CiviContribute', 'edit contributions'); |
| 367 | list($lineItems, $contribution) = $this->createParticipantWithContribution(); |
| 368 | |
| 369 | //Create partial payment by passing line item array is params |
| 370 | $params = array( |
| 371 | 'contribution_id' => $contribution['id'], |
| 372 | 'total_amount' => 50, |
| 373 | ); |
| 374 | |
| 375 | $payment = $this->callAPISuccess('payment', 'create', $params); |
| 376 | $expectedResult = array( |
| 377 | $payment['id'] => array( |
| 378 | 'from_financial_account_id' => 7, |
| 379 | 'to_financial_account_id' => 6, |
| 380 | 'total_amount' => 50, |
| 381 | 'status_id' => 1, |
| 382 | 'is_payment' => 1, |
| 383 | ), |
| 384 | ); |
| 385 | $this->checkPaymentResult($payment, $expectedResult); |
| 386 | |
| 387 | $params = array( |
| 388 | 'entity_table' => 'civicrm_financial_item', |
| 389 | 'financial_trxn_id' => $payment['id'], |
| 390 | ); |
| 391 | $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params); |
| 392 | $amounts = array(33.33, 16.67); |
| 393 | foreach ($eft['values'] as $value) { |
| 394 | $this->assertEquals($value['amount'], array_pop($amounts)); |
| 395 | } |
| 396 | CRM_Core_Config::singleton()->userPermissionClass->permissions = array('administer CiviCRM', 'access CiviContribute'); |
| 397 | |
| 398 | // update the amount for payment |
| 399 | $params = array( |
| 400 | 'contribution_id' => $contribution['id'], |
| 401 | 'total_amount' => 100, |
| 402 | 'id' => $payment['id'], |
| 403 | 'check_permissions' => TRUE, |
| 404 | ); |
| 405 | $payment = $this->callAPIFailure('payment', 'create', $params, 'API permission check failed for Payment/create call; insufficient permission: require access CiviCRM and access CiviContribute and edit contributions'); |
| 406 | |
| 407 | array_push(CRM_Core_Config::singleton()->userPermissionClass->permissions, 'access CiviCRM', 'edit contributions'); |
| 408 | $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__, 'Update Payment', 'UpdatePayment'); |
| 409 | |
| 410 | // Check for proportional cancelled payment against lineitems. |
| 411 | $minParams = array( |
| 412 | 'entity_table' => 'civicrm_financial_item', |
| 413 | 'financial_trxn_id' => $payment['id'] - 1, |
| 414 | ); |
| 415 | |
| 416 | $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $minParams); |
| 417 | $amounts = array(-33.33, -16.67); |
| 418 | |
| 419 | foreach ($eft['values'] as $value) { |
| 420 | $this->assertEquals($value['amount'], array_pop($amounts)); |
| 421 | } |
| 422 | |
| 423 | // Check for proportional updated payment against lineitems. |
| 424 | $params = array( |
| 425 | 'entity_table' => 'civicrm_financial_item', |
| 426 | 'financial_trxn_id' => $payment['id'], |
| 427 | ); |
| 428 | $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params); |
| 429 | $amounts = array(66.67, 33.33); |
| 430 | foreach ($eft['values'] as $value) { |
| 431 | $this->assertEquals($value['amount'], array_pop($amounts)); |
| 432 | } |
| 433 | |
| 434 | $params = array( |
| 435 | 'contribution_id' => $contribution['id'], |
| 436 | ); |
| 437 | $payment = $this->callAPISuccess('payment', 'get', $params); |
| 438 | $amounts = array(100.00, -50.00, 50.00, 150.00); |
| 439 | foreach ($payment['values'] as $value) { |
| 440 | $amount = array_pop($amounts); |
| 441 | $this->assertEquals($value['total_amount'], $amount, 'Mismatch total amount'); |
| 442 | |
| 443 | // Check entity financial trxn created properly |
| 444 | $params = array( |
| 445 | 'entity_id' => $contribution['id'], |
| 446 | 'entity_table' => 'civicrm_contribution', |
| 447 | 'financial_trxn_id' => $value['id'], |
| 448 | ); |
| 449 | $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params); |
| 450 | $this->assertEquals($eft['values'][$eft['id']]['amount'], $amount); |
| 451 | } |
| 452 | |
| 453 | $this->callAPISuccess('Contribution', 'Delete', array( |
| 454 | 'id' => $contribution['id'], |
| 455 | )); |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * Test create payment api for paylater contribution |
| 460 | */ |
| 461 | public function testCreatePaymentPayLater() { |
| 462 | $this->createLoggedInUser(); |
| 463 | $contributionParams = array( |
| 464 | 'total_amount' => 100, |
| 465 | 'currency' => 'USD', |
| 466 | 'contact_id' => $this->_individualId, |
| 467 | 'financial_type_id' => 1, |
| 468 | 'contribution_status_id' => 2, |
| 469 | 'is_pay_later' => 1, |
| 470 | ); |
| 471 | $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams); |
| 472 | //add payment for pay later transaction |
| 473 | $params = array( |
| 474 | 'contribution_id' => $contribution['id'], |
| 475 | 'total_amount' => 100, |
| 476 | ); |
| 477 | $payment = $this->callAPISuccess('Payment', 'create', $params); |
| 478 | $expectedResult = array( |
| 479 | $payment['id'] => array( |
| 480 | 'from_financial_account_id' => 7, |
| 481 | 'to_financial_account_id' => 6, |
| 482 | 'total_amount' => 100, |
| 483 | 'status_id' => 1, |
| 484 | 'is_payment' => 1, |
| 485 | ), |
| 486 | ); |
| 487 | $this->checkPaymentResult($payment, $expectedResult); |
| 488 | // Check entity financial trxn created properly |
| 489 | $params = array( |
| 490 | 'entity_id' => $contribution['id'], |
| 491 | 'entity_table' => 'civicrm_contribution', |
| 492 | 'financial_trxn_id' => $payment['id'], |
| 493 | ); |
| 494 | $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params); |
| 495 | $this->assertEquals($eft['values'][$eft['id']]['amount'], 100); |
| 496 | $params = array( |
| 497 | 'entity_table' => 'civicrm_financial_item', |
| 498 | 'financial_trxn_id' => $payment['id'], |
| 499 | ); |
| 500 | $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params); |
| 501 | $this->assertEquals($eft['values'][$eft['id']]['amount'], 100); |
| 502 | // Check contribution for completed status |
| 503 | $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'])); |
| 504 | $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed'); |
| 505 | $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00); |
| 506 | $this->callAPISuccess('Contribution', 'Delete', array( |
| 507 | 'id' => $contribution['id'], |
| 508 | )); |
| 509 | } |
| 510 | |
| 511 | /** |
| 512 | * Test create payment api for paylater contribution with partial payment. |
| 513 | */ |
| 514 | public function testCreatePaymentPayLaterPartialPayment() { |
| 515 | $this->createLoggedInUser(); |
| 516 | $contributionParams = array( |
| 517 | 'total_amount' => 100, |
| 518 | 'currency' => 'USD', |
| 519 | 'contact_id' => $this->_individualId, |
| 520 | 'financial_type_id' => 1, |
| 521 | 'contribution_status_id' => 2, |
| 522 | 'is_pay_later' => 1, |
| 523 | ); |
| 524 | $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams); |
| 525 | //Create partial payment |
| 526 | $params = array( |
| 527 | 'contribution_id' => $contribution['id'], |
| 528 | 'total_amount' => 60, |
| 529 | ); |
| 530 | $payment = $this->callAPISuccess('Payment', 'create', $params); |
| 531 | $expectedResult = array( |
| 532 | $payment['id'] => array( |
| 533 | 'total_amount' => 60, |
| 534 | 'status_id' => 1, |
| 535 | 'is_payment' => 1, |
| 536 | ), |
| 537 | ); |
| 538 | $this->checkPaymentResult($payment, $expectedResult); |
| 539 | // Check entity financial trxn created properly |
| 540 | $params = array( |
| 541 | 'entity_id' => $contribution['id'], |
| 542 | 'entity_table' => 'civicrm_contribution', |
| 543 | 'financial_trxn_id' => $payment['id'], |
| 544 | ); |
| 545 | $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params); |
| 546 | $this->assertEquals($eft['values'][$eft['id']]['amount'], 60); |
| 547 | $params = array( |
| 548 | 'entity_table' => 'civicrm_financial_item', |
| 549 | 'financial_trxn_id' => $payment['id'], |
| 550 | ); |
| 551 | $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params); |
| 552 | $this->assertEquals($eft['values'][$eft['id']]['amount'], 60); |
| 553 | $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'])); |
| 554 | $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Partially paid'); |
| 555 | $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00); |
| 556 | //Create full payment |
| 557 | $params = array( |
| 558 | 'contribution_id' => $contribution['id'], |
| 559 | 'total_amount' => 40, |
| 560 | ); |
| 561 | $payment = $this->callAPISuccess('Payment', 'create', $params); |
| 562 | $expectedResult = array( |
| 563 | $payment['id'] => array( |
| 564 | 'from_financial_account_id' => 7, |
| 565 | 'to_financial_account_id' => 6, |
| 566 | 'total_amount' => 40, |
| 567 | 'status_id' => 1, |
| 568 | 'is_payment' => 1, |
| 569 | ), |
| 570 | ); |
| 571 | $this->checkPaymentResult($payment, $expectedResult); |
| 572 | // Check entity financial trxn created properly |
| 573 | $params = array( |
| 574 | 'entity_id' => $contribution['id'], |
| 575 | 'entity_table' => 'civicrm_contribution', |
| 576 | 'financial_trxn_id' => $payment['id'], |
| 577 | ); |
| 578 | $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params); |
| 579 | $this->assertEquals($eft['values'][$eft['id']]['amount'], 40); |
| 580 | $params = array( |
| 581 | 'entity_table' => 'civicrm_financial_item', |
| 582 | 'financial_trxn_id' => $payment['id'], |
| 583 | ); |
| 584 | $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params); |
| 585 | $this->assertEquals($eft['values'][$eft['id']]['amount'], 40); |
| 586 | // Check contribution for completed status |
| 587 | $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'])); |
| 588 | $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed'); |
| 589 | $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00); |
| 590 | $this->callAPISuccess('Contribution', 'Delete', array( |
| 591 | 'id' => $contribution['id'], |
| 592 | )); |
| 593 | } |
| 594 | |
| 595 | } |