Skip to content

Commit

Permalink
Add support for hidden fields in general and 'token' specifically on …
Browse files Browse the repository at this point in the history
…payment form.

It is common enough for payment processors to store a hidden token on the form that we should specifically support it. In general
the processor can add other fields of type hidden and they should show up.
  • Loading branch information
eileenmcnaughton committed Jun 18, 2018
1 parent 92cc044 commit c42f1a1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 32 deletions.
6 changes: 1 addition & 5 deletions CRM/Contribute/Form/ContributionBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -614,11 +614,7 @@ public function assignToTemplate() {
$this->assign($paymentField, $this->_params[$paymentField]);
}
}
$paymentFieldsetLabel = ts('%1 Information', array($paymentProcessorObject->getPaymentTypeLabel()));
if (empty($paymentFields)) {
$paymentFieldsetLabel = '';
}
$this->assign('paymentFieldsetLabel', $paymentFieldsetLabel);
$this->assign('paymentFieldsetLabel', CRM_Core_Payment_Form::getPaymentLabel($paymentProcessorObject));
$this->assign('paymentFields', $paymentFields);

}
Expand Down
17 changes: 7 additions & 10 deletions CRM/Core/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,6 @@ public function getPaymentFormFieldsMetadata() {
'htmlType' => 'text',
'name' => 'credit_card_number',
'title' => ts('Card Number'),
'cc_field' => TRUE,
'attributes' => array(
'size' => 20,
'maxlength' => 20,
Expand All @@ -695,7 +694,6 @@ public function getPaymentFormFieldsMetadata() {
'htmlType' => 'text',
'name' => 'cvv2',
'title' => ts('Security Code'),
'cc_field' => TRUE,
'attributes' => array(
'size' => 5,
'maxlength' => 10,
Expand All @@ -714,7 +712,6 @@ public function getPaymentFormFieldsMetadata() {
'htmlType' => 'date',
'name' => 'credit_card_exp_date',
'title' => ts('Expiration Date'),
'cc_field' => TRUE,
'attributes' => CRM_Core_SelectValues::date('creditCard'),
'is_required' => TRUE,
'rules' => array(
Expand All @@ -729,15 +726,13 @@ public function getPaymentFormFieldsMetadata() {
'htmlType' => 'select',
'name' => 'credit_card_type',
'title' => ts('Card Type'),
'cc_field' => TRUE,
'attributes' => $creditCardType,
'is_required' => FALSE,
),
'account_holder' => array(
'htmlType' => 'text',
'name' => 'account_holder',
'title' => ts('Account Holder'),
'cc_field' => TRUE,
'attributes' => array(
'size' => 20,
'maxlength' => 34,
Expand All @@ -750,7 +745,6 @@ public function getPaymentFormFieldsMetadata() {
'htmlType' => 'text',
'name' => 'bank_account_number',
'title' => ts('Bank Account Number'),
'cc_field' => TRUE,
'attributes' => array(
'size' => 20,
'maxlength' => 34,
Expand All @@ -770,7 +764,6 @@ public function getPaymentFormFieldsMetadata() {
'htmlType' => 'text',
'name' => 'bank_identification_number',
'title' => ts('Bank Identification Number'),
'cc_field' => TRUE,
'attributes' => array(
'size' => 20,
'maxlength' => 11,
Expand All @@ -789,7 +782,6 @@ public function getPaymentFormFieldsMetadata() {
'htmlType' => 'text',
'name' => 'bank_name',
'title' => ts('Bank Name'),
'cc_field' => TRUE,
'attributes' => array(
'size' => 20,
'maxlength' => 64,
Expand All @@ -803,15 +795,13 @@ public function getPaymentFormFieldsMetadata() {
'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,
Expand All @@ -826,6 +816,13 @@ public function getPaymentFormFieldsMetadata() {
),
),
),
'payment_token' => array(
'htmlType' => 'hidden',
'name' => 'payment_token',
'title' => ts('Authorization token'),
'is_required' => FALSE,
'attributes' => ['size' => 10, 'autocomplete' => 'off'],
),
);
}

Expand Down
50 changes: 34 additions & 16 deletions CRM/Core/Payment/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ static public function setPaymentFieldsByProcessor(&$form, $processor, $billing_
$processor['object']->setPaymentInstrumentID($paymentInstrumentID);
$paymentTypeName = self::getPaymentTypeName($processor);
$form->assign('paymentTypeName', $paymentTypeName);
$paymentTypeLabel = self::getPaymentTypeLabel($processor);
$form->assign('paymentTypeLabel', $paymentTypeLabel);
$form->assign('paymentTypeLabel', self::getPaymentLabel($processor['object']));
$form->assign('isBackOffice', $isBackOffice);
$form->_paymentFields = $form->billingFieldSets[$paymentTypeName]['fields'] = self::getPaymentFieldMetadata($processor);
$form->_paymentFields = array_merge($form->_paymentFields, self::getBillingAddressMetadata($processor, $form->_bltID));
Expand Down Expand Up @@ -116,24 +115,22 @@ static protected function setBillingAddressFields(&$form, $processor) {
protected static function addCommonFields(&$form, $paymentFields) {
$requiredPaymentFields = array();
foreach ($paymentFields as $name => $field) {
// @todo - remove the cc_field check - no longer useful.
if (!empty($field['cc_field'])) {
if ($field['htmlType'] == 'chainSelect') {
$form->addChainSelect($field['name'], array('required' => FALSE));
}
else {
$form->add($field['htmlType'],
$field['name'],
$field['title'],
$field['attributes'],
FALSE
);
}
if ($field['htmlType'] == 'chainSelect') {
$form->addChainSelect($field['name'], array('required' => FALSE));
}
else {
$form->add($field['htmlType'],
$field['name'],
$field['title'],
$field['attributes'],
FALSE
);
}
// This will cause the fields to be marked as required - but it is up to the payment processor to
// validate it.
$requiredPaymentFields[$field['name']] = $field['is_required'];
}

$form->assign('requiredPaymentFields', $requiredPaymentFields);
}

Expand Down Expand Up @@ -207,7 +204,7 @@ public static function getPaymentTypeName($paymentProcessor) {
* @return string
*/
public static function getPaymentTypeLabel($paymentProcessor) {
return ts(($paymentProcessor['object']->getPaymentTypeLabel()) . ' Information');
return ts('%1 Information', [$paymentProcessor->getPaymentTypeLabel()]);
}

/**
Expand Down Expand Up @@ -426,4 +423,25 @@ public static function getCreditCardExpirationYear($src) {
return CRM_Utils_Array::value('Y', $src['credit_card_exp_date']);
}

/**
* Get the label for the processor.
*
* We do not use a label if there are no enterable fields.
*
* @param \CRM_Core_Payment $processor
*
* @return string
*/
public static function getPaymentLabel($processor) {
$isVisible = FALSE;
$paymentTypeLabel = self::getPaymentTypeLabel($processor);
foreach (self::getPaymentFieldMetadata(['object' => $processor]) as $paymentField) {
if ($paymentField['htmlType'] !== 'hidden') {
$isVisible = TRUE;
}
}
return $isVisible ? $paymentTypeLabel : '';

}

}
2 changes: 1 addition & 1 deletion templates/CRM/Core/BillingBlock.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<div class="label">{$form.$paymentField.label}
{if $requiredPaymentFields.$name}<span class="crm-marker" title="{ts}This field is required.{/ts}">*</span>{/if}
</div>
<div class="content">{$form.$paymentField.html}
<div class="content">{if $form.$paymentField.html}{$form.$paymentField.html}{else}<input id="{$paymentField}" name="{$paymentField}" type="hidden" />{/if}
{if $paymentField == 'cvv2'}{* @todo move to form assignment*}
<span class="cvv2-icon" title="{ts}Usually the last 3-4 digits in the signature area on the back of the card.{/ts}"> </span>
{/if}
Expand Down

0 comments on commit c42f1a1

Please sign in to comment.