3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Financial_Form_Payment
extends CRM_Core_Form
{
29 public $_paymentProcessor;
34 public $isBackOffice = FALSE;
39 public $_formName = '';
42 * Set variables up before form is built.
44 public function preProcess() {
47 $this->_formName
= CRM_Utils_Request
::retrieve('formName', 'String', $this);
49 $this->_values
['custom_pre_id'] = CRM_Utils_Request
::retrieve('pre_profile_id', 'Integer', $this);
51 $this->_paymentProcessorID
= CRM_Utils_Request
::retrieve('processor_id', 'Integer', CRM_Core_DAO
::$_nullObject,
53 $this->currency
= CRM_Utils_Request
::retrieve('currency', 'String', CRM_Core_DAO
::$_nullObject,
56 $this->paymentInstrumentID
= CRM_Utils_Request
::retrieve('payment_instrument_id', 'Integer');
57 $this->isBackOffice
= CRM_Utils_Request
::retrieve('is_back_office', 'Integer');
59 $this->assignBillingType();
61 $this->_paymentProcessor
= CRM_Financial_BAO_PaymentProcessor
::getPayment($this->_paymentProcessorID
);
63 CRM_Core_Payment_ProcessorForm
::preProcess($this);
65 $this->assign('suppressForm', TRUE);
66 $this->controller
->_generateQFKey
= FALSE;
72 * @param array $submittedValues
73 * Required for consistency with other form methods.
77 public function getCurrency($submittedValues = []) {
78 return $this->currency
;
84 public function buildQuickForm() {
85 CRM_Core_Payment_ProcessorForm
::buildQuickForm($this);
89 * Set default values for the form.
91 public function setDefaultValues() {
92 $contactID = $this->getContactID();
93 CRM_Core_Payment_Form
::setDefaultValues($this, $contactID);
94 return $this->_defaults
;
98 * Add JS to show icons for the accepted credit cards.
100 * @param int $paymentProcessorID
101 * @param string $region
103 public static function addCreditCardJs($paymentProcessorID = NULL, $region = 'billing-block') {
104 $creditCards = CRM_Financial_BAO_PaymentProcessor
::getCreditCards($paymentProcessorID);
105 if (empty($creditCards)) {
106 $creditCards = CRM_Contribute_PseudoConstant
::creditCard();
108 $creditCardTypes = [];
109 foreach ($creditCards as $name => $label) {
110 $creditCardTypes[$name] = [
113 'css_key' => self
::getCssLabelFriendlyName($name),
114 'pattern' => self
::getCardPattern($name),
118 CRM_Core_Resources
::singleton()
119 // CRM-20516: add BillingBlock script on billing-block region
120 // to support this feature in payment form snippet too.
121 ->addScriptFile('civicrm', 'templates/CRM/Core/BillingBlock.js', 10, $region, FALSE)
122 // workaround for CRM-13634
123 // ->addSetting(array('config' => array('creditCardTypes' => $creditCardTypes)));
124 ->addScript('CRM.config.creditCardTypes = ' . json_encode($creditCardTypes) . ';', '-9999', $region);
128 * Get css friendly labels for credit cards.
130 * We add the icons based on these css names which are lower cased
131 * and only AlphaNumeric (+ _).
137 protected static function getCssLabelFriendlyName($key) {
138 $key = str_replace(' ', '', $key);
139 $key = preg_replace('/[^a-zA-Z0-9]/', '_', $key);
140 $key = strtolower($key);
146 * Get the pattern that can be used to determine the card type.
148 * We do a strotolower comparison as we don't know what case people might have if they
149 * are using a non-std one like dinersclub.
153 * Based on http://davidwalsh.name/validate-credit-cards
154 * See also https://en.wikipedia.org/wiki/Credit_card_numbers
158 protected static function getCardPattern($key) {
160 'mastercard' => '(5[1-5][0-9]{2}|2[3-6][0-9]{2}|22[3-9][0-9]|222[1-9]|27[0-1][0-9]|2720)[0-9]{12}',
161 'visa' => '4(?:[0-9]{12}|[0-9]{15})',
162 'amex' => '3[47][0-9]{13}',
163 'dinersclub' => '3(?:0[0-5][0-9]{11}|[68][0-9]{12})',
164 'carteblanche' => '3(?:0[0-5][0-9]{11}|[68][0-9]{12})',
165 'discover' => '6011[0-9]{12}',
166 'jcb' => '(?:3[0-9]{15}|(2131|1800)[0-9]{11})',
167 'unionpay' => '62(?:[0-9]{14}|[0-9]{17})',
169 return isset($cardMappings[strtolower($key)]) ?
$cardMappings[strtolower($key)] : '';