99
1010namespace CodeBugLab \Like4Card \Services ;
1111
12+ use Exception ;
1213use Illuminate \Support \Carbon ;
1314use CodeBugLab \Like4Card \Contracts \Like4CardInterface ;
15+ use CodeBugLab \Like4Card \Exceptions \ProductsNotFoundException ;
16+ use CodeBugLab \Like4Card \Exceptions \WrongCredentialsException ;
1417
1518class Like4CardAPI implements Like4CardInterface
1619{
@@ -30,8 +33,31 @@ class Like4CardAPI implements Like4CardInterface
3033 */
3134 private static function cURL ($ url , $ json = [])
3235 {
33- dd ( ' there ' );
36+ $ response = self :: executeCURL ( $ url , $ json );
3437
38+ if ($ response ->response > 0 ) {
39+ return $ response ;
40+ }
41+
42+ switch (optional ($ response )->message ) {
43+ case "Incorrect Login - invalid email or password " :
44+ throw new WrongCredentialsException ($ response ->message );
45+ case "No available products " :
46+ throw new ProductsNotFoundException ($ response ->message );
47+ default :
48+ throw new Exception (optional ($ response )->message ?? "Unknown error " );
49+ }
50+ }
51+
52+ /**
53+ * Prepare CURL options before execution
54+ *
55+ * @param string $url
56+ * @param array $json
57+ * @return object
58+ */
59+ private static function executeCURL (string $ url , array $ json )
60+ {
3561 $ ch = curl_init ();
3662
3763 $ headers = array ();
@@ -93,8 +119,10 @@ public static function balance()
93119 */
94120 public static function categories ()
95121 {
96- return self ::cURL (
97- "categories "
122+ return optional (
123+ self ::cURL (
124+ "categories "
125+ )
98126 )->data ;
99127 }
100128
@@ -106,12 +134,14 @@ public static function categories()
106134 */
107135 public static function products (array $ ids )
108136 {
109- $ request = self ::cURL (
110- "products " ,
111- ["ids[] " => implode (", " , $ ids )]
112- );
113-
114- return isset ($ request ->data ) ? $ request ->data : $ request ->message ;
137+ return optional (
138+ self ::cURL (
139+ "products " ,
140+ [
141+ "ids[] " => implode (", " , $ ids )
142+ ]
143+ )
144+ )->data ;
115145 }
116146
117147 /**
@@ -122,12 +152,12 @@ public static function products(array $ids)
122152 */
123153 public static function getProductsByCategoryId (int $ category_id )
124154 {
125- $ request = self :: cURL (
126- " products " ,
127- [ " categoryId " => $ category_id ]
128- );
129-
130- return isset ( $ request -> data ) ? $ request ->data : $ request -> message ;
155+ return optional (
156+ self :: cURL (
157+ " products " ,
158+ [ " categoryId " => $ category_id ]
159+ )
160+ ) ->data ;
131161 }
132162
133163 /**
@@ -138,9 +168,11 @@ public static function getProductsByCategoryId(int $category_id)
138168 */
139169 public static function orders (array $ options = [])
140170 {
141- return self ::cURL (
142- "orders " ,
143- $ options
171+ return optional (
172+ self ::cURL (
173+ "orders " ,
174+ $ options
175+ )
144176 )->data ;
145177 }
146178
@@ -171,7 +203,7 @@ public static function createOrder(int $product_id, int $local_id)
171203 {
172204 $ time = now ();
173205
174- return self ::cURL (
206+ $ order = self ::cURL (
175207 "create_order " ,
176208 [
177209 'referenceId ' => $ local_id ,
@@ -181,6 +213,10 @@ public static function createOrder(int $product_id, int $local_id)
181213 'hash ' => self ::generateHash ($ time )
182214 ]
183215 );
216+
217+ logger ($ order );
218+
219+ return $ order ;
184220 }
185221
186222 /**
0 commit comments