-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from wedevBr/develop
Develop
- Loading branch information
Showing
9 changed files
with
308 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |