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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php
 
namespace PayOrder;
 
include_once '/Common/CommFunc.php';
include_once "/db/DBOper.php";
include_once "/ProjComm/CfgReader.php";
 
/**
 * 中心执行创建充值订单
 */
function CreatePayOrder($Channel, $AccountID, $ServerID, $OrderAmount, $OrderInfo)
{
    $OrderID = date("YmdHis") . rand(10000, 99999) . rand(10000, 99999);
    if (!\DBOper\Insert("PayOrder", array(
        "OrderID" => $OrderID,
        "Channel" => $Channel,
        "AccountID" => $AccountID,
        "ServerID" => intval($ServerID),
        "OrderInfo" => $OrderInfo,
        "OrderAmount" => $OrderAmount,
        "Createtime" => date("Y-m-d H:i:s"),
        "State" => 0,
        "SyncRet" => array(),
    ))) {
        return "";
    }
    return $OrderID;
}
 
/**
 * 中心执行平台充值订单回调
 * @param string $appID appID
 * @param string $OrderID cp订单ID
 * @param string $OrderIDSDK sp订单ID
 * @param string $AccountID sp账号,如果有区分渠道ID,需要组合上渠道ID
 * @param int $ServerID 区服ID
 * @param string $OrderInfo 商品编号
 * @param string $OrderAmount 订单金额,精确到小数点后面两位,如果有区分支付金额及下单金额(原价),则参数格式为: "支付金额|下单金额"
 * @param string $payTime 支付时间,具体看sp是否有同步,没有的话传空即可,格式 Y-m-d H:i:s
 * @param string $checkCreate 是否检查订单号是否存在,如果有先创建订单的步骤,额需要传true进行检查,否则可不检查,直接等sp回调后插入订单信息
 * @param string $checkFunc 额外逻辑函数名,根据不同平台对接sdk决定,默认参数传入 $dbOrderInfo
 * @param bool $repeat 是否补单
 * @param array $extras 扩展数据,json字符串格式
 * @return array {"errorcode":返回码, "errordesc":"描述信息"} 返回码为0时代表失败,1-成功,2-重复成功记录
 */
function DoReceivePayOrder($appID, $OrderID, $OrderIDSDK, $AccountID, $ServerID, $OrderInfo, $OrderAmount, $payTime, $checkCreate, $checkFunc = null, $repeat = false, $extras = "")
{
    \Logging\LogInfo("DoReceivePayOrder appID:" . $appID . " OrderID:" . $OrderID . " OrderIDSDK:" . $OrderIDSDK . " AccountID:" . $AccountID .
        " ServerID:" . $ServerID . " OrderInfo:" . $OrderInfo . " OrderAmount:" . $OrderAmount . " payTime:" . $payTime .
        " checkCreate:" . $checkCreate);
    if (!$OrderID) {
        $OrderID = $OrderIDSDK;
    }
    $OriginalAmount = $OrderAmount;    // 下单金额
    $ServerID = intval($ServerID);
    $returnArr = array("errorcode" => 0, "errordesc" => "failed");
    if (!\DBOper\FindOne("PayOrder", array("OrderID" => $OrderID), $dbOrderInfo, null, false)) {
        $returnArr["errordesc"] = "failed:find error";
        return $returnArr;
    }
    \Logging\LogInfo("dbOrderInfo:" . print_r($dbOrderInfo, true));
 
    if ($checkCreate && !isset($dbOrderInfo)) {
        $returnArr["errordesc"] = "failed:orderID is not exist";
        \Logging\LogWarn("orderID is not exist: OrderID:" . $OrderID);
        return $returnArr;
    }
 
    if ($checkFunc) {
        $errordesc = $checkFunc($dbOrderInfo);
        if ($errordesc) {
            $returnArr["errordesc"] = $errordesc;
            return $returnArr;
        }
    }
 
    // 有创建订单的,验证数据是否一致
    if (isset($dbOrderInfo)) {
        // 已经处理过的订单
        if (!$repeat && $dbOrderInfo["State"] == 1) {
            $returnArr["errorcode"] = 2;
            $returnArr["errordesc"] = "failed:repeat commit";
            \Logging\LogWarn("repeat payorder: OrderID:" . $OrderID);
            return $returnArr;
        }
 
        if ($appID && $appID != $dbOrderInfo["Channel"]) {
            $returnArr["errordesc"] = "failed:order appID error";
            return $returnArr;
        }
        $appID = $dbOrderInfo["Channel"];
 
        if ($AccountID && $AccountID != $dbOrderInfo["AccountID"]) {
            $returnArr["errordesc"] = "failed:order AccountID error";
            return $returnArr;
        }
        $AccountID = $dbOrderInfo["AccountID"];
 
        if ($ServerID && $ServerID != $dbOrderInfo["ServerID"]) {
            $returnArr["errordesc"] = "failed:order ServerID error";
            return $returnArr;
        }
        $ServerID = $dbOrderInfo["ServerID"];
 
        if ($OrderAmount && $OrderAmount != $dbOrderInfo["OrderAmount"]) {
            $returnArr["errordesc"] = "failed:order OrderAmount error";
            return $returnArr;
        }
        $OrderAmount = $dbOrderInfo["OrderAmount"];
 
        if ($dbOrderInfo["OriginalAmount"]) {
            $OriginalAmount = $dbOrderInfo["OriginalAmount"];
        }
 
        if ($OrderInfo && $OrderInfo != $dbOrderInfo["OrderInfo"]) {
            $returnArr["errordesc"] = "failed:order OrderInfo error";
            return $returnArr;
        }
        $OrderInfo = $dbOrderInfo["OrderInfo"];
 
        if ($dbOrderInfo["PayTime"]) {
            $payTime = $dbOrderInfo["PayTime"];
        }
 
        if ($dbOrderInfo["Extras"]) {
            $extras = $dbOrderInfo["Extras"];
        }
    } else {
        $amounts = explode("|", $OrderAmount);
        if (count($amounts) > 1) {
            $OrderAmount = $amounts[0];
            $OriginalAmount = $amounts[1];
            \Logging\LogInfo("OrderAmount:" . $OrderAmount . " OriginalAmount:" . $OriginalAmount);
        }
    }
 
    if (!$appID || !$AccountID || !$ServerID || !$OrderInfo || !$OrderAmount) {
        $returnArr["errordesc"] = "failed:order param error";
        return $returnArr;
    }
 
    if (!$payTime) {
        $payTime = date("Y-m-d H:i:s");
    }
 
    $setDBOrderData = array(
        "OrderID" => $OrderID,
        "OrderIDSDK" => $OrderIDSDK,
        "Channel" => $appID,
        "AccountID" => $AccountID,
        "ServerID" => $ServerID,
        "OrderInfo" => $OrderInfo,
        "OrderAmount" => $OrderAmount, // 中心的这个字段是实际支付金额
        "OriginalAmount" => $OriginalAmount,
        "PayTime" => $payTime,
        "State" => 0,
        "SyncRet" => "", // 同步游戏服务器结果明细
        "Extras" => $extras,
    );
 
    $payData = array(
        "AccountID" => $AccountID,
        "RegionName" => "s" . $ServerID,
        "OrderAmount" => $OriginalAmount, // 同步给游戏服务器的这个是用原价
        "BillNO" => $OrderID,
        "OrderInfo" => $OrderInfo,
        "OperatorID" => $appID,
        "Extras" => $extras,
    );
 
    if (!isset($dbOrderInfo)) {
        $setDBOrderData["Createtime"] = date("Y-m-d H:i:s");
    }
    $returnArr = SendExchangeToGameServers($appID, $ServerID, $appID, $payData);
    $setDBOrderData["UpdateTime"] = date("Y-m-d H:i:s");
    switch ($returnArr["errorcode"]) {
        case 1:
            $setDBOrderData["State"] = 1;
            $setDBOrderData["SyncRet"] = $returnArr;
            break;
        case 2:
            $setDBOrderData["State"] = 1;
            $setDBOrderData["SyncRet"] = $returnArr;
            break;
        default:
            $setDBOrderData["State"] = 2;
            $setDBOrderData["SyncRet"] = $returnArr;
            $returnArr["errordesc"] = "failed:" . $returnArr["errordesc"];
            break;
    }
 
    if (!\DBOper\Update("PayOrder", array("OrderID" => $OrderID), $setDBOrderData, false, true)) {
        $returnArr["errorcode"] = 0;
        $returnArr["errordesc"] = "failed:update error";
    } else {
        \Logging\LogInfo("setDBOrderData:" . print_r($setDBOrderData, true));
    }
    AddAccountFirstPay($appID, $AccountID, $setDBOrderData);
    return $returnArr;
}
 
/**添加平台账号首充 */
function AddAccountFirstPay($appID, $AccountID, $payOrderInfo)
{
    $find = array("Channel" => $appID, "AccountID" => $AccountID);
    if (!\DBOper\FindOne("AccountFirstPay", $find, $findData, null, false)) {
        return;
    }
    if (isset($findData)) {
        // 已存在,不再重复添加
        return;
    }
    $insArray = array(
        "Channel" => $appID,
        "AccountID" => $AccountID,
        "OrderID" => $payOrderInfo["OrderID"],
        "OrderIDSDK" => $payOrderInfo["OrderIDSDK"],
        "ServerID" => $payOrderInfo["ServerID"],
        "OrderInfo" => $payOrderInfo["OrderInfo"],
        "OrderAmount" => $payOrderInfo["OrderAmount"], // 中心的这个字段是实际支付金额
        "OriginalAmount" => $payOrderInfo["OriginalAmount"],
        "PayTime" => $payOrderInfo["PayTime"],
        "PayYMD" => substr($payOrderInfo["PayTime"], 0, 10),
        "Extras" => $payOrderInfo["Extras"],
    );
    \DBOper\Insert("AccountFirstPay", $insArray);
}
 
/**
 * 发送渠道回调充值到游戏服务器
 * @param string $spid 渠道
 * @param int $serverID 服务器ID
 * @param string $appID appID
 * @param array $payData 同步游戏服务器的充值数据 
 *                array(
 *                     "AccountID" => 渠道账号,
 *                     "RegionName" => "s" . 区服ID,
 *                     "OrderAmount" => 充值金额,单位分, 
 *                     "BillNO" => 订单编号,
 *                     "OrderInfo" => 商品编号,
 *                     "OperatorID" => $appID,
 *                     );
 * @return array array("errorcode" => xxx, "errordesc" => "xxx") errorcode:1-成功,2-成功重复数据,其他失败
 */
function SendExchangeToGameServers($spid, $serverID, $appID, $payData)
{
    \Logging\LogInfo("SendExchangeToGameServers spid:" . $spid . " serverID:" . $serverID . " appID:" . $appID);
    $returnArr = array("errorcode" => 0, "errordesc" => "fail");
    $serverIDArray = array($serverID);
    $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;
    }
 
    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);
 
    $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);
    $ret = json_decode($payretStr, true);
    \Logging\LogInfo("ret: " . print_r($ret, true));
    if (isset($ret)) {
        return $ret;
    }
    return $returnArr;
}