Skip to content

Commit

Permalink
Merge pull request #430 from mercadopago/feature/CORECH2-347-add-card…
Browse files Browse the repository at this point in the history
…-token

add card token request
  • Loading branch information
edmarSoaress committed Aug 22, 2023
2 parents ffaa843 + 5650a1b commit 6610985
Show file tree
Hide file tree
Showing 9 changed files with 305 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.phpunit.cache
.php-cs-fixer.cache
.phpunit.result.cache
.vscode
53 changes: 27 additions & 26 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<testsuite name="Unit Tests">
<directory>./tests/MercadoPago</directory>
<exclude>./tests/MercadoPago/Client/Payment/PaymentClientITTest.php</exclude>
<exclude>./tests/MercadoPago/Client/Cardtoken/CardTokenClientITTest.php</exclude>
</testsuite>
</testsuites>
</phpunit>
58 changes: 58 additions & 0 deletions src/MercadoPago/Client/Cardtoken/CardTokenClient.php
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;
}
}
}
2 changes: 0 additions & 2 deletions src/MercadoPago/Net/MPDefaultHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public function __construct(?HttpRequest $httpRequest = null)
*/
public function send(MPRequest $request): MPResponse
{

$request_options = $this->createHttpRequestOptions($request);
$this->httpRequest->setOptionArray($request_options);
$api_result = $this->httpRequest->execute();
Expand All @@ -42,7 +41,6 @@ public function send(MPRequest $request): MPResponse
$this->httpRequest->close();
throw new Exception($error_message);
}

if ($status_code < 200 || $status_code >= 300) {
$this->httpRequest->close();
throw new MPApiException("Api error. Check response for details", $mp_response);
Expand Down
74 changes: 74 additions & 0 deletions src/MercadoPago/Resources/CardToken.php
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 tests/MercadoPago/Client/Cardtoken/CardTokenClientITTest.php
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;
}
}
Loading

0 comments on commit 6610985

Please sign in to comment.