Cleanup date handling on Payment.create
authoreileen <emcnaughton@wikimedia.org>
Wed, 30 Oct 2019 21:53:13 +0000 (10:53 +1300)
committereileen <emcnaughton@wikimedia.org>
Fri, 1 Nov 2019 03:14:38 +0000 (16:14 +1300)
Fix a bug identified by @kcristiano whereby adding a payment with the
additional payment form changes the contribution receive_date.

Also, add a default to Payment.create of 'now' for the
trxn_date and require it to be set. Use that value later on.

Some string fixes

CRM/Contribute/Form/AdditionalPayment.php
CRM/Financial/BAO/Payment.php
api/v3/Payment.php
tests/phpunit/CRM/Contribute/Form/AdditionalPaymentTest.php

index 3bfda8b13a5cba40398cdce1a5b54b6e0bf50680..dbb3136f3fff8ec07747a92730152ca4b25bcc3a 100644 (file)
@@ -411,14 +411,6 @@ class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_Abstract
       $config->defaultCurrency
     );
 
-    if (!empty($this->_params['trxn_date'])) {
-      $this->_params['receive_date'] = $this->_params['trxn_date'];
-    }
-
-    if (empty($this->_params['receive_date'])) {
-      $this->_params['receive_date'] = date('YmdHis');
-    }
-
     if (empty($this->_params['invoice_id'])) {
       $this->_params['invoiceID'] = md5(uniqid(rand(), TRUE));
     }
@@ -469,10 +461,6 @@ class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_Abstract
       $this->_params = array_merge($this->_params, $result);
     }
 
-    if (empty($this->_params['receive_date'])) {
-      $this->_params['receive_date'] = $now;
-    }
-
     $this->set('params', $this->_params);
 
     // set source if not set
index 15c9359b841a0666759bb3870c18e0ff7dfd5658..841827e8aa8abe97780c7629b7f36920809594b0 100644 (file)
@@ -59,7 +59,7 @@ class CRM_Financial_BAO_Payment {
     $isPaymentCompletesContribution = self::isPaymentCompletesContribution($params['contribution_id'], $params['total_amount']);
     $lineItems = self::getPayableLineItems($params);
 
-    $whiteList = ['check_number', 'payment_processor_id', 'fee_amount', 'total_amount', 'contribution_id', 'net_amount', 'card_type_id', 'pan_truncation', 'trxn_result_code', 'payment_instrument_id', 'trxn_id'];
+    $whiteList = ['check_number', 'payment_processor_id', 'fee_amount', 'total_amount', 'contribution_id', 'net_amount', 'card_type_id', 'pan_truncation', 'trxn_result_code', 'payment_instrument_id', 'trxn_id', 'trxn_date'];
     $paymentTrxnParams = array_intersect_key($params, array_fill_keys($whiteList, 1));
     $paymentTrxnParams['is_payment'] = 1;
     if (!empty($params['payment_processor'])) {
@@ -89,7 +89,6 @@ class CRM_Financial_BAO_Payment {
     if ($params['total_amount'] > 0) {
       $paymentTrxnParams['to_financial_account_id'] = CRM_Contribute_BAO_Contribution::getToFinancialAccount($contribution, $params);
       $paymentTrxnParams['from_financial_account_id'] = CRM_Financial_BAO_FinancialAccount::getFinancialAccountForFinancialTypeByRelationship($contribution['financial_type_id'], 'Accounts Receivable Account is');
-      $paymentTrxnParams['trxn_date'] = CRM_Utils_Array::value('trxn_date', $params, CRM_Utils_Array::value('contribution_receive_date', $params, date('YmdHis')));
       $paymentTrxnParams['currency'] = $contribution['currency'];
       $paymentTrxnParams['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_FinancialTrxn', 'status_id', 'Completed');
 
index 3dad955fbd219458fc132c40def66b6be5e0631f..47e883f7b4e9f24ed8f5602e8e16abf36ff0fba5 100644 (file)
@@ -201,8 +201,10 @@ function _civicrm_api3_payment_create_spec(&$params) {
       'api.aliases' => ['payment_id'],
     ],
     'trxn_date' => [
-      'title' => ts('Cancel Date'),
+      'title' => ts('Payment Date'),
       'type' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME,
+      'api.default' => 'now',
+      'api.required' => TRUE,
     ],
     'is_send_contribution_notification' => [
       'title' => ts('Send out notifications based on contribution status change?'),
@@ -321,22 +323,26 @@ function _civicrm_api3_payment_create_spec(&$params) {
 function _civicrm_api3_payment_get_spec(&$params) {
   $params = [
     'contribution_id' => [
-      'title' => 'Contribution ID',
+      'title' => ts('Contribution ID'),
       'type' => CRM_Utils_Type::T_INT,
     ],
     'entity_table' => [
-      'title' => 'Entity Table',
+      'title' => ts('Entity Table'),
       'api.default' => 'civicrm_contribution',
     ],
     'entity_id' => [
-      'title' => 'Entity ID',
+      'title' => ts('Entity ID'),
       'type' => CRM_Utils_Type::T_INT,
       'api.aliases' => ['contribution_id'],
     ],
     'trxn_id' => [
-      'title' => 'Transaction ID',
+      'title' => ts('Transaction ID'),
       'type' => CRM_Utils_Type::T_STRING,
     ],
+    'trxn_date' => [
+      'title' => ts('Payment Date'),
+      'type' => CRM_Utils_Type::T_TIMESTAMP,
+    ],
   ];
 }
 
index 7c3e9e40cdb856b6102189c5fe24664c07c59b2e..85101096a7fa7b974da6a11931a5ef9f03c16ec7 100644 (file)
@@ -296,6 +296,9 @@ class CRM_Contribute_Form_AdditionalPaymentTest extends CiviUnitTestCase {
     $this->submitPayment(70);
     $contribution = $this->callAPISuccessGetSingle('Contribution', ['id' => $this->_contributionId]);
     $this->assertEquals('Partially paid', $contribution['contribution_status']);
+    $this->assertEquals('2019-04-01 00:00:00', $contribution['receive_date']);
+    $payment = $this->callAPISuccessGetSingle('Payment', ['contribution_id' => $contribution['id']]);
+    $this->assertEquals('2017-04-11 13:05:11', $payment['trxn_date']);
 
     // pay additional amount
     $this->submitPayment(30);
@@ -430,8 +433,6 @@ class CRM_Contribute_Form_AdditionalPaymentTest extends CiviUnitTestCase {
       'total_amount' => $amount,
       'currency' => 'USD',
       'financial_type_id' => 1,
-      'receive_date' => '04/21/2015',
-      'receive_date_time' => '11:27PM',
       'trxn_date' => '2017-04-11 13:05:11',
       'payment_processor_id' => 0,
       'is_email_receipt' => $isEmailReceipt,
@@ -493,6 +494,7 @@ class CRM_Contribute_Form_AdditionalPaymentTest extends CiviUnitTestCase {
     $orderParams = array_merge($this->_params, [
       'contribution_status_id' => 'Pending',
       'is_pay_later' => 1,
+      'receive_date' => '2019-04-01',
     ]);
     $contribution = $this->callAPISuccess('Order', 'create', $orderParams);
     $contribution = $this->callAPISuccessGetSingle('Contribution', ['id' => $contribution['id']]);