Passed
Pull Request — master (#122)
by
unknown
02:29
created

wsRequestGet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 12
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 19
rs 9.8666
1
<?php
2
3
define('ATIPAY_URL', 'https://mipg.atipay.net/v1/');
4
define('ATIPAY_TOKEN_URL', ATIPAY_URL . 'get-token');
5
define('ATIPAY_REDIRECT_TO_PSP_URL', ATIPAY_URL . 'redirect-to-gateway');
6
define('ATIPAY_VERIFY_URL', ATIPAY_URL . 'verify-payment');
7
8
function fn_atipay_get_token($params)
9
{
10
    $r = wsRequestPost(ATIPAY_TOKEN_URL, $params);
11
    $return = array();
12
    if ($r) {
13
        if (isset($r['status']) && !empty($r['status'])) {
14
            $status = $r['status'];
15
            if ($status == 1) {
16
                $return['success']=1;
17
                $return['token']=$r['token'];
18
            } else {
19
                $return['success']=0;
20
                $return['errorMessage']=$r['errorDescription'];
21
            }
22
        } else {
23
            $return['success']=0;
24
            if (isset($r['faMessage']) && !empty($r['faMessage'])) {
25
                $return['errorMessage'] = $r['faMessage'];
26
            } else {
27
                $return['errorMessage'] = "خطا در دریافت توکن پرداخت";
28
            }
29
        }
30
    } else {
31
        $return['success']=0;
32
        $return['errorMessage'] = "خطا در دریافت اطلاعات توکن پرداخت";
33
    }
34
35
    return $return;
36
}
37
38
39
function fn_atipay_redirect_to_psp_form($token)
40
{
41
    $form = _fn_generate_redirect_form($token);
42
    return $form;
43
}
44
45
function _fn_generate_redirect_form($token)
46
{
47
    $form = '<form action="'.ATIPAY_REDIRECT_TO_PSP_URL.'" method="POST" align="center" name="atipay_psp_form" id="atipay_psp_form">';
48
    $form .= '<input type="hidden" value="'.$token.'" name="token" >';
49
    $form .= "<input type='submit' value='' class='d-none'/>";
50
    $form .= '</form><script>document.getElementById("atipay_psp_form").submit(); </script>';
51
52
    return $form;
53
}
54
55
function fn_atipay_get_token_form($params, $submit_text, $action)
56
{
57
    $form = _fn_generate_get_token_form($params, $submit_text, $action);
58
    return $form;
59
}
60
61
function _fn_generate_get_token_form($params, $submit_text, $action)
62
{
63
    $form ="<form action='$action' method='POST' align='center' name='atipay_payment_form_token' id='atipay_payment_form_token' >";
64
    foreach ($params as $k=>$v) {
65
        $form .= "<input type='hidden' value='$v' name='$k' >";
66
    }
67
68
    $form .= "<input type='submit' value='$submit_text' name='submit' >";
69
    $form .= "</form>";
70
71
    return $form;
72
}
73
74
function fn_check_callback_data($params)
75
{
76
    $result = array();
77
    if (isset($params['state']) && !empty($params['state'])) {
78
        $state = $params['state'];
79
        if ($state == 'OK') {
80
            $result['success']=1;
81
            $result['error']="";
82
        } else {
83
            $result['success']=0;
84
            $result['error']= _fn_return_state_text($state);
85
        }
86
    } else {
87
        $result['success']=0;
88
        $result['error']="خطای نامشخص در پرداخت. در صورتیکه مبلغی از شما کسر شده باشد، برگشت داده می شود.";
89
    }
90
91
    return $result;
92
}
93
94
function fn_atipay_verify_payment($params, $amount)
95
{
96
    $r = wsRequestPost(ATIPAY_VERIFY_URL, $params);
97
    $return = array();
98
    if ($r) {
99
        if (isset($r['amount']) && !empty($r['amount'])) {
100
            if ($r['amount'] == $amount) {
101
                $return['success']=1;
102
                $return['errorMessage']="";
103
            } else {
104
                $return['success']=0;
105
                $return['errorMessage']="خطا در تایید مبلغ پرداخت.در صورتیکه مبلغی از شما کسر شده باشد، برگشت داده می شود.";
106
            }
107
        } else {
108
            $return['success']=0;
109
            $return['errorMessage']="خطا در تایید اطلاعات پرداخت. در صورتیکه مبلغی از شما کسر شده باشد، برگشت داده می شود.";
110
        }
111
    } else {
112
        $return['success']=0;
113
        $return['errorMessage'] = "خطا در تایید نهایی پرداخت. در صورتیکه مبلغی از شما کسر شده باشد، برگشت داده می شود.";
114
    }
115
116
    return $return;
117
}
118
119
function _fn_return_state_text($state)
120
{
121
    switch ($state) {
122
        case "CanceledByUser":
123
            return "پرداخت توسط شما لغو شده است.";
124
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
125
        case "Failed":
126
            return "پرداخت انجام نشد.";
127
            break;
128
        case "SessionIsNull":
129
            return "کاربر در بازه زمانی تعیین شده پاسخی ارسال نکرده است";
130
            break;
131
        case "InvalidParameters":
132
            return "پارامترهاي ارسالی نامعتبر است";
133
            break;
134
        case "MerchantIpAddressIsInvalid":
135
            return "آدرس سرور پذیرنده نامعتبر است";
136
            break;
137
        case "TokenNotFound":
138
            return "توکن ارسال شده یافت نشد";
139
            break;
140
        case "TokenRequired":
141
            return "با این شماره ترمینال فقط تراکنش هاي توکنی قابل پرداخت هستند";
142
            break;
143
        case "TerminalNotFound":
144
            return "شماره ترمینال ارسال شده یافت نشد";
145
            break;
146
        default:
147
            return "خطای نامشخص در عملیات پرداخت";
148
149
    }
150
}
151
152
153
function fn_check_callback_params($params)
154
{
155
    if (!isset($params['state']) ||
156
        !isset($params['status']) ||
157
        !isset($params['reservationNumber']) ||
158
        !isset($params['referenceNumber']) ||
159
        !isset($params['terminalId']) ||
160
        !isset($params['traceNumber'])) {
161
        return false;
162
    } else {
163
        return true;
164
    }
165
}
166
167
168
169
170
171
function wsRequestGet($url)
172
{
173
    set_time_limit(30);
174
175
    $ch = curl_init($url);
176
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
177
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
178
    curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout in seconds
179
    $json = curl_exec($ch);
180
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
181
    curl_close($ch);
182
183
    if ($httpcode == "200") {
184
        //nothing YET
185
    } else {
186
        $json= json_encode(array('error'=>'Y'));
187
    }
188
189
    return $json;
190
}
191
192
function wsRequestPost($url, $params)
193
{
194
    set_time_limit(30);
195
    $ch = curl_init($url);
196
    $postFields = json_encode($params);
197
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
198
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
199
    curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout in seconds
200
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json;"));
201
    curl_setopt($ch, CURLOPT_POST, 1);
202
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
203
    $json = curl_exec($ch);
204
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
205
    curl_close($ch);
206
207
    if ($httpcode == "200") {
208
        return json_decode($json, true);
0 ignored issues
show
Bug introduced by
It seems like $json 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

208
        return json_decode(/** @scrutinizer ignore-type */ $json, true);
Loading history...
209
    } else {
210
        $json = array('error'=>'Y','jsonError'=>$httpcode,'message'=>$httpcode);
211
    }
212
213
    return $json;
214
}
215
216
217
function fn_atipay_get_invoice($invoice_id)
218
{
219
    $command = 'GetInvoice';
220
    $postData = array(
221
        'invoiceid' => $invoice_id,
222
    );
223
    $results = localAPI($command, $postData);
0 ignored issues
show
Bug introduced by
The function localAPI was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

223
    $results = /** @scrutinizer ignore-call */ localAPI($command, $postData);
Loading history...
224
    return $results;
225
}
226