-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form.php
443 lines (399 loc) · 19.7 KB
/
Form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.1 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2011 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2011
* $Id$
*
*/
class CRM_Core_Payment_Form {
/**
* create all common fields needed for a credit card or direct debit transaction
*
* @return void
* @access protected
*/
protected function _setPaymentFields( &$form) {
$bltID = $form->_bltID;
$form->_fields['billing_first_name'] =
array( 'htmlType' => 'text',
'name' => 'billing_first_name',
'title' => ts('Billing First Name'),
'cc_field' => true,
'attributes' => array( 'size' => 30, 'maxlength' => 60, 'autocomplete' => 'off' ),
'is_required'=> true
);
$form->_fields['billing_middle_name'] =
array( 'htmlType' => 'text',
'name' => 'billing_middle_name',
'title' => ts('Billing Middle Name'),
'cc_field' => true,
'attributes' => array( 'size' => 30, 'maxlength' => 60, 'autocomplete' => 'off' ),
'is_required'=> false
);
$form->_fields['billing_last_name'] =
array( 'htmlType' => 'text',
'name' => 'billing_last_name',
'title' => ts('Billing Last Name'),
'cc_field' => true,
'attributes' => array( 'size' => 30, 'maxlength' => 60, 'autocomplete' => 'off' ),
'is_required'=> true
);
$form->_fields["billing_street_address-{$bltID}"] =
array( 'htmlType' => 'text',
'name' => "billing_street_address-{$bltID}",
'title' => ts('Street Address'),
'cc_field' => true,
'attributes' => array( 'size' => 30, 'maxlength' => 60, 'autocomplete' => 'off' ),
'is_required'=> true
);
$form->_fields["billing_city-{$bltID}"] =
array( 'htmlType' => 'text',
'name' => "billing_city-{$bltID}",
'title' => ts('City'),
'cc_field' => true,
'attributes' => array( 'size' => 30, 'maxlength' => 60, 'autocomplete' => 'off' ),
'is_required'=> true
);
$form->_fields["billing_state_province_id-{$bltID}"] =
array( 'htmlType' => 'select',
'name' => "billing_state_province_id-{$bltID}",
'title' => ts('State / Province'),
'cc_field' => true,
'attributes' => array( '' => ts( '- select -' ) ) +
CRM_Core_PseudoConstant::stateProvince( ),
'is_required'=> self::checkRequiredStateProvince( $form )
);
$form->_fields["billing_postal_code-{$bltID}"] =
array( 'htmlType' => 'text',
'name' => "billing_postal_code-{$bltID}",
'title' => ts('Postal Code'),
'cc_field' => true,
'attributes' => array( 'size' => 30, 'maxlength' => 60, 'autocomplete' => 'off' ),
'is_required'=> true
);
$form->_fields["billing_country_id-{$bltID}"] =
array( 'htmlType' => 'select',
'name' => "billing_country_id-{$bltID}",
'title' => ts('Country'),
'cc_field' => true,
'attributes' => array( '' => ts( '- select -' ) ) +
CRM_Core_PseudoConstant::country( ),
'is_required'=> true
);
}
/**
* create all fields needed for a credit card transaction
*
* @return void
* @access public
*/
function setCreditCardFields( &$form ) {
CRM_Core_Payment_Form::_setPaymentFields( $form );
$form->_fields['credit_card_number'] =
array( 'htmlType' => 'text',
'name' => 'credit_card_number',
'title' => ts('Card Number'),
'cc_field' => true,
'attributes' => array( 'size' => 20, 'maxlength' => 20, 'autocomplete' => 'off' ),
'is_required'=> true
);
$form->_fields['cvv2'] =
array( 'htmlType' => 'text',
'name' => 'cvv2',
'title' => ts('Security Code'),
'cc_field' => true,
'attributes' => array( 'size' => 5, 'maxlength' => 10, 'autocomplete' => 'off' ),
'is_required'=> true
);
$form->_fields['credit_card_exp_date'] =
array( 'htmlType' => 'date',
'name' => 'credit_card_exp_date',
'title' => ts('Expiration Date'),
'cc_field' => true,
'attributes' => CRM_Core_SelectValues::date( 'creditCard' ),
'is_required'=> true
);
require_once 'CRM/Contribute/PseudoConstant.php';
$creditCardType =
array( '' => ts('- select -')) + CRM_Contribute_PseudoConstant::creditCard( );
$form->_fields['credit_card_type'] =
array( 'htmlType' => 'select',
'name' => 'credit_card_type',
'title' => ts('Card Type'),
'cc_field' => true,
'attributes' => $creditCardType,
'is_required'=> true
);
}
/** create all fields needed for direct debit transaction
*
* @return void
* @access public
*/
function setDirectDebitFields( &$form ) {
CRM_Core_Payment_Form::_setPaymentFields( $form );
$form->_fields['account_holder'] =
array( 'htmlType' => 'text',
'name' => 'account_holder',
'title' => ts('Account Holder'),
'cc_field' => true,
'attributes' => array( 'size' => 20, 'maxlength' => 34, 'autocomplete' => 'on' ),
'is_required'=> true
);
//e.g. IBAN can have maxlength of 34 digits
$form->_fields['bank_account_number'] =
array( 'htmlType' => 'text',
'name' => 'bank_account_number',
'title' => ts('Bank Account Number'),
'cc_field' => true,
'attributes' => array( 'size' => 20, 'maxlength' => 34, 'autocomplete' => 'off' ),
'is_required'=> true
);
//e.g. SWIFT-BIC can have maxlength of 11 digits
$form->_fields['bank_identification_number'] =
array( 'htmlType' => 'text',
'name' => 'bank_identification_number',
'title' => ts('Bank Identification Number888'),
'cc_field' => true,
'attributes' => array( 'size' => 20, 'maxlength' => 11, 'autocomplete' => 'off' ),
'is_required'=> true
);
$form->_fields['bank_name'] =
array( 'htmlType' => 'text',
'name' => 'bank_name',
'title' => ts('Bank Name'),
'cc_field' => true,
'attributes' => array( 'size' => 20, 'maxlength' => 64, 'autocomplete' => 'off' ),
'is_required'=> true
);
}
/**
* Function to add all the credit card fields
*
* @return None
* @access public
*/
function buildCreditCard( &$form, $useRequired = false ) {
require_once 'CRM/Core/Payment.php';
if ( $form->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_FORM) {
foreach ( $form->_fields as $name => $field ) {
if ( isset( $field['cc_field'] ) &&
$field['cc_field'] ) {
$form->add( $field['htmlType'],
$field['name'],
$field['title'],
$field['attributes'],
$useRequired ? $field['is_required'] : false );
}
}
$form->addRule( 'cvv2',
ts( 'Please enter a valid value for your card security code. This is usually the last 3-4 digits on the card\'s signature panel.' ),
'integer' );
$form->addRule( 'credit_card_exp_date',
ts('Credit card expiration date cannot be a past date.'),
'currentDate', true );
// also take care of state country widget
require_once 'CRM/Core/BAO/Address.php';
$stateCountryMap = array( 1 => array( 'country' => "billing_country_id-{$form->_bltID}" ,
'state_province' => "billing_state_province_id-{$form->_bltID}" ) );
CRM_Core_BAO_Address::addStateCountryMap( $stateCountryMap );
}
if ( $form->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_BUTTON ) {
$form->_expressButtonName = $form->getButtonName( 'upload', 'express' );
$form->assign( 'expressButtonName', $form->_expressButtonName );
$form->add('image',
$form->_expressButtonName,
$form->_paymentProcessor['url_button'],
array( 'class' => 'form-submit' ) );
}
}
/**
* Function to add all the direct debit fields
*
* @return None
* @access public
*/
function buildDirectDebit( &$form, $useRequired = false ) {
require_once 'CRM/Core/Payment.php';
/* PS 24/10/2012 START OF DD CODE
THIS CODE WILL NO LONGER BE REQUIRED WHEN WE UPGRADE TO CIVICRM 4.3
*/
require_once 'CRM/Core/BAO/PaymentProcessor.php';
$mode = null; // set to 'test' when in Testing environment.
$paymentProcessor = CRM_Core_BAO_PaymentProcessor::getPayment( $form->_paymentProcessor['id'], $mode );
// Should never be empty - we already established this processor_id exists and is active.
if ( empty( $paymentProcessor ) ) {
return;
}
$ext = new CRM_Core_Extensions();
$paymentClass = null;
$paymentClassPath = null;
if ( $ext->isExtensionKey( $form->_paymentProcessor['class_name'] ) ) {
$paymentClass = $ext->keyToClass( $form->_paymentProcessor['class_name'], 'payment' );
$paymentClassPath = $ext->keyToPath( $form->_paymentProcessor['class_name'], 'payment' );
require_once $paymentClassPath;
}
// Instantiate PP
$eval_sring = '$processorInstance = ' . $paymentClass . '::singleton( $mode, $paymentProcessor );';
eval( $eval_sring );
// Does PP implement this method, and can we call it?
$method = 'buildForm';
if ( !method_exists( $processorInstance, $method ) ||
!is_callable( array( $processorInstance, $method ) )
) {
// No? Do nothing
// CRM_Core_Error::fatal("Payment processor does not implement a '$method' method");
//
} else {
CRM_Core_Payment_Form::_setPaymentFields( $form );
$processorInstance->$method( $form );
}
/* PS 24/10/2012 END OF DD CODE
THIS CODE WILL NO LONGER BE REQUIRED WHEN WE UPGRADE TO CIVICRM 4.3
*/
if ( $form->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_FORM ) {
foreach ( $form->_fields as $name => $field ) {
if ( isset( $field['cc_field'] ) && $field['cc_field'] ) {
$form->add( $field['htmlType']
, $field['name']
, $field['title']
, $field['attributes']
, $useRequired ? $field['is_required'] : false
);
}
}
/* PS 01/11/2012 We dont do this anymore for DD's as the rules are set in the PP
*
$form->addRule( 'bank_identification_number',
ts( 'Please enter a valid Bank Identification Number (value must not contain punctuation characters).' ),
'nopunctuation' );
$form->addRule( 'bank_account_number',
ts('Please enter a valid Bank Account Number (value must not contain punctuation characters).'),
'nopunctuation' );
*
*/
}
if ( $form->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_BUTTON ) {
$form->_expressButtonName = $form->getButtonName( $form->buttonType(), 'express' );
$form->add( 'image'
, $form->_expressButtonName
, $form->_paymentProcessor['url_button']
, array( 'class' => 'form-submit' )
);
}
}
/**
* function to map address fields
*
* @return void
* @static
*/
static function mapParams( $id, &$src, &$dst, $reverse = false ) {
static $map = null;
if ( ! $map ) {
$map = array( 'first_name' => 'billing_first_name' ,
'middle_name' => 'billing_middle_name' ,
'last_name' => 'billing_last_name' ,
'email' => "email-$id" ,
'street_address' => "billing_street_address-$id" ,
'supplemental_address_1' => "billing_supplemental_address_1-$id",
'city' => "billing_city-$id" ,
'state_province' => "billing_state_province-$id" ,
'postal_code' => "billing_postal_code-$id" ,
'country' => "billing_country-$id" ,
);
}
foreach ( $map as $n => $v ) {
if ( ! $reverse ) {
if ( isset( $src[$n] ) ) {
$dst[$v] = $src[$n];
}
} else {
if ( isset( $src[$v] ) ) {
$dst[$n] = $src[$v];
}
}
}
}
/**
* function to get the credit card expiration month
* The date format for this field should typically be "M Y" (ex: Feb 2011) or "m Y" (02 2011)
* See CRM-9017
*
* @return int
* @static
*/
static function getCreditCardExpirationMonth( $src ) {
if ($month = CRM_Utils_Array::value( 'M' , $src['credit_card_exp_date'] ) ) {
return $month;
}
return CRM_Utils_Array::value( 'm' , $src['credit_card_exp_date'] );
}
/**
* function to get the credit card expiration year
* The date format for this field should typically be "M Y" (ex: Feb 2011) or "m Y" (02 2011)
* This function exists only to make it consistant with getCreditCardExpirationMonth
*
* @return int
* @static
*/
static function getCreditCardExpirationYear( $src ) {
return CRM_Utils_Array::value( 'Y' , $src['credit_card_exp_date'] );
}
/**
* function to return state/province is_required = true/false
*
*/
function checkRequiredStateProvince( $form )
{
// If selected country has possible values for state/province mark the
// state/province field as required.
require_once 'CRM/Core/DAO/StateProvince.php';
$config = CRM_Core_Config::singleton( );
$stateProvince = new CRM_Core_DAO_StateProvince( );
$stateProvince->country_id = CRM_Utils_Array::value( "billing_country_id-{$form->_bltID}", $form->_submitValues );
if ( $stateProvince->count( ) > 0 ) {
// check that the state/province data is not excluded by a
// limitation in the localisation settings.
require_once 'CRM/Core/PseudoConstant.php';
$countryIsoCodes = CRM_Core_PseudoConstant::countryIsoCode( );
$limitCodes = $config->provinceLimit( );
$limitIds = array( );
foreach ( $limitCodes as $code ) {
$limitIds = array_merge( $limitIds, array_keys( $countryIsoCodes, $code ) );
}
$limitCountryId = CRM_Utils_Array::value( "billing_country_id-{$form->_bltID}", $form->_submitValues );
if ( $limitCountryId && in_array( $limitCountryId, $limitIds ) ) {
return true;
}
return false;
}
return false;
}
}