1 | <?php |
||
2 | |||
3 | namespace BPT\pay; |
||
4 | |||
5 | use BPT\pay\idpay\errorInterface; |
||
6 | use BPT\pay\idpay\paymentCreateInterface; |
||
7 | use BPT\pay\idpay\paymentInterface; |
||
8 | use BPT\pay\idpay\paymentListInterface; |
||
9 | use CurlHandle; |
||
10 | |||
11 | class idpay { |
||
12 | const API_BASE = 'https://api.idpay.ir/v1.1/'; |
||
13 | |||
14 | private static CurlHandle $session; |
||
15 | |||
16 | public static function init (string $api_key = '', bool $sandbox = false): void { |
||
17 | self::$session = curl_init(); |
||
18 | curl_setopt(self::$session, CURLOPT_RETURNTRANSFER, true); |
||
19 | curl_setopt(self::$session, CURLOPT_SSL_VERIFYPEER, 1); |
||
20 | curl_setopt(self::$session, CURLOPT_SSL_VERIFYHOST, 2); |
||
21 | curl_setopt(self::$session, CURLOPT_HTTPHEADER, [ |
||
22 | 'Content-Type: application/json', |
||
23 | 'X-API-KEY: ' . $api_key, |
||
24 | 'X-SANDBOX: ' . (int) ($sandbox), |
||
25 | ]); |
||
26 | curl_setopt(self::$session, CURLOPT_POST, true); |
||
27 | } |
||
28 | |||
29 | private static function execute (string $endpoint, array $params) { |
||
30 | foreach ($params as $key => $value) { |
||
31 | if (empty($value)) { |
||
32 | unset($params[$key]); |
||
33 | } |
||
34 | } |
||
35 | |||
36 | $session = self::$session; |
||
37 | |||
38 | curl_setopt($session, CURLOPT_POSTFIELDS, json_encode($params)); |
||
39 | curl_setopt($session, CURLOPT_URL, self::API_BASE . $endpoint); |
||
40 | |||
41 | return json_decode(curl_exec($session)); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @return paymentCreateInterface|errorInterface|object|bool |
||
46 | */ |
||
47 | public static function createPayment (string $order_id, int $amount, string $name = '', string $phone = '', string $mail = '', string $desc = '', string $callback = ''): object|bool { |
||
48 | return self::execute('payment', [ |
||
49 | 'order_id' => $order_id, |
||
50 | 'amount' => $amount, |
||
51 | 'name' => $name, |
||
52 | 'phone' => $phone, |
||
53 | 'mail' => $mail, |
||
54 | 'desc' => $desc, |
||
55 | 'callback' => $callback, |
||
56 | ]); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @return paymentInterface|errorInterface|object|bool |
||
61 | */ |
||
62 | public static function paymentDetail (string $id, string $order_id): object { |
||
63 | return self::execute('payment/inquiry', [ |
||
64 | 'order_id' => $order_id, |
||
65 | 'id' => $id |
||
66 | ]); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @return paymentInterface|errorInterface|object|bool |
||
71 | */ |
||
72 | public static function paymentConfirm (string $id, string $order_id): object { |
||
73 | return self::execute('payment/verify', [ |
||
74 | 'order_id' => $order_id, |
||
75 | 'id' => $id |
||
76 | ]); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @return paymentListInterface|errorInterface|object|bool |
||
81 | */ |
||
82 | public static function paymentList (int $page = 0, int $page_size = 25): object { |
||
83 | return self::execute('payment/transactions', [ |
||
84 | 'page' => $page, |
||
85 | 'page_size' => $page_size |
||
86 | ]); |
||
87 | } |
||
88 | |||
89 | public static function processCallback (): bool|int { |
||
90 | if ($_SERVER['REQUEST_METHOD'] === 'POST') { |
||
91 | $response = $_POST; |
||
92 | } |
||
93 | elseif ($_SERVER['REQUEST_METHOD'] === 'GET') { |
||
94 | $response = $_GET; |
||
95 | } |
||
96 | else { |
||
97 | return false; |
||
98 | } |
||
99 | |||
100 | if (empty($response['status']) || empty($response['id']) || empty($response['track_id']) || empty($response['order_id'])) { |
||
101 | return false; |
||
102 | } |
||
103 | if ($response['status'] != 10) { |
||
104 | return $response['status']; |
||
105 | } |
||
106 | |||
107 | $detail = self::paymentDetail($response['id'], $response['order_id']); |
||
108 | if (!isset($detail->status)) { |
||
109 | return false; |
||
110 | } |
||
111 | if ($detail->status != 10) { |
||
112 | return $detail->status; |
||
113 | } |
||
114 | |||
115 | return self::paymentConfirm($response['id'], $response['order_id'])->status; |
||
116 | } |
||
117 | } |