From: Pradeep Nayak Date: Wed, 27 Jan 2016 23:42:31 +0000 (+0530) Subject: CRM-16259, minor fix and added test for pending with partial payment X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=d5b39a17c04694405bae41142337ef31419ba66c;p=civicrm-core.git CRM-16259, minor fix and added test for pending with partial payment ---------------------------------------- * CRM-16259: Create Payment API https://issues.civicrm.org/jira/browse/CRM-16259 --- diff --git a/api/v3/Payment.php b/api/v3/Payment.php index ade66f0f22..0884bb4d4f 100644 --- a/api/v3/Payment.php +++ b/api/v3/Payment.php @@ -136,7 +136,7 @@ function civicrm_api3_payment_create(&$params) { $contribution = civicrm_api3('Contribution', 'getsingle', array('id' => $params['contribution_id'])); $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); if ($contributionStatus[$contribution['contribution_status_id']] != 'Partially paid' - && !($contributionStatus[$contribution['contribution_status_id']] = 'Pending' && $contribution['is_pay_later'] == TRUE) + && !($contributionStatus[$contribution['contribution_status_id']] == 'Pending' && $contribution['is_pay_later'] == TRUE) ) { throw new API_Exception('Please select a contribution which has a partial or pending payment'); } @@ -155,10 +155,10 @@ function civicrm_api3_payment_create(&$params) { $fullyPaidPayLater = TRUE; } else { - civicrm_api3('Contribution', 'create', + civicrm_api3('Contribution', 'create', array( - 'id' => $contribution['id'], - 'contribution_status_id' => array_search('Partially paid', $contributionStatus), + 'id' => $contribution['id'], + 'contribution_status_id' => array_search('Partially paid', $contributionStatus), ) ); } diff --git a/tests/phpunit/api/v3/PaymentTest.php b/tests/phpunit/api/v3/PaymentTest.php index 672e54eab7..70ce0b5306 100644 --- a/tests/phpunit/api/v3/PaymentTest.php +++ b/tests/phpunit/api/v3/PaymentTest.php @@ -462,7 +462,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { ); $payment = $this->callAPISuccess('Payment', 'create', $params); $expectedResult = array( - $payment['id'] => array( + $payment['id'] => array( 'from_financial_account_id' => 7, 'to_financial_account_id' => 6, 'total_amount' => 100, @@ -494,4 +494,88 @@ class api_v3_PaymentTest extends CiviUnitTestCase { )); } + /** + * Test create payment api for paylater contribution with partial payment + */ + public function testCreatePaymentPayLaterPartialPayment() { + $this->createLoggedInUser(); + $contributionParams = array( + 'total_amount' => 100, + 'currency' => 'USD', + 'contact_id' => $this->_individualId, + 'financial_type_id' => 1, + 'contribution_status_id' => 2, + 'is_pay_later' => 1, + ); + $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams); + //Create partial payment + $params = array( + 'contribution_id' => $contribution['id'], + 'total_amount' => 60, + ); + $payment = $this->callAPISuccess('Payment', 'create', $params); + $expectedResult = array( + $payment['id'] => array( + 'total_amount' => 60, + 'status_id' => 1, + 'is_payment' => 1, + ), + ); + $this->checkPaymentResult($payment, $expectedResult); + // Check entity financial trxn created properly + $params = array( + 'entity_id' => $contribution['id'], + 'entity_table' => 'civicrm_contribution', + 'financial_trxn_id' => $payment['id'], + ); + $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params); + $this->assertEquals($eft['values'][$eft['id']]['amount'], 60); + $params = array( + 'entity_table' => 'civicrm_financial_item', + 'financial_trxn_id' => $payment['id'], + ); + $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params); + $this->assertEquals($eft['values'][$eft['id']]['amount'], 60); + $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'])); + $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Partially paid'); + $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00); + //Create full payment + $params = array( + 'contribution_id' => $contribution['id'], + 'total_amount' => 40, + ); + $payment = $this->callAPISuccess('Payment', 'create', $params); + $expectedResult = array( + $payment['id'] => array( + 'from_financial_account_id' => 7, + 'to_financial_account_id' => 6, + 'total_amount' => 40, + 'status_id' => 1, + 'is_payment' => 1, + ), + ); + $this->checkPaymentResult($payment, $expectedResult); + // Check entity financial trxn created properly + $params = array( + 'entity_id' => $contribution['id'], + 'entity_table' => 'civicrm_contribution', + 'financial_trxn_id' => $payment['id'], + ); + $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params); + $this->assertEquals($eft['values'][$eft['id']]['amount'], 40); + $params = array( + 'entity_table' => 'civicrm_financial_item', + 'financial_trxn_id' => $payment['id'], + ); + $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params); + $this->assertEquals($eft['values'][$eft['id']]['amount'], 40); + // Check contribution for completed status + $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'])); + $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed'); + $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00); + $this->callAPISuccess('Contribution', 'Delete', array( + 'id' => $contribution['id'], + )); + } + }