-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #430 from mercadopago/feature/CORECH2-347-add-card…
…-token add card token request
- Loading branch information
Showing
9 changed files
with
305 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
.phpunit.cache | ||
.php-cs-fixer.cache | ||
.phpunit.result.cache | ||
.vscode |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace MercadoPago\Client\CardToken; | ||
|
||
use MercadoPago\Client\MercadoPagoClient; | ||
use MercadoPago\Exceptions\MPApiException; | ||
use MercadoPago\MercadoPagoConfig; | ||
use MercadoPago\Net\HttpMethod; | ||
use MercadoPago\Resources\CardToken; | ||
use MercadoPago\Serialization\Serializer; | ||
|
||
/** Client responsible for performing cardtoken actions. */ | ||
class CardTokenClient extends MercadoPagoClient | ||
{ | ||
private static $URL = "/v1/card_tokens"; | ||
|
||
private static $URL_WITH_ID = "/v1/card_tokens/%s"; | ||
|
||
/** Default constructor. Uses the default http client used by the SDK. */ | ||
public function __construct() | ||
{ | ||
parent::__construct(MercadoPagoConfig::getHttpClient()); | ||
} | ||
|
||
/** | ||
* Method responsible for creating card token. | ||
* @param array $request card token data. | ||
* @return \MercadoPago\Resources\CardToken card token created. | ||
*/ | ||
public function create(array $request): CardToken | ||
{ | ||
try { | ||
$response = parent::send(self::$URL, HttpMethod::POST, json_encode($request)); | ||
$result = Serializer::deserializeFromJson(CardToken::class, $response->getContent()); | ||
$result->setResponse($response); | ||
return $result; | ||
} catch (MPApiException | \Exception $e) { | ||
throw $e; | ||
} | ||
} | ||
|
||
/** | ||
* Method responsible for getting card token by id. | ||
* @param string $id card token id. | ||
* @return \MercadoPago\Resources\CardToken card token found. | ||
*/ | ||
public function get(string $id): CardToken | ||
{ | ||
try { | ||
$response = parent::send(sprintf(self::$URL_WITH_ID, $id), HttpMethod::GET); | ||
$result = Serializer::deserializeFromJson(CardToken::class, $response->getContent()); | ||
$result->setResponse($response); | ||
return $result; | ||
} catch (MPApiException | \Exception $e) { | ||
throw $e; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace MercadoPago\Resources; | ||
|
||
use MercadoPago\Net\MPResource; | ||
use MercadoPago\Serialization\Mapper; | ||
|
||
/** Card class. */ | ||
class CardToken extends MPResource | ||
{ | ||
/** Class mapper. */ | ||
use Mapper; | ||
|
||
/** Id of the card. */ | ||
public ?string $id; | ||
|
||
/** Last four digits of card number. */ | ||
public ?string $last_four_digits; | ||
|
||
/** First six digit of card number. */ | ||
public ?string $first_six_digits; | ||
|
||
/** Card expiration year. */ | ||
public ?int $expiration_year; | ||
|
||
/** Card expiration month. */ | ||
public ?int $expiration_month; | ||
|
||
/** Creation date of card. */ | ||
public ?string $date_created; | ||
|
||
/** Last update of data from the card. */ | ||
public ?string $date_last_updated; | ||
|
||
/** Card's owner data. */ | ||
public object|array|null $cardholder; | ||
|
||
/** Card id. */ | ||
public ?int $card_id; | ||
|
||
/** Current status of card. E.g. active. */ | ||
public string $status; | ||
|
||
/** Date token expires. */ | ||
public ?string $date_due; | ||
|
||
/** Flag indicating if Luhn validation is used. */ | ||
public bool $luhn_validation; | ||
|
||
/** Flag indicating if this is a production card token. */ | ||
public bool $live_mode; | ||
|
||
/** Require esc. */ | ||
public bool $require_esc; | ||
|
||
/** Security code of the card. */ | ||
public int $card_number_length; | ||
|
||
/** Security code of the card. */ | ||
public int $security_code_length; | ||
|
||
|
||
private $map = [ | ||
"cardholder" => "MercadoPago\Resources\Payment\Cardholder" | ||
]; | ||
|
||
/** | ||
* Method responsible for getting map of entities. | ||
*/ | ||
public function getMap(): array | ||
{ | ||
return $this->map; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
tests/MercadoPago/Client/Cardtoken/CardTokenClientITTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace MercadoPago\Client\Cardtoken; | ||
|
||
use MercadoPago\Exceptions\MPApiException; | ||
use MercadoPago\MercadoPagoConfig; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* CardTokenClient integration tests. | ||
*/ | ||
final class CardTokenClientITTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
MercadoPagoConfig::setAccessToken(getenv("ACCESS_TOKEN")); | ||
} | ||
|
||
public function testCreateSuccess(): void | ||
{ | ||
$client = new CardTokenClient(); | ||
$card_token = $client->create($this->createRequest()); | ||
$this->assertNotNull($card_token->id); | ||
} | ||
|
||
public function testCreateWithAccessTokenFailure(): void | ||
{ | ||
$this->expectException(MPApiException::class); | ||
$client = new CardTokenClient(); | ||
$request = $this->createRequest(); | ||
MercadoPagoConfig::setAccessToken("invalid_access_token"); | ||
$client->create($request); | ||
} | ||
|
||
public function testGetSuccess(): void | ||
{ | ||
$client = new CardTokenClient(); | ||
$card_token = $client->get("60aca73f30e817fcf074cebc616897ba"); | ||
$this->assertNotNull($card_token->id); | ||
} | ||
|
||
public function testGetWithAccessTokenFailure(): void | ||
{ | ||
$this->expectException(MPApiException::class); | ||
$client = new CardTokenClient(); | ||
MercadoPagoConfig::setAccessToken("invalid_access_token"); | ||
$client->get("123"); | ||
} | ||
|
||
private function createRequest(): array | ||
{ | ||
$request = [ | ||
"card_id" => "9067121741", | ||
"security_code" => "123" | ||
]; | ||
return $request; | ||
} | ||
} |
Oops, something went wrong.