From 2614ab7e0a16ddefb892a6ef09e484ac968a1523 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Lucas?= Date: Sat, 25 Jan 2025 13:53:16 -0300 Subject: [PATCH 1/2] PHPAY-55: refactor(asaas charge): refactory asaas charge --- .docker/php/Dockerfile | 7 ++ .husky/commit-msg | 5 +- .husky/pre-commit | 29 ++--- .husky/pre-push | 19 ++-- .vscode/settings.json | 1 + examples/asaas/charges.php | 100 ++++-------------- examples/asaas/webhook.php | 21 +--- examples/helper/termwind-cli.php | 49 +++++++++ examples/helper/termwind.php | 53 ++++++++++ package.json | 16 +-- src/Contracts/GatewayInterface.php | 3 +- src/Gateways/Asaas/AsaasGateway.php | 5 +- .../Asaas/Interface/AsaasGatewayInterface.php | 3 +- .../Asaas/Resources/Charge/Charge.php | 23 ++-- .../Charge/Interface/ChargeInterface.php | 8 ++ src/PHPay.php | 5 +- 16 files changed, 189 insertions(+), 158 deletions(-) mode change 100644 => 100755 .husky/pre-commit create mode 100644 examples/helper/termwind-cli.php create mode 100644 examples/helper/termwind.php diff --git a/.docker/php/Dockerfile b/.docker/php/Dockerfile index b39fe4b..c9d24e3 100644 --- a/.docker/php/Dockerfile +++ b/.docker/php/Dockerfile @@ -1,8 +1,15 @@ FROM php:8.2-cli-alpine +RUN apk add --no-cache \ + curl \ + bash \ + nodejs \ + npm + COPY --from=composer /usr/bin/composer /usr/bin/composer RUN composer self-update + WORKDIR /usr/src/app COPY . . diff --git a/.husky/commit-msg b/.husky/commit-msg index 3e3eea6..4f7ad03 100644 --- a/.husky/commit-msg +++ b/.husky/commit-msg @@ -11,7 +11,8 @@ ISSUE_ID=$(echo "$BRANCH_NAME" | grep -o -E "$REGEX_ISSUE_ID") COMMIT_MESSAGE=$(cat "$1") if [ -z "$ISSUE_ID" ]; then - echo "🛑 ${BRed} Branch name does not follow the expected pattern...${KN}" + php examples/helper/termwind-cli.php error "${BRed} Branch name does not follow the expected pattern...${KN}" + exit 1 fi @@ -21,4 +22,6 @@ case "$COMMIT_MESSAGE" in ;; esac +php examples/helper/termwind-cli.php success "PHPAY-$ISSUE_ID: $COMMIT_MESSAGE" + echo "PHPAY-$ISSUE_ID: $COMMIT_MESSAGE" > "$1" diff --git a/.husky/pre-commit b/.husky/pre-commit old mode 100644 new mode 100755 index 29a0027..41a28b8 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,33 +1,20 @@ -#!/usr/bin/env sh - -echo "=============================================" -echo "Pre Commit Hook - Running pint tests 🧪" -echo "=============================================" - -pint_output=$(./vendor/bin/pint --test 2>&1) +pint_output=$(vendor/bin/pint --test -v 2>&1) if [ $? -ne 0 ]; then - echo "Pint tests failed. Please fix the issues before pushing." + php examples/helper/termwind-cli.php error "Pint tests failed." "$pint_output" + exit 1 fi -echo "🟢 Pint Ok!" - -echo "\n" +php examples/helper/termwind-cli.php success "Pint Ok!" -echo "=============================================" -echo "Pre Commit Hook - Running PHPStan tests 🧪" -echo "=============================================" - -pint_output=$(./vendor/bin/phpstan analyse -l 9 src tests 2>&1) +phpstan=$(vendor/bin/phpstan analyse --level=9 2>&1) if [ $? -ne 0 ]; then - echo "PHPStan tests failed. Please fix the issues before commit." + php examples/helper/termwind-cli.php error "PHPStan tests failed." "$phpstan" exit 1 fi -echo "🟢 PHPStan Ok!" - -echo "\n" - +php examples/helper/termwind-cli.php success "PHPStan Ok!" +exit 0 \ No newline at end of file diff --git a/.husky/pre-push b/.husky/pre-push index 61798db..aefb007 100755 --- a/.husky/pre-push +++ b/.husky/pre-push @@ -1,18 +1,11 @@ -#!/usr/bin/env sh +pest_output=$(./vendor/bin/pest --colors=always --stop-on-failure 2>&1) -# echo "=============================================" -# echo "Pre Push Hook - Running pest tests 🧪" -# echo "=============================================" +if [ $? -ne 0 ]; then + php examples/helper/termwind-cli.php error "Pest tests failed." "$pest_output" -# pest_output=$(./vendor/bin/pest --colors=always --stop-on-failure 2>&1) + exit 1 +fi -# if [ $? -ne 0 ]; then -# echo "🛑 Pest tests failed. Please fix the issues before pushing." -# echo "$pest_output" -# exit 1 -# fi - -# echo "🟢 Pest Ok!" -# echo "\n" +php examples/helper/termwind-cli.php success "Pest Ok!" exit 0 \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index efdb6c2..0add341 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -58,4 +58,5 @@ "window.zoomLevel": 1, "editor.fontVariations": false, "phpunit.phpunit": "vendor/bin/pest", + "jira-plugin.workingProject": "", } \ No newline at end of file diff --git a/examples/asaas/charges.php b/examples/asaas/charges.php index 2682fa2..a2d81eb 100644 --- a/examples/asaas/charges.php +++ b/examples/asaas/charges.php @@ -1,141 +1,83 @@ NAME, - 'cpfCnpj' => CPF_CNPJ, -]; - -$charge = [ - 'billingType' => 'PIX', - 'value' => 100.00, - 'description' => 'Teste de fatura', - 'dueDate' => date('Y-m-d', strtotime('+1 day')), -]; - /** - * @var AsaasGateway $phpay + * @var Charge $phpay */ -$phpay = new PHPay(new AsaasGateway(TOKEN_ASAAS_SANDBOX)); +$phpay = (new PHPay(new AsaasGateway(TOKEN_ASAAS_SANDBOX)))->charge(); /** * create charge - * - * @return array charges */ $phpay - ->charge($charge) + ->setCharge($charge) ->setCustomer($customer) ->create(); /** - * get all charges - * - * @return array charges + * find charge */ -$phpay - ->charge() - ->find($chargeCreated['id']); +$phpay->find($chargeId); /** * get all charges - * - * @return array charges */ -$phpay - ->charge() - ->getAll(); +$phpay->getAll(); /** * get all charges with filters - * - * @return array charges */ $phpay - ->charge() - ->setQueryParams(['limit' => 5]) + ->setQueryParams(['limit' => 2]) ->getAll(); /** * update charge - * - * @return array charge */ -$phpay - ->charge() - ->update($chargeId, [ - 'description' => 'Teste de fatura atualizado', - ]); +$phpay->update($chargeId, $data); /** * destroy charge - * - * @return array bool */ -$phpay - ->charge() - ->destroy($chargeId); +$phpay->destroy($chargeId); /** * restore charge - * - * @return array charge */ -$phpay - ->charge() - ->restore($chargeId); +$phpay->restore($chargeId); /** - * get charge status - * - * @return array charge status + * get status charge */ -$phpay - ->charge() - ->getStatus($chargeId); +$phpay->getStatus($chargeId); /** * get digitable line charge - * - * @return string */ -$phpay - ->charge() - ->getDigitableLine($chargeId); +$phpay->getDigitableLine($chargeId); /** * get qrcode charge - * - * @return string */ -$qrcode = $phpay - ->charge() - ->getQrCodePix($chargeId); +$phpay->getQrCodePix($chargeId); /** * confirm receipt charge - * - * @return array charges */ -$confirmed = $phpay - ->charge() - ->confirmReceipt($chargeId, [ - 'paymentDate' => date('Y-m-d'), - 'value' => 100.00, - 'notifyCustomer' => true, - ]); +$phpay->confirmReceipt($chargeId, [ + 'paymentDate' => date('Y-m-d'), + 'value' => 100.00, + 'notifyCustomer' => true, +]); /** * undo confirm receipt - * - * @return array charges */ -$phpay - ->charge() - ->undoConfirmReceipt($chargeId); +$phpay->undoConfirmReceipt($chargeId); diff --git a/examples/asaas/webhook.php b/examples/asaas/webhook.php index a9999fc..f50371e 100644 --- a/examples/asaas/webhook.php +++ b/examples/asaas/webhook.php @@ -12,25 +12,6 @@ 'cpfCnpj' => CPF_CNPJ, ]; -$webhook = [ - 'name' => 'Webhook de Teste', - 'url' => 'https://sixtec.com.br/webhook', - 'email' => 'mariolucasdev@gmail.com', - 'enabled' => true, - 'interrupted' => false, - 'apiVersion' => 3, - 'authToken' => '123456', - 'sendType' => 'SEQUENTIALLY', - 'events' => [ - 'PAYMENT_CREATED', - 'PAYMENT_UPDATED', - 'PAYMENT_CONFIRMED', - 'PAYMENT_RECEIVED', - 'PAYMENT_OVERDUE', - 'PAYMENT_DELETED', - ], -]; - /** * initialize phpay * @@ -45,7 +26,7 @@ * @see available fields in https://docs.asaas.com/reference/criar-novo-webhook */ $phpay - ->webhook($webhook) + ->webhook(WEBHOOK) ->create(); /** diff --git a/examples/helper/termwind-cli.php b/examples/helper/termwind-cli.php new file mode 100644 index 0000000..bbf74a3 --- /dev/null +++ b/examples/helper/termwind-cli.php @@ -0,0 +1,49 @@ + 1) { + $type = $argv[1]; + $message = $argv[2]; + $error = $argv[3] ?? null; + + renderCLI($message, $type, $error); +} + +/** + * render success message + */ +function renderCLI( + string $message, + string $type, + ?string $error +): void { + switch ($type) { + case 'success': + $icon = '✔️'; + + break; + case 'error': + $icon = '❌'; + + break; + default: + $icon = '⚠️'; + + break; + } + + $contentError = $error ? "{$error}" : ''; + + render(<< + $icon + {$message} + {$contentError} + + HTML); +} diff --git a/examples/helper/termwind.php b/examples/helper/termwind.php new file mode 100644 index 0000000..24889f8 --- /dev/null +++ b/examples/helper/termwind.php @@ -0,0 +1,53 @@ +{$content}" : ''; + + render(<< + {$gateway} + {$message} + {$aditionalContent} + + HTML); +} + +/** + * render error message + */ +function renderError(string $message, string $gateway, ?string $content): void +{ + $aditionalContent = $content ? "
{$content}" : ''; + + render(<< + {$gateway} + {$message} + {$aditionalContent} + + HTML); +} + +/** + * render info message + */ +function renderMessage(string $message, string $gateway, ?string $content): void +{ + $aditionalContent = $content ? "
{$content}" : ''; + + render(<< + {$gateway} + {$message} + {$aditionalContent} + + HTML); +} diff --git a/package.json b/package.json index c51f063..f55f875 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,9 @@ { - "devDependencies": { - "husky": "^9.1.7" - }, - "scripts": { - "prepare": "husky", - "postinstall": "husky install" - } -} \ No newline at end of file + "devDependencies": { + "husky": "^9.1.7" + }, + "scripts": { + "prepare": "husky", + "postinstall": "husky install" + } +} diff --git a/src/Contracts/GatewayInterface.php b/src/Contracts/GatewayInterface.php index 69d296e..425c56d 100644 --- a/src/Contracts/GatewayInterface.php +++ b/src/Contracts/GatewayInterface.php @@ -15,10 +15,9 @@ public function customer(array $customer = []): object; /** * get resource charge from gateway. * - * @param array $charge * @return object */ - public function charge(array $charge = []): object; + public function charge(): object; /** * get resource webhook from gateway. diff --git a/src/Gateways/Asaas/AsaasGateway.php b/src/Gateways/Asaas/AsaasGateway.php index 26b3dad..cd17db5 100644 --- a/src/Gateways/Asaas/AsaasGateway.php +++ b/src/Gateways/Asaas/AsaasGateway.php @@ -51,12 +51,11 @@ public function customer(array $customer = []): Customer /** * charge * - * @param array $charge * @return Charge */ - public function charge(array $charge = []): Charge + public function charge(): Charge { - return new Charge($this->token, $charge, $this->sandbox); + return new Charge($this->token, $this->sandbox); } /** diff --git a/src/Gateways/Asaas/Interface/AsaasGatewayInterface.php b/src/Gateways/Asaas/Interface/AsaasGatewayInterface.php index 373a522..626c3e2 100644 --- a/src/Gateways/Asaas/Interface/AsaasGatewayInterface.php +++ b/src/Gateways/Asaas/Interface/AsaasGatewayInterface.php @@ -21,10 +21,9 @@ public function customer(array $customer = []): Customer; /** * get resource charge from gateway. * - * @param array $charge * @return Charge */ - public function charge(array $charge = []): Charge; + public function charge(): Charge; /** * get resource webhook from gateway. diff --git a/src/Gateways/Asaas/Resources/Charge/Charge.php b/src/Gateways/Asaas/Resources/Charge/Charge.php index ffc8ed8..89d5b7e 100644 --- a/src/Gateways/Asaas/Resources/Charge/Charge.php +++ b/src/Gateways/Asaas/Resources/Charge/Charge.php @@ -25,21 +25,32 @@ class Charge implements ChargeInterface */ private array $queryParams = []; + /** + * @var array + */ + private array $charge = []; + /** * construct - * - * @param array $charge */ public function __construct( private string $token, - private array $charge = [], private bool $sandbox = true, ) { $this->client = $this->clientAsaasBoot(); + } - if (!empty($charge)) { - $this->charge = $charge; - } + /** + * set charge + * + * @param array $charge + * @return ChargeInterface + */ + public function setCharge(array $charge): ChargeInterface + { + $this->charge = $charge; + + return $this; } /** diff --git a/src/Gateways/Asaas/Resources/Charge/Interface/ChargeInterface.php b/src/Gateways/Asaas/Resources/Charge/Interface/ChargeInterface.php index c4f8548..36c4a31 100644 --- a/src/Gateways/Asaas/Resources/Charge/Interface/ChargeInterface.php +++ b/src/Gateways/Asaas/Resources/Charge/Interface/ChargeInterface.php @@ -59,6 +59,14 @@ public function restore(string $id): array; */ public function setCustomer(array $customer): ChargeInterface; + /** + * set charge + * + * @param array $charge + * @return ChargeInterface + */ + public function setCharge(array $charge): ChargeInterface; + /** * set query params * diff --git a/src/PHPay.php b/src/PHPay.php index 3e14a96..3e7f111 100644 --- a/src/PHPay.php +++ b/src/PHPay.php @@ -47,12 +47,11 @@ public function customer(array $customer = []): object /** * get resource charge from gateway. * - * @param array $charge * @return object */ - public function charge(array $charge = []): object + public function charge(): object { - return $this->gateway->charge($charge); + return $this->gateway->charge(); } /** From be7bd41809a144017a19b961135ba120573fe36e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Lucas?= Date: Sat, 25 Jan 2025 19:31:22 -0300 Subject: [PATCH 2/2] PHPAY-55: doc(charge): adding updated doc charges --- README.md | 84 ++++++++++++++++++++++++++++++++----- tests/Feature/PHPayTest.php | 14 ------- tests/Unit/ExampleTest.php | 1 - 3 files changed, 73 insertions(+), 26 deletions(-) delete mode 100644 tests/Feature/PHPayTest.php delete mode 100644 tests/Unit/ExampleTest.php diff --git a/README.md b/README.md index 31eb5e3..5ecaf5c 100644 --- a/README.md +++ b/README.md @@ -17,31 +17,93 @@ composer require phpay-io/phpay ## ⚙️ Como usar o PHPay? -### Asaas - ```php /** + * instance with gateway inject * @var AsaasGateway $phpay */ -$phpay = new PHPay(new AsaasGateway(TOKEN_ASAAS_SANDBOX)); +$phpay = (new PHPay(new AsaasGateway(TOKEN_ASAAS_SANDBOX))); ``` -### Efí +### Cobranças ```php /** - * @var EfiGateway $phpay + * instance with gateway inject and resource call + * + * @var Charge $phpay */ -$phpay = new PHPay(new EfiGateway(CLIENT_ID, CLIENT_SECRET)); -``` - -## Gerando uma Cobrança +$phpay = (new PHPay(new AsaasGateway(TOKEN_ASAAS_SANDBOX)))->charge(); -```php +/** + * create charge + */ $phpay - ->charge($charge) + ->setCharge($charge) ->setCustomer($customer) ->create(); + +/** + * find charge + */ +$phpay->find($chargeId); + +/** + * get all charges + */ +$phpay->getAll(); + +/** + * get all charges with filters + */ +$phpay + ->setQueryParams(['limit' => 2]) + ->getAll(); + +/** + * update charge + */ +$phpay->update($chargeId, $data); + +/** + * destroy charge + */ +$phpay->destroy($chargeId); + +/** + * restore charge + */ +$phpay->restore($chargeId); + +/** + * get status charge + */ +$phpay->getStatus($chargeId); + +/** + * get digitable line charge + */ +$phpay->getDigitableLine($chargeId); + +/** + * get qrcode charge + */ +$phpay->getQrCodePix($chargeId); + +/** + * confirm receipt charge + */ +$phpay->confirmReceipt($chargeId, [ + 'paymentDate' => date('Y-m-d'), + 'value' => 100.00, + 'notifyCustomer' => true, +]); + +/** + * undo confirm receipt + */ +$phpay->undoConfirmReceipt($chargeId); + ``` ## 📝 Roadmap diff --git a/tests/Feature/PHPayTest.php b/tests/Feature/PHPayTest.php deleted file mode 100644 index 163010f..0000000 --- a/tests/Feature/PHPayTest.php +++ /dev/null @@ -1,14 +0,0 @@ -toBeInstanceOf(PHPay::class) - ->toHaveProperty('gateway') - ->toBeInstanceOf(GatewayInterface::class); -}); diff --git a/tests/Unit/ExampleTest.php b/tests/Unit/ExampleTest.php deleted file mode 100644 index b3d9bbc..0000000 --- a/tests/Unit/ExampleTest.php +++ /dev/null @@ -1 +0,0 @@ -