5
5
use Illuminate \Support \Facades \Http ;
6
6
use Payhub \Contracts \GatewayInterface ;
7
7
use Payhub \Exceptions \AsaasExceptions ;
8
+ use Payhub \Gateways \Asaas \Enums \{BillingType , ClientMethods };
8
9
use Payhub \Gateways \Asaas \Requests \AsaasPixRequest ;
9
- use Payhub \Gateways \Asaas \Resources \Auth ;
10
- use Payhub \Gateways \Asaas \Resources \Client ;
10
+ use Payhub \Gateways \Asaas \Resources \{Auth , Client };
11
11
12
12
class AsaasGateway implements GatewayInterface
13
13
{
14
+ /**
15
+ * @var string
16
+ */
17
+ private string $ client ;
18
+
14
19
/**
15
20
* @var array
16
21
*/
17
- protected array $ client ;
22
+ private array $ payment ;
18
23
19
24
/**
20
25
* auth
@@ -34,37 +39,55 @@ public function auth(array $credentials, bool $sandbox = true): self
34
39
* set client
35
40
*
36
41
* @param array $client
37
- * @return Client
42
+ * @return self
38
43
*/
39
- public function client (): Client
44
+ public function client (array $ client , string $ method = ' store ' ): self | AsaasExceptions
40
45
{
41
- return new Client ();
46
+ if (! ClientMethods::tryFrom ($ method )) {
47
+ return (new AsaasExceptions ())('Method not found ' );
48
+ }
49
+
50
+ if (! method_exists (Client::class, $ method )) {
51
+ return (new AsaasExceptions ())('Method not found ' );
52
+ }
53
+
54
+ try {
55
+ $ this ->client = (new Client ())::$ method ($ client );
56
+ } catch (\Exception $ e ) {
57
+ return (new AsaasExceptions ())($ e ->getMessage ());
58
+ }
59
+
60
+ return $ this ;
42
61
}
43
62
44
63
/**
45
64
* set pix
46
65
*
47
66
* @param array $pix
48
67
*/
49
- public function pix (array $ pix ): self
68
+ public function pix (array $ pix ): self | AsaasExceptions
50
69
{
51
70
try {
52
71
AsaasPixRequest::validate ($ pix );
53
72
54
- $ pix = Http::asaas ()
73
+ extract ($ pix );
74
+
75
+ $ payment = Http::asaas ()
55
76
->post ('/payments ' , [
56
77
'customer ' => $ this ->client ,
57
- 'billingType ' => ' BOLETO ' ,
58
- 'value ' => $ pix [ ' amount ' ] ,
59
- 'dueDate ' => $ pix [ ' due_date ' ] ,
60
- 'description ' => $ pix [ ' description ' ] ,
78
+ 'billingType ' => BillingType:: PIX -> value ,
79
+ 'value ' => $ amount ,
80
+ 'dueDate ' => $ due_date ,
81
+ 'description ' => $ description ,
61
82
])->json ();
62
83
63
- print_r ($ pix );
84
+ print_r ($ payment );
64
85
65
- return $ this ;
86
+ $ this -> payment = $ payment ;
66
87
} catch (\Exception $ e ) {
67
88
return (new AsaasExceptions ())($ e ->getMessage ());
68
89
}
90
+
91
+ return $ this ;
69
92
}
70
93
}
0 commit comments