Skip to content
This repository was archived by the owner on Dec 11, 2020. It is now read-only.

Not luhn compliant #403

Closed
fedeisas opened this issue Aug 27, 2014 · 6 comments
Closed

Not luhn compliant #403

fedeisas opened this issue Aug 27, 2014 · 6 comments

Comments

@fedeisas
Copy link

Hi guys, I noticed the credit cards numbers generated by this library won't pass a luhn validator. Is this by design?

@ulrikjohansson
Copy link
Contributor

From a 5-second googling it seems like all the major credit card providers all use the same checksum validation algorithm (Luhn). There are also personal identification numbers that use this algorithm for their checksums.
Perhaps it would be a good idea to pull this checksum calculator into core, and let the providers use it as a utility function? That way we don't end up with copies everywhere in the different providers, which seems to be the way it's done in the localized versions today (I did this myself for the sv_SE translation).

@nineinchnick
Copy link
Contributor

I think it's also used for IBANs. If it were available in the core I'd update those generators.

@fedeisas
Copy link
Author

Many PHP Validation libraries have luhn validation. I don't think it's possible to generate luhn-valid numbers. Instead, we could wrap the creditCardNumber() method do..while using that checksum validator?

Taken from the lithium validation class:

<?php

function isValidLuhn($value) {
  if (empty($value) || !is_string($value)) {
    return false;
  }
  $sum = 0;
  $length = strlen($value);

  for ($position = 1 - ($length % 2); $position < $length; $position += 2) {
    $sum += $value[$position];
  }
  for ($position = ($length % 2); $position < $length; $position += 2) {
    $number = $value[$position] * 2;
    $sum += ($number < 10) ? $number : $number - 9;
  }
  return ($sum % 10 === 0);
}

@fzaninotto
Copy link
Owner

Fixed in #414

@fedeisas
Copy link
Author

fedeisas commented Sep 2, 2014

@fzaninotto amazing! Thanks 😃

@ulrikjohansson
Copy link
Contributor

Great work! Much better implementation now.

I noticed one little naming thing in the rewritten tests in test/Faker/Provider/sv_SE/PersonTest.php. I've written a comment here: https://github.com/fzaninotto/Faker/pull/414/files#r17042304

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

No branches or pull requests

4 participants