Skip to content

Commit

Permalink
add more changes :D
Browse files Browse the repository at this point in the history
  • Loading branch information
abr4xas committed Dec 22, 2024
1 parent 385544a commit a8a7a82
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 98 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
/docs export-ignore
/UPGRADING.md export-ignore
/help export-ignore
/.phpunit.cache export-ignore
9 changes: 1 addition & 8 deletions .github/workflows/run-tests-pest.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
name: Tests

on:
push:
paths:
- '**.php'
- '.github/workflows/run-tests.yml'
- 'phpunit.xml.dist'
- 'composer.json'
- 'composer.lock'
on: [pull_request]

jobs:
test:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/update-changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: main
ref: master

- name: Update Changelog
uses: stefanzweifel/changelog-updater-action@v1
Expand All @@ -27,6 +27,6 @@ jobs:
- name: Commit updated CHANGELOG
uses: stefanzweifel/git-auto-commit-action@v5
with:
branch: main
branch: master
commit_message: Update CHANGELOG
file_pattern: CHANGELOG.md
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ psalm.xml
vendor
.php-cs-fixer.cache
test.php
.phpunit.cache
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ require 'vendor/autoload.php';
use \Instapago\Instapago\Api;
use \Instapago\Instapago\Exceptions\{
InstapagoException,
AuthException,
BankRejectException,
InvalidInputException,
TimeoutException,
InstapagoAuthException,
InstapagoBankRejectException,
InstapagoInvalidInputException,
InstapagoTimeoutException,
ValidationException,
GenericException,
};
Expand All @@ -66,19 +66,19 @@ try{

echo $e->getMessage(); // manejar el error

}catch(AuthException $e){
}catch(InstapagoAuthException $e){

echo $e->getMessage(); // manejar el error

}catch(BankRejectException $e){
}catch(InstapagoBankRejectException $e){

echo $e->getMessage(); // manejar el error

}catch(InvalidInputException $e){
}catch(InstapagoInvalidInputException $e){

echo $e->getMessage(); // manejar el error

}catch(TimeoutException $e){
}catch(InstapagoTimeoutException $e){

echo $e->getMessage(); // manejar el error

Expand Down
65 changes: 33 additions & 32 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
use GuzzleHttp\Client as Client;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\GuzzleException;
use Instapago\Instapago\Exceptions\AuthException;
use Instapago\Instapago\Exceptions\BankRejectException;
use Instapago\Instapago\Exceptions\InstapagoAuthException;
use Instapago\Instapago\Exceptions\InstapagoBankRejectException;
use Instapago\Instapago\Exceptions\GenericException;
use Instapago\Instapago\Exceptions\InstapagoException;
use Instapago\Instapago\Exceptions\InvalidInputException;
use Instapago\Instapago\Exceptions\TimeoutException;
use Instapago\Instapago\Exceptions\InstapagoInvalidInputException;
use Instapago\Instapago\Exceptions\InstapagoTimeoutException;
use Instapago\Instapago\Exceptions\ValidationException;

/**
* Clase para la pasarela de pagos Instapago.
Expand Down Expand Up @@ -42,7 +43,7 @@ public function directPayment(array $fields): array | string
{
try {
return $this->payment('2', $fields);
} catch (AuthException | BankRejectException | GenericException | InstapagoException | InvalidInputException | TimeoutException | Exceptions\ValidationException | GuzzleException $e) {
} catch (InstapagoAuthException | InstapagoBankRejectException | GenericException | InstapagoException | InstapagoInvalidInputException | InstapagoTimeoutException | ValidationException | GuzzleException $e) {
return $e->getMessage();
}
}
Expand All @@ -54,7 +55,7 @@ public function reservePayment($fields): array | string
{
try {
return $this->payment('1', $fields);
} catch (AuthException | BankRejectException | GenericException | InstapagoException | InvalidInputException | TimeoutException | Exceptions\ValidationException | GuzzleException $e) {
} catch (InstapagoAuthException | InstapagoBankRejectException | GenericException | InstapagoException | InstapagoInvalidInputException | InstapagoTimeoutException | ValidationException | GuzzleException $e) {
return $e->getMessage();
}
}
Expand All @@ -64,14 +65,14 @@ public function reservePayment($fields): array | string
* Este método funciona para procesar un bloqueo o pre-autorización
* para así procesarla y hacer el cobro respectivo.
*
* @throws Exceptions\ValidationException
* @throws ValidationException
* @throws GenericException
* @throws GuzzleException
* @throws TimeoutException
* @throws InstapagoTimeoutException
*/
public function completePayment(array $fields): array | string
{
(new Validator())->validate($fields);
(new Validator())->setValidations('release')->validate($fields);

$fields = [
'KeyID' => $this->keyId, //required
Expand All @@ -84,8 +85,8 @@ public function completePayment(array $fields): array | string

try {
return $this->checkResponseCode($obj);
} catch (AuthException | BankRejectException | GenericException | InstapagoException | InvalidInputException $e) {
return $e->getMessage();
} catch (InstapagoAuthException | InstapagoBankRejectException | GenericException | InstapagoException | InstapagoInvalidInputException $e) {
return $e;
}
}

Expand All @@ -94,14 +95,14 @@ public function completePayment(array $fields): array | string
* Este método funciona para procesar un bloqueo o pre-autorización
* para así procesarla y hacer el cobro respectivo.
*
* @throws Exceptions\ValidationException
* @throws ValidationException
* @throws GenericException
* @throws GuzzleException
* @throws TimeoutException
* @throws InstapagoTimeoutException
*/
public function query(string $id_pago): array | string
{
(new Validator())->validate([
(new Validator())->setValidations('query')->validate([
'id' => $id_pago,
]);

Expand All @@ -115,7 +116,7 @@ public function query(string $id_pago): array | string

try {
return $this->checkResponseCode($obj);
} catch (AuthException | BankRejectException | GenericException | InstapagoException | InvalidInputException $e) {
} catch (InstapagoAuthException | InstapagoBankRejectException | GenericException | InstapagoException | InstapagoInvalidInputException $e) {
return $e->getMessage();
}
}
Expand All @@ -124,11 +125,11 @@ public function query(string $id_pago): array | string
* Cancelar Pago
* Este método funciona para cancelar un pago previamente procesado.
*
* @throws Exceptions\ValidationException
* @throws ValidationException
*/
public function cancel(string $id_pago): array | string
{
(new Validator())->validate([
(new Validator())->setValidations('query')->validate([
'id' => $id_pago,
]);

Expand All @@ -140,7 +141,7 @@ public function cancel(string $id_pago): array | string

try {
return $this->curlTransaction('payment', $fields, 'DELETE');
} catch (GuzzleException | GenericException | TimeoutException $e) {
} catch (GuzzleException | GenericException | InstapagoTimeoutException $e) {
return $e->getMessage();
}
}
Expand All @@ -150,17 +151,17 @@ public function cancel(string $id_pago): array | string
*
* @param string $type tipo de pago ('1' o '0')
*
* @throws AuthException
* @throws BankRejectException
* @throws Exceptions\ValidationException
* @throws InstapagoAuthException
* @throws InstapagoBankRejectException
* @throws ValidationException
* @throws GenericException
* @throws InstapagoException
* @throws InvalidInputException
* @throws TimeoutException|GuzzleException
* @throws InstapagoInvalidInputException
* @throws InstapagoTimeoutException|GuzzleException
*/
private function payment(string $type, array $fields): array
{
(new Validator())->validate($fields);
(new Validator())->setValidations('payment')->validate($fields);

$fields = [
'KeyID' => $this->keyId,
Expand Down Expand Up @@ -190,7 +191,7 @@ private function payment(string $type, array $fields): array
* @return array resultados de la transacción
*
* @throws GenericException
* @throws TimeoutException
* @throws InstapagoTimeoutException
* @throws GuzzleException
*/
private function curlTransaction(string $url, array $fields, string $method): array
Expand All @@ -213,7 +214,7 @@ private function curlTransaction(string $url, array $fields, string $method): ar

return json_decode($body, true);
} catch (ConnectException $e) {
throw new TimeoutException('Cannot connect to api.instapago.com');
throw new InstapagoTimeoutException('Cannot connect to api.instapago.com');
}
}

Expand All @@ -223,18 +224,18 @@ private function curlTransaction(string $url, array $fields, string $method): ar
* @param array $obj datos de la consulta
* @return array datos de transacción
*
* @throws AuthException
* @throws BankRejectException
* @throws InstapagoAuthException
* @throws InstapagoBankRejectException
* @throws GenericException
* @throws InstapagoException
* @throws InvalidInputException
* @throws InstapagoInvalidInputException
*/
private function checkResponseCode(array $obj): array
{
return match ($obj['code']) {
'400' => throw new InvalidInputException('Datos inválidos.'),
'401' => throw new AuthException('Error de autenticación.'),
'403' => throw new BankRejectException('Pago rechazado por el banco.'),
'400' => throw new InstapagoInvalidInputException('Datos inválidos.'),
'401' => throw new InstapagoAuthException('Error de autenticación.'),
'403' => throw new InstapagoBankRejectException('Pago rechazado por el banco.'),
'500' => throw new InstapagoException('Error interno del servidor.'),
'503' => throw new InstapagoException('Error al procesar los parámetros de entrada.'),
'201' => $this->getResponse($obj),
Expand Down
12 changes: 0 additions & 12 deletions src/Exceptions/AuthException.php

This file was deleted.

12 changes: 0 additions & 12 deletions src/Exceptions/BankRejectException.php

This file was deleted.

12 changes: 12 additions & 0 deletions src/Exceptions/InstapagoAuthException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Instapago\Instapago\Exceptions;

use Exception;

/**
* Custom InstapagoAuthException.
*/
class InstapagoAuthException extends Exception
{
}
12 changes: 12 additions & 0 deletions src/Exceptions/InstapagoBankRejectException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Instapago\Instapago\Exceptions;

use Exception;

/**
* Custom InstapagoBankRejectException.
*/
class InstapagoBankRejectException extends Exception
{
}
12 changes: 12 additions & 0 deletions src/Exceptions/InstapagoInvalidInputException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Instapago\Instapago\Exceptions;

use Exception;

/**
* Custom InstapagoInvalidInputException.
*/
class InstapagoInvalidInputException extends Exception
{
}
12 changes: 12 additions & 0 deletions src/Exceptions/InstapagoTimeoutException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Instapago\Instapago\Exceptions;

use Exception;

/**
* Custom InstapagoTimeoutException.
*/
class InstapagoTimeoutException extends Exception
{
}
12 changes: 0 additions & 12 deletions src/Exceptions/InvalidInputException.php

This file was deleted.

12 changes: 0 additions & 12 deletions src/Exceptions/TimeoutException.php

This file was deleted.

14 changes: 14 additions & 0 deletions tests/ApiInstapagoTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Instapago\Instapago\Api;
use Instapago\Instapago\Exceptions\InstapagoException;

beforeEach(function () {
$this->api = new Api('1E488391-7934-4301-9F8E-17DC99AB49B3', '691f77db9d62c0f2fe191ce69ed9bb41');
Expand Down Expand Up @@ -76,3 +77,16 @@

expect($payment['message'])->toBe('El pago ha sido anulado');
})->depends('it can create a direct payment');

it('throws an exception if keys are missing', function () {
new Api('', 'publicKey');
})->throws(InstapagoException::class);

it('throws an exception if public key is missing', function () {
new Api('key', '');
})->throws(InstapagoException::class);

it('throws validation exception for missing fields in direct payment', function () {
$payment = $this->api->directPayment([]);
expect($payment)->toBe('{"amount":"Invalid value for amount","card_holder":"Invalid value for card_holder","card_holder_id":"Invalid value for card_holder_id","card_number":"Invalid value for card_number","cvc":"Invalid value for cvc","expiration":"Invalid value for expiration","ip":"Invalid value for ip"}');
});

0 comments on commit a8a7a82

Please sign in to comment.