diff --git a/.github/README.md b/.github/README.md index 094d1d9..b5608d3 100644 --- a/.github/README.md +++ b/.github/README.md @@ -28,13 +28,10 @@ composer require phpay-io/phpay ### Asaas ```php -use PHPay\Gateways\Asaas\AsaasGateway; -use PHPay\PHPay; - /** * @var AsaasGateway $phpay */ -$asaas = new PHPay(new AsaasGateway(TOKEN_ASAAS_SANDBOX)); +$phpay = new PHPay(new AsaasGateway(TOKEN_ASAAS_SANDBOX)); ``` ### Efí @@ -43,9 +40,6 @@ O Efí exige um token do tipo Bearer que é gerado com o envio do client_id e client_secret, para isso, basta utilizar o método do phpay de autorização. ```php -use PHPay\Gateways\Efi\EfiGateway; -use PHPay\PHPay; - /** * @var EfiGateway $phpay */ diff --git a/examples/asaas/charges.php b/examples/asaas/charges.php index 82af1ca..8e31b98 100644 --- a/examples/asaas/charges.php +++ b/examples/asaas/charges.php @@ -24,16 +24,7 @@ * * @var AsaasGateway $phpay */ -$asaas = new PHPay(new AsaasGateway(TOKEN_ASAAS_SANDBOX)); - -/** - * create customer - * - * @return array customer - */ -$customerCreated = $phpay - ->customer() - ->create($customer); +$phpay = new PHPay(new AsaasGateway(TOKEN_ASAAS_SANDBOX)); /** * create charge @@ -42,7 +33,7 @@ */ $chargeCreated = $phpay ->charge($charge) - ->setCustomer($customerCreated['id']) + ->setCustomer($customer) ->create(); /** diff --git a/examples/efi/charges.php b/examples/efi/charges.php index 3ceb9d9..4a25deb 100644 --- a/examples/efi/charges.php +++ b/examples/efi/charges.php @@ -8,15 +8,15 @@ require_once __DIR__ . '/credentials.php'; $customer = [ - 'name' => NAME, - 'cpfCnpj' => CPF_CNPJ, + 'name' => NAME, + 'cpf_cnpj' => CPF_CNPJ, ]; $charge = [ - 'billingType' => 'PIX', 'value' => 100.00, 'description' => 'Teste de fatura', - 'dueDate' => date('Y-m-d', strtotime('+1 day')), + 'expire_at' => date('Y-m-d', strtotime('+1 day')), + 'message' => 'Teste de mensagem', ]; /** @@ -24,18 +24,11 @@ * * @var EfiGateway $phpay */ -$phpay = new PHPay(new EfiGateway()); +$phpay = new PHPay(new EfiGateway(CLIENT_ID, CLIENT_SECRET)); -$token = $phpay - ->authorization(CLIENT_ID, CLIENT_SECRET) - ->getToken(); - -/** - * create charge - * - * @return array charges - */ -$chargeCreated = $phpay +$charge = $phpay ->charge($charge) ->setCustomer($customer) ->create(); + +print_r($charge); diff --git a/src/Gateways/Asaas/Resources/Charge/Charge.php b/src/Gateways/Asaas/Resources/Charge/Charge.php index 16bc5bd..628a20b 100644 --- a/src/Gateways/Asaas/Resources/Charge/Charge.php +++ b/src/Gateways/Asaas/Resources/Charge/Charge.php @@ -4,6 +4,7 @@ use Asaas\Requests\AsaasChargeRequest; use Asaas\Resources\Charge\Interface\ChargeInterface; +use Asaas\Resources\Customer\Customer; use Asaas\Traits\HasAsaasClient; use GuzzleHttp\Client; @@ -57,12 +58,18 @@ public function setFilters(array $filters): ChargeInterface /** * set customer * - * @param string $customerId + * @param array $customer * @return ChargeInterface */ - public function setCustomer(string $customerId): ChargeInterface + public function setCustomer(array $customer): ChargeInterface { - $this->charge['customer'] = $customerId; + $customer = (new Customer( + $this->token, + $customer, + $this->sandbox + ))->create(); + + $this->charge['customer'] = $customer['id']; return $this; } diff --git a/src/Gateways/Asaas/Resources/Charge/Interface/ChargeInterface.php b/src/Gateways/Asaas/Resources/Charge/Interface/ChargeInterface.php index 9e1f4b5..4800e0f 100644 --- a/src/Gateways/Asaas/Resources/Charge/Interface/ChargeInterface.php +++ b/src/Gateways/Asaas/Resources/Charge/Interface/ChargeInterface.php @@ -52,12 +52,12 @@ public function destroy(string $id): bool; public function restore(string $id): array; /** - * get charge notifications by customer + * set customer * - * @param string $customer + * @param array $customer * @return ChargeInterface */ - public function setCustomer(string $customer): ChargeInterface; + public function setCustomer(array $customer): ChargeInterface; /** * set filter diff --git a/src/Gateways/Efi/EfiGateway.php b/src/Gateways/Efi/EfiGateway.php index b732ee0..cd1092b 100644 --- a/src/Gateways/Efi/EfiGateway.php +++ b/src/Gateways/Efi/EfiGateway.php @@ -4,6 +4,7 @@ use Efi\Interface\EfiGatewayInterface; use Efi\Resources\Authorization\Authorization; +use Efi\Resources\Charge\Charge; use Efi\Traits\HasEfiClient; use GuzzleHttp\Client; use stdClass; @@ -17,37 +18,49 @@ class EfiGateway implements EfiGatewayInterface */ public Client $client; + /** + * @var array $token + */ + private array $token; + /** * construct * + * @param string $clientId + * @param string $clientSecret * @param bool $sandbox */ public function __construct( - private bool $sandbox = true + private string $clientId, + private string $clientSecret, + private bool $sandbox = true, ) { + $this->authorization(); } /** - * autorization + * get token * - * @param string $clientId - * @param string $clientSecret - * @return Authorization + * @return array token */ - public function authorization(string $clientId, string $clientSecret): Authorization + public function getToken(): array { - return new Authorization($clientId, $clientSecret, $this->sandbox); + return $this->token; } /** * create charge * * @param array $charge - * @return object charge + * @return Charge */ - public function charge(array $charge = []): object + public function charge(array $charge = []): Charge { - return new stdClass(); + return new Charge( + $this->token, + $charge, + $this->sandbox + ); } /** @@ -71,4 +84,24 @@ public function webhook(array $webhook = []): object { return new stdClass(); } + + /** + * authorization + * + * @return void + */ + private function authorization(): void + { + $authorization = new Authorization( + $this->clientId, + $this->clientSecret, + $this->sandbox + ); + + $this->token = $authorization->getToken(); + + if (!isset($this->token['access_token'])) { + throw new \Exception('Token not generated'); + } + } } diff --git a/src/Gateways/Efi/Interface/EfiGatewayInterface.php b/src/Gateways/Efi/Interface/EfiGatewayInterface.php index 3e173a1..24c6ce2 100644 --- a/src/Gateways/Efi/Interface/EfiGatewayInterface.php +++ b/src/Gateways/Efi/Interface/EfiGatewayInterface.php @@ -2,10 +2,23 @@ namespace Efi\Interface; -use Efi\Resources\Authorization\Authorization; +use Efi\Resources\Charge\Charge; use PHPay\Contracts\GatewayInterface; interface EfiGatewayInterface extends GatewayInterface { - public function authorization(string $clientId, string $clientSecrete): Authorization; + /** + * get token + * + * @return array token + */ + public function getToken(): array; + + /** + * create charge + * + * @param array $charge + * @return Charge charge + */ + public function charge(array $charge = []): Charge; } diff --git a/src/Gateways/Efi/Resources/Authorization/Authorization.php b/src/Gateways/Efi/Resources/Authorization/Authorization.php index c2f9594..6bcc19a 100644 --- a/src/Gateways/Efi/Resources/Authorization/Authorization.php +++ b/src/Gateways/Efi/Resources/Authorization/Authorization.php @@ -32,14 +32,19 @@ public function __construct( /** * get token * - * @return array + * @return array */ public function getToken(): array { $this->client = $this->clientEfiAuthorize($this->clientId, $this->clientSecret); - return $this->post("v1/authorize", [ + /** + * @var array $response + */ + $response = $this->post("v1/authorize", [ 'grant_type' => 'client_credentials', ]); + + return $response; } } diff --git a/src/Gateways/Efi/Resources/Charge/Charge.php b/src/Gateways/Efi/Resources/Charge/Charge.php index 7a14328..1d5ad79 100644 --- a/src/Gateways/Efi/Resources/Charge/Charge.php +++ b/src/Gateways/Efi/Resources/Charge/Charge.php @@ -3,7 +3,245 @@ namespace Efi\Resources\Charge; use Efi\Resources\Charge\Interface\ChargeInterface; +use Efi\Traits\HasEfiClient; +use GuzzleHttp\Client; class Charge implements ChargeInterface { + /** + * trait Efi client + */ + use HasEfiClient; + + /** + * client guzzle + */ + private Client $client; + + /** + * @var array + */ + private array $customer; + + /** + * @var array + */ + private array $items = []; + + /** + * @var array + */ + private array $configuration = []; + + /** + * construct + * + * @param array $token + * @param array $charge + * @param bool $sandbox + */ + public function __construct( + array $token, + private array $charge = [], + private bool $sandbox = true, + ) { + $this->client = $this->clientEfiBoot( + $token['access_token'], + $token['token_type'] + ); + + if (!empty($charge)) { + $this->charge = $charge; + } + } + + /** + * set customer + * + * @param array $customer + * @return Charge + */ + public function setCustomer(array $customer): Charge + { + $this->customer = $this->bootCustomer($customer); + + return $this; + } + + /** + * create charge + * + * @return array + * @see fields available in https://docs.Efi.com/reference/criar-nova-cobranca + */ + public function create(): array + { + // EfiChargeRequest::validate($this->charge); + + $items = $this->getItems(); + $configurations = $this->getConfigurations(); + + return $this->post('v1/charge/one-step', [ + 'items' => $items, + 'payment' => [ + 'banking_billet' => [ + 'expire_at' => $this->charge['expire_at'], + 'customer' => $this->customer, + 'configurations' => $configurations, + ], + ], + ]); + } + + /** + * update charge + * + * @param string $id + * @param array $data + * @return array + */ + public function update(string $id, array $data): array + { + return $this->put("payments/{$id}", $data); + } + + /** + * destroy charge + * + * @param string $id + * @return bool + */ + public function destroy(string $id): bool + { + return $this->delete("payments/{$id}"); + } + + /** + * get status charge + * + * @param string $id + * @return array + */ + public function getStatus(string $id): array + { + return $this->get("payments/{$id}/status"); + } + + /** + * get digitable line + * + * @param string $id + * @return mixed + */ + public function getDigitableLine(string $id): mixed + { + return $this->get("payments/{$id}/identificationField")['identificationField']; + } + + /** + * get qrcode pix + * + * @param string $id + * @return array + */ + public function getQrCodePix(string $id): array + { + return $this->get("payments/{$id}/pixQrCode"); + } + + /** + * confirm receipt + * + * @param string $id + * @param array $data + * @return array + */ + public function confirmReceipt(string $id, array $data): array + { + return $this->post("payments/{$id}/receiveInCash", $data); + } + + /** + * get items + * + * @return array + */ + private function getItems(): array + { + $items = $this->items; + + if (empty($this->items)) { + $items[] = [ + 'name' => $this->charge['description'], + 'value' => $this->charge['value'], + ]; + + unset($this->charge['description']); + unset($this->charge['value']); + } + + return $items; + } + + /** + * get configuration + * + * @return array + */ + private function getConfigurations(): array + { + $configurations = $this->configuration; + + if (empty($configurations)) { + $configurations = [ + 'fine' => 200, + 'interest' => 33, + ]; + } + + return $configurations; + } + + /** + * boot customer + * + * @param array $customer + * @return array + */ + private function bootCustomer(array $customer): array + { + if (!isset($customer['name'])) { + throw new \Exception('Efí: Nome é obrigatório'); + } + + if (!isset($customer['cpf_cnpj'])) { + throw new \Exception('Efí: CPF/CNPJ é obrigatório'); + } + + $isFiscalPerson = (strlen($customer['cpf_cnpj']) === 11); + + if ($isFiscalPerson) { + $customerMounted = [ + 'name' => $customer['name'], + 'cpf' => $customer['cpf_cnpj'], + ]; + } else { + $customerMounted = [ + 'juridical_person' => [ + 'corporate_name' => $customer['name'], + 'cnpj' => $customer['cpf_cnpj'], + ], + ]; + } + + if (array_key_exists('email', $customer)) { + $customerMounted['email'] = $customer['email']; + } + + if (array_key_exists('phone_number', $customer)) { + $customerMounted['phone_number'] = $customer['phone_number']; + } + + return $customerMounted; + } } diff --git a/src/Gateways/Efi/Resources/Charge/Interface/ChargeInterface.php b/src/Gateways/Efi/Resources/Charge/Interface/ChargeInterface.php index 871c149..1b808ee 100644 --- a/src/Gateways/Efi/Resources/Charge/Interface/ChargeInterface.php +++ b/src/Gateways/Efi/Resources/Charge/Interface/ChargeInterface.php @@ -2,6 +2,15 @@ namespace Efi\Resources\Charge\Interface; +use Efi\Resources\Charge\Charge; + interface ChargeInterface { + /** + * set customer + * + * @param array $customer + * @return Charge + */ + public function setCustomer(array $customer): Charge; } diff --git a/src/Gateways/Efi/Traits/HasEfiClient.php b/src/Gateways/Efi/Traits/HasEfiClient.php index d461083..5f63b31 100644 --- a/src/Gateways/Efi/Traits/HasEfiClient.php +++ b/src/Gateways/Efi/Traits/HasEfiClient.php @@ -33,9 +33,11 @@ protected function clientEfiAuthorize( /** * boot client * + * @param string $token + * @param string $type * @return Client */ - protected function clientEfiBoot(): Client + protected function clientEfiBoot(string $token, string $type): Client { $baseUrl = $this->sandbox ? 'https://cobrancas-h.api.efipay.com.br/' : @@ -44,9 +46,8 @@ protected function clientEfiBoot(): Client return new Client([ 'base_uri' => $baseUrl, 'headers' => [ - 'content-type' => 'application/json', - 'user-agent' => 'PHPay', - 'access_token' => '', + 'Authorization' => "{$type} {$token}", + 'content-type' => 'application/json', ], ]); } diff --git a/src/PHPay.php b/src/PHPay.php index ac9bcb4..8ea8d0c 100644 --- a/src/PHPay.php +++ b/src/PHPay.php @@ -20,20 +20,18 @@ public function __construct( /** * ATTENTION!!! ONLY EFÍ GATEWAY - * get resource authorize from gateway. + * get token. * - * @param string $clientId - * @param string $clientSecret - * @return object + * @return array */ - public function authorization(string $clientId, string $clientSecret): object + public function getToken(): array { /** * @var EfiGatewayInterface $gateway */ $gateway = $this->gateway; - return $gateway->authorization($clientId, $clientSecret); + return $gateway->getToken(); } /**