Commit | Line | Data |
---|---|---|
aaff4c69 EM |
1 | <?php |
2 | /* | |
4f6cdd27 | 3 | +--------------------------------------------------------------------+ |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
4f6cdd27 | 5 | | | |
bc77d7c0 TO |
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 | | |
4f6cdd27 | 9 | +--------------------------------------------------------------------+ |
aaff4c69 EM |
10 | */ |
11 | ||
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
aaff4c69 EM |
16 | */ |
17 | class CRM_Financial_Form_Payment extends CRM_Core_Form { | |
1d1fee72 | 18 | |
19 | /** | |
a3d83c42 | 20 | * @var string |
1d1fee72 | 21 | */ |
f61437d3 | 22 | protected $currency; |
a55e39e9 | 23 | |
be2fb01f | 24 | public $_values = []; |
1d1fee72 | 25 | |
26 | /** | |
27 | * @var array | |
28 | */ | |
29 | public $_paymentProcessor; | |
18135422 | 30 | |
31 | /** | |
32 | * @var bool | |
33 | */ | |
34 | public $isBackOffice = FALSE; | |
35 | ||
fb5e89bc | 36 | /** |
9f266042 | 37 | * @var string |
fb5e89bc SP |
38 | */ |
39 | public $_formName = ''; | |
40 | ||
aaff4c69 EM |
41 | /** |
42 | * Set variables up before form is built. | |
aaff4c69 EM |
43 | */ |
44 | public function preProcess() { | |
45 | parent::preProcess(); | |
423b9af4 | 46 | |
fb5e89bc SP |
47 | $this->_formName = CRM_Utils_Request::retrieve('formName', 'String', $this); |
48 | ||
d38c288e | 49 | $this->_values['custom_pre_id'] = CRM_Utils_Request::retrieve('pre_profile_id', 'Integer', $this); |
423b9af4 | 50 | |
aaff4c69 EM |
51 | $this->_paymentProcessorID = CRM_Utils_Request::retrieve('processor_id', 'Integer', CRM_Core_DAO::$_nullObject, |
52 | TRUE); | |
f61437d3 K |
53 | $this->currency = CRM_Utils_Request::retrieve('currency', 'String', CRM_Core_DAO::$_nullObject, |
54 | TRUE); | |
aaff4c69 | 55 | |
18135422 | 56 | $this->paymentInstrumentID = CRM_Utils_Request::retrieve('payment_instrument_id', 'Integer'); |
57 | $this->isBackOffice = CRM_Utils_Request::retrieve('is_back_office', 'Integer'); | |
58 | ||
aaff4c69 EM |
59 | $this->assignBillingType(); |
60 | ||
a9768188 | 61 | $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($this->_paymentProcessorID); |
1d1fee72 | 62 | |
aaff4c69 EM |
63 | CRM_Core_Payment_ProcessorForm::preProcess($this); |
64 | ||
d87cd28c | 65 | $this->assign('suppressForm', TRUE); |
a38068fe | 66 | $this->controller->_generateQFKey = FALSE; |
bc44463a CW |
67 | } |
68 | ||
f61437d3 | 69 | /** |
fdcab137 | 70 | * Get currency |
71 | * | |
72 | * @param array $submittedValues | |
73 | * Required for consistency with other form methods. | |
74 | * | |
f61437d3 K |
75 | * @return string |
76 | */ | |
be2fb01f | 77 | public function getCurrency($submittedValues = []) { |
f61437d3 K |
78 | return $this->currency; |
79 | } | |
80 | ||
1d1fee72 | 81 | /** |
82 | * Build quickForm. | |
83 | */ | |
bc44463a CW |
84 | public function buildQuickForm() { |
85 | CRM_Core_Payment_ProcessorForm::buildQuickForm($this); | |
86 | } | |
87 | ||
70d1766d | 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 | ||
bc44463a | 97 | /** |
1d1fee72 | 98 | * Add JS to show icons for the accepted credit cards. |
bf48aa29 | 99 | * |
100 | * @param int $paymentProcessorID | |
9b2e3ee6 | 101 | * @param string $region |
bc44463a | 102 | */ |
9b2e3ee6 | 103 | public static function addCreditCardJs($paymentProcessorID = NULL, $region = 'billing-block') { |
cb5962bd | 104 | $creditCards = CRM_Financial_BAO_PaymentProcessor::getCreditCards($paymentProcessorID); |
ce895597 | 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 | ||
aaff4c69 | 118 | CRM_Core_Resources::singleton() |
7b9cb12a | 119 | // CRM-20516: add BillingBlock script on billing-block region |
120 | // to support this feature in payment form snippet too. | |
9b2e3ee6 | 121 | ->addScriptFile('civicrm', 'templates/CRM/Core/BillingBlock.js', 10, $region, FALSE) |
aaff4c69 EM |
122 | // workaround for CRM-13634 |
123 | // ->addSetting(array('config' => array('creditCardTypes' => $creditCardTypes))); | |
9b2e3ee6 | 124 | ->addScript('CRM.config.creditCardTypes = ' . json_encode($creditCardTypes) . ';', '-9999', $region); |
aaff4c69 EM |
125 | } |
126 | ||
ce895597 | 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 | * | |
fa3fdebc | 133 | * @param string $key |
ce895597 | 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 | * | |
fa3fdebc | 151 | * @param string $key |
ce895597 | 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 | ||
aaff4c69 | 172 | } |