Passed
Push — main ( 6b17f2...1e3b41 )
by Miaad
11:18
created

idpay::processCallback()   B

Complexity

Conditions 10
Paths 11

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 10
eloc 16
c 1
b 0
f 1
nc 11
nop 0
dl 0
loc 27
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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));
0 ignored issues
show
Bug introduced by
It seems like curl_exec($session) can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
        return json_decode(/** @scrutinizer ignore-type */ curl_exec($session));
Loading history...
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
}