Skip to content

Commit 99a8021

Browse files
committed
PHPAY-40: feat: adding create charge efi
1 parent b962a42 commit 99a8021

File tree

12 files changed

+345
-63
lines changed

12 files changed

+345
-63
lines changed

.github/README.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,10 @@ composer require phpay-io/phpay
2828
### Asaas
2929

3030
```php
31-
use PHPay\Gateways\Asaas\AsaasGateway;
32-
use PHPay\PHPay;
33-
3431
/**
3532
* @var AsaasGateway $phpay
3633
*/
37-
$asaas = new PHPay(new AsaasGateway(TOKEN_ASAAS_SANDBOX));
34+
$phpay = new PHPay(new AsaasGateway(TOKEN_ASAAS_SANDBOX));
3835
```
3936

4037
### Efí
@@ -43,9 +40,6 @@ O Efí exige um token do tipo Bearer que é gerado com o
4340
envio do client_id e client_secret, para isso, basta utilizar o método do phpay de autorização.
4441

4542
```php
46-
use PHPay\Gateways\Efi\EfiGateway;
47-
use PHPay\PHPay;
48-
4943
/**
5044
* @var EfiGateway $phpay
5145
*/

examples/asaas/charges.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,7 @@
2424
*
2525
* @var AsaasGateway $phpay
2626
*/
27-
$asaas = new PHPay(new AsaasGateway(TOKEN_ASAAS_SANDBOX));
28-
29-
/**
30-
* create customer
31-
*
32-
* @return array customer
33-
*/
34-
$customerCreated = $phpay
35-
->customer()
36-
->create($customer);
27+
$phpay = new PHPay(new AsaasGateway(TOKEN_ASAAS_SANDBOX));
3728

3829
/**
3930
* create charge
@@ -42,7 +33,7 @@
4233
*/
4334
$chargeCreated = $phpay
4435
->charge($charge)
45-
->setCustomer($customerCreated['id'])
36+
->setCustomer($customer)
4637
->create();
4738

4839
/**

examples/efi/charges.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,27 @@
88
require_once __DIR__ . '/credentials.php';
99

1010
$customer = [
11-
'name' => NAME,
12-
'cpfCnpj' => CPF_CNPJ,
11+
'name' => NAME,
12+
'cpf_cnpj' => CPF_CNPJ,
1313
];
1414

1515
$charge = [
16-
'billingType' => 'PIX',
1716
'value' => 100.00,
1817
'description' => 'Teste de fatura',
19-
'dueDate' => date('Y-m-d', strtotime('+1 day')),
18+
'expire_at' => date('Y-m-d', strtotime('+1 day')),
19+
'message' => 'Teste de mensagem',
2020
];
2121

2222
/**
2323
* initialize phpay
2424
*
2525
* @var EfiGateway $phpay
2626
*/
27-
$phpay = new PHPay(new EfiGateway());
27+
$phpay = new PHPay(new EfiGateway(CLIENT_ID, CLIENT_SECRET));
2828

29-
$token = $phpay
30-
->authorization(CLIENT_ID, CLIENT_SECRET)
31-
->getToken();
32-
33-
/**
34-
* create charge
35-
*
36-
* @return array charges
37-
*/
38-
$chargeCreated = $phpay
29+
$charge = $phpay
3930
->charge($charge)
4031
->setCustomer($customer)
4132
->create();
33+
34+
print_r($charge);

src/Gateways/Asaas/Resources/Charge/Charge.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Asaas\Requests\AsaasChargeRequest;
66
use Asaas\Resources\Charge\Interface\ChargeInterface;
7+
use Asaas\Resources\Customer\Customer;
78
use Asaas\Traits\HasAsaasClient;
89
use GuzzleHttp\Client;
910

@@ -57,12 +58,18 @@ public function setFilters(array $filters): ChargeInterface
5758
/**
5859
* set customer
5960
*
60-
* @param string $customerId
61+
* @param array<mixed> $customer
6162
* @return ChargeInterface
6263
*/
63-
public function setCustomer(string $customerId): ChargeInterface
64+
public function setCustomer(array $customer): ChargeInterface
6465
{
65-
$this->charge['customer'] = $customerId;
66+
$customer = (new Customer(
67+
$this->token,
68+
$customer,
69+
$this->sandbox
70+
))->create();
71+
72+
$this->charge['customer'] = $customer['id'];
6673

6774
return $this;
6875
}

src/Gateways/Asaas/Resources/Charge/Interface/ChargeInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ public function destroy(string $id): bool;
5252
public function restore(string $id): array;
5353

5454
/**
55-
* get charge notifications by customer
55+
* set customer
5656
*
57-
* @param string $customer
57+
* @param array<mixed> $customer
5858
* @return ChargeInterface
5959
*/
60-
public function setCustomer(string $customer): ChargeInterface;
60+
public function setCustomer(array $customer): ChargeInterface;
6161

6262
/**
6363
* set filter

src/Gateways/Efi/EfiGateway.php

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Efi\Interface\EfiGatewayInterface;
66
use Efi\Resources\Authorization\Authorization;
7+
use Efi\Resources\Charge\Charge;
78
use Efi\Traits\HasEfiClient;
89
use GuzzleHttp\Client;
910
use stdClass;
@@ -17,37 +18,49 @@ class EfiGateway implements EfiGatewayInterface
1718
*/
1819
public Client $client;
1920

21+
/**
22+
* @var array<string> $token
23+
*/
24+
private array $token;
25+
2026
/**
2127
* construct
2228
*
29+
* @param string $clientId
30+
* @param string $clientSecret
2331
* @param bool $sandbox
2432
*/
2533
public function __construct(
26-
private bool $sandbox = true
34+
private string $clientId,
35+
private string $clientSecret,
36+
private bool $sandbox = true,
2737
) {
38+
$this->authorization();
2839
}
2940

3041
/**
31-
* autorization
42+
* get token
3243
*
33-
* @param string $clientId
34-
* @param string $clientSecret
35-
* @return Authorization
44+
* @return array<mixed> token
3645
*/
37-
public function authorization(string $clientId, string $clientSecret): Authorization
46+
public function getToken(): array
3847
{
39-
return new Authorization($clientId, $clientSecret, $this->sandbox);
48+
return $this->token;
4049
}
4150

4251
/**
4352
* create charge
4453
*
4554
* @param array<string> $charge
46-
* @return object charge
55+
* @return Charge
4756
*/
48-
public function charge(array $charge = []): object
57+
public function charge(array $charge = []): Charge
4958
{
50-
return new stdClass();
59+
return new Charge(
60+
$this->token,
61+
$charge,
62+
$this->sandbox
63+
);
5164
}
5265

5366
/**
@@ -71,4 +84,24 @@ public function webhook(array $webhook = []): object
7184
{
7285
return new stdClass();
7386
}
87+
88+
/**
89+
* authorization
90+
*
91+
* @return void
92+
*/
93+
private function authorization(): void
94+
{
95+
$authorization = new Authorization(
96+
$this->clientId,
97+
$this->clientSecret,
98+
$this->sandbox
99+
);
100+
101+
$this->token = $authorization->getToken();
102+
103+
if (!isset($this->token['access_token'])) {
104+
throw new \Exception('Token not generated');
105+
}
106+
}
74107
}

src/Gateways/Efi/Interface/EfiGatewayInterface.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,23 @@
22

33
namespace Efi\Interface;
44

5-
use Efi\Resources\Authorization\Authorization;
5+
use Efi\Resources\Charge\Charge;
66
use PHPay\Contracts\GatewayInterface;
77

88
interface EfiGatewayInterface extends GatewayInterface
99
{
10-
public function authorization(string $clientId, string $clientSecrete): Authorization;
10+
/**
11+
* get token
12+
*
13+
* @return array<mixed> token
14+
*/
15+
public function getToken(): array;
16+
17+
/**
18+
* create charge
19+
*
20+
* @param array<string> $charge
21+
* @return Charge charge
22+
*/
23+
public function charge(array $charge = []): Charge;
1124
}

src/Gateways/Efi/Resources/Authorization/Authorization.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,19 @@ public function __construct(
3232
/**
3333
* get token
3434
*
35-
* @return array<mixed>
35+
* @return array<string>
3636
*/
3737
public function getToken(): array
3838
{
3939
$this->client = $this->clientEfiAuthorize($this->clientId, $this->clientSecret);
4040

41-
return $this->post("v1/authorize", [
41+
/**
42+
* @var array<string> $response
43+
*/
44+
$response = $this->post("v1/authorize", [
4245
'grant_type' => 'client_credentials',
4346
]);
47+
48+
return $response;
4449
}
4550
}

0 commit comments

Comments
 (0)