33namespace ZarinPal \Sdk \Endpoint \GraphQL ;
44
55use ZarinPal \Sdk \ClientBuilder ;
6- use ZarinPal \Sdk \Options ;
76use ZarinPal \Sdk \Endpoint \GraphQL \RequestTypes \RefundRequest ;
87use ZarinPal \Sdk \Endpoint \GraphQL \ResponseTypes \RefundResponse ;
9- use ZarinPal \Sdk \HttpClient \Exception \ResponseException ;
10- use Psr \Http \Message \ResponseInterface ;
11- use JsonException ;
12- use Exception ;
8+ use ZarinPal \Sdk \Options ;
139
14- class RefundService
10+ class RefundService extends BaseGraphQLService
1511{
16- private ClientBuilder $ clientBuilder ;
17- private Options $ options ;
18- private string $ graphqlUrl ;
19-
2012 public function __construct (ClientBuilder $ clientBuilder , Options $ options )
2113 {
22- $ this ->clientBuilder = $ clientBuilder ;
23- $ this ->options = $ options ;
24- $ this ->graphqlUrl = $ options ->getGraphqlUrl ();
14+ parent ::__construct ($ clientBuilder , $ options );
2515 }
2616
2717 public function refund (RefundRequest $ request ): RefundResponse
@@ -32,65 +22,4 @@ public function refund(RefundRequest $request): RefundResponse
3222
3323 return new RefundResponse ($ response ['data ' ]['resource ' ]);
3424 }
35-
36- private function httpHandler (string $ uri , string $ body ): array
37- {
38- try {
39- $ httpClient = $ this ->clientBuilder ->getHttpClient ();
40-
41- $ response = $ httpClient ->post ($ uri , [
42- 'User-Agent ' => sprintf ('%sSdk/v.0.1 (php %s) ' , $ this ->getClassName (), PHP_VERSION ),
43- 'Authorization ' => 'Bearer ' . $ this ->options ->getAccessToken (),
44- 'Content-Type ' => 'application/json ' ,
45- ], $ body );
46-
47- $ this ->checkHttpError ($ response );
48-
49- $ responseData = json_decode ($ response ->getBody ()->getContents (), true , 512 , JSON_THROW_ON_ERROR );
50-
51- } catch (JsonException $ e ) {
52- throw new ResponseException ('JSON parsing error: ' . $ e ->getMessage (), -98 , null , ['details ' => $ e ->getMessage ()]);
53- } catch (ResponseException $ e ) {
54- throw $ e ;
55- } catch (Exception $ e ) {
56- throw new ResponseException ('Request failed: ' . $ e ->getMessage (), -99 , null , ['details ' => $ e ->getMessage ()]);
57- }
58-
59- return $ this ->checkGraphQLError ($ responseData );
60- }
61-
62- private function checkHttpError (ResponseInterface $ response ): void
63- {
64- $ statusCode = $ response ->getStatusCode ();
65- if ($ statusCode !== 200 ) {
66- $ body = $ response ->getBody ()->getContents ();
67- $ parsedBody = json_decode ($ body , true );
68-
69- $ errorData = [
70- 'data ' => [],
71- 'errors ' => [
72- 'message ' => $ response ->getReasonPhrase (),
73- 'code ' => $ statusCode ,
74- 'details ' => $ parsedBody ?? []
75- ]
76- ];
77-
78- throw new ResponseException ($ errorData ['errors ' ]['message ' ], $ errorData ['errors ' ]['code ' ], null , $ errorData );
79- }
80- }
81-
82- private function checkGraphQLError (array $ response ): array
83- {
84- if (isset ($ response ['errors ' ]) || empty ($ response ['data ' ])) {
85- $ errorDetails = $ response ['errors ' ] ?? ['message ' => 'Unknown error ' , 'code ' => -1 ];
86- throw new ResponseException ('GraphQL query error: ' . json_encode ($ errorDetails ), $ errorDetails ['code ' ]);
87- }
88-
89- return $ response ;
90- }
91-
92- private function getClassName (): string
93- {
94- return basename (str_replace ('\\' , '/ ' , __CLASS__ ));
95- }
9625}
0 commit comments