Skip to content

Commit

Permalink
Merge pull request #36 from wedevBr/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
adeildo-jr authored Nov 6, 2023
2 parents ebb01e3 + c155c62 commit 5ba04fa
Show file tree
Hide file tree
Showing 9 changed files with 308 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Celcoin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use WeDevBr\Celcoin\Clients\CelcoinAssistant;
use WeDevBr\Celcoin\Clients\CelcoinBAAS;
use WeDevBr\Celcoin\Clients\CelcoinBAASBillPayment;
use WeDevBr\Celcoin\Clients\CelcoinBAASBillet;
use WeDevBr\Celcoin\Clients\CelcoinBAASPIX;
use WeDevBr\Celcoin\Clients\CelcoinBAASTED;
Expand Down Expand Up @@ -262,6 +263,15 @@ public static function clientKyc(?string $mtlsPassphrase = null): CelcoinKyc
return new CelcoinKyc($mtlsPassphrase);
}

/**
* @param string|null $mtlsPassphrase
* @return CelcoinBAASBillPayment
*/
public static function clientBAASBillPayment(?string $mtlsPassphrase = null): CelcoinBAASBillPayment
{
return new CelcoinBAASBillPayment($mtlsPassphrase);
}

/**
* @param string|null $mtlsPassphrase
* @return CelcoinBAASBillet
Expand Down
49 changes: 49 additions & 0 deletions src/Clients/CelcoinBAASBillPayment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace WeDevBr\Celcoin\Clients;

use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Validator;
use WeDevBr\Celcoin\Common\CelcoinBaseApi;
use WeDevBr\Celcoin\Rules\BAAS\BillPaymentRule;
use WeDevBr\Celcoin\Rules\BAAS\GetPaymentStatusRule;
use WeDevBr\Celcoin\Types\BAAS\BillPayment;
use WeDevBr\Celcoin\Types\BAAS\GetPaymentStatusRequest;

/**
* Class CelcoinBAASBillPayment
* Essa funcionalidade permite realizar pagamentos das mais diversas modalidades, incluindo contas de água, luz, gás,
* telefone, internet, multas, tributos e boletos de contas BAAS.
* @package WeDevBr\Celcoin
*/
class CelcoinBAASBillPayment extends CelcoinBaseApi
{

const MAKE_PAYMENT_ENDPOINT = '/api-integration-billpayment-webservice/v1/billpayment';
const GET_PAYMENT_STATUS = self::MAKE_PAYMENT_ENDPOINT . '/status';

/**
* @throws RequestException
*/
public function makePayment(BillPayment $data): mixed
{
$body = Validator::validate($data->toArray(), BillPaymentRule::rules());
return $this->post(
self::MAKE_PAYMENT_ENDPOINT,
$body
);
}

/**
* @throws RequestException
*/
public function getPaymentStatus(GetPaymentStatusRequest $paymentStatusRequest): mixed
{
$query = Validator::validate($paymentStatusRequest->toArray(), GetPaymentStatusRule::rules());

return $this->get(
self::GET_PAYMENT_STATUS,
$query
);
}
}
26 changes: 26 additions & 0 deletions src/Rules/BAAS/BillPaymentRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace WeDevBr\Celcoin\Rules\BAAS;

use Illuminate\Validation\Rule;
use WeDevBr\Celcoin\Enums\BarCodeTypeEnum;

class BillPaymentRule
{
public static function rules(): array
{
return [
'clientRequestId' => ['required', 'string', 'max:20'],
'amount' => ['required', 'decimal:0,2', 'min: 0.01'],
'account' => ['required', 'string', 'numeric'],
'transactionIdAuthorize' => ['required', 'integer'],
'tags' => ['sometimes', 'nullable', 'array'],
'tags.*.key' => ['required_if:tags', 'string'],
'tags.*.value' => ['required_if:tags', 'string'],
"barCodeInfo" => ['required', 'array'],
"barCodeInfo.type" => ['required', Rule::in(array_column(BarCodeTypeEnum::cases(), 'value'))],
"barCodeInfo.digitable" => ['nullable', 'string'],
"barCodeInfo.barCode" => ['nullable', 'string'],
];
}
}
14 changes: 14 additions & 0 deletions src/Rules/BAAS/GetPaymentStatusRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace WeDevBr\Celcoin\Rules\BAAS;

class GetPaymentStatusRule
{
public static function rules(): array
{
return [
'ClientRequestId' => ['sometimes', 'nullable', 'string'],
'Id' => ['sometimes', 'nullable', 'string'],
];
}
}
29 changes: 29 additions & 0 deletions src/Types/BAAS/BillPayment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace WeDevBr\Celcoin\Types\BAAS;

use WeDevBr\Celcoin\Types\BillPayments\BarCode;
use WeDevBr\Celcoin\Types\Data;

class BillPayment extends Data
{
public string $clientRequestId;

public float $amount;

public string $account;

public int $transactionIdAuthorize;

/**
* @var array<int, Tag>|null
*/
public ?array $tags;

public BarCode $barCodeInfo;

public function __construct(array $data = [])
{
parent::__construct($data);
}
}
26 changes: 26 additions & 0 deletions src/Types/BAAS/GetPaymentStatusRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace WeDevBr\Celcoin\Types\BAAS;

use WeDevBr\Celcoin\Types\Data;

class GetPaymentStatusRequest extends Data
{
public ?string $clientRequestId = null;

public ?string $id = null;
private ?string $ClientRequestId = null;
private ?string $Id = null;

public function __construct(array $data = [])
{
parent::__construct($data);
}

public function toArray(): array
{
$this->Id = $this?->id;
$this->ClientRequestId = $this?->clientRequestId;
return collect(parent::toArray())->only(['Id', 'ClientRequestId'])->toArray(); // TODO: Change the autogenerated stub
}
}
17 changes: 17 additions & 0 deletions src/Types/BAAS/Tag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace WeDevBr\Celcoin\Types\BAAS;

use WeDevBr\Celcoin\Types\Data;

class Tag extends Data
{
public ?string $key;

public ?string $data;

public function __construct(array $data = [])
{
parent::__construct($data);
}
}
7 changes: 7 additions & 0 deletions tests/Feature/CelcoinClientInstancesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use WeDevBr\Celcoin\Celcoin;
use WeDevBr\Celcoin\Clients\CelcoinAssistant;
use WeDevBr\Celcoin\Clients\CelcoinBAAS;
use WeDevBr\Celcoin\Clients\CelcoinBAASBillPayment;
use WeDevBr\Celcoin\Clients\CelcoinBAASPIX;
use WeDevBr\Celcoin\Clients\CelcoinBAASTED;
use WeDevBr\Celcoin\Clients\CelcoinBAASWebhooks;
Expand Down Expand Up @@ -154,4 +155,10 @@ public function testSuccessCreateInstanceKyc()
$instance = Celcoin::clientKyc();
$this->assertInstanceOf(CelcoinKyc::class, $instance);
}

public function testSuccessCreateBaasBillPayment()
{
$instance = Celcoin::clientBAASBillPayment();
$this->assertInstanceOf(CelcoinBAASBillPayment::class, $instance);
}
}
130 changes: 130 additions & 0 deletions tests/Integration/BAAS/BillPaymentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

namespace WeDevBr\Celcoin\Tests\Integration\BAAS;

use GuzzleHttp\Promise\PromiseInterface;
use Illuminate\Support\Facades\Http;
use WeDevBr\Celcoin\Clients\CelcoinBAASBillPayment;
use WeDevBr\Celcoin\Enums\BarCodeTypeEnum;
use WeDevBr\Celcoin\Tests\GlobalStubs;
use WeDevBr\Celcoin\Tests\TestCase;
use WeDevBr\Celcoin\Types\BAAS\BillPayment;
use WeDevBr\Celcoin\Types\BAAS\GetPaymentStatusRequest;
use WeDevBr\Celcoin\Types\BillPayments\BarCode;

class BillPaymentTest extends TestCase
{
public function testSuccess(): void
{
Http::fake(
[
config('celcoin.login_url') => GlobalStubs::loginResponse(),
sprintf(
'%s%s',
config('api_url'),
CelcoinBAASBillPayment::MAKE_PAYMENT_ENDPOINT,
) => self::makePaymentStubSuccess(),
sprintf(
'%s%s%s',
config('api_url'),
CelcoinBAASBillPayment::GET_PAYMENT_STATUS,
'*'
) => self::getPaymentStubSuccess(),
],
);

$client = new CelcoinBAASBillPayment();

$billet = new BillPayment();

$billet->account = '12345';
$billet->clientRequestId = '5555';
$billet->barCodeInfo = new BarCode(['type' => BarCodeTypeEnum::COMPENSATION_FORM, 'digitable' => '23793381286008301352856000063307789840000150000']);
$billet->transactionIdAuthorize = 1234;
$billet->amount = '59.9';

$result = $client->makePayment($billet);


$this->assertEquals('PROCESSING', $result['status']);
$this->assertEquals('ce9b8d9b-0617-42e1-b500-80bf9d8154cf', $result['body']['id']);
}

public function testGetBilletSuccess(): void
{
Http::fake(
[
config('celcoin.login_url') => GlobalStubs::loginResponse(),
sprintf(
'%s%s',
config('api_url'),
CelcoinBAASBillPayment::MAKE_PAYMENT_ENDPOINT,
) => self::makePaymentStubSuccess(),
sprintf(
'%s%s%s',
config('api_url'),
CelcoinBAASBillPayment::GET_PAYMENT_STATUS,
'*'
) => self::getPaymentStubSuccess(),
],
);

$client = new CelcoinBAASBillPayment();

$query = new GetPaymentStatusRequest(['id' => 'ce9b8d9b-0617-42e1-b500-80bf9d8154cf']);

$result = $client->getPaymentStatus($query);

$this->assertEquals('CONFIRMED', $result['status']);
$this->assertEquals('10a806a1-267g-5803-93e3-fc215a8b156f', $result['body']['id']);
}

public static function makePaymentStubSuccess(): PromiseInterface
{
return Http::response([
"body" => [
"id" => "ce9b8d9b-0617-42e1-b500-80bf9d8154cf",
"clientRequestId" => "5555",
"amount" => 59.9,
"transactionIdAuthorize" => 1234,
"tags" => [
[
"key" => "PaymentType",
"value" => "Contas Internas",
],
],
"barCodeInfo" => [
"digitable" => "23793381286008301352856000063307789840000150000",
],
],
"status" => "PROCESSING",
"version" => "1.0.0",
], 200);
}

public static function getPaymentStubSuccess(): PromiseInterface
{
return Http::response([
"body" => [
"id" => "10a806a1-267g-5803-93e3-fc215a8b156f",
"clientRequestId" => "clientRequest01",
"account" => 321,
"amount" => 5,
"transactionIdAuthorize" => 123,
"hasOccurrence" => false,
"tags" => [
[
"key" => "PaymentType",
"value" => "Contas Internas",
],
],
"barCodeInfo" => [
"digitable" => "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
],
"paymentDate" => "2023-09-01T00:00:00Z",
],
"status" => "CONFIRMED",
"version" => "1.1.0",
], 200);
}
}

0 comments on commit 5ba04fa

Please sign in to comment.