diff --git a/.gitignore b/.gitignore index bbc3072..e39b05d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ vendor/ node_modules/ -examples/asaas/credentials.php \ No newline at end of file +examples/asaas/credentials.php +examples/efi/credentials.php \ No newline at end of file diff --git a/composer.json b/composer.json index f599215..35d43dc 100644 --- a/composer.json +++ b/composer.json @@ -5,6 +5,7 @@ "psr-4": { "PHPay\\": "src/", "Asaas\\": "src/Gateways/Asaas/", + "Efi\\": "src/Gateways/Efi/", "AsaasCustomer\\": "src/Gateways/Asaas/Resources/Customer/" } }, @@ -32,4 +33,4 @@ "scripts": { "test": "vendor/bin/pest" } -} +} \ No newline at end of file diff --git a/examples/efi/charges.php b/examples/efi/charges.php new file mode 100644 index 0000000..665adf8 --- /dev/null +++ b/examples/efi/charges.php @@ -0,0 +1,45 @@ + NAME, + 'cpfCnpj' => CPF_CNPJ, +]; + +$charge = [ + 'billingType' => 'PIX', + 'value' => 100.00, + 'description' => 'Teste de fatura', + 'dueDate' => date('Y-m-d', strtotime('+1 day')), +]; + +/** + * initialize phpay + * + * @var EfiGateway $phpay + */ +$phpay = new PHPay(new EfiGateway()); + +$token = $phpay + ->authorization(CLIENT_ID, CLIENT_SECRET) + ->getToken(); + +print_r($token); + +die(); + +/** + * create charge + * + * @return array charges + */ +$chargeCreated = $phpay + ->charge($charge) + ->setCustomer($customer) + ->create(); diff --git a/examples/efi/credentials.example.php b/examples/efi/credentials.example.php new file mode 100644 index 0000000..ce25cbb --- /dev/null +++ b/examples/efi/credentials.example.php @@ -0,0 +1,6 @@ +sandbox); + } + + /** + * create charge + * + * @param array $charge + * @return object charge + */ + public function charge(array $charge = []): object + { + return new stdClass(); + } + + /** + * create customer + * + * @param array $customer + * @return object customer + */ + public function customer(array $customer = []): object + { + return new stdClass(); + } + + /** + * create webhook + * + * @param array $webhook + * @return object webhook + */ + public function webhook(array $webhook = []): object + { + return new stdClass(); + } +} diff --git a/src/Gateways/Efi/Interface/EfiGatewayInterface.php b/src/Gateways/Efi/Interface/EfiGatewayInterface.php new file mode 100644 index 0000000..3e173a1 --- /dev/null +++ b/src/Gateways/Efi/Interface/EfiGatewayInterface.php @@ -0,0 +1,11 @@ + + */ + public function getToken(): array + { + $this->client = $this->clientEfiAuthorize($this->clientId, $this->clientSecret); + + return $this->post("v1/authorize", [ + 'grant_type' => 'client_credentials', + ]); + } +} diff --git a/src/Gateways/Efi/Resources/Authorization/Interface/AuthorizationInterface.php b/src/Gateways/Efi/Resources/Authorization/Interface/AuthorizationInterface.php new file mode 100644 index 0000000..bc7e4e4 --- /dev/null +++ b/src/Gateways/Efi/Resources/Authorization/Interface/AuthorizationInterface.php @@ -0,0 +1,7 @@ +sandbox ? + 'https://cobrancas-h.api.efipay.com.br/' : + 'https://cobrancas.api.efipay.com.br/'; + + return new Client([ + 'base_uri' => $baseUrl, + 'headers' => [ + 'Authorization' => 'Basic ' . base64_encode("{$clientId}:{$clientSecret}"), + 'content-type' => 'application/json', + ], + ]); + } + + /** + * boot client + * + * @return Client + */ + protected function clientEfiBoot(): Client + { + $baseUrl = $this->sandbox ? + 'https://cobrancas-h.api.efipay.com.br/' : + 'https://cobrancas.api.efipay.com.br/'; + + return new Client([ + 'base_uri' => $baseUrl, + 'headers' => [ + 'content-type' => 'application/json', + 'user-agent' => 'PHPay', + 'access_token' => '', + ], + ]); + } + + /** + * get data + * + * @param string $endpoint + * @param array $filters + * @return array + */ + protected function get(string $endpoint, array $filters = []): array + { + try { + $reposonse = $this->client->get($endpoint, [ + 'query' => $filters, + ]); + + $content = $reposonse + ->getBody() + ->getContents(); + + return (array) json_decode($content, true); + } catch (\Exception $e) { + return [ + 'error' => $e->getCode(), + 'message' => $e->getMessage(), + ]; + } + } + + /** + * post data + * + * @param string $endpoint + * @param array $data + * @return array + */ + protected function post(string $endpoint, array $data = []): array + { + try { + $reposonse = $this->client->post($endpoint, [ + 'json' => $data, + ]); + + $content = $reposonse + ->getBody() + ->getContents(); + + return (array) json_decode($content, true); + } catch (\Exception $e) { + return [ + 'error' => $e->getCode(), + 'message' => $e->getMessage(), + ]; + } + } + + /** + * put data + * + * @param string $endpoint + * @param array $data + * @return array + */ + protected function put(string $endpoint, array $data): array + { + try { + $response = $this->client->put($endpoint, [ + 'json' => $data, + ]); + + $content = $response + ->getBody() + ->getContents(); + + return (array) json_decode($content, true); + } catch (\Exception $e) { + return [ + 'error' => $e->getCode(), + 'message' => $e->getMessage(), + ]; + } + } + + /** + * delete data + * + * @param string $endpoint + * @return bool + */ + protected function delete(string $endpoint): bool + { + try { + $response = $this->client->delete($endpoint); + + return ($response->getStatusCode() == 200); + } catch (\Exception $e) { + return false; + } + } +} diff --git a/src/PHPay.php b/src/PHPay.php index b2cb48c..ac9bcb4 100644 --- a/src/PHPay.php +++ b/src/PHPay.php @@ -2,6 +2,7 @@ namespace PHPay; +use Efi\Interface\EfiGatewayInterface; use PHPay\Contracts\GatewayInterface; class PHPay implements GatewayInterface @@ -17,6 +18,24 @@ public function __construct( $this->gateway = $gateway; } + /** + * ATTENTION!!! ONLY EFÍ GATEWAY + * get resource authorize from gateway. + * + * @param string $clientId + * @param string $clientSecret + * @return object + */ + public function authorization(string $clientId, string $clientSecret): object + { + /** + * @var EfiGatewayInterface $gateway + */ + $gateway = $this->gateway; + + return $gateway->authorization($clientId, $clientSecret); + } + /** * get resource customer from gateway. *