Skip to content

Commit aa7b32b

Browse files
committed
New function; addSupportedBrand.
1 parent 3d7b925 commit aa7b32b

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
@@ -104,6 +104,32 @@ class CreditCard
104104
const BRAND_FORBRUGSFORENINGEN = 'forbrugsforeningen';
105105
const BRAND_LASER = 'laser';
106106

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

153188
/**

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)