55use GuzzleHttp \Client ;
66use GuzzleHttp \Exception \GuzzleException ;
77use Illuminate \Http \Request ;
8+ use Thepeer \Sdk \Exceptions \ForbiddenException ;
89use Thepeer \Sdk \Exceptions \InvalidPayloadException ;
9- use Thepeer \Sdk \Exceptions \InvalidReceiptException ;
10- use Thepeer \Sdk \Exceptions \InvalidSecretKeyException ;
11- use Thepeer \Sdk \Exceptions \SeverErrorException ;
12- use Thepeer \Sdk \Exceptions \UserNotFoundException ;
10+ use Thepeer \Sdk \Exceptions \InvalidResourceException ;
11+ use Thepeer \Sdk \Exceptions \NotAcceptableException ;
12+ use Thepeer \Sdk \Exceptions \ServiceUnavailableException ;
13+ use Thepeer \Sdk \Exceptions \UnauthorizedException ;
14+ use Thepeer \Sdk \Exceptions \ServerErrorException ;
1315
1416class Thepeer
1517{
@@ -54,43 +56,33 @@ public function validateSignature(Request $payload)
5456 return false ;
5557 }
5658
57- public function getReceipt ( $ receipt )
59+ public function getSendReceipt ( string $ receipt )
5860 {
5961 try {
6062 $ request = $ this ->client ->get ("/send/ {$ receipt }" );
6163
6264 $ payload = json_decode ($ request ->getBody ()->getContents ());
6365
64- if ($ request ->getStatusCode () === 401 ) {
65- throw new InvalidSecretKeyException ($ payload ->message );
66- } else if ($ request ->getStatusCode () === 404 ) {
67- throw new InvalidReceiptException ($ payload ->message );
68- } else if ($ request ->getStatusCode () === 200 ) {
69- return $ payload ;
70- }
71-
72- throw new SeverErrorException ("something went wrong " );
66+ return $ this ->processResponse ($ payload , $ request );
7367 } catch (GuzzleException $ e ) {
74- throw new SeverErrorException ($ e ->getMessage ());
68+ throw new ServerErrorException ($ e ->getMessage ());
7569 }
7670 }
7771
78- public function processReceipt ( $ receipt )
72+ public function processSendReceipt ( string $ receipt, bool $ insufficient_funds )
7973 {
8074 try {
81- $ request = $ this ->client ->post ("/send/ {$ receipt }" );
75+ $ request = $ this ->client ->post ("/send/ {$ receipt }" , [
76+ "body " => json_encode ([
77+ 'insufficient_funds ' => $ insufficient_funds ,
78+ ])
79+ ]);
8280
83- if ($ request ->getStatusCode () === 401 ) {
84- throw new InvalidSecretKeyException ("invalid secret key " );
85- } else if ($ request ->getStatusCode () === 404 ) {
86- throw new InvalidReceiptException ("invalid receipt " );
87- } else if ($ request ->getStatusCode () === 200 ) {
88- return json_decode ($ request ->getBody ()->getContents ());
89- }
81+ $ payload = json_decode ($ request ->getBody ()->getContents ());
9082
91- throw new SeverErrorException ( " something went wrong " );
83+ return $ this -> processResponse ( $ payload , $ request );
9284 } catch (GuzzleException $ e ) {
93- throw new SeverErrorException ($ e ->getMessage ());
85+ throw new ServerErrorException ($ e ->getMessage ());
9486 }
9587 }
9688
@@ -107,21 +99,9 @@ public function indexUser(string $name, string $email, string $identifier)
10799
108100 $ payload = json_decode ($ request ->getBody ()->getContents ());
109101
110- if ($ request ->getStatusCode () === 401 ) {
111- throw new InvalidSecretKeyException ($ payload ->message );
112- } else if ($ request ->getStatusCode () === 406 ) {
113- throw new InvalidPayloadException ($ payload ->message );
114- } else if ($ request ->getStatusCode () === 422 ) {
115- foreach ($ payload ->errors as $ error ) {
116- throw new InvalidPayloadException ($ error [0 ]);
117- }
118- } else if ($ request ->getStatusCode () === 200 ) {
119- return $ payload ;
120- }
121-
122- throw new SeverErrorException ("something went wrong " );
102+ return $ this ->processResponse ($ payload , $ request );
123103 } catch (GuzzleException $ e ) {
124- throw new SeverErrorException ($ e ->getMessage ());
104+ throw new ServerErrorException ($ e ->getMessage ());
125105 }
126106 }
127107
@@ -136,23 +116,9 @@ public function updateUser(string $reference, string $identifier)
136116
137117 $ payload = json_decode ($ request ->getBody ()->getContents ());
138118
139- if ($ request ->getStatusCode () === 401 ) {
140- throw new InvalidSecretKeyException ("invalid secret key " );
141- } else if ($ request ->getStatusCode () === 406 ) {
142- throw new InvalidPayloadException ($ payload ->message );
143- } else if ($ request ->getStatusCode () === 404 ) {
144- throw new UserNotFoundException ($ payload ->message );
145- } else if ($ request ->getStatusCode () === 422 ) {
146- foreach ($ payload ->errors as $ error ) {
147- throw new InvalidPayloadException ($ error [0 ]);
148- }
149- } else if ($ request ->getStatusCode () === 200 ) {
150- return $ payload ;
151- }
152-
153- throw new SeverErrorException ("something went wrong " );
119+ return $ this ->processResponse ($ payload , $ request );
154120 } catch (GuzzleException $ e ) {
155- throw new SeverErrorException ($ e ->getMessage ());
121+ throw new ServerErrorException ($ e ->getMessage ());
156122 }
157123 }
158124
@@ -163,23 +129,82 @@ public function deleteUser(string $reference)
163129
164130 $ payload = json_decode ($ request ->getBody ()->getContents ());
165131
166- if ($ request ->getStatusCode () === 401 ) {
167- throw new InvalidSecretKeyException ("invalid secret key " );
168- } else if ($ request ->getStatusCode () === 406 ) {
169- throw new InvalidPayloadException ($ payload ->message );
170- } else if ($ request ->getStatusCode () === 404 ) {
171- throw new UserNotFoundException ($ payload ->message );
172- } else if ($ request ->getStatusCode () === 422 ) {
132+ return $ this ->processResponse ($ payload , $ request );
133+ } catch (GuzzleException $ e ) {
134+ throw new ServerErrorException ($ e ->getMessage ());
135+ }
136+ }
137+
138+ public function getLink (string $ link )
139+ {
140+ try {
141+ $ request = $ this ->client ->get ("/link/ {$ link }" );
142+
143+ $ payload = json_decode ($ request ->getBody ()->getContents ());
144+
145+ return $ this ->processResponse ($ payload , $ request );
146+ } catch (GuzzleException $ e ) {
147+ throw new ServerErrorException ($ e ->getMessage ());
148+ }
149+ }
150+
151+ public function chargeLink (string $ link , int $ amount , string $ remark )
152+ {
153+ try {
154+ $ request = $ this ->client ->post ("/link/ {$ link }/charge " , [
155+ "body " => json_encode ([
156+ 'amount ' => $ amount ,
157+ 'remark ' => $ remark
158+ ])
159+ ]);
160+
161+ $ payload = json_decode ($ request ->getBody ()->getContents ());
162+
163+ return $ this ->processResponse ($ payload , $ request );
164+ } catch (GuzzleException $ e ) {
165+ throw new ServerErrorException ($ e ->getMessage ());
166+ }
167+ }
168+
169+ public function authorizaDirectCharge (string $ reference , bool $ insufficient_funds )
170+ {
171+ try {
172+ $ request = $ this ->client ->post ("/debit/ {$ reference }" , [
173+ "body " => json_encode ([
174+ 'insufficient_funds ' => $ insufficient_funds ,
175+ ])
176+ ]);
177+
178+ $ payload = json_decode ($ request ->getBody ()->getContents ());
179+
180+ return $ this ->processResponse ($ payload , $ request );
181+ } catch (GuzzleException $ e ) {
182+ throw new ServerErrorException ($ e ->getMessage ());
183+ }
184+ }
185+
186+ private function processResponse ($ payload , $ request )
187+ {
188+ switch ($ request ->getStatusCode ()) {
189+ case 201 :
190+ case 200 :
191+ return $ payload ;
192+ case 401 :
193+ throw new UnauthorizedException ($ payload ->message );
194+ case 403 :
195+ throw new ForbiddenException ($ payload ->message );
196+ case 404 :
197+ throw new InvalidResourceException ($ payload ->message );
198+ case 422 :
173199 foreach ($ payload ->errors as $ error ) {
174200 throw new InvalidPayloadException ($ error [0 ]);
175201 }
176- } else if ($ request ->getStatusCode () === 200 ) {
177- return true ;
178- }
179-
180- throw new SeverErrorException ("something went wrong " );
181- } catch (GuzzleException $ e ) {
182- throw new SeverErrorException ($ e ->getMessage ());
202+ case 406 :
203+ throw new NotAcceptableException ($ payload ->message );
204+ case 503 :
205+ throw new ServiceUnavailableException ($ payload ->message );
206+ default :
207+ throw new ServerErrorException ($ payload ->message );
183208 }
184209 }
185210}
0 commit comments