CRM-20264, Added Card type and Pan Truncation form fields on Payment form
authoreileen <emcnaughton@wikimedia.org>
Thu, 13 Apr 2017 09:43:03 +0000 (21:43 +1200)
committereileen <emcnaughton@wikimedia.org>
Wed, 19 Apr 2017 07:03:54 +0000 (19:03 +1200)
I deliberately excluded card_type because I think we need to review the appropriateness of
the field first. My issue is that the wrangling wound up really wierd & it was apparent the issue
is that card_type is 'Visa' whereas card_type_id is 1 - we should either rename the field
or store the word.

CRM/Contribute/BAO/Contribution.php
CRM/Contribute/Form/Contribution.php
CRM/Core/Payment.php
CRM/Core/Payment/Manual.php
templates/CRM/Contribute/Form/AdditionalPayment.tpl
templates/CRM/Contribute/Form/Contribution.tpl
templates/CRM/common/paymentBlock.tpl

index 16169a4fa82ad3ff65907b69af0520306f9a520d..23b2f9d317f999e0702f551a30decfebe638b533 100644 (file)
@@ -3381,6 +3381,7 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac
         // records finanical trxn and entity financial trxn
         // also make it available as return value
         self::recordAlwaysAccountsReceivable($trxnParams, $params);
+        $trxnParams['pan_truncation'] = CRM_Utils_Array::value('pan_truncation', $params);
         $return = $financialTxn = CRM_Core_BAO_FinancialTrxn::create($trxnParams);
         $params['entity_id'] = $financialTxn->id;
         if (empty($params['partial_payment_total']) && empty($params['partial_amount_pay'])) {
@@ -3803,6 +3804,7 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac
       $params['partial_payment_total'] = $contributionDAO->total_amount;
       $params['partial_amount_pay'] = $trxnsData['total_amount'];
       $trxnsData['net_amount'] = !empty($trxnsData['net_amount']) ? $trxnsData['net_amount'] : $trxnsData['total_amount'];
+      $params['pan_truncation'] = CRM_Utils_Array::value('pan_truncation', $trxnsData);
 
       // record the entry
       $financialTrxn = CRM_Contribute_BAO_Contribution::recordFinancialAccounts($params, $trxnsData);
index 95c8e4cc910e155ee164d65ae0a7ea5a27a1f5bc..e693a3797d2368fbde39c23e4f00119010646b0b 100644 (file)
@@ -384,7 +384,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
     }
 
     if (empty($defaults['payment_instrument_id'])) {
-      $defaults['payment_instrument_id'] = key(CRM_Core_OptionGroup::values('payment_instrument', FALSE, FALSE, FALSE, 'AND is_default = 1'));
+      $defaults['payment_instrument_id'] = $this->getDefaultPaymentInstrumentId();
     }
 
     if (!empty($defaults['is_test'])) {
@@ -561,26 +561,24 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
       $paneNames[ts('Premium Information')] = 'Premium';
     }
 
-    if ($this->_mode) {
-      if (CRM_Core_Payment_Form::buildPaymentForm($this, $this->_paymentProcessor, FALSE, TRUE) == TRUE) {
-        if (!empty($this->_recurPaymentProcessors)) {
-          $buildRecurBlock = TRUE;
-          if ($this->_ppID) {
-            // ppID denotes a pledge payment.
-            foreach ($this->_paymentProcessors as $processor) {
-              if (!empty($processor['is_recur']) && !empty($processor['object']) && $processor['object']->supports('recurContributionsForPledges')) {
-                $buildRecurBlock = TRUE;
-                break;
-              }
-              $buildRecurBlock = FALSE;
+    if (CRM_Core_Payment_Form::buildPaymentForm($this, $this->_paymentProcessor, FALSE, TRUE, $this->getDefaultPaymentInstrumentId()) == TRUE) {
+      if (!empty($this->_recurPaymentProcessors)) {
+        $buildRecurBlock = TRUE;
+        if ($this->_ppID) {
+          // ppID denotes a pledge payment.
+          foreach ($this->_paymentProcessors as $processor) {
+            if (!empty($processor['is_recur']) && !empty($processor['object']) && $processor['object']->supports('recurContributionsForPledges')) {
+              $buildRecurBlock = TRUE;
+              break;
             }
+            $buildRecurBlock = FALSE;
           }
-          if ($buildRecurBlock) {
-            CRM_Contribute_Form_Contribution_Main::buildRecur($this);
-            $this->setDefaults(array('is_recur' => 0));
-            $this->assign('buildRecurBlock', TRUE);
-            $recurJs = array('onChange' => "buildRecurBlock( this.value ); return false;");
-          }
+        }
+        if ($buildRecurBlock) {
+          CRM_Contribute_Form_Contribution_Main::buildRecur($this);
+          $this->setDefaults(array('is_recur' => 0));
+          $this->assign('buildRecurBlock', TRUE);
+          $recurJs = array('onChange' => "buildRecurBlock( this.value ); return false;");
         }
       }
     }
@@ -1901,4 +1899,17 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
     return 0;
   }
 
+  /**
+   * Get the default payment instrument id.
+   *
+   * @return int
+   */
+  protected function getDefaultPaymentInstrumentId() {
+    $paymentInstrumentID = CRM_Utils_Request::retrieve('payment_instrument_id', 'Integer');
+    if ($paymentInstrumentID) {
+      return $paymentInstrumentID;
+    }
+    return key(CRM_Core_OptionGroup::values('payment_instrument', FALSE, FALSE, FALSE, 'AND is_default = 1'));
+  }
+
 }
index 3138d1f95b1aadbde27dbb4340fcf63eba9d4f72..c88aad8f564cce29ecc4439b6cf9672af1a1bf21 100644 (file)
@@ -774,6 +774,26 @@ abstract class CRM_Core_Payment {
         'is_required' => TRUE,
 
       ),
+      'check_number' => array(
+        'htmlType' => 'text',
+        'name' => 'check_number',
+        'title' => ts('Check Number'),
+        'is_required' => FALSE,
+        'cc_field' => TRUE,
+        'attributes' => NULL,
+      ),
+      'pan_truncation' => array(
+        'htmlType' => 'text',
+        'name' => 'pan_truncation',
+        'title' => ts('Last 4 digits of the card'),
+        'is_required' => FALSE,
+        'cc_field' => TRUE,
+        'attributes' => array(
+          'size' => 4,
+          'maxlength' => 4,
+          'autocomplete' => 'off',
+        ),
+      ),
     );
   }
 
index 55bae03df26b461a16284010b8b96b2bcd836a3c..f43fca15f1564273bc5ff8c06f4e0915d2be79f1 100644 (file)
@@ -84,8 +84,28 @@ class CRM_Core_Payment_Manual extends CRM_Core_Payment {
    * @return array
    */
   public function getPaymentFormFields() {
+    if (!$this->isBackOffice()) {
+      return array();
+    }
+
+    $paymentInstrument = CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', $this->getPaymentInstrumentID());
+    if ($paymentInstrument === 'Credit Card') {
+      // @todo - return credit_card_type field once the underlying code works to extract this field.
+      // Note the problem is this should be stored in civicrm_financial_trxn.credit_card_type.
+      // However there is an ambiguity as that field is an integer & should hence be called
+      // credit_card_type_id, or it should store 'visa' It probably makes sense to fix that before going much
+      // further as the code I've seen makes it clear that it will require work arounds.
+      return array('pan_truncation');
+    }
+    elseif ($paymentInstrument === 'Check') {
+      // Really we should render check_number here, but we need to review how we edit
+      // check_numebr since we expose it as editable on the contribution form,
+      // even though it should only be editable from a transation specific form.
+      return array();
+    }
     return array();
   }
+
   /**
    * Process payment.
    *
@@ -161,7 +181,7 @@ class CRM_Core_Payment_Manual extends CRM_Core_Payment {
    * @return string
    */
   public function getPaymentTypeLabel() {
-    return '';
+    return 'Payment';
   }
 
   /**
index 84ecc66ae1e76a07b0c08b9158b7fea7e1f80bbe..9a18a9c9e187ba98b05b8f8473e4abca291f6b12 100644 (file)
@@ -99,7 +99,9 @@
       </td>
     </tr>
    </table>
+
     <div class="crm-accordion-wrapper crm-accordion_title-accordion crm-accordion-processed" id="paymentDetails_Information">
+      {if !$contributionMode}
       <div class="crm-accordion-header">
         {if $paymentType EQ 'refund'}{ts}Refund Details{/ts}{else}{ts}Payment Details{/ts}{/if}
       </div>
             <span class="description">{ts}Net value of the payment (Total Amount minus Fee).{/ts}</span></td></tr>
         </table>
       </div>
+      {/if}
       {include file='CRM/Core/BillingBlockWrapper.tpl'}
     </div>
 
index 1b4a2f35ca976b011a7beb14a688bc7bef7a3c74..3191ccf6e9a6eee2e2343f3a7fdb4e51e6d6eb98 100644 (file)
   </table>
 
   {if !$contributionMode}
-    <div class="crm-accordion-wrapper crm-accordion_title-accordion crm-accordion-processed" id="paymentDetails_Information">
-      <div class="crm-accordion-header">
-        {ts}Payment Details{/ts}
-      </div>
-      <div class="crm-accordion-body">
         <table class="form-layout-compressed" >
           <tr class="crm-contribution-form-block-receive_date">
             <td class="label">{$form.receive_date.label}</td>
             <td>{$form.from_email_address.html}</td>
           </tr>
         </table>
-      </div>
-    </div>
   {/if}
 
   {include file='CRM/Core/BillingBlockWrapper.tpl'}
index c2e74f96cbbcf61abb07876fee492c0a29da9c9b..acbafb5ca3dcc2361fafa39cd87df6797b16b1cc 100644 (file)
         buildPaymentBlock($(this).val());
     });
 
+    $('#payment_instrument_id').on('change.paymentBlock', function() {
+      buildPaymentBlock(0);
+    });
+
     $('#billing-payment-block').on('crmLoad', function() {
       $('.crm-submit-buttons input').prop('disabled', false);
     })