Passed
Pull Request — master (#122)
by
unknown
07:52
created

fn_check_callback_params()   B

Complexity

Conditions 7
Paths 2

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 9
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
rs 8.8333
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
11
    $r = wsRequestPost(ATIPAY_TOKEN_URL,$params);
12
    $return = array();
13
    if ($r){
14
        if (isset($r['status']) && !empty($r['status'])){
15
            $status = $r['status'];
16
            if ($status == 1){
17
                $return['success']=1;
18
                $return['token']=$r['token'];
19
            }else{
20
                $return['success']=0;
21
                $return['']=$r['errorDescription'];
22
            }
23
        }else{
24
            $return['success']=0;
25
            if (isset($r['faMessage']) && !empty($r['faMessage'])){
26
                $return['errorMessage'] = $r['faMessage'];
27
            }else{
28
                $return['errorMessage'] = "خطا در دریافت توکن پرداخت";
29
            }
30
        }
31
32
    }else{
33
        $return['success']=0;
34
        $return['errorMessage'] = "خطا در دریافت اطلاعات توکن پرداخت";
35
    }
36
37
    return $return;
38
}
39
40
41
function fn_atipay_redirect_to_psp_form($token)
42
{
43
    $form = _fn_generate_redirect_form($token);
44
    return $form;
45
}
46
47
function _fn_generate_redirect_form($token)
48
{
49
50
    $form = '<form action="'.ATIPAY_REDIRECT_TO_PSP_URL.'" method="POST" align="center" name="atipay_psp_form" id="atipay_psp_form">';
51
    $form .= '<input type="hidden" value="'.$token.'" name="token" >';
52
    $form .= "<input type='submit' value='' class='d-none'/>";
53
    $form .= '</form><script>document.getElementById("atipay_psp_form").submit(); </script>';
54
55
    return $form;
56
}
57
58
function fn_atipay_get_token_form($params, $submit_text,$action)
59
{
60
    $form = _fn_generate_get_token_form($params, $submit_text, $action);
61
    return $form;
62
}
63
64
function _fn_generate_get_token_form($params, $submit_text, $action)
65
{
66
67
    $form ="<form action='$action' method='POST' align='center' name='atipay_payment_form_token' id='atipay_payment_form_token' >";
68
    foreach ($params as $k=>$v){
69
        $form .= "<input type='hidden' value='$v' name='$k' >";
70
    }
71
72
    $form .= "<input type='submit' value='$submit_text' name='submit' >";
73
    $form .= "</form>";
74
75
    return $form;
76
}
77
78
function fn_check_callback_data($params)
79
{
80
    $result = array();
81
    if (isset($params['state']) && !empty($params['state'])){
82
        $state = $params['state'];
83
        if ($state == 'OK'){
84
            $result['success']=1;
85
            $result['error']="";
86
        }else{
87
            $result['success']=0;
88
            $result['error']= _fn_return_state_text($state);
89
        }
90
    }else{
91
        $result['success']=0;
92
        $result['error']="خطای نامشخص در پرداخت. در صورتیکه مبلغی از شما کسر شده باشد، برگشت داده می شود.";
93
    }
94
95
    return $result;
96
}
97
98
function fn_atipay_verify_payment($params,$amount)
99
{
100
    $r = wsRequestPost(ATIPAY_VERIFY_URL,$params);
101
    $return = array();
102
    if ($r){
103
104
        if (isset($r['amount']) && !empty($r['amount'])){
105
            if ($r['amount'] == $amount){
106
                $return['success']=1;
107
                $return['errorMessage']="";
108
            }else{
109
                $return['success']=0;
110
                $return['errorMessage']="خطا در تایید مبلغ پرداخت.در صورتیکه مبلغی از شما کسر شده باشد، برگشت داده می شود.";
111
            }
112
        }else{
113
            $return['success']=0;
114
            $return['errorMessage']="خطا در تایید اطلاعات پرداخت. در صورتیکه مبلغی از شما کسر شده باشد، برگشت داده می شود.";
115
        }
116
    }else{
117
        $return['success']=0;
118
        $return['errorMessage'] = "خطا در تایید نهایی پرداخت. در صورتیکه مبلغی از شما کسر شده باشد، برگشت داده می شود.";
119
    }
120
121
    return $return;
122
}
123
124
function _fn_return_state_text($state)
125
{
126
    switch ($state){
127
        case "CanceledByUser":
128
            return "پرداخت توسط شما لغو شده است.";
129
            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...
130
        case "Failed":
131
            return "پرداخت انجام نشد.";
132
            break;
133
        case "SessionIsNull":
134
            return "کاربر در بازه زمانی تعیین شده پاسخی ارسال نکرده است";
135
            break;
136
        case "InvalidParameters":
137
            return "پارامترهاي ارسالی نامعتبر است";
138
            break;
139
        case "MerchantIpAddressIsInvalid":
140
            return "آدرس سرور پذیرنده نامعتبر است";
141
            break;
142
        case "TokenNotFound":
143
            return "توکن ارسال شده یافت نشد";
144
            break;
145
        case "TokenRequired":
146
            return "با این شماره ترمینال فقط تراکنش هاي توکنی قابل پرداخت هستند";
147
            break;
148
        case "TerminalNotFound":
149
            return "شماره ترمینال ارسال شده یافت نشد";
150
            break;
151
        default:
152
            return "خطای نامشخص در عملیات پرداخت";
153
154
    }
155
}
156
157
158
function fn_check_callback_params($params)
159
{
160
    if (!isset($params['state']) ||
161
        !isset($params['status']) ||
162
        !isset($params['reservationNumber']) ||
163
        !isset($params['referenceNumber']) ||
164
        !isset($params['terminalId']) ||
165
        !isset($params['traceNumber'])){
166
        return false;
167
    }else{
168
        return true;
169
    }
170
}
171
172
173
174
175
176
function wsRequestGet($url)
177
{
178
    set_time_limit(30);
179
180
    $ch = curl_init($url);
181
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
182
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
183
    curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout in seconds
184
    $json = curl_exec($ch);
185
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
186
    curl_close($ch);
187
188
    if ($httpcode == "200"){
189
        //nothing YET
190
    }else{
191
        $json= json_encode(array('error'=>'Y'));
192
    }
193
194
    return $json;
195
}
196
197
function wsRequestPost($url,$params)
198
{
199
    set_time_limit(30);
200
    $ch = curl_init($url);
201
    $postFields = json_encode($params);
202
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
203
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
204
    curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout in seconds
205
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json;"));
206
    curl_setopt($ch, CURLOPT_POST, 1);
207
    curl_setopt($ch, CURLOPT_POSTFIELDS,$postFields);
208
    $json = curl_exec($ch);
209
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
210
    curl_close($ch);
211
212
    if ($httpcode == "200"){
213
        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

213
        return json_decode(/** @scrutinizer ignore-type */ $json,true);
Loading history...
214
    }else{
215
        $json = array('error'=>'Y','jsonError'=>$httpcode,'message'=>$httpcode);
216
    }
217
218
    return $json;
219
}
220
221
222
function fn_atipay_get_invoice($invoice_id)
223
{
224
    $command = 'GetInvoice';
225
    $postData = array(
226
        'invoiceid' => $invoice_id,
227
    );
228
    $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

228
    $results = /** @scrutinizer ignore-call */ localAPI($command, $postData);
Loading history...
229
    return $results;
230
231
}
232
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
233