Skip to content

Commit

Permalink
Merge pull request #75 from wedevBr/feature/NewFacadeClients
Browse files Browse the repository at this point in the history
Feature/new facade clients
  • Loading branch information
adeildo-jr authored Aug 29, 2024
2 parents ff09070 + 8415237 commit 585e0f4
Show file tree
Hide file tree
Showing 28 changed files with 145 additions and 110 deletions.
11 changes: 11 additions & 0 deletions src/Celcoin.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use WeDevBr\Celcoin\Clients\CelcoinDDAUser;
use WeDevBr\Celcoin\Clients\CelcoinDDAWebhooks;
use WeDevBr\Celcoin\Clients\CelcoinElectronicTransactions;
use WeDevBr\Celcoin\Clients\CelcoinInternationalTopups;
use WeDevBr\Celcoin\Clients\CelcoinKyc;
use WeDevBr\Celcoin\Clients\CelcoinPIXCOB;
use WeDevBr\Celcoin\Clients\CelcoinPIXCOBV;
Expand Down Expand Up @@ -169,4 +170,14 @@ public static function clientBAASBillet(): CelcoinBAASBillet
{
return new CelcoinBAASBillet();
}

public static function clientBAASWebhook(): CelcoinBAASWebhooks
{
return new CelcoinBAASWebhooks();
}

public static function clientInternationalTopup(): CelcoinInternationalTopups
{
return new CelcoinInternationalTopups();
}
}
7 changes: 6 additions & 1 deletion src/Clients/CelcoinBAASBillet.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class CelcoinBAASBillet extends CelcoinBaseApi
{
public const BILLET_URL = '/api-integration-baas-webservice/v1/charge';
public const BILLET_URL = '/baas/v2/Charge';

/**
* @throws RequestException
Expand Down Expand Up @@ -42,4 +42,9 @@ public function cancelBillet($transactionId)
{
return $this->delete(sprintf('%s/%s', self::BILLET_URL, $transactionId));
}

public function printBillet(string $billetId)
{
return $this->get(sprintf('%s/pdf/%s', self::BILLET_URL, $billetId));
}
}
17 changes: 8 additions & 9 deletions src/Clients/CelcoinBAASPIX.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ public function getParticipant(?string $ISPB = null, ?string $name = null)
{
return $this->get(
self::GET_PARTICIPANT_ENDPOINT,
[
array_filter([
'ISPB' => $ISPB,
'Name' => $name,
]
])
);
}

Expand All @@ -89,17 +89,17 @@ public function statusPix(?string $id = null, ?string $clientCode = null, ?strin
{
return $this->get(
self::STATUS_PIX_ENDPOINT,
[
array_filter([
'id' => $id,
'clientCode' => $clientCode,
'endToEndId' => $endToEndId,
]
])
);
}

public function registerPixKey(RegisterPixKey $data)
{
$body = Validator::validate($data->toArray(), BAASRegisterPixKey::rules());
$body = Validator::validate(array_filter($data->toArray()), BAASRegisterPixKey::rules());

return $this->post(
self::REGISTER_PIX_KEY_ENDPOINT,
Expand Down Expand Up @@ -138,11 +138,11 @@ public function statusRefundPix(?string $id = null, ?string $clientCode = null,
{
return $this->get(
self::STATUS_REFUND_PIX_ENDPOINT,
[
array_filter([
'id' => $id,
'clientCode' => $clientCode,
'returnIdentification' => $returnIdentification,
]
])
);
}

Expand Down Expand Up @@ -193,7 +193,7 @@ public function claimConsult(string $claimId): ?array
$validatedClaim = Validator::validate(['claimId' => $claimId], ['claimId' => ['string', 'uuid']]);

return $this->get(
self::CLAIM_CANCEL.'/'.$validatedClaim['claimId']
self::CLAIM_DICT.'/'.$validatedClaim['claimId']
);
}

Expand All @@ -218,5 +218,4 @@ public function claimList(array $params = []): ?array
$validatedParams
);
}

}
6 changes: 5 additions & 1 deletion src/Clients/CelcoinPIXCOBV.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
use WeDevBr\Celcoin\Common\CelcoinBaseApi;
use WeDevBr\Celcoin\Rules\PIX\COBVCreate;
use WeDevBr\Celcoin\Rules\PIX\COBVGet;
Expand Down Expand Up @@ -62,7 +63,10 @@ final public function payloadCOBVPIX(string $url): ?array
Validator::validate(compact('url'), COBVPayload::rules());

return $this->get(
sprintf(self::PAYLOAD_COBV_PIX, urlencode($url))
sprintf(
self::PAYLOAD_COBV_PIX,
urlencode(Str::replace('https://', '', $url))
)
);
}

Expand Down
35 changes: 17 additions & 18 deletions src/Common/CelcoinBaseApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,8 @@ public function get(string $endpoint, array|string|null $query = null, $response
{
$token = $this->getToken() ?? $this->auth->getToken();
$request = Http::withToken($token)
->withHeaders([
'accept' => 'application/json',
'content-type' => 'application/json',
]);
->asJson()
->acceptJson();

if ($this->mtlsCert && $this->mtlsKey && $this->mtlsPassphrase) {
$request = $this->setRequestMtls($request);
Expand All @@ -100,10 +98,8 @@ public function post(string $endpoint, array $body = [], ?Attachable $attachment
{
$token = $this->getToken() ?? $this->auth->getToken();
$request = Http::withToken($token)
->withHeaders([
'accept' => 'application/json',
'content-type' => 'application/json',
]);
->asJson()
->acceptJson();

if ($this->mtlsCert && $this->mtlsKey && $this->mtlsPassphrase) {
$request = $this->setRequestMtls($request);
Expand All @@ -112,11 +108,18 @@ public function post(string $endpoint, array $body = [], ?Attachable $attachment
foreach ($body as $field => $document) {
if ($document instanceof File) {
$request->attach($field, $document->getContent(), $document->getFileName());
$request->contentType('multipart/form-data; boundary=*');
}
}

if ($attachment) {
$request->attach($attachment->getField(), $attachment->getContents(), $attachment->getFileName());
$request->attach(
$attachment->getField(),
$attachment->getContents(),
$attachment->getFileName(),
$attachment->getHeaders()
);
$request->contentType('multipart/form-data; boundary=*');
}

return $request->post($this->getFinalUrl($endpoint), $body)
Expand All @@ -133,10 +136,8 @@ public function put(
): mixed {
$token = $this->getToken() ?? $this->auth->getToken();
$request = Http::withToken($token)
->withHeaders([
'accept' => 'application/json',
'content-type' => 'application/json',
]);
->asJson()
->acceptJson();

if ($this->mtlsCert && $this->mtlsKey && $this->mtlsPassphrase) {
$request = $this->setRequestMtls($request);
Expand All @@ -158,11 +159,9 @@ public function patch(
$body_format = $asJson ? 'json' : 'form_params';
$token = $this->getToken() ?? $this->auth->getToken();
$request = Http::withToken($token)
->bodyFormat($body_format)
->withHeaders([
'accept' => 'application/json',
'content-type' => 'application/json',
]);
->asJson()
->acceptJson()
->bodyFormat($body_format);

if ($this->mtlsCert && $this->mtlsKey && $this->mtlsPassphrase) {
$request = $this->setRequestMtls($request);
Expand Down
4 changes: 4 additions & 0 deletions src/Facades/CelcoinFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
* @uses Celcoin::clientPIXReverse
* @uses Celcoin::clientPixWebhooks
* @uses Celcoin::clientKyc
* @uses Celcoin::clientBAASWebhook
* @uses Celcoin::clientInternationalTopup
* @uses Celcoin::clientBAASBillPayment
* @uses Celcoin::clientBAASBillet
*/
class CelcoinFacade extends Facade
{
Expand Down
2 changes: 2 additions & 0 deletions src/Interfaces/Attachable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ public function getField(): string;
public function getContents();

public function getFileName();

public function getHeaders(): array;
}
12 changes: 5 additions & 7 deletions src/Rules/BAAS/Billet.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ public static function rules(): array
{
return [
'externalId' => ['required'],
'merchantCategoryCode' => ['sometimes', 'required'],
'expirationAfterPayment' => ['required', 'numeric', 'min:1'],
'duedate' => ['required', 'date'],
'dueDate' => ['required', 'date', 'after:yesterday'],
'amount' => ['required', 'decimal:0,2'],
'key' => ['required'],
'debtor' => ['required', 'array'],
'debtor.name' => ['required', 'string', 'max: 25'],
'debtor.document' => ['required'],
Expand All @@ -26,10 +24,10 @@ public static function rules(): array
'receiver.document' => ['required'],
'receiver.account' => ['required', 'numeric'],
'instructions' => ['sometimes', 'array', 'nullable'],
'instructions.discount' => ['required_with:instructions', 'array'],
'instructions.discount.amount' => ['required_with:discount', 'decimal:0,2'],
'instructions.discount.modality' => ['required_with:discount'],
'instructions.discount.limitDate' => ['required_with:discount', 'date'],
'instructions.discount' => ['sometimes', 'nullable'],
'instructions.discount.amount' => ['required_with:instructions.discount.limitDate', 'decimal:0,2'],
'instructions.discount.modality' => ['required_with:discount.modality'],
'instructions.discount.limitDate' => ['required_with:discount.amount', 'date'],
'instructions.fine' => ['nullable', 'decimal:0,2'],
'instructions.interest' => ['nullable', 'decimal:0,2'],
'split' => ['sometimes', 'array'],
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/BAAS/RegisterPixKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static function rules()
return [
'account' => ['required', 'string'],
'keyType' => ['required', Rule::enum(PixKeyTypeEnum::class)],
'key' => ['required_unless:keyType,EVP', 'string'],
'key' => ['required_unless:keyType,EVP', 'string', 'nullable'],
];
}
}
2 changes: 1 addition & 1 deletion src/Rules/BAAS/TEDTransfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static function rules()
'creditParty.accountType' => ['required', 'string'],
'creditParty.personType' => ['required', 'string'],
'clientFinality' => ['required', 'string'],
'description' => ['some', 'required_if:clientFinality,'.ClientFinalityEnum::OTHERS->value, 'string'],
'description' => ['sometimes', 'required_if:clientFinality,'.ClientFinalityEnum::OTHERS->value, 'string'],
];
}
}
6 changes: 1 addition & 5 deletions src/Types/BAAS/Billet.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@ class Billet extends Data
{
public string $externalId;

public ?string $merchantCategoryCode;

public int $expirationAfterPayment;

public string $duedate;
public string $dueDate;

public float $amount;

public ?string $key;

public BilletDebtor $debtor;

public BilletReceiver $receiver;
Expand Down
5 changes: 5 additions & 0 deletions src/Types/KYC/KycDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ public function getField(): string
{
return $this->field;
}

public function getHeaders(): array
{
return ['Content-Type' => $this->file->getMimeType()];
}
}
6 changes: 6 additions & 0 deletions tests/Feature/CelcoinClientInstancesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,10 @@ public function testSuccessCreateBaasBillPayment()
$instance = Celcoin::clientBAASBillPayment();
$this->assertInstanceOf(CelcoinBAASBillPayment::class, $instance);
}

public function testSuccessCreteBaasWebhooks()
{
$instance = Celcoin::clientBAASWebhooks();
$this->assertInstanceOf(CelcoinBAASWebhooks::class, $instance);
}
}
4 changes: 1 addition & 3 deletions tests/Integration/BAAS/CelcoinBASSBilletTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ public static function billetBodyRequest(): Billet
return new Billet([
'externalId' => 'externalId1',
'expirationAfterPayment' => 1,
'merchantCatagoryCode' => '0000',
'duedate' => '2023-12-30T00:00:00.0000000',
'dueDate' => now()->format('Y-m-d'),
'amount' => 12.5,
'key' => '[email protected]',
'debtor' => new BilletDebtor([
'name' => 'João teste de teste',
'document' => '12345678910',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use WeDevBr\Celcoin\Tests\GlobalStubs;
use WeDevBr\Celcoin\Tests\TestCase;
use WeDevBr\Celcoin\Types\PIX\ClaimAnswer;

use function PHPUnit\Framework\assertEquals;

class PixKeyCancelConfirmTest extends TestCase
Expand Down
3 changes: 1 addition & 2 deletions tests/Integration/BAAS/PixClaim/PixKeyClaimConfirmTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
use WeDevBr\Celcoin\Tests\GlobalStubs;
use WeDevBr\Celcoin\Tests\TestCase;
use WeDevBr\Celcoin\Types\PIX\ClaimAnswer;

use function PHPUnit\Framework\assertEquals;

class PixKeyClaimConfirmTest extends TestCase
{
use WithFaker;

/**
*/
public function testClaimPixKey()
{
Http::fake(
Expand Down
1 change: 1 addition & 0 deletions tests/Integration/BAAS/PixClaim/PixKeyClaimConsultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use WeDevBr\Celcoin\Clients\CelcoinBAASPIX;
use WeDevBr\Celcoin\Tests\GlobalStubs;
use WeDevBr\Celcoin\Tests\TestCase;

use function PHPUnit\Framework\assertEquals;

class PixKeyClaimConsultTest extends TestCase
Expand Down
Loading

0 comments on commit 585e0f4

Please sign in to comment.