Skip to content

Commit b980a16

Browse files
committed
Merge pull request #51 from timgws/change-supported-cards
New function; addSupportedBrand.
2 parents 5a618b4 + aa7b32b commit b980a16

File tree

2 files changed

+67
-17
lines changed

2 files changed

+67
-17
lines changed

src/Omnipay/Common/CreditCard.php

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,32 @@ class CreditCard
105105
const BRAND_FORBRUGSFORENINGEN = 'forbrugsforeningen';
106106
const BRAND_LASER = 'laser';
107107

108+
/**
109+
* All known/supported card brands, and a regular expression to match them.
110+
*
111+
* The order of the card brands is important, as some of the regular expressions overlap.
112+
*
113+
* Note: The fact that a particular card brand has been added to this array does not imply
114+
* that a selected gateway will support the card.
115+
*
116+
* @link https://github.com/Shopify/active_merchant/blob/master/lib/active_merchant/billing/credit_card_methods.rb
117+
* @var array
118+
*/
119+
protected $supported_cards = array(
120+
self::BRAND_VISA => '/^4\d{12}(\d{3})?$/',
121+
self::BRAND_MASTERCARD => '/^(5[1-5]\d{4}|677189)\d{10}$/',
122+
self::BRAND_DISCOVER => '/^(6011|65\d{2}|64[4-9]\d)\d{12}|(62\d{14})$/',
123+
self::BRAND_AMEX => '/^3[47]\d{13}$/',
124+
self::BRAND_DINERS_CLUB => '/^3(0[0-5]|[68]\d)\d{11}$/',
125+
self::BRAND_JCB => '/^35(28|29|[3-8]\d)\d{12}$/',
126+
self::BRAND_SWITCH => '/^6759\d{12}(\d{2,3})?$/',
127+
self::BRAND_SOLO => '/^6767\d{12}(\d{2,3})?$/',
128+
self::BRAND_DANKORT => '/^5019\d{12}$/',
129+
self::BRAND_MAESTRO => '/^(5[06-8]|6\d)\d{10,17}$/',
130+
self::BRAND_FORBRUGSFORENINGEN => '/^600722\d{10}$/',
131+
self::BRAND_LASER => '/^(6304|6706|6709|6771(?!89))\d{8}(\d{4}|\d{6,7})?$/',
132+
);
133+
108134
/**
109135
* Internal storage of all of the card parameters.
110136
*
@@ -125,30 +151,39 @@ public function __construct($parameters = null)
125151
/**
126152
* All known/supported card brands, and a regular expression to match them.
127153
*
128-
* The order of the card brands is important, as some of the regular expressions overlap.
129-
*
130154
* Note: The fact that this class knows about a particular card brand does not imply
131155
* that your gateway supports it.
132156
*
157+
* @see self::$supported_cards
133158
* @return array
134-
* @link https://github.com/Shopify/active_merchant/blob/master/lib/active_merchant/billing/credit_card_methods.rb
135159
*/
136160
public function getSupportedBrands()
137161
{
138-
return array(
139-
static::BRAND_VISA => '/^4\d{12}(\d{3})?$/',
140-
static::BRAND_MASTERCARD => '/^(5[1-5]\d{4}|677189)\d{10}$/',
141-
static::BRAND_DISCOVER => '/^(6011|65\d{2}|64[4-9]\d)\d{12}|(62\d{14})$/',
142-
static::BRAND_AMEX => '/^3[47]\d{13}$/',
143-
static::BRAND_DINERS_CLUB => '/^3(0[0-5]|[68]\d)\d{11}$/',
144-
static::BRAND_JCB => '/^35(28|29|[3-8]\d)\d{12}$/',
145-
static::BRAND_SWITCH => '/^6759\d{12}(\d{2,3})?$/',
146-
static::BRAND_SOLO => '/^6767\d{12}(\d{2,3})?$/',
147-
static::BRAND_DANKORT => '/^5019\d{12}$/',
148-
static::BRAND_MAESTRO => '/^(5[06-8]|6\d)\d{10,17}$/',
149-
static::BRAND_FORBRUGSFORENINGEN => '/^600722\d{10}$/',
150-
static::BRAND_LASER => '/^(6304|6706|6709|6771(?!89))\d{8}(\d{4}|\d{6,7})?$/',
151-
);
162+
return $this->supported_cards;
163+
}
164+
165+
/**
166+
* Set a custom supported card brand with a regular expression to match it.
167+
*
168+
* Note: The fact that a particular card is known does not imply that your
169+
* gateway supports it.
170+
*
171+
* Set $add_to_front to true if the key should be added to the front of the array
172+
*
173+
* @param string $name The name of the new supported brand.
174+
* @param string $expression The regular expression to check if a card is supported.
175+
* @return boolean success
176+
*/
177+
public function addSupportedBrand($name, $expression)
178+
{
179+
$known_brands = array_keys($this->supported_cards);
180+
181+
if (in_array($name, $known_brands)) {
182+
return false;
183+
}
184+
185+
$this->supported_cards[$name] = $expression;
186+
return true;
152187
}
153188

154189
/**

tests/Omnipay/Common/CreditCardTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,21 @@ public function testGetSupportedBrands()
109109
$this->assertArrayHasKey(CreditCard::BRAND_VISA, $brands);
110110
}
111111

112+
public function testCustomSupportedBrand()
113+
{
114+
$this->card->addSupportedBrand('omniexpress', '/^9\d{12}(\d{3})?$/');
115+
$this->assertArrayHasKey('omniexpress', $this->card->getSupportedBrands());
116+
}
117+
118+
public function testCustomBrandWorks()
119+
{
120+
$this->card->addSupportedBrand('omniexpress', '/^9\d{12}(\d{3})?$/');
121+
$this->assertArrayHasKey('omniexpress', $this->card->getSupportedBrands());
122+
$this->card->setNumber('9111111111111110');
123+
$this->card->validate();
124+
$this->assertEquals('omniexpress', $this->card->getBrand());
125+
}
126+
112127
public function testTitle()
113128
{
114129
$this->card->setTitle('Mr.');

0 commit comments

Comments
 (0)