From b016ce6d4cb9b3668445b3868f0b904701c590ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Lucas?= Date: Tue, 17 Dec 2024 13:01:10 -0300 Subject: [PATCH 1/5] PHPAY-36: feat: adding create webhook --- examples/asaas/webhook.php | 51 +++++++++ src/Contracts/GatewayInterface.php | 4 +- src/Gateways/Asaas/AsaasGateway.php | 12 ++ .../Asaas/Resources/Charge/Charge.php | 6 + .../Charge/Enum/WebhookEventsEnum.php | 34 ++++++ .../Resources/Interfaces/InvoiceInterface.php | 13 --- src/Gateways/Asaas/Resources/Invoice.php | 107 ------------------ .../Webhook/Interface/WebhookInterface.php | 8 ++ .../Asaas/Resources/Webhook/Webhook.php | 49 ++++++++ 9 files changed, 163 insertions(+), 121 deletions(-) create mode 100644 examples/asaas/webhook.php create mode 100644 src/Gateways/Asaas/Resources/Charge/Enum/WebhookEventsEnum.php delete mode 100644 src/Gateways/Asaas/Resources/Interfaces/InvoiceInterface.php delete mode 100644 src/Gateways/Asaas/Resources/Invoice.php create mode 100644 src/Gateways/Asaas/Resources/Webhook/Interface/WebhookInterface.php create mode 100644 src/Gateways/Asaas/Resources/Webhook/Webhook.php diff --git a/examples/asaas/webhook.php b/examples/asaas/webhook.php new file mode 100644 index 0000000..bb945d5 --- /dev/null +++ b/examples/asaas/webhook.php @@ -0,0 +1,51 @@ + NAME, + '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 + */ +$phpay = PHPay::getInstance(new AsaasGateway(TOKEN_ASAAS_SANDBOX)) + ->getGateway(); + +/** + * store asaas customer + * + * @param array $customer + * @return array + * @see available fields in https://docs.asaas.com/reference/criar-novo-cliente + */ +$webhook = $phpay + ->webhook($webhook) + ->create(); + +print_r($webhook); diff --git a/src/Contracts/GatewayInterface.php b/src/Contracts/GatewayInterface.php index 9279805..cd88ba4 100644 --- a/src/Contracts/GatewayInterface.php +++ b/src/Contracts/GatewayInterface.php @@ -2,11 +2,13 @@ namespace PHPay\Contracts; -use Asaas\Resources\Charge\Charge as AsaasCharge; use Asaas\Resources\Customer\Customer as AsaasCustomer; +use Asaas\Resources\Charge\Charge as AsaasCharge; +use Asaas\Resources\Webhook\Webhook as AsaasWebhook; interface GatewayInterface { public function customer(array $customer = []): AsaasCustomer; public function charge(array $charge = []): AsaasCharge; + public function webhook(array $webhook = []): AsaasWebhook; } diff --git a/src/Gateways/Asaas/AsaasGateway.php b/src/Gateways/Asaas/AsaasGateway.php index a09b7ca..6597f8b 100644 --- a/src/Gateways/Asaas/AsaasGateway.php +++ b/src/Gateways/Asaas/AsaasGateway.php @@ -4,6 +4,7 @@ use Asaas\Resources\Charge\Charge; use Asaas\Resources\Customer\Customer; +use Asaas\Resources\Webhook\Webhook; use GuzzleHttp\Client; use PHPay\Contracts\GatewayInterface; @@ -46,8 +47,19 @@ public function customer(array $customer = []): Customer return $this->customer; } + /** + * charge + * + * @param array $charge + * @return Charge + */ public function charge(array $charge = []): Charge { return new Charge($charge, $this->token, $this->sandbox); } + + public function webhook(array $webhook = []): Webhook + { + return new Webhook($webhook, $this->token, $this->sandbox); + } } diff --git a/src/Gateways/Asaas/Resources/Charge/Charge.php b/src/Gateways/Asaas/Resources/Charge/Charge.php index 92fbd65..199fabb 100644 --- a/src/Gateways/Asaas/Resources/Charge/Charge.php +++ b/src/Gateways/Asaas/Resources/Charge/Charge.php @@ -183,6 +183,12 @@ public function confirmReceipt(string $id, array $data): array return $this->post("payments/{$id}/receiveInCash", $data); } + /** + * undo confirm receipt + * + * @param string $id + * @return array + */ public function undoConfirmReceipt(string $id): array { return $this->post("payments/{$id}/undoReceivedInCash"); diff --git a/src/Gateways/Asaas/Resources/Charge/Enum/WebhookEventsEnum.php b/src/Gateways/Asaas/Resources/Charge/Enum/WebhookEventsEnum.php new file mode 100644 index 0000000..17cf29d --- /dev/null +++ b/src/Gateways/Asaas/Resources/Charge/Enum/WebhookEventsEnum.php @@ -0,0 +1,34 @@ +invoice = $invoice; - $this->gateway = $gateway; - $this->client = $gateway->client; - } - - /** - * get all invoices - * - * @return array - */ - public function all(): array - { - $invoices = Http::asaas() - ->get(env('ASSAS_PAYMENTS') . '/?' . http_build_query($this->filters)) - ->json(); - - $this->invoices = $invoices['data']; - - return $this->invoices; - } - - /** - * create invoice - * - * @return self|AsaasExceptions - */ - public function create(): self|AsaasExceptions - { - $this->invoice['customer'] = $this->gateway->client['id']; - - AsaasInvoiceRequest::validate( - $this->client, - $this->invoice - ); - - $invoice = Http::asaas() - ->post(env('ASSAS_PAYMENTS'), $this->invoice) - ->json(); - - $this->invoice = $invoice; - - return $this; - } - - /** - * set filters - * - * - * @param array $filters - * @return self - * @throws AsaasExceptions - * @see PHPay\Gateways\Asaas\Enums\InvoiceFiltersEnum - */ - public function with(array $filters): self - { - foreach ($filters as $key => $value) { - if (!in_array($key, InvoiceFiltersEnum::cases())) { - throw new AsaasExceptions('filter not found' . $key . ' not found'); - } - } - - $this->filters = $filters; - - return $this; - } - - /** - * get qr code from invoice - * - * @return array - */ - public function qrCodePix(): array - { - $this->qrcode = Http::asaas() - ->get(str_replace('{id}', $this->invoice['id'], env('ASAAS_PAYMENT_QRCODE'))) - ->json(); - - return $this->qrcode; - } -} diff --git a/src/Gateways/Asaas/Resources/Webhook/Interface/WebhookInterface.php b/src/Gateways/Asaas/Resources/Webhook/Interface/WebhookInterface.php new file mode 100644 index 0000000..2eadfc0 --- /dev/null +++ b/src/Gateways/Asaas/Resources/Webhook/Interface/WebhookInterface.php @@ -0,0 +1,8 @@ +client = $this->clientAsaasBoot(); + + if (!empty($webhook)) { + $this->webhook = $webhook; + } + + return $this; + } + + /** + * construct + * + * @param array $webhook + */ + public function create(array $webhook = []): array + { + return $this->post('webhooks', $this->webhook); + } +} From 68eb05172890880df3bcec19bbdec04267291416 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Lucas?= Date: Tue, 17 Dec 2024 13:06:17 -0300 Subject: [PATCH 2/5] PHPAY-36: feat: adding webhooks list --- examples/asaas/webhook.php | 16 ++++++++++++---- .../Webhook/Interface/WebhookInterface.php | 1 + src/Gateways/Asaas/Resources/Webhook/Webhook.php | 11 +++++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/examples/asaas/webhook.php b/examples/asaas/webhook.php index bb945d5..db4b375 100644 --- a/examples/asaas/webhook.php +++ b/examples/asaas/webhook.php @@ -38,14 +38,22 @@ ->getGateway(); /** - * store asaas customer + * store a new webhook * - * @param array $customer + * @param array $webhook * @return array - * @see available fields in https://docs.asaas.com/reference/criar-novo-cliente + * @see available fields in https://docs.asaas.com/reference/criar-novo-webhook */ $webhook = $phpay ->webhook($webhook) ->create(); -print_r($webhook); +/** + * get all webhooks + * + * @param array $customer + * @return array + */ +$webhooks = $phpay + ->webhook() + ->getAll(); diff --git a/src/Gateways/Asaas/Resources/Webhook/Interface/WebhookInterface.php b/src/Gateways/Asaas/Resources/Webhook/Interface/WebhookInterface.php index 2eadfc0..756ab94 100644 --- a/src/Gateways/Asaas/Resources/Webhook/Interface/WebhookInterface.php +++ b/src/Gateways/Asaas/Resources/Webhook/Interface/WebhookInterface.php @@ -5,4 +5,5 @@ interface WebhookInterface { public function create(array $webhook = []): array; + public function getAll(): array; } diff --git a/src/Gateways/Asaas/Resources/Webhook/Webhook.php b/src/Gateways/Asaas/Resources/Webhook/Webhook.php index 0120b6e..cfc5397 100644 --- a/src/Gateways/Asaas/Resources/Webhook/Webhook.php +++ b/src/Gateways/Asaas/Resources/Webhook/Webhook.php @@ -46,4 +46,15 @@ public function create(array $webhook = []): array { return $this->post('webhooks', $this->webhook); } + + /** + * get all webhooks + * + * @param string $id + * @return array + */ + public function getAll(): array + { + return $this->get("webhooks"); + } } From 0cc330ce70db4d562974a742e28da1aaa88f7bc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Lucas?= Date: Tue, 17 Dec 2024 13:14:24 -0300 Subject: [PATCH 3/5] PHPAY-36: feat: update webhook --- examples/asaas/webhook.php | 26 +++++++++++++++++-- .../Webhook/Interface/WebhookInterface.php | 2 ++ .../Asaas/Resources/Webhook/Webhook.php | 24 ++++++++++++++++- 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/examples/asaas/webhook.php b/examples/asaas/webhook.php index db4b375..ce9ecec 100644 --- a/examples/asaas/webhook.php +++ b/examples/asaas/webhook.php @@ -40,7 +40,6 @@ /** * store a new webhook * - * @param array $webhook * @return array * @see available fields in https://docs.asaas.com/reference/criar-novo-webhook */ @@ -51,9 +50,32 @@ /** * get all webhooks * - * @param array $customer * @return array */ $webhooks = $phpay ->webhook() ->getAll(); + +/** + * find a webhook + * + * @return array + */ +$webhook = $phpay + ->webhook() + ->find('84c3c34b-9d23-44a3-95b2-d232c3f06dba'); + +/** + * update a webhook + * + * @return array + * @see available fields in https://docs.asaas.com/reference/atualizar-webhook-existente + */ +$webhookUpdated = $phpay + ->webhook() + ->update('84c3c34b-9d23-44a3-95b2-d232c3f06dba', [ + 'name' => 'Webhook de Teste Atualizado', + 'url' => 'https://sixtec.com.br/webhook/atualizado' + ]); + +print_r($webhookUpdated); diff --git a/src/Gateways/Asaas/Resources/Webhook/Interface/WebhookInterface.php b/src/Gateways/Asaas/Resources/Webhook/Interface/WebhookInterface.php index 756ab94..b51a140 100644 --- a/src/Gateways/Asaas/Resources/Webhook/Interface/WebhookInterface.php +++ b/src/Gateways/Asaas/Resources/Webhook/Interface/WebhookInterface.php @@ -6,4 +6,6 @@ interface WebhookInterface { public function create(array $webhook = []): array; public function getAll(): array; + public function find(string $id): array; + public function update(string $id, array $webhook = []): array; } diff --git a/src/Gateways/Asaas/Resources/Webhook/Webhook.php b/src/Gateways/Asaas/Resources/Webhook/Webhook.php index cfc5397..ae6853e 100644 --- a/src/Gateways/Asaas/Resources/Webhook/Webhook.php +++ b/src/Gateways/Asaas/Resources/Webhook/Webhook.php @@ -50,11 +50,33 @@ public function create(array $webhook = []): array /** * get all webhooks * - * @param string $id * @return array */ public function getAll(): array { return $this->get("webhooks"); } + + /** + * get webhook by id + * + * @param string $id + * @return array + */ + public function find(string $id): array + { + return $this->get("webhooks/{$id}"); + } + + /** + * update webhook by id + * + * @param string $id + * @param array $webhook + * @return array + */ + public function update(string $id, array $webhook = []): array + { + return $this->put("webhooks/{$id}", $webhook); + } } From fa74f00753202616445012f3a7cb1f9eda13e011 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Lucas?= Date: Tue, 17 Dec 2024 13:18:32 -0300 Subject: [PATCH 4/5] PHPAY-36: feat: delete webhook --- examples/asaas/webhook.php | 9 ++++++++- .../Resources/Webhook/Interface/WebhookInterface.php | 1 + src/Gateways/Asaas/Resources/Webhook/Webhook.php | 11 +++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/examples/asaas/webhook.php b/examples/asaas/webhook.php index ce9ecec..81f334f 100644 --- a/examples/asaas/webhook.php +++ b/examples/asaas/webhook.php @@ -78,4 +78,11 @@ 'url' => 'https://sixtec.com.br/webhook/atualizado' ]); -print_r($webhookUpdated); +/** + * delete a webhook + * + * @return bool + */ +$webhookDeleted = $phpay + ->webhook() + ->destroy('84c3c34b-9d23-44a3-95b2-d232c3f06dba'); diff --git a/src/Gateways/Asaas/Resources/Webhook/Interface/WebhookInterface.php b/src/Gateways/Asaas/Resources/Webhook/Interface/WebhookInterface.php index b51a140..d966caa 100644 --- a/src/Gateways/Asaas/Resources/Webhook/Interface/WebhookInterface.php +++ b/src/Gateways/Asaas/Resources/Webhook/Interface/WebhookInterface.php @@ -8,4 +8,5 @@ public function create(array $webhook = []): array; public function getAll(): array; public function find(string $id): array; public function update(string $id, array $webhook = []): array; + public function destroy(string $id): bool; } diff --git a/src/Gateways/Asaas/Resources/Webhook/Webhook.php b/src/Gateways/Asaas/Resources/Webhook/Webhook.php index ae6853e..867393f 100644 --- a/src/Gateways/Asaas/Resources/Webhook/Webhook.php +++ b/src/Gateways/Asaas/Resources/Webhook/Webhook.php @@ -79,4 +79,15 @@ public function update(string $id, array $webhook = []): array { return $this->put("webhooks/{$id}", $webhook); } + + /** + * delete webhook by id + * + * @param string $id + * @return array + */ + public function destroy(string $id): bool + { + return $this->delete("webhooks/{$id}"); + } } From 18abb92ea0a058c47730cf7e6be4b1941e8c1690 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Lucas?= Date: Tue, 17 Dec 2024 13:19:33 -0300 Subject: [PATCH 5/5] PHPAY-36: feat: delete webhook --- examples/asaas/webhook.php | 20 +++---- src/Contracts/GatewayInterface.php | 2 +- .../Charge/Enum/WebhookEventsEnum.php | 52 +++++++++---------- 3 files changed, 37 insertions(+), 37 deletions(-) diff --git a/examples/asaas/webhook.php b/examples/asaas/webhook.php index 81f334f..57522ca 100644 --- a/examples/asaas/webhook.php +++ b/examples/asaas/webhook.php @@ -13,22 +13,22 @@ ]; $webhook = [ - 'name' => 'Webhook de Teste', - 'url' => 'https://sixtec.com.br/webhook', - 'email' => 'mariolucasdev@gmail.com', - 'enabled' => true, + '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' => [ + 'apiVersion' => 3, + 'authToken' => '123456', + 'sendType' => 'SEQUENTIALLY', + 'events' => [ 'PAYMENT_CREATED', 'PAYMENT_UPDATED', 'PAYMENT_CONFIRMED', 'PAYMENT_RECEIVED', 'PAYMENT_OVERDUE', 'PAYMENT_DELETED', - ] + ], ]; /** @@ -75,7 +75,7 @@ ->webhook() ->update('84c3c34b-9d23-44a3-95b2-d232c3f06dba', [ 'name' => 'Webhook de Teste Atualizado', - 'url' => 'https://sixtec.com.br/webhook/atualizado' + 'url' => 'https://sixtec.com.br/webhook/atualizado', ]); /** diff --git a/src/Contracts/GatewayInterface.php b/src/Contracts/GatewayInterface.php index cd88ba4..32908d2 100644 --- a/src/Contracts/GatewayInterface.php +++ b/src/Contracts/GatewayInterface.php @@ -2,8 +2,8 @@ namespace PHPay\Contracts; -use Asaas\Resources\Customer\Customer as AsaasCustomer; use Asaas\Resources\Charge\Charge as AsaasCharge; +use Asaas\Resources\Customer\Customer as AsaasCustomer; use Asaas\Resources\Webhook\Webhook as AsaasWebhook; interface GatewayInterface diff --git a/src/Gateways/Asaas/Resources/Charge/Enum/WebhookEventsEnum.php b/src/Gateways/Asaas/Resources/Charge/Enum/WebhookEventsEnum.php index 17cf29d..ba23dba 100644 --- a/src/Gateways/Asaas/Resources/Charge/Enum/WebhookEventsEnum.php +++ b/src/Gateways/Asaas/Resources/Charge/Enum/WebhookEventsEnum.php @@ -4,31 +4,31 @@ enum WebhookEventsEnum: string { - case PAYMENT_CREATED = 'PAYMENT_CREATED'; - case PAYMENT_UPDATED = 'PAYMENT_UPDATED'; - case PAYMENT_CONFIRMED = 'PAYMENT_CONFIRMED'; - case PAYMENT_RECEIVED = 'PAYMENT_RECEIVED'; - case PAYMENT_OVERDUE = 'PAYMENT_OVERDUE'; - case PAYMENT_DELETED = 'PAYMENT_DELETED'; - case PAYMENT_AWAITING_RISK_ANALYSIS = 'PAYMENT_AWAITING_RISK_ANALYSIS'; - case PAYMENT_APPROVED_BY_RISK_ANALYSIS = 'PAYMENT_APPROVED_BY_RISK_ANALYSIS'; - case PAYMENT_REPROVED_BY_RISK_ANALYSIS = 'PAYMENT_REPROVED_BY_RISK_ANALYSIS'; - case PAYMENT_AUTHORIZED = 'PAYMENT_AUTHORIZED'; - case PAYMENT_CREDIT_CARD_CAPTURE_REFUSED = 'PAYMENT_CREDIT_CARD_CAPTURE_REFUSED'; - case PAYMENT_ANTICIPATED = 'PAYMENT_ANTICIPATED'; - case PAYMENT_RESTORED = 'PAYMENT_RESTORED'; - case PAYMENT_REFUNDED = 'PAYMENT_REFUNDED'; - case PAYMENT_PARTIALLY_REFUNDED = 'PAYMENT_PARTIALLY_REFUNDED'; - case PAYMENT_REFUND_IN_PROGRESS = 'PAYMENT_REFUND_IN_PROGRESS'; - case PAYMENT_RECEIVED_IN_CASH_UNDONE = 'PAYMENT_RECEIVED_IN_CASH_UNDONE'; - case PAYMENT_CHARGEBACK_REQUESTED = 'PAYMENT_CHARGEBACK_REQUESTED'; - case PAYMENT_CHARGEBACK_DISPUTE = 'PAYMENT_CHARGEBACK_DISPUTE'; - case PAYMENT_AWAITING_CHARGEBACK_REVERSAL = 'PAYMENT_AWAITING_CHARGEBACK_REVERSAL'; - case PAYMENT_DUNNING_RECEIVED = 'PAYMENT_DUNNING_RECEIVED'; - case PAYMENT_DUNNING_REQUESTED = 'PAYMENT_DUNNING_REQUESTED'; - case PAYMENT_BANK_SLIP_VIEWED = 'PAYMENT_BANK_SLIP_VIEWED'; - case PAYMENT_CHECKOUT_VIEWED = 'PAYMENT_CHECKOUT_VIEWED'; - case PAYMENT_SPLIT_CANCELLED = 'PAYMENT_SPLIT_CANCELLED'; - case PAYMENT_SPLIT_DIVERGENCE_BLOCK = 'PAYMENT_SPLIT_DIVERGENCE_BLOCK'; + case PAYMENT_CREATED = 'PAYMENT_CREATED'; + case PAYMENT_UPDATED = 'PAYMENT_UPDATED'; + case PAYMENT_CONFIRMED = 'PAYMENT_CONFIRMED'; + case PAYMENT_RECEIVED = 'PAYMENT_RECEIVED'; + case PAYMENT_OVERDUE = 'PAYMENT_OVERDUE'; + case PAYMENT_DELETED = 'PAYMENT_DELETED'; + case PAYMENT_AWAITING_RISK_ANALYSIS = 'PAYMENT_AWAITING_RISK_ANALYSIS'; + case PAYMENT_APPROVED_BY_RISK_ANALYSIS = 'PAYMENT_APPROVED_BY_RISK_ANALYSIS'; + case PAYMENT_REPROVED_BY_RISK_ANALYSIS = 'PAYMENT_REPROVED_BY_RISK_ANALYSIS'; + case PAYMENT_AUTHORIZED = 'PAYMENT_AUTHORIZED'; + case PAYMENT_CREDIT_CARD_CAPTURE_REFUSED = 'PAYMENT_CREDIT_CARD_CAPTURE_REFUSED'; + case PAYMENT_ANTICIPATED = 'PAYMENT_ANTICIPATED'; + case PAYMENT_RESTORED = 'PAYMENT_RESTORED'; + case PAYMENT_REFUNDED = 'PAYMENT_REFUNDED'; + case PAYMENT_PARTIALLY_REFUNDED = 'PAYMENT_PARTIALLY_REFUNDED'; + case PAYMENT_REFUND_IN_PROGRESS = 'PAYMENT_REFUND_IN_PROGRESS'; + case PAYMENT_RECEIVED_IN_CASH_UNDONE = 'PAYMENT_RECEIVED_IN_CASH_UNDONE'; + case PAYMENT_CHARGEBACK_REQUESTED = 'PAYMENT_CHARGEBACK_REQUESTED'; + case PAYMENT_CHARGEBACK_DISPUTE = 'PAYMENT_CHARGEBACK_DISPUTE'; + case PAYMENT_AWAITING_CHARGEBACK_REVERSAL = 'PAYMENT_AWAITING_CHARGEBACK_REVERSAL'; + case PAYMENT_DUNNING_RECEIVED = 'PAYMENT_DUNNING_RECEIVED'; + case PAYMENT_DUNNING_REQUESTED = 'PAYMENT_DUNNING_REQUESTED'; + case PAYMENT_BANK_SLIP_VIEWED = 'PAYMENT_BANK_SLIP_VIEWED'; + case PAYMENT_CHECKOUT_VIEWED = 'PAYMENT_CHECKOUT_VIEWED'; + case PAYMENT_SPLIT_CANCELLED = 'PAYMENT_SPLIT_CANCELLED'; + case PAYMENT_SPLIT_DIVERGENCE_BLOCK = 'PAYMENT_SPLIT_DIVERGENCE_BLOCK'; case PAYMENT_SPLIT_DIVERGENCE_BLOCK_FINISHED = 'PAYMENT_SPLIT_DIVERGENCE_BLOCK_FINISHED'; }