dev/core#153: Pending Pay Later /w Custom payment method
authordeb.monish <monish.deb@jmaconsulting.biz>
Sat, 23 Jun 2018 15:41:09 +0000 (21:11 +0530)
committereileen <emcnaughton@wikimedia.org>
Thu, 15 Nov 2018 12:01:59 +0000 (01:01 +1300)
CRM/Contribute/BAO/Contribution.php
tests/phpunit/CRM/Event/BAO/AdditionalPaymentTest.php
tests/phpunit/CiviTest/CiviUnitTestCase.php

index da0d03a5ce32030f2a376e5cdf8f3612c598281d..1c6209e79fc1badbec9927aa57251524caa9c3ff 100644 (file)
@@ -3802,6 +3802,7 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac
     $params = array_merge($defaults, $params);
     $params['skipLineItem'] = TRUE;
     $trxnsData['trxn_date'] = !empty($trxnsData['trxn_date']) ? $trxnsData['trxn_date'] : date('YmdHis');
+    $params['payment_instrument_id'] = CRM_Utils_Array::value('payment_instrument_id', $trxnsData, CRM_Utils_Array::value('payment_instrument_id', $params));
     $arAccountId = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($contributionDAO->financial_type_id, 'Accounts Receivable Account is');
 
     $completedStatusId = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed');
@@ -3813,6 +3814,7 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac
       $trxnsData['net_amount'] = !empty($trxnsData['net_amount']) ? $trxnsData['net_amount'] : $trxnsData['total_amount'];
       $params['pan_truncation'] = CRM_Utils_Array::value('pan_truncation', $trxnsData);
       $params['card_type_id'] = CRM_Utils_Array::value('card_type_id', $trxnsData);
+      $params['check_number'] = CRM_Utils_Array::value('check_number', $trxnsData);
 
       // record the entry
       $financialTrxn = CRM_Contribute_BAO_Contribution::recordFinancialAccounts($params, $trxnsData);
index 54af4b5a6e6cb5f146171f5c4e4772733af533b6..85d8043d748670c269ad0df96d62d15cdd9b6ac5 100644 (file)
@@ -61,45 +61,53 @@ class CRM_Event_BAO_AdditionalPaymentTest extends CiviUnitTestCase {
    *
    * @param int $feeTotal
    * @param int $actualPaidAmt
+   * @param array $participantParams
+   * @param array $contributionParams
    *
    * @return array
    * @throws Exception
    */
-  protected function addParticipantWithPayment($feeTotal, $actualPaidAmt) {
+  protected function addParticipantWithPayment($feeTotal, $actualPaidAmt, $participantParams = [], $contributionParams = []) {
     $priceSetId = $this->eventPriceSetCreate($feeTotal);
     CRM_Price_BAO_PriceSet::addTo('civicrm_event', $this->_eventId, $priceSetId);
 
     // create participant record
     $eventId = $this->_eventId;
-    $participantParams = array(
-      'send_receipt' => 1,
-      'is_test' => 0,
-      'is_pay_later' => 0,
-      'event_id' => $eventId,
-      'register_date' => date('Y-m-d') . " 00:00:00",
-      'role_id' => 1,
-      'status_id' => 14,
-      'source' => 'Event_' . $eventId,
-      'contact_id' => $this->_contactId,
-      'note' => 'Note added for Event_' . $eventId,
-      'fee_level' => '\ 1Price_Field - 55\ 1',
+    $participantParams = array_merge(
+      [
+        'send_receipt' => 1,
+        'is_test' => 0,
+        'is_pay_later' => 0,
+        'event_id' => $eventId,
+        'register_date' => date('Y-m-d') . " 00:00:00",
+        'role_id' => 1,
+        'status_id' => 14,
+        'source' => 'Event_' . $eventId,
+        'contact_id' => $this->_contactId,
+        'note' => 'Note added for Event_' . $eventId,
+        'fee_level' => '\ 1Price_Field - 55\ 1',
+      ],
+      $participantParams
     );
     $participant = $this->callAPISuccess('participant', 'create', $participantParams);
     $this->callAPISuccessGetSingle('participant', array('id' => $participant['id']));
     // create participant contribution with partial payment
-    $contributionParams = array(
-      'total_amount' => $actualPaidAmt,
-      'source' => 'Fall Fundraiser Dinner: Offline registration',
-      'currency' => 'USD',
-      'receipt_date' => date('Y-m-d') . " 00:00:00",
-      'contact_id' => $this->_contactId,
-      'financial_type_id' => 4,
-      'payment_instrument_id' => 4,
-      'contribution_status_id' => 1,
-      'receive_date' => date('Y-m-d') . " 00:00:00",
-      'skipLineItem' => 1,
-      'partial_payment_total' => $feeTotal,
-      'partial_amount_to_pay' => $actualPaidAmt,
+    $contributionParams = array_merge(
+      [
+        'total_amount' => $actualPaidAmt,
+        'source' => 'Fall Fundraiser Dinner: Offline registration',
+        'currency' => 'USD',
+        'receipt_date' => date('Y-m-d') . " 00:00:00",
+        'contact_id' => $this->_contactId,
+        'financial_type_id' => 4,
+        'payment_instrument_id' => 4,
+        'contribution_status_id' => 1,
+        'receive_date' => date('Y-m-d') . " 00:00:00",
+        'skipLineItem' => 1,
+        'partial_payment_total' => $feeTotal,
+        'partial_amount_to_pay' => $actualPaidAmt,
+      ],
+      $contributionParams
     );
 
     $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
@@ -136,6 +144,61 @@ class CRM_Event_BAO_AdditionalPaymentTest extends CiviUnitTestCase {
     );
   }
 
+  /**
+   * See https://lab.civicrm.org/dev/core/issues/153
+   */
+  public function testPaymentWithCustomPaymentInstrument() {
+    $feeAmt = 100;
+    $amtPaid = 0;
+
+    // Create undetermined Payment Instrument
+    $paymentInstrumentID = $this->createPaymentInstrument(['label' => 'Undetermined'], 'Accounts Receivable');
+
+    // record pending payment for an event
+    $result = $this->addParticipantWithPayment(
+      $feeAmt, $amtPaid,
+      ['is_pay_later' => 1],
+      [
+        'total_amount' => 100,
+        'payment_instrument_id' => $paymentInstrumentID,
+        'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'),
+      ]
+    );
+    $contributionID = $result['contribution']['id'];
+    extract($result);
+
+    // check payment info
+    $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($participant['id'], 'event');
+    $this->assertEquals(round($paymentInfo['total']), $feeAmt, 'Total amount recorded is not proper');
+    $this->assertEquals(round($paymentInfo['paid']), $amtPaid, 'Amount paid is not proper');
+    $this->assertEquals(round($paymentInfo['balance']), $feeAmt, 'Balance amount is not proper');
+    $this->assertEquals($paymentInfo['contribution_status'], 'Pending', 'Contribution status is not proper');
+
+    // make additional payment via 'Record Payment' form
+    $form = new CRM_Contribute_Form_AdditionalPayment();
+    $submitParams = array(
+      'contact_id' => $result['contribution']['contact_id'],
+      'contribution_id' => $contributionID,
+      'total_amount' => 100,
+      'currency' => 'USD',
+      'trxn_date' => '2017-04-11 13:05:11',
+      'payment_processor_id' => 0,
+      'payment_instrument_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check'),
+      'check_number' => '#123',
+    );
+    $form->cid = $result['contribution']['contact_id'];
+    $form->testSubmit($submitParams);
+
+    // check payment info again and see if the payment is completed
+    $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($participant['id'], 'event');
+    $this->assertEquals(round($paymentInfo['total']), $feeAmt, 'Total amount recorded is not proper');
+    $this->assertEquals(round($paymentInfo['paid']), $feeAmt, 'Amount paid is not proper');
+    $this->assertEquals(round($paymentInfo['balance']), 0, 'Balance amount is not proper');
+    $this->assertEquals($paymentInfo['contribution_status'], 'Completed', 'Contribution status is not proper');
+
+    $this->callAPISuccess('OptionValue', 'delete', ['id' => $paymentInstrumentID]);
+  }
+
   /**
    * CRM-13964
    */
index 986a89169ffb8501be87882874202c92d7e24852..ae350e1f5ce96927d938d9831034cbed2dc8bb5a 100644 (file)
@@ -3384,6 +3384,37 @@ AND    ( TABLE_NAME LIKE 'civicrm_value_%' )
     return $this->callAPISuccess('FinancialType', 'create', $params);
   }
 
+  /**
+   * Create Payment Instrument.
+   *
+   * @param array $params
+   * @param string $financialAccountName
+   *
+   * @return int
+   */
+  protected function createPaymentInstrument($params = array(), $financialAccountName = 'Donation') {
+    $params = array_merge(array(
+      'label' => 'Payment Instrument -' . substr(sha1(rand()), 0, 7),
+      'option_group_id' => 'payment_instrument',
+      'is_active' => 1,
+      ),
+      $params
+    );
+    $newPaymentInstrument = $this->callAPISuccess('OptionValue', 'create', $params);
+
+    $relationTypeID = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
+
+    $financialAccountParams = [
+      'entity_table' => 'civicrm_option_value',
+      'entity_id' => key($newPaymentInstrument),
+      'account_relationship' => $relationTypeID,
+      'financial_account_id' => $this->callAPISuccess('FinancialAccount', 'getValue', ['name' => $financialAccountName, 'return' => 'id']),
+    ];
+    CRM_Financial_BAO_FinancialTypeAccount::add($financialAccountParams);
+
+    return CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', $params['label']);
+  }
+
   /**
    * Enable Tax and Invoicing
    */