diff --git a/examples/asaas/webhook.php b/examples/asaas/webhook.php new file mode 100644 index 0000000..57522ca --- /dev/null +++ b/examples/asaas/webhook.php @@ -0,0 +1,88 @@ + 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 a new webhook + * + * @return array + * @see available fields in https://docs.asaas.com/reference/criar-novo-webhook + */ +$webhook = $phpay + ->webhook($webhook) + ->create(); + +/** + * get all webhooks + * + * @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', + ]); + +/** + * delete a webhook + * + * @return bool + */ +$webhookDeleted = $phpay + ->webhook() + ->destroy('84c3c34b-9d23-44a3-95b2-d232c3f06dba'); diff --git a/src/Contracts/GatewayInterface.php b/src/Contracts/GatewayInterface.php index 9279805..32908d2 100644 --- a/src/Contracts/GatewayInterface.php +++ b/src/Contracts/GatewayInterface.php @@ -4,9 +4,11 @@ use Asaas\Resources\Charge\Charge as AsaasCharge; use Asaas\Resources\Customer\Customer as AsaasCustomer; +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..ba23dba --- /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..d966caa --- /dev/null +++ b/src/Gateways/Asaas/Resources/Webhook/Interface/WebhookInterface.php @@ -0,0 +1,12 @@ +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); + } + + /** + * get all webhooks + * + * @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); + } + + /** + * delete webhook by id + * + * @param string $id + * @return array + */ + public function destroy(string $id): bool + { + return $this->delete("webhooks/{$id}"); + } +}