hxp
2025-06-04 f4a514d5ac952110da846636ecbb9de951eaf3d2
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
<?php
include_once '/Common/CommFunc.php';
include_once '/Common/Logging.php';
include_once "/ProjComm/CfgReader.php";
 
header("Content-type: text/html; charset=utf-8");
 
// https://developer.shg.vn/article/view?id=87&lang=ch
 
\Logging\CreateLogging("sohagame.payment.php");
\Logging\LogInfo("_POST: " . print_r($_POST, true));
 
$result = array('status' => 'failed', 'message' => '', 'repeat' => 0, 'request' => $_REQUEST);
 
$signed_request = $_POST['signed_request'];
if ($signed_request == '') {
    $result['message'] = "signed_request empty";
    echo json_encode($result);
    exit;
}
 
$spid = "sohagame";
if ($_POST["spid"]) {
    // 可选参数,也可用于测试用
    $spid = $_POST["spid"];
}
 
\CommFunc\GetKeyFromConfig(dirname(__FILE__) . '\\..\\..\\InterfaceConfig.php', "SP." . $spid, "app_secret", $app_secret);
$redata = parse_signed_request($signed_request, $app_secret);
if ($redata) { //verify ok
    \Logging\LogInfo("redata: " . print_r($redata, true));
    // detail $re:
 
    //user_id: id of user platform
    //app_id: app_id of user platform
    //order_id: id of order (unix)(note :  order_id is unix,game must check before add money in game for ensure one order_id add one time only)
    //order_info: code of package
    //role_id: code of character
    //area_id: code of  server
    //time: time call
 
    //handle add money game to account here
    $returnArr = send_to_gameserver_exchange($spid, $redata);
    \Logging\LogInfo("returnArr: " . print_r($returnArr, true));
 
    //if order_id has added before then no add more and set $result = array('status' => 'settled','message'=>'success','repeat'=>1);
 
    //if add money game success then
    // $result = array('status' => 'settled', 'message' => 'success', 'repeat' => 0, 'request' => $_REQUEST);
 
    //else add money game error then
    //$result = array('status' => 'failed','message'=>'');
    //  message : reason fail
    //if fail platform's server will try call again after if fail too then rollback money of platform and notice to user
 
    switch ($returnArr["errorcode"]) {
        case 1:
            $result['status'] = "settled";
            $result['message'] = "success";
            $result['repeat'] = 0;
            break;
        case 2:
            $result['status'] = "settled";
            $result['message'] = "success";
            $result['repeat'] = 1;
            break;
        default:
            $result['status'] = "failed";
            $result['message'] = $returnArr["errordesc"];
            break;
    }
} else {
    $result['message'] = "verify incorrect";
}
\Logging\LogInfo("result: " . print_r($result, true));
echo json_encode($result);
exit;
 
function send_to_gameserver_exchange($spid, $redata)
{
    $returnArr = array("errorcode" => 0, "errordesc" => "fail");
    $area_id = $redata["area_id"];
    $serverIDArray = array(intval($area_id));
    $serverPageInfo = \CommFunc\GetGameServerPageInfo($spid, $serverIDArray);
    if (count($serverPageInfo) <= 0) {
        $returnArr["errordesc"] = "not serverPageInfo";
        return $returnArr;
    }
 
    // 目标角色对应游戏服务器gmtool地址
    $serverPageValues = array_values($serverPageInfo);
 
    $pageUrl = $serverPageValues[0]["Page"];
    if (!$pageUrl) {
        $returnArr["errordesc"] = "not page url";
        return $returnArr;
    }
 
    $appID = $redata["app_id"];
 
    if (!\CfgReader\ReadConfig()) {
        $returnArr["errordesc"] = "cfgreader error";
        return $returnArr;
    }
 
    if (
        !\CfgReader\GetConfigData("ExChange", "Key_" . $appID, $strKey) ||
        !\CfgReader\GetConfigData("Config", "IsDecryptKey", $isDecryptKey)
    ) {
        $returnArr["errordesc"] = "not exChange appid key";
        return $returnArr;
    }
 
    //如果配置了需要解密,对登录key进行解密
    if ($isDecryptKey == 1) {
        $strKey = \CommFunc\GetDecodePsw($strKey);
    }
 
    \Logging\LogInfo("strKey: " . $strKey);
 
    $postUrl = str_replace("Server/Tool.php", "api/exchange/index.php", $pageUrl);
    \Logging\LogInfo("postUrl: " . $postUrl);
 
    $payData = array(
        "AccountID" => $redata["user_id"],
        "RegionName" => "s" . $area_id,
        "OrderAmount" => 0, // 越南版没有传订单金额
        "BillNO" => $redata["order_id"],
        "OrderInfo" => $redata["order_info"],
        "OperatorID" => $appID,
    );
 
    $opqid = $payData['AccountID'];
    $orderID = $payData["BillNO"];
    $orderAmount = $payData['OrderAmount'];
    $getServer_id = $payData['RegionName'];
    $sign = md5($opqid . $orderAmount . $orderID . $getServer_id . $strKey);
    $payData["Sign"] = $sign;
    \Logging\LogInfo("payData: " . print_r($payData, true));
    $payretStr = \CommFunc\DoPost($postUrl, $payData);
    \Logging\LogInfo("payretStr: " . $payretStr);
    return json_decode($payretStr, true);
}
 
function parse_signed_request($signed_request, $secret)
{
    list($encoded_sig, $payload) = explode('.', $signed_request, 2);
    // decode the data
    $sig = base64_decode(strtr($encoded_sig, '-_', '+/'));
    $data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
 
    if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
        // error_log('Unknown algorithm. Expected HMAC-SHA256');
        return false;
    }
    // check sig
    $expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
    if ($sig !== $expected_sig) {
        return false;
    }
    return $data;
}