Skip to content

Commit

Permalink
Merge pull request #57 from wedevBr/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
adeildo-jr authored Mar 25, 2024
2 parents cf61e88 + e0a2ca0 commit d913097
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 26 deletions.
6 changes: 4 additions & 2 deletions src/Clients/CelcoinKyc.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Validator;
use WeDevBr\Celcoin\Common\CelcoinBaseApi;
use WeDevBr\Celcoin\Interfaces\Attachable;
use WeDevBr\Celcoin\Rules\KYC\CreateKycRule;
use WeDevBr\Celcoin\Types\KYC\CreateKyc;

Expand All @@ -15,13 +16,14 @@ class CelcoinKyc extends CelcoinBaseApi
/**
* @throws RequestException
*/
public function createKyc(CreateKyc $data): array
public function createKyc(CreateKyc $data, Attachable $attachable = null): array
{
$body = Validator::validate($data->toArray(), CreateKycRule::rules());

return $this->post(
self::CREATE_KYC_ENDPOINT,
$body
$body,
$attachable
);
}
}
7 changes: 6 additions & 1 deletion src/Common/CelcoinBaseApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use WeDevBr\Celcoin\Auth\Auth;
use WeDevBr\Celcoin\Interfaces\Attachable;

class CelcoinBaseApi
{
Expand Down Expand Up @@ -94,7 +95,7 @@ public function get(string $endpoint, array|string|null $query = null, $response
/**
* @throws RequestException
*/
public function post(string $endpoint, array $body = [])
public function post(string $endpoint, array $body = [], Attachable $attachment = null)
{
$token = $this->getToken() ?? $this->auth->getToken();
$request = Http::withToken($token)
Expand All @@ -113,6 +114,10 @@ public function post(string $endpoint, array $body = [])
}
}

if ($attachment) {
$request->attach($attachment->getField(), $attachment->getContents(), $attachment->getFileName());
}

return $request->post($this->getFinalUrl($endpoint), $body)
->throw()
->json();
Expand Down
12 changes: 12 additions & 0 deletions src/Interfaces/Attachable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace WeDevBr\Celcoin\Interfaces;

interface Attachable
{
public function getField(): string;

public function getContents();

public function getFileName();
}
6 changes: 3 additions & 3 deletions src/Rules/KYC/CreateKycRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public static function rules(): array
return [
'documentnumber' => ['required', 'digits:11,14'],
'filetype' => ['required', Rule::enum(KycDocumentEnum::class)],
'front' => ['required', 'file'],
'verse' => ['sometimes', 'file'],
'cnpj' => ['sometimes', 'same:documentnumber', 'digits:11,14'],
'front' => ['sometimes',],
'verse' => ['sometimes',],
'cnpj' => ['sometimes', 'digits:11,14'],
];
}
}
16 changes: 1 addition & 15 deletions src/Types/KYC/CreateKyc.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace WeDevBr\Celcoin\Types\KYC;

use WeDevBr\Celcoin\Enums\KycDocumentEnum;
use WeDevBr\Celcoin\Interfaces\Attachable;
use WeDevBr\Celcoin\Types\Data;

class CreateKyc extends Data
Expand All @@ -13,30 +14,15 @@ class CreateKyc extends Data

public KycDocumentEnum $filetype;

public KycDocument $front;

public ?KycDocument $verse = null;

public function __construct(array $data = [])
{
if (empty($data['cnpj']) && strlen($data['documentnumber']) === 14) {
$data['cnpj'] = $data['documentnumber'];
}
parent::__construct($data);
}

public function toArray(): array
{
$array = parent::toArray();

if (!empty($array['front'])) {
$array['front'] = $this->front->file;
}

if (!empty($array['verse'])) {
$array['verse'] = $this->verse->file;
}

return array_filter($array);
}
}
14 changes: 11 additions & 3 deletions src/Types/KYC/KycDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@
namespace WeDevBr\Celcoin\Types\KYC;

use Illuminate\Http\File;
use WeDevBr\Celcoin\Interfaces\Attachable;
use WeDevBr\Celcoin\Types\Data;

class KycDocument extends Data
class KycDocument extends Data implements Attachable
{
public string $fileName;
public string $contents;
public ?File $file;

public function __construct(?File $file)
public string $field;

public function __construct(?File $file, string $field)
{
$data = [];
if (!empty($file)) {
$data['contents'] = $file->getContent();
$data['fileName'] = $file->getFilename();
$data['file'] = $file;
$data['field'] = $field;
}

parent::__construct($data);
Expand All @@ -32,4 +35,9 @@ public function getFileName(): string
{
return $this->fileName;
}

public function getField(): string
{
return $this->field;
}
}
9 changes: 7 additions & 2 deletions tests/Integration/KYC/CelcoinSendKycTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,16 @@ public function testSendKycValidationRules()
{
Http::fake([
config('celcoin.login_url') => GlobalStubs::loginResponse(),
sprintf(
'%s%s',
config('celcoin.api_url'),
CelcoinKyc::CREATE_KYC_ENDPOINT
) => static::successResponse(),
]);

$kyc = new CelcoinKyc();
$body = static::getKycBody();
unset($body['front']);
unset($body['documentnumber']);
$this->expectException(ValidationException::class);
$kyc->createKyc(new CreateKyc($body));
}
Expand Down Expand Up @@ -132,6 +137,6 @@ public static function internalErrorResponse(): PromiseInterface

public function getFile(string $path = null): KycDocument
{
return new KycDocument(new File($path));
return new KycDocument(new File($path), 'verse');
}
}

0 comments on commit d913097

Please sign in to comment.