Skip to content

Commit

Permalink
Add Card Bin query endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mathmarques committed Jul 1, 2019
1 parent 267e23c commit 2f0f6f8
Show file tree
Hide file tree
Showing 6 changed files with 365 additions and 1 deletion.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ Requires PHP 7.1 or newer.
* [x] Update Payment
* [x] Capture
* [x] Void
* [ ] Update Recurrent Payment
* [ ] Recurrent Payment
* [x] Query Payment
* [x] By Payment ID
* [ ] By Order ID
* [x] Query Card Bin
* [ ] Tokenize Card
* [ ] Fraud Analysis
* [ ] Velocity
* [ ] Zero Auth
* [ ] Silent Order Post

## Usage

Expand Down
26 changes: 26 additions & 0 deletions examples/CardBinRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

require '../vendor/autoload.php';

use \ChicoRei\Packages\Cielo\Cielo;
use \ChicoRei\Packages\Cielo\Merchant;
use \ChicoRei\Packages\Cielo\Util;

$merchant = Merchant::create([
'id' => 'ID',
'key' => 'KEY',
]);

$cielo = new Cielo($merchant, true);

try {
$response = $cielo->cardBin()->query('506775');

$arrayResponse = $response->toArray();
var_dump(Util::cleanArray($arrayResponse));
} catch (Exception $e) {
echo 'Code: ' . $e->getCode() . PHP_EOL;
echo 'Message: ' . $e->getMessage() . PHP_EOL;
}
18 changes: 18 additions & 0 deletions src/Cielo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ChicoRei\Packages\Cielo;

use ChicoRei\Packages\Cielo\Handler\CardBinHandler;
use ChicoRei\Packages\Cielo\Handler\SaleHandler;

class Cielo
Expand Down Expand Up @@ -45,6 +46,11 @@ class Cielo
*/
private $saleHandler;

/**
* @var CardBinHandler
*/
private $cardBinHandler;

/**
* Cielo Service constructor.
*
Expand Down Expand Up @@ -78,4 +84,16 @@ public function sale(): SaleHandler

return $this->saleHandler;
}

/**
* @return CardBinHandler
*/
public function cardBin(): CardBinHandler
{
if (!isset($this->cardBinHandler)) {
$this->cardBinHandler = new CardBinHandler($this->client);
}

return $this->cardBinHandler;
}
}
51 changes: 51 additions & 0 deletions src/Handler/CardBinHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace ChicoRei\Packages\Cielo\Handler;

use ChicoRei\Packages\Cielo\Client;
use ChicoRei\Packages\Cielo\Exception\CieloAPIException;
use ChicoRei\Packages\Cielo\Exception\CieloClientException;
use ChicoRei\Packages\Cielo\Request\CardBinRequest;
use ChicoRei\Packages\Cielo\Response\CardBinResponse;
use RuntimeException;

class CardBinHandler
{
/**
* @var Client
*/
private $client;

/**
* @param Client $client
*/
public function __construct(Client $client)
{
$this->client = $client;
}

/**
* Query Card Bin
*
* @param string|array|CardBinRequest $payload
* @return CardBinResponse
* @throws CieloAPIException
* @throws CieloClientException
*/
public function query($payload): CardBinResponse
{
if (is_string($payload)) {
$payload = CardBinRequest::create(['bin' => $payload]);
} elseif (is_array($payload)) {
$payload = CardBinRequest::create($payload);
}

if (!$payload instanceof CardBinRequest) {
throw new RuntimeException('Payload must be a string, an array or an instance of CardBinRequest');
}

$response = $this->client->sendQueryApiRequest($payload);

return CardBinResponse::create($response);
}
}
65 changes: 65 additions & 0 deletions src/Request/CardBinRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace ChicoRei\Packages\Cielo\Request;

class CardBinRequest extends CieloRequest
{
/**
* Bin
*
* @var string|null
*/
public $bin;

/**
* @return string|null
*/
public function getBin(): ?string
{
return $this->bin;
}

/**
* @param string|null $bin
* @return CardBinRequest
*/
public function setBin(?string $bin): CardBinRequest
{
$this->bin = $bin;
return $this;
}

/**
* @return string
*/
public function getMethod(): string
{
return 'GET';
}

/**
* @return string
*/
public function getPath(): string
{
return sprintf('/1/cardBin/%s', $this->getBin());
}

/**
* @return array|null
*/
public function getPayload(): ?array
{
return null;
}

/**
* Returns array representation of object
*
* @return array
*/
public function toArray(): array
{
return ['Bin' => $this->getBin()];
}
}
199 changes: 199 additions & 0 deletions src/Response/CardBinResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
<?php

namespace ChicoRei\Packages\Cielo\Response;

use ChicoRei\Packages\Cielo\CieloObject;

class CardBinResponse extends CieloObject
{
/**
* Status
*
* @var string|null
*/
public $status;

/**
* Provider
*
* @var string|null
*/
public $provider;

/**
* Card Type
*
* @var string|null
*/
public $cardType;

/**
* Foreign Card
*
* @var bool|null
*/
public $foreignCard;

/**
* Corporate Card
*
* @var bool|null
*/
public $corporateCard;

/**
* Issuer
*
* @var string|null
*/
public $issuer;

/**
* Issuer Code
*
* @var string|null
*/
public $issuerCode;

/**
* @return string|null
*/
public function getStatus(): ?string
{
return $this->status;
}

/**
* @param string|null $status
* @return CardBinResponse
*/
public function setStatus(?string $status): CardBinResponse
{
$this->status = $status;
return $this;
}

/**
* @return string|null
*/
public function getProvider(): ?string
{
return $this->provider;
}

/**
* @param string|null $provider
* @return CardBinResponse
*/
public function setProvider(?string $provider): CardBinResponse
{
$this->provider = $provider;
return $this;
}

/**
* @return string|null
*/
public function getCardType(): ?string
{
return $this->cardType;
}

/**
* @param string|null $cardType
* @return CardBinResponse
*/
public function setCardType(?string $cardType): CardBinResponse
{
$this->cardType = $cardType;
return $this;
}

/**
* @return bool|null
*/
public function getForeignCard(): ?bool
{
return $this->foreignCard;
}

/**
* @param bool|null $foreignCard
* @return CardBinResponse
*/
public function setForeignCard(?bool $foreignCard): CardBinResponse
{
$this->foreignCard = $foreignCard;
return $this;
}

/**
* @return bool|null
*/
public function getCorporateCard(): ?bool
{
return $this->corporateCard;
}

/**
* @param bool|null $corporateCard
* @return CardBinResponse
*/
public function setCorporateCard(?bool $corporateCard): CardBinResponse
{
$this->corporateCard = $corporateCard;
return $this;
}

/**
* @return string|null
*/
public function getIssuer(): ?string
{
return $this->issuer;
}

/**
* @param string|null $issuer
* @return CardBinResponse
*/
public function setIssuer(?string $issuer): CardBinResponse
{
$this->issuer = $issuer;
return $this;
}

/**
* @return string|null
*/
public function getIssuerCode(): ?string
{
return $this->issuerCode;
}

/**
* @param string|null $issuerCode
* @return CardBinResponse
*/
public function setIssuerCode(?string $issuerCode): CardBinResponse
{
$this->issuerCode = $issuerCode;
return $this;
}

/**
* @inheritdoc
*/
public function toArray(): array
{
return [
'Status' => $this->getStatus(),
'Provider' => $this->getProvider(),
'CardType' => $this->getCardType(),
'ForeignCard' => $this->getForeignCard(),
'CorporateCard' => $this->getCorporateCard(),
'Issuer' => $this->getIssuer(),
'IssuerCode' => $this->getIssuerCode(),
];
}
}

0 comments on commit 2f0f6f8

Please sign in to comment.