Skip to content

Commit

Permalink
Merge pull request #290 from TransbankDevelopers/feat/improve-oneclic…
Browse files Browse the repository at this point in the history
…k-delete-response

feat: improve oneclick delete response
  • Loading branch information
Matiasnickolas authored Sep 3, 2024
2 parents bf1da94 + 29234dd commit 15863b2
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Utils/HttpClientRequestService.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function request(
throw new WebpayRequestException($message, $tbkErrorMessage, $responseStatusCode, $request);
}

return json_decode($response->getBody(), true);
return json_decode($response->getBody(), true) ?? [];
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Webpay/Oneclick/MallInscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public function finish(string $token): InscriptionFinishResponse
*/
public function delete(string $tbkUser, string $username): InscriptionDeleteResponse
{
$successDeletedCode = 204;
$payload = [
'tbk_user' => $tbkUser,
'username' => $username,
Expand All @@ -109,7 +110,7 @@ public function delete(string $tbkUser, string $username): InscriptionDeleteResp
$payload
);
} catch (WebpayRequestException $exception) {
if ($exception->getHttpCode() !== 204) {
if ($exception->getHttpCode() !== $successDeletedCode) {
return new InscriptionDeleteResponse(false, $exception->getHttpCode());
}

Expand All @@ -122,6 +123,6 @@ public function delete(string $tbkUser, string $username): InscriptionDeleteResp
);
}

return new InscriptionDeleteResponse(true);
return new InscriptionDeleteResponse(true, $successDeletedCode);
}
}
13 changes: 13 additions & 0 deletions tests/RequestServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,17 @@ public function it_uses_the_base_url_provided_by_the_given_options()
);
(new HttpClientRequestService($httpClientMock))->request('POST', $endpoint, [], $optionsMock);
}

/** @test */
public function it_returns_an_empty_array()
{
$options = new Options('ApiKey', 'commerceCode', Options::ENVIRONMENT_INTEGRATION);
$httpClientMock = $this->createMock(HttpClient::class);
$httpClientMock
->expects($this->once())
->method('request')
->willReturn(new Response(204));
$response = (new HttpClientRequestService($httpClientMock))->request('DELETE', '/inscriptions', [], $options);
$this->assertSame([], $response);
}
}
34 changes: 34 additions & 0 deletions tests/Webpay/OneClick/TransbankOneclickTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Transbank\Webpay\Oneclick\MallTransaction;
use Transbank\Webpay\Oneclick\Responses\InscriptionFinishResponse;
use Transbank\Webpay\Oneclick\Responses\InscriptionStartResponse;
use Transbank\Utils\HttpClientRequestService;
use Transbank\Webpay\Exceptions\WebpayRequestException;

class TransbankOneclickTest extends TestCase
{
Expand Down Expand Up @@ -241,4 +243,36 @@ public function it_configures_transaction_with_options()
$this->assertSame(Options::ENVIRONMENT_PRODUCTION, $transactionOptions->getIntegrationType());
$this->assertSame(Options::BASE_URL_PRODUCTION, $transactionOptions->getApiBaseUrl());
}

/** @test */
public function it_deletes_an_existing_inscription()
{
$requestServiceMock = $this->createMock(HttpClientRequestService::class);
$requestServiceMock
->expects($this->once())
->method('request')
->willReturn([]);
$options = new Options('apiKey', 'commerceCode', Options::ENVIRONMENT_INTEGRATION);
$inscription = new MallInscription($options, $requestServiceMock);
$deleteResponse = $inscription->delete('tbkTestUser', 'useNameTest');

$this->assertTrue($deleteResponse->wasSuccessfull());
$this->assertSame(204, $deleteResponse->getCode());
}

/** @test */
public function it_deletes_an_unexisting_inscription()
{
$requestServiceMock = $this->createMock(HttpClientRequestService::class);
$requestServiceMock
->expects($this->once())
->method('request')
->willThrowException(new WebpayRequestException("Could not obtain a response from Transbank API", null, 404));
$options = new Options('apiKey', 'commerceCode', Options::ENVIRONMENT_INTEGRATION);
$inscription = new MallInscription($options, $requestServiceMock);
$deleteResponse = $inscription->delete('tbkTestUser', 'useNameTest');

$this->assertFalse($deleteResponse->wasSuccessfull());
$this->assertSame(404, $deleteResponse->getCode());
}
}

0 comments on commit 15863b2

Please sign in to comment.