/var/www/hkosl.com/b2b2c/api.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
    $dbh 
= new PDO("mysql:host=192.168.171.23;dbname=b2b2c_api"'admin''PHac7dru');

    if (!
function_exists('bind_pdo')) {
        function 
bind_pdo($sql$parameters NULL$action NULL$escape true$_dbh=null)
        {
            if(empty(
$_dbh)){
                global 
$dbh;
            }else{
                
$dbh $_dbh;
            }

            if (
$action == "insert" || $action == "update" || $action == "delete" || empty($action)) {
                if (!(
$sth $dbh->prepare($sql))) {
                    throw new 
Exception('[' $sth->errorCode() . ']: ' print_r($sth->errorInfo()));
                }

                if (!
$sth->execute($parameters)) {
                    throw new 
Exception('[' $sth->errorCode() . ']: ' print_r($sth->errorInfo()));
                }
            }

            if (
$action == "selectone") {
                if (!(
$sth $dbh->prepare($sql))) {
                    throw new 
Exception('[' $sth->errorCode() . ']: ' print_r($sth->errorInfo()));
                }

                if (!
$sth->execute($parameters)) {
                    throw new 
Exception('[' $sth->errorCode() . ']: ' print_r($sth->errorInfo()));
                }

                
$result $sth->fetch(PDO::FETCH_ASSOC);
                
/*if (!empty($result)) {
                    foreach ($result as $key => $row) {
                        $result[$key] = escape_string($result[$key]);
                    }
                }*/
                
return $result;

            }

            if (
$action == "selectall") {
                if (!(
$sth $dbh->prepare($sql))) {
                    throw new 
Exception('[' $sth->errorCode() . ']: ' print_r($sth->errorInfo()));
                }

                if (!
$sth->execute($parameters)) {
                    throw new 
Exception('[' $sth->errorCode() . ']: ' print_r($sth->errorInfo()));
                }

                
$result $sth->fetchAll(PDO::FETCH_ASSOC);
                
/*if (!empty($result)) {
                    foreach ($result as $key => $row) {
                        foreach ($row as $key2 => $row2) {
                            $result[$key][$key2] = escape_string($result[$key][$key2]);
                        }
                    }
                }*/
                
return $result;
            }

            if (
$action == "dump") {
                return 
dump_sql($sql$parameters);
            }

        }
    }

    if (!
function_exists('insert_record')) {
        function 
insert_record($table_name$data$dump_sql NULL)
        {
            
$result "";

            if (!empty(
$table_name) && !empty($data)) {
                
$sql        "insert into `" $table_name "` set ";
                
$parameters = array();
                foreach (
$data as $table_field => $value) {
                    
$sql .= "`" $table_field "` = ?, ";
                    
$parameters[] = $value;
                }

                
$sql substr_replace($sql"", -2);
            }

            if (!empty(
$dump_sql)) {
                
$result dump_sql($sql$parameters);
            } else {
                
$result bind_pdo($sql$parameters);
            }

            return 
$result;
        }
    }

    if (!
function_exists('update_record')) {
        function 
update_record($table_name$data$where$dump_sql NULL)
        {
            
$result "";

            if (!empty(
$table_name) && !empty($data)) {
                
$sql        "update `" $table_name "` set ";
                
$parameters = array();
                foreach (
$data as $table_field => $value) {
                    
$sql .= "`" $table_field "` = ?, ";
                    
$parameters[] = $value;
                }
                
$sql substr_replace($sql"", -2);

                
$sql .= " where " $where["sql"];
                foreach (
$where["parameters"] as $table_field => $value) {
                    
$parameters[] = $value;
                }

                if (!empty(
$dump_sql)) {
                    
$result dump_sql($sql$parameters);
                } else {
                    
$result bind_pdo($sql$parameters);
                }

            }

            return 
$result;
        }
    }

    if (!
function_exists('dump_sql')) {
        function 
dump_sql($sql$parameters)
        {
            
$keys = array();

            
# build a regular expression for each parameter
            
foreach ($parameters as $key => $value) {
                if (
is_string($key)) {
                    
$keys[] = '/:' $key '/';
                } else {
                    
$keys[] = '/[?]/';
                }
            }

            foreach (
$parameters as $key2 => $value) {
                
$parameters[$key2] = "'" $value "'";
            }

            
$sql preg_replace($keys$parameters$sql1$count);

            
#trigger_error('replaced '.$count.' keys');

            
return $sql;
        }
    }

    if (!
function_exists('debug_log')) {
        function 
debug_log($code$data)
        {
            
$date = new DateTime();
            
$date->modify("-2 month");

            
$sql        "delete from debug_log where createdate <= ?";
            
$parameters = array($date->format("Y-m-d"));
            
bind_pdo($sql$parameters);

            
$sql        "insert into debug_log (code, log_data, createdate) value (?,?,?)";
            
$parameters = array($codejson_encode($data), date("Y-m-d H:i:s"));
            
bind_pdo($sql$parameters);
        }
    }

    
//include 'webadmin/libraies/autoload.php';

    /*
     * https://github.com/klein/klein.php
     * composer require klein/klein
     *
     * api list:
     * - payease settlement
     * - merchant application result
     */

    
function _msg_sign($text$key "test")
    {
        return 
hash_hmac('md5'$text$key);
    }

    
/*
     * v_mid 商戶編號
     * v_subid 子商戶編號
     * v_auditstatus 審核狀態
     * v_auditstatusdesc 審核結果說明
     * v_mac 數字指紋
     *
     */

    /*$_REQUEST["v_mid"]         = 10903;
    $_REQUEST["v_subid"]       = 1;
    $_REQUEST["v_auditstatus"] = 1;*/

    
insert_record('api_log', array('api' => 'merchant application''data' => json_encode(array($_REQUEST))));

    
$v_mid             $_REQUEST["v_mid"];
    
$v_subid           $_REQUEST["v_subid"];
    
$v_auditstatus     $_REQUEST["v_auditstatus"];
    
$v_auditstatusdesc $_REQUEST["v_auditstatusdesc"];
    
$v_mac             $_REQUEST["v_mac"];

    function 
hmac($key$data)
    {
        
// 创建 md5的HMAC

        
$b 64// md5加密字节长度
        
if (strlen($key) > $b) {
            
$key pack("H*"md5($key));
        }
        
$key    str_pad($key$bchr(0x00));
        
$ipad   str_pad(''$bchr(0x36));
        
$opad   str_pad(''$bchr(0x5c));
        
$k_ipad $key $ipad;
        
$k_opad $key $opad;

        return 
md5($k_opad pack("H*"md5($k_ipad $data)));
    }

    if (!empty(
$v_mid)) {
        
$key  'test';//商户的密钥
        
$data $v_mid $v_subid $v_auditstatus;//七个参数的拼串

        //$data="error";
        
$v_md5info hmac($key$data);

        
header("Content-type: text/xml");

        echo 
'<?xml version="1.0" encoding="utf-8"?><message><status>0</status><statusdesc>success</statusdesc><mid>';
        echo 
$v_mid;
        echo 
'</mid><subid>';
        echo 
$v_subid;
        echo 
'</subid><auditstatus>';
        echo 
$v_auditstatus;
        echo 
'</auditstatus><mac>';
        echo 
$v_md5info;
        echo 
'</mac></message>';

    } else {
        
header("Content-type: text/xml");
        echo 
'<?xml version="1.0" encoding="utf-8"?><message><status>1</status><statusdesc>other error</statusdesc></message>';
    }