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

fn_atipay_get_token()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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