<?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; 
 | 
} 
 |