CRM-16259, minor fix and added test for pending with partial payment
authorPradeep Nayak <pradpnayak@gmail.com>
Wed, 27 Jan 2016 23:42:31 +0000 (05:12 +0530)
committerPradeep Nayak <pradpnayak@gmail.com>
Wed, 17 Feb 2016 12:33:28 +0000 (18:03 +0530)
----------------------------------------
* CRM-16259: Create Payment API
  https://issues.civicrm.org/jira/browse/CRM-16259

api/v3/Payment.php
tests/phpunit/api/v3/PaymentTest.php

index ade66f0f22812472d5fe1184fb8d765bd50f2d25..0884bb4d4f27050857d1f350e92ceff737001fce 100644 (file)
@@ -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),
           )
         );
       }
index 672e54eab75b7face8e9362aff7a3290fb601200..70ce0b5306c1b4301fa688bad8359d8691ae7c9c 100644 (file)
@@ -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'],
+    ));
+  }
+
 }