Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Card #416

Open
morgan2233 opened this issue Dec 13, 2018 · 7 comments
Open

Card #416

morgan2233 opened this issue Dec 13, 2018 · 7 comments

Comments

@morgan2233
Copy link

Hello, All works fine however when someone enters there details, the card doesnt change type for example showing its a visa like in the demo

@QuintonC
Copy link

I had a similar issue where my card would not update types at all. No matter how many different (correct) card numbers I provided, it would not display the card type.

So I went about looking into the source code for this library and found a solution myself.

$("#credit-number").blur(function() {
        // NOTE: $("#credit-number") is what my field name is. 
        // This can/should be replaced with your credit card number input field.
	check_card_type($(this).val());
});

function check_card_type(card_number) {
	var cards = Payment.getCardArray(); // Returns the complete cards validation
	var card_type = "";

	for (var i = 0; i < cards.length; i += 1) {
	  	if (cards[i].pattern.test(card_number)) {
			card_type = cards[i].type;
			console.log(card_type); // the credit card type
		}
	}

	if (card_type != "") {
		set_card_type(card_type);
	} else {
		set_card_type("unknown");
	}
}

function set_card_type(type) {
	var card_types = Card.prototype.cardTypes.join(' ');
	
	// Remove identifying classes.
	$(".jp-card").removeClass("jp-card-unknown");
	$(".jp-card").removeClass("jp-card-identified");
	$(".jp-card").removeClass(card_types);
	
	if (type != "" && type != "unknown") {
		$(".jp-card").addClass("jp-card-" + type);
		$(".jp-card").addClass("jp-card-identified");
	} else {
		$(".jp-card").addClass("jp-card-unknown");
		$(".jp-card").removeClass("jp-card-identified");
	}
}

@morgan2233
Copy link
Author

so where would i add that code to make it work?

@QuintonC
Copy link

@morgan2233 Just in the JavaScript. Replace "credit-number" with your credit card number input field and it will call those functions to check the card type and format it properly.

@morgan2233
Copy link
Author

Do i put it on the web file or inside the .js file

@QuintonC
Copy link

In theory either should work. It's best practice to put it in your .js file though.

@morgan2233
Copy link
Author

morgan2233 commented Jan 25, 2019

Still doesnt work sadly, im using the jquery one if that makes a difference to your code

@thiagok
Copy link

thiagok commented Aug 21, 2019

Thanks @QuintonC! Clever solution :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants