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