· 9 months ago · Dec 24, 2024, 11:35 AM
1<?php
2
3namespace App\Helpers;
4
5/**
6 * Class VipPaymentHelper.
7 * @references <https://github.com/ahmdaka06/vipay-curl-example/blob/main/vipayment.class.php>
8 */
9class VipPaymentHelper
10{
11 // Endpoint API
12 public $end_point = 'https://vip-reseller.co.id/api';
13
14 // API ID
15 protected $api_id;
16 // API Key
17 protected $api_key;
18 // Signature
19 protected $signature;
20
21 /**
22 * Constructor
23 * @param string $api_id
24 * @param string $api_key
25 */
26 public function __construct(
27 $api_id,
28 $api_key
29 ) {
30 $this->api_id = $api_id;
31 $this->api_key = $api_key;
32 $this->signature = md5($api_id . $api_key);
33 }
34
35 /**
36 * Profile
37 *
38 * @return array
39 *
40 * @example profile()
41 */
42 public function profile(): array
43 {
44 $end_point = $this->end_point . '/profile';
45
46 $params = [
47 'key' => $this->api_key,
48 'sign' => $this->signature
49 ];
50
51 $request = $this->connect($end_point, $params);
52
53 $response = json_decode($request, true);
54
55 if (isset($response['result']) && $response['result'] == false) {
56 return [
57 'status' => false,
58 'message' => $response['message']
59 ];
60 }
61
62 return [
63 'status' => true,
64 'data' => $response['data'],
65 'message' => $response['message']
66 ];
67 }
68
69 /**
70 * Order Prepaid
71 * @param string $service_code service code
72 * @param string $data_no target number
73 *
74 * @return array
75 *
76 * @example order_prepaid('PLN5', '3335211111133')
77 * @example order_prepaid('PULSA5', '081234567890')
78 */
79 public function order_prepaid(
80 $service_code,
81 $data_no
82 ): array
83 {
84 $end_point = $this->end_point . '/prepaid';
85
86 $params = [
87 'key' => $this->api_key,
88 'sign' => $this->signature,
89 'type' => 'order',
90 'service' => $service_code,
91 'data_no' => $data_no
92 ];
93
94 $request = $this->connect($end_point, $params);
95
96 $response = json_decode($request, true);
97
98 if (isset($response['result']) && $response['result'] == false) {
99 return [
100 'status' => false,
101 'message' => $response['message']
102 ];
103 }
104
105 return [
106 'status' => true,
107 'data' => $response['data'],
108 'message' => $response['message']
109 ];
110 }
111
112 /**
113 * Status Order Prepaid
114 * @param string $trxid transaction id
115 * @param null|int $limit (optional)
116 *
117 * @return array
118 *
119 * @example status_order_prepaid('1234567890', 1)
120 * @example status_order_prepaid('1234567890')
121 */
122 public function status_order_prepaid(
123 $trxid,
124 $limit = null
125 ): array
126 {
127 $end_point = $this->end_point . '/prepaid';
128
129 $params = [
130 'key' => $this->api_key,
131 'sign' => $this->signature,
132 'type' => 'status',
133 'trxid' => $trxid,
134 'limit'=> $limit
135 ];
136
137 $request = $this->connect($end_point, $params);
138
139 $response = json_decode($request, true);
140
141 if (isset($response['result']) && $response['result'] == false) {
142 return [
143 'status' => false,
144 'message' => $response['message']
145 ];
146 }
147
148 return [
149 'status' => true,
150 'data' => $response['data'],
151 'message' => $response['message']
152 ];
153 }
154
155 /**
156 * Service Prepaid
157 * @param string $filter_type (optional | type, brand)
158 * @param null|string $filter_value (optional | pulsa-reguler, telkomsel)
159 *
160 * @return array
161 *
162 * @example service_prepaid('type', 'pulsa-reguler')
163 * @example service_prepaid('brand', 'telkomsel')
164 */
165 public function service_prepaid(
166 $filter_type = null,
167 $filter_value = null
168 ): array
169 {
170 $end_point = $this->end_point . '/prepaid';
171
172 $params = [
173 'key'=> $this->api_key,
174 'sign'=> $this->signature,
175 'type'=> 'services',
176 'filter_type'=> $filter_type,
177 'filter_value'=> $filter_value
178 ];
179
180 $request = $this->connect($end_point, $params);
181
182 $response = json_decode($request, true);
183
184 if (isset($response['result']) && $response['result'] == false) {
185 return [
186 'status'=> false,
187 'message'=> $response['message']
188 ];
189 }
190
191 return [
192 'status'=> true,
193 'data'=> $response['data'],
194 'message'=> $response['message']
195 ];
196 }
197
198 /**
199 * Order Game & Streaming
200 * @param string $service service code
201 * @param string $data_no target number
202 * @param null|string $data_zone (optional)
203 *
204 * @return array
205 *
206 * @example order_game('GARENA', '1234567890', 'ID')
207 * @example order_game('STEAM', '1234567890')
208 */
209 public function order_game(
210 $service,
211 $data_no,
212 $data_zone = null
213 ): array
214 {
215 $end_point = $this->end_point . '/game-feature';
216
217 $params = [
218 'key'=> $this->api_key,
219 'sign'=> $this->signature,
220 'type'=> 'order',
221 'service'=> $service,
222 'data_no'=> $data_no,
223 'data_zone'=> $data_zone
224 ];
225
226 $request = $this->connect($end_point, $params);
227
228 $response = json_decode($request, true);
229
230 if (isset($response['result']) && $response['result'] == false) {
231 return [
232 'status'=> false,
233 'message'=> $response['message']
234 ];
235 }
236
237 return [
238 'status'=> true,
239 'data'=> $response['data'],
240 'message'=> $response['message']
241 ];
242 }
243
244 /**
245 * Status Order Game & Streaming
246 * @param string $trxid
247 * @param null|int $limit (optional)
248 *
249 * @return array
250 *
251 * @example status_order_game('1234567890', 1)
252 * @example status_order_game('1234567890')
253 */
254
255 public function status_order_game(
256 $trxid,
257 $limit = null
258 ): array
259 {
260 $end_point = $this->end_point . '/game-feature';
261
262 $params = [
263 'key'=> $this->api_key,
264 'sign'=> $this->signature,
265 'type'=> 'status',
266 'trxid'=> $trxid,
267 'limit'=> $limit
268 ];
269
270 $request = $this->connect($end_point, $params);
271
272 $response = json_decode($request, true);
273
274 if (isset($response['result']) && $response['result'] == false) {
275 return [
276 'status'=> false,
277 'message'=> $response['message']
278 ];
279 }
280
281 return [
282 'status'=> true,
283 'data'=> $response['data'],
284 'message'=> $response['message']
285 ];
286 }
287
288 /**
289 * Service Game & Streaming
290 * @param string $filter_type (optional | type, brand)
291 * @param null|string $filter_value (optional | game, streaming)
292 * @param null|string $filter_status (optional | available / empty)
293 *
294 * @return array
295 *
296 * @example service_game('type', 'game')
297 * @example service_game('brand', 'streaming')
298 * @example service_game('brand', 'streaming', 'available')
299 */
300
301 public function service_game(
302 $filter_type = null,
303 $filter_value = null,
304 $filter_status = null
305 ): array
306 {
307 $end_point = $this->end_point . '/game-feature';
308
309 $params = [
310 'key'=> $this->api_key,
311 'sign'=> $this->signature,
312 'type'=> 'services',
313 'filter_type'=> $filter_type,
314 'filter_value'=> $filter_value,
315 'filter_status'=> $filter_status
316 ];
317
318 $request = $this->connect($end_point, $params);
319
320 $response = json_decode($request, true);
321
322 if (isset($response['result']) && $response['result'] == false) {
323 return [
324 'status'=> false,
325 'message'=> $response['message']
326 ];
327 }
328
329 return [
330 'status'=> true,
331 'data'=> $response['data'],
332 'message'=> $response['message']
333 ];
334 }
335
336 /**
337 * Connect
338 * @param string $endpoint
339 * @param array $params
340 *
341 * @return string|bool
342 */
343 private function connect(
344 $endpoint,
345 $params
346 ) {
347 $_post = [];
348 if (is_array($params)) {
349 foreach ($params as $name => $value) {
350 $_post[] = $name . '=' . urlencode($value);
351 }
352 }
353
354 $ch = curl_init($endpoint);
355 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
356 curl_setopt($ch, CURLOPT_POST, 1);
357 curl_setopt($ch, CURLOPT_HEADER, 0);
358 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
359 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
360 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
361
362 if (is_array($params)) {
363 curl_setopt($ch, CURLOPT_POSTFIELDS, join('&', $_post));
364 }
365 curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
366 $response = curl_exec($ch);
367 if (curl_errno($ch) != 0 && empty($response)) {
368 $response = false;
369 }
370 curl_close($ch);
371 return $response;
372 }
373}