Revert "Schema - Fix boolean fields in various tables"
[civicrm-core.git] / CRM / Financial / Form / Payment.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Financial_Form_Payment extends CRM_Core_Form {
18
19 /**
20 * @var string
21 */
22 protected $currency;
23
24 public $_values = [];
25
26 /**
27 * @var array
28 */
29 public $_paymentProcessor;
30
31 /**
32 * @var bool
33 */
34 public $isBackOffice = FALSE;
35
36 /**
37 * @var string
38 */
39 public $_formName = '';
40
41 /**
42 * Set variables up before form is built.
43 */
44 public function preProcess() {
45 parent::preProcess();
46
47 $this->_formName = CRM_Utils_Request::retrieve('formName', 'String', $this);
48
49 $this->_values['custom_pre_id'] = CRM_Utils_Request::retrieve('pre_profile_id', 'Integer', $this);
50
51 $this->_paymentProcessorID = CRM_Utils_Request::retrieve('processor_id', 'Integer', CRM_Core_DAO::$_nullObject,
52 TRUE);
53 $this->currency = CRM_Utils_Request::retrieve('currency', 'String', CRM_Core_DAO::$_nullObject,
54 TRUE);
55
56 $this->paymentInstrumentID = CRM_Utils_Request::retrieve('payment_instrument_id', 'Integer');
57 $this->isBackOffice = CRM_Utils_Request::retrieve('is_back_office', 'Integer');
58
59 $this->assignBillingType();
60
61 $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($this->_paymentProcessorID);
62
63 CRM_Core_Payment_ProcessorForm::preProcess($this);
64
65 $this->assign('suppressForm', TRUE);
66 $this->controller->_generateQFKey = FALSE;
67 }
68
69 /**
70 * Get currency
71 *
72 * @param array $submittedValues
73 * Required for consistency with other form methods.
74 *
75 * @return string
76 */
77 public function getCurrency($submittedValues = []) {
78 return $this->currency;
79 }
80
81 /**
82 * Build quickForm.
83 */
84 public function buildQuickForm() {
85 CRM_Core_Payment_ProcessorForm::buildQuickForm($this);
86 }
87
88 /**
89 * Set default values for the form.
90 */
91 public function setDefaultValues() {
92 $contactID = $this->getContactID();
93 CRM_Core_Payment_Form::setDefaultValues($this, $contactID);
94 return $this->_defaults;
95 }
96
97 /**
98 * Add JS to show icons for the accepted credit cards.
99 *
100 * @param int $paymentProcessorID
101 * @param string $region
102 */
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();
107 }
108 $creditCardTypes = [];
109 foreach ($creditCards as $name => $label) {
110 $creditCardTypes[$name] = [
111 'label' => $label,
112 'name' => $name,
113 'css_key' => self::getCssLabelFriendlyName($name),
114 'pattern' => self::getCardPattern($name),
115 ];
116 }
117
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);
125 }
126
127 /**
128 * Get css friendly labels for credit cards.
129 *
130 * We add the icons based on these css names which are lower cased
131 * and only AlphaNumeric (+ _).
132 *
133 * @param string $key
134 *
135 * @return string
136 */
137 protected static function getCssLabelFriendlyName($key) {
138 $key = str_replace(' ', '', $key);
139 $key = preg_replace('/[^a-zA-Z0-9]/', '_', $key);
140 $key = strtolower($key);
141
142 return $key;
143 }
144
145 /**
146 * Get the pattern that can be used to determine the card type.
147 *
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.
150 *
151 * @param string $key
152 *
153 * Based on http://davidwalsh.name/validate-credit-cards
154 * See also https://en.wikipedia.org/wiki/Credit_card_numbers
155 *
156 * @return string
157 */
158 protected static function getCardPattern($key) {
159 $cardMappings = [
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})',
168 ];
169 return isset($cardMappings[strtolower($key)]) ? $cardMappings[strtolower($key)] : '';
170 }
171
172 }