6b8c23ca6a48d85a4a7852ed590004be30619db0
1 // http://civicrm.org/licensing
3 civicrm_billingblock_creditcard_helper();
5 $('#crm-container .credit_card_type-section').on('crmFormLoad', '*', function() {
6 civicrm_billingblock_creditcard_helper();
10 * Adds the icons of enabled credit cards
11 * Handles clicking on a icon.
12 * Changes the icon depending on the credit card number.
13 * Removes spaces and dashes from credit card numbers.
15 function civicrm_billingblock_creditcard_helper() {
16 $.each(CRM
.config
.creditCardTypes
, function(key
, val
) {
17 var html
= '<a href="#" title="' + val
+ '" class="crm-credit_card_type-icon-' + key
+ '"><span>' + val
+ '</span></a>';
18 $('.crm-credit_card_type-icons').append(html
);
20 $('.crm-credit_card_type-icon-' + key
).click(function() {
21 $('#crm-container .credit_card_type-section #credit_card_type').val(val
);
22 $('#crm-container .credit_card_type-section a').css('opacity', 0.25);
23 $('#crm-container .credit_card_type-section .crm-credit_card_type-icon-' + key
).css('opacity', 1);
28 // Hide the CC type field (redundant)
29 $('#crm-container .credit_card_type-section select#credit_card_type').hide();
30 $('#crm-container .credit_card_type-section .label').hide();
32 // Select according to the number entered
33 $('#crm-container input#credit_card_number').change(function() {
34 var ccnumber
= cj(this).val();
36 // Remove spaces and dashes
37 ccnumber
= ccnumber
.replace(/[- ]/g, '');
38 cj(this).val(ccnumber
);
40 // Semi-hide all images, we will un-hide the right one afterwards
41 $('#crm-container .credit_card_type-section a').css('opacity', 0.25);
42 $('#credit_card_type').val('');
44 civicrm_billingblock_set_card_type(ccnumber
);
48 function civicrm_billingblock_set_card_type(ccnumber
) {
49 // Based on http://davidwalsh.name/validate-credit-cards
50 // See also https://en.wikipedia.org/wiki/Credit_card_numbers
52 'mastercard': '5[1-5][0-9]{14}',
53 'visa': '4(?:[0-9]{12}|[0-9]{15})',
54 'amex': '3[47][0-9]{13}',
55 'dinersclub': '3(?:0[0-5][0-9]{11}|[68][0-9]{12})',
56 'carteblanche': '3(?:0[0-5][0-9]{11}|[68][0-9]{12})',
57 'discover': '6011[0-9]{12}',
58 'jcb': '(?:3[0-9]{15}|(2131|1800)[0-9]{11})',
59 'unionpay': '62(?:[0-9]{14}|[0-9]{17})'
63 'mastercard': 'MasterCard',
66 'dinersclub': 'Diners Club',
67 'carteblanche': 'Carte Blanche',
68 'discover': 'Discover',
70 'unionpay': 'UnionPay'
73 $.each(card_types
, function(key
, pattern
) {
74 if (ccnumber
.match('^' + pattern
+ '$')) {
75 var value
= card_values
[key
];
76 $('#crm-container .credit_card_type-section .crm-credit_card_type-icon-' + key
).css('opacity', 1);
77 $('select#credit_card_type').val(value
);