Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* User: Matheus Gomes
* E-maiL: matheus@lenzy.com.br
* Date: 07/03/19
* Time: 15:32
*/
class CreditCard
{
const AMERICAN_EXPRESS = 'american_express';
const DINERS_CLUB = 'diners_club';
const ELO = 'elo';
const HIPERCARD = 'hipercard';
const MASTERCARD = 'mastercard';
const VISA = 'visa';
/**
* @var array
*/
private $brands = [
self::AMERICAN_EXPRESS => '/^3[47]\d{13}$/',
self::DINERS_CLUB => '/^3(?:0[0-5]|[68]\d)\d{11}$/',
self::ELO => '/(4011|431274|438935|451416|457393|4576|457631|457632|504175|627780|636297|636368|636369|(6503[1-3])|(6500(3[5-9]|4[0-9]|5[0-1]))|(6504(0[5-9]|1[0-9]|2[0-9]|3[0-9]))|(650(48[5-9]|49[0-9]|50[0-9]|51[1-9]|52[0-9]|53[0-7]))|(6505(4[0-9]|5[0-9]|6[0-9]|7[0-9]|8[0-9]|9[0-8]))|(6507(0[0-9]|1[0-8]))|(6507(2[0-7]))|(650(90[1-9]|91[0-9]|920))|(6516(5[2-9]|6[0-9]|7[0-9]))|(6550(0[0-9]|1[1-9]))|(6550(2[1-9]|3[0-9]|4[0-9]|5[0-8]))|(506(699|77[0-8]|7[1-6][0-9))|(509([0-9][0-9][0-9])))/',
self::HIPERCARD => '/^(606282\d{10}(\d{3})?)|(3841\d{15})$/',
self::MASTERCARD => '/^5[1-5]\d{14}$/',
self::VISA => '/^4\d{12}(?:\d{3})?$/',
];
/**
* @param int $number
* @return mixed
*/
public static function getBrandByCardNumber(int $number)
{
return (new CreditCard)->verifyBrand($number);
}
/**
* @param int $number
* @return string
*/
private function verifyBrand(int $number): string
{
switch ($number) {
case $this->getBrandPattern($this::AMERICAN_EXPRESS, $number):
return $this::AMERICAN_EXPRESS;
case $this->getBrandPattern($this::DINERS_CLUB, $number):
return $this::DINERS_CLUB;
case $this->getBrandPattern($this::ELO, $number):
return $this::ELO;
case $this->getBrandPattern($this::HIPERCARD, $number):
return $this::HIPERCARD;
case $this->getBrandPattern($this::MASTERCARD, $number):
return $this::MASTERCARD;
case $this->getBrandPattern($this::VISA, $number):
return $this::VISA;
default:
return 'is_invalid';
}
}
/**
* @param string $pattern
* @param int $number
* @return bool
*/
private function getBrandPattern(string $pattern, int $number): bool
{
return preg_match($this->brands[$pattern], $number) > 0;
}
}
@betinhosilva

This comment has been minimized.

Copy link

commented May 5, 2019

Como eu faço a implementação?

@isaacduarte

This comment has been minimized.

Copy link

commented May 17, 2019

Uma pergunta como a partir do numero da bin do cartão posso puxar o banco emissor do cartão ??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.