Luhn validation and card types for credit/debit card numbers
Here are some useful resources on the Luhn algorithm:
https://en.wikipedia.org/wiki/Luhn_algorithm
https://www.rosettacode.org/wiki/Luhn_test_of_credit_card_numbers
Thank you to Max Kramer for the initial inspiration.
- Check the type of a card number (Amex, DinersClub, Discover, JCB, MasterCard or Visa)
- Check if a given card number is valid
let cardNumber = "3782 822463 10005"
let isValid = cardNumber.plastic_luhnValidate()
do {
print(try cardNumber.plastic_cardType()) // Prints "Amex" in the console
} catch {
// Handle error
// Only error that Plastic throws is PlasticError.InvalidCardNumber
// Thrown if the card number is < 9 digits and not Luhn valid
}