Skip to content

Commit

Permalink
Merge pull request #51 from timgws/change-supported-cards
Browse files Browse the repository at this point in the history
New function; addSupportedBrand.
  • Loading branch information
delatbabel committed Jan 28, 2016
2 parents 5a618b4 + aa7b32b commit b980a16
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 17 deletions.
69 changes: 52 additions & 17 deletions src/Omnipay/Common/CreditCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,32 @@ class CreditCard
const BRAND_FORBRUGSFORENINGEN = 'forbrugsforeningen';
const BRAND_LASER = 'laser';

/**
* All known/supported card brands, and a regular expression to match them.
*
* The order of the card brands is important, as some of the regular expressions overlap.
*
* Note: The fact that a particular card brand has been added to this array does not imply
* that a selected gateway will support the card.
*
* @link https://github.com/Shopify/active_merchant/blob/master/lib/active_merchant/billing/credit_card_methods.rb
* @var array
*/
protected $supported_cards = array(
self::BRAND_VISA => '/^4\d{12}(\d{3})?$/',
self::BRAND_MASTERCARD => '/^(5[1-5]\d{4}|677189)\d{10}$/',
self::BRAND_DISCOVER => '/^(6011|65\d{2}|64[4-9]\d)\d{12}|(62\d{14})$/',
self::BRAND_AMEX => '/^3[47]\d{13}$/',
self::BRAND_DINERS_CLUB => '/^3(0[0-5]|[68]\d)\d{11}$/',
self::BRAND_JCB => '/^35(28|29|[3-8]\d)\d{12}$/',
self::BRAND_SWITCH => '/^6759\d{12}(\d{2,3})?$/',
self::BRAND_SOLO => '/^6767\d{12}(\d{2,3})?$/',
self::BRAND_DANKORT => '/^5019\d{12}$/',
self::BRAND_MAESTRO => '/^(5[06-8]|6\d)\d{10,17}$/',
self::BRAND_FORBRUGSFORENINGEN => '/^600722\d{10}$/',
self::BRAND_LASER => '/^(6304|6706|6709|6771(?!89))\d{8}(\d{4}|\d{6,7})?$/',
);

/**
* Internal storage of all of the card parameters.
*
Expand All @@ -125,30 +151,39 @@ public function __construct($parameters = null)
/**
* All known/supported card brands, and a regular expression to match them.
*
* The order of the card brands is important, as some of the regular expressions overlap.
*
* Note: The fact that this class knows about a particular card brand does not imply
* that your gateway supports it.
*
* @see self::$supported_cards
* @return array
* @link https://github.com/Shopify/active_merchant/blob/master/lib/active_merchant/billing/credit_card_methods.rb
*/
public function getSupportedBrands()
{
return array(
static::BRAND_VISA => '/^4\d{12}(\d{3})?$/',
static::BRAND_MASTERCARD => '/^(5[1-5]\d{4}|677189)\d{10}$/',
static::BRAND_DISCOVER => '/^(6011|65\d{2}|64[4-9]\d)\d{12}|(62\d{14})$/',
static::BRAND_AMEX => '/^3[47]\d{13}$/',
static::BRAND_DINERS_CLUB => '/^3(0[0-5]|[68]\d)\d{11}$/',
static::BRAND_JCB => '/^35(28|29|[3-8]\d)\d{12}$/',
static::BRAND_SWITCH => '/^6759\d{12}(\d{2,3})?$/',
static::BRAND_SOLO => '/^6767\d{12}(\d{2,3})?$/',
static::BRAND_DANKORT => '/^5019\d{12}$/',
static::BRAND_MAESTRO => '/^(5[06-8]|6\d)\d{10,17}$/',
static::BRAND_FORBRUGSFORENINGEN => '/^600722\d{10}$/',
static::BRAND_LASER => '/^(6304|6706|6709|6771(?!89))\d{8}(\d{4}|\d{6,7})?$/',
);
return $this->supported_cards;
}

/**
* Set a custom supported card brand with a regular expression to match it.
*
* Note: The fact that a particular card is known does not imply that your
* gateway supports it.
*
* Set $add_to_front to true if the key should be added to the front of the array
*
* @param string $name The name of the new supported brand.
* @param string $expression The regular expression to check if a card is supported.
* @return boolean success
*/
public function addSupportedBrand($name, $expression)
{
$known_brands = array_keys($this->supported_cards);

if (in_array($name, $known_brands)) {
return false;
}

$this->supported_cards[$name] = $expression;
return true;
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/Omnipay/Common/CreditCardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,21 @@ public function testGetSupportedBrands()
$this->assertArrayHasKey(CreditCard::BRAND_VISA, $brands);
}

public function testCustomSupportedBrand()
{
$this->card->addSupportedBrand('omniexpress', '/^9\d{12}(\d{3})?$/');
$this->assertArrayHasKey('omniexpress', $this->card->getSupportedBrands());
}

public function testCustomBrandWorks()
{
$this->card->addSupportedBrand('omniexpress', '/^9\d{12}(\d{3})?$/');
$this->assertArrayHasKey('omniexpress', $this->card->getSupportedBrands());
$this->card->setNumber('9111111111111110');
$this->card->validate();
$this->assertEquals('omniexpress', $this->card->getBrand());
}

public function testTitle()
{
$this->card->setTitle('Mr.');
Expand Down

0 comments on commit b980a16

Please sign in to comment.