$mysql_conf['host'], 'port' => $mysql_conf['port'], 'user' => $mysql_conf['user'], 'passwd' => $mysql_conf['passwd'], 'dbname' => DBNAME_PREFIX . $mysql_conf['instance_id'] )); return $conn; } public function prePay() { $account_id = $_REQUEST['account_id']; //登录校验 $login = loginVerify($account_id, $_REQUEST['session_id']); if (!$login) { phpcommon\sendError(ERR_USER_BASE + 1, 'session无效'); return; } $conn = $this->getMysql($account_id); if (!$conn) { phpcommon\sendError(ERR_USER_BASE + 1, '没有这个玩家'); return; } $goods_id = $_REQUEST['goods_id']; $url = ''; if (SERVER_ENV == _ONLINE) { $url = 'https://payservice.kingsome.cn/api/ingame/spreorder'; } else { $url = 'https://payservice-test.kingsome.cn/api/ingame/spreorder'; } $sign = md5($_REQUEST['account_id'] . $_REQUEST['goods_id'] . 'f3a6a9a5-217a-4079-ab99-b5d69b8212be' . $_REQUEST['session_id']); $params = array( 'account_id' => $_REQUEST['account_id'], 'goods_id' => $goods_id, 'session_id' => $_REQUEST['session_id'], 'user_ip' => phpcommon\getIPv4(), 'sign' => $sign ); if (!phpcommon\HttpClient::get($url, $params, $rsp)) { phpcommon\sendError(ERR_RETRY, '系统繁忙'); return; } if ($rsp == null || $rsp == '') { phpcommon\sendError(ERR_RETRY, '系统繁忙2'); return; } $response = json_decode($rsp, true); echo json_encode(array( 'errcode' => $response['errcode'], 'errmsg' => $response['errmsg'], 'order_id' => $response['order_id'] )); } public function payDone() { $account_id = $_REQUEST['account_id']; //登录校验 $login = loginVerify($account_id, $_REQUEST['session_id']); if (!$login) { phpcommon\sendError(ERR_USER_BASE + 1, 'session无效'); return; } $conn = $this->getMysql($account_id); if (!$conn) { phpcommon\sendError(ERR_USER_BASE + 1, '没有这个玩家'); return; } $order_id = $_REQUEST['order_id']; $url = ''; if (SERVER_ENV == _ONLINE) { $url = 'https://payservice.kingsome.cn/api/ingame/paid'; } else { $url = 'https://payservice-test.kingsome.cn/api/ingame/paid'; } $sign = md5($_REQUEST['account_id'] . $_REQUEST['order_id'] . 'f3a6a9a5-217a-4079-ab99-b5d69b8212be' . $_REQUEST['session_id']); $params = array( 'account_id' => $_REQUEST['account_id'], 'order_id' => $order_id, 'session_id' => $_REQUEST['session_id'], 'user_ip' => phpcommon\getIPv4(), 'sign' => $sign ); if (!phpcommon\HttpClient::get($url, $params, $rsp)) { phpcommon\sendError(ERR_RETRY, '系统繁忙'); return; } if ($rsp == null || $rsp == '') { phpcommon\sendError(ERR_RETRY, '系统繁忙 2'); return; } $response = json_decode($rsp, true); $diamonds = $response['diamond']; $diamond_num = -1; $diamond_present = 0; if ($diamonds > 0) { $userrow = $conn->execQueryOne( 'SELECT diamond_num, free_diamond FROM user WHERE accountid=:accountid;', array( ':accountid' => $account_id ) ); if (!$userrow) { phpcommon\sendError(ERR_USER_BASE + 1, '没有这个玩家'); return; } $shopgoods_conf = metatable\getShopGoodsConf(); $goodsid = 0; if ($shopgoods_conf) { for ($i = 1; $i <= count($shopgoods_conf); $i++) { if ($diamonds < $shopgoods_conf[$i]['item_num']) { continue; } if ($shopgoods_conf[$i]['first_present'] > $diamond_present) { $diamond_present = $shopgoods_conf[$i]['first_present']; $goodsid = $shopgoods_conf[$i]['shop_id']; } } } $rechargerow = $conn->execQueryOne( 'SELECT * FROM recharge WHERE accountid=:accountid;', array( ':accountid' => $account_id ) ); if (!$rechargerow) { $ret = $conn->execScript( 'INSERT INTO recharge(accountid, first_data, recharge_diamond, present_diamond, create_time, modify_time) ' . ' VALUES(:account_id, :first_data, :recharge_diamond, :present_diamond, :create_time, :modify_time) ' . ' ON DUPLICATE KEY UPDATE accountid=:account_id, first_data=:first_data, recharge_diamond=:recharge_diamond, present_diamond=:present_diamond, modify_time=:modify_time;', array( ':account_id' => $account_id, ':first_data' => $goodsid, ':recharge_diamond' => $diamonds, ':present_diamond' => $diamond_present, ':create_time' => time(), ':modify_time' => time() ) ); } else { $firstlist = explode(',', $rechargerow['first_data']); $firstrecharge = true; for ($i = 0; $i < count($firstlist); $i++) { if ($firstlist[$i] == $goodsid) { $firstrecharge = false; } } $firstdata = $rechargerow['first_data']; if ($firstrecharge) { $firstdata = $rechargerow['first_data'] . ',' . $goodsid; } else { $diamond_present = 0; } $ret = $conn->execScript( 'UPDATE recharge SET first_data=:first_data, recharge_diamond=:recharge_diamond, present_diamond=:present_diamond, modify_time=:modify_time' . ' WHERE accountid=:accountid;', array( ':accountid' => $account_id, ':first_data' => $firstdata, ':recharge_diamond' => $rechargerow['recharge_diamond'] + $diamonds, ':present_diamond' => $rechargerow['present_diamond'] + $diamond_present, ':modify_time' => time() ) ); } $diamond_num = $userrow['diamond_num'] + $diamonds + $diamond_present; $ret = $conn->execScript( 'UPDATE user SET diamond_num=:diamond_num' . ' WHERE accountid=:accountid;', array( ':accountid' => $account_id, ':diamond_num' => $diamond_num ) ); if (!$ret) { die(); return; } } echo json_encode(array( 'errcode' => $response['errcode'], 'errmsg' => $response['errmsg'], 'order_id' => $response['order_id'], 'diamond_nums' => $diamond_num, 'diamond_present' => $diamond_present, )); } public function purchaseNotify() { $params = array( 'account_id' => $_REQUEST['account_id'], 'goodsid' => $_REQUEST['goodsid'], 'orderid' => $_REQUEST['orderid'], 'amount' => $_REQUEST['amount'], ); $sign = phpcommon\md5Sign($params, 'NrN3zkVOzXF1vRzUc9eJM9ZKRO5eLooD', $_REQUEST['timestamp']); if ($sign != $_REQUEST['sign']) { error_log('game2004api payNotify sign error:' + json_encode($_REQUEST)); echo json_encode(array( 'errcode' => 1, 'errmsg' => '签名校验失败' )); die(); } $shopgoods = metatable\getShopGoodsById($_REQUEST['goodsid']); if (!$shopgoods) { error_log('game2004api payNotify goods error:' + json_encode($_REQUEST)); echo json_encode(array( 'errcode' => 2, 'errmsg' => 'goods 未找到' )); die(); } if ( $shopgoods['type'] != 2 && //特惠礼包 $shopgoods['type'] != 3 //月卡 ) { error_log('game2004api payNotify goods type error:' + json_encode($_REQUEST)); echo json_encode(array( 'errcode' => 3, 'errmsg' => 'goods type error' )); die(); } $privilegecard_conf = null; if ($shopgoods['type'] == 3) { $privilegecard_conf = metatable\getPrivilegeCardById($_REQUEST['goodsid']); if (!$privilegecard_conf) { error_log('game2004api payNotify privilege card goods config error:' + json_encode($_REQUEST)); echo json_encode(array( 'errcode' => 4, 'errmsg' => 'privilege card error' )); die(); } } $nowtime = time(); $conn = $this->getMysql($_REQUEST['account_id']); $this->insertNewOrder($conn, $nowtime); $this->updateUserTable($conn); $this->addToBuyHis($conn, $nowtime); $account_id = $_REQUEST['account_id']; $rechargerow = $conn->execQueryOne( 'SELECT * FROM recharge WHERE accountid=:accountid;', array( ':accountid' => $account_id ) ); $addreward = new classes\AddReward(); if ($shopgoods['first_present'] > 0) { $addreward->addReward(10003, $shopgoods['first_present'], $account_id, 0, 0); } if ($shopgoods['type'] == 3) { $nowtime = time(); // update vip_info if (!$rechargerow) { $vip_info = array(); $expire = 0; if ($privilegecard_conf['time'] > 0) { $expire = phpcommon\getdayseconds($nowtime) + $privilegecard_conf['time'] * 3600 * 24 + 3600 * 24 - 1; } array_push($vip_info, array( 'id' => $_REQUEST['goodsid'], 'expire' => $expire, 'daily_time' => 0, )); $ret = $conn->execScript( 'INSERT INTO recharge(accountid, vip_info, create_time, modify_time) ' . ' VALUES(:account_id, :vip_info, :create_time, :modify_time) ' . ' ON DUPLICATE KEY UPDATE accountid=:account_id, vip_info=:vip_info, modify_time=:modify_time;', array( ':account_id' => $account_id, ':vip_info' => json_encode($vip_info), ':create_time' => $nowtime, ':modify_time' => $nowtime, ) ); } else { $vip_info = json_decode($rechargerow['vip_info']); $found = false; foreach ($vip_info as $key => $carditem) { if ($carditem['id'] == $_REQUEST['goodsid']) { $found = true; if ($vip_info[$key]['expire'] > $nowtime) { $vip_info[$key]['expire'] += $privilegecard_conf['time'] * 3600 * 24; } else { if ($privilegecard_conf['time'] > 0) { $vip_info[$key]['expire'] = phpcommon\getdayseconds($nowtime) + $privilegecard_conf['time'] * 3600 * 24 + 3600 * 24 - 1; } } } } if (!$found) { $expire = 0; if ($privilegecard_conf['time'] > 0) { $expire = phpcommon\getdayseconds($nowtime) + $privilegecard_conf['time'] * 3600 * 24 + 3600 * 24 - 1; } array_push($vip_info, array( 'id' => $_REQUEST['goodsid'], 'expire' => $expire, 'daily_time' => 0, )); } $ret = $conn->execScript( 'UPDATE recharge SET daily_purchase=:daily_purchase, modify_time=:modify_time' . ' WHERE accountid=:accountid;', array( ':accountid' => $account_id, ':vip_info' => json_encode($vip_info), ':modify_time' => time(), ) ); } } else { $item_list = $addreward->addReward($shopgoods['item_id'], 1, $_REQUEST['account_id'], 0, 0); foreach ($item_list as &$value) { $value['itemnum'] = (float)$value['itemnum']; } error_log(json_encode($item_list)); //update daily purchase if (!$rechargerow) { $daily_purchase = array(); array_push($daily_purchase, array( 'id' => $_REQUEST['goodsid'], 'time' => $_REQUEST['timestamp'], )); $ret = $conn->execScript( 'INSERT INTO recharge(accountid, daily_purchase, create_time, modify_time) ' . ' VALUES(:account_id, :daily_purchase, :create_time, :modify_time) ' . ' ON DUPLICATE KEY UPDATE accountid=:account_id, daily_purchase=:daily_purchase, modify_time=:modify_time;', array( ':account_id' => $account_id, ':daily_purchase' => json_encode($daily_purchase), ':create_time' => time(), ':modify_time' => time() ) ); } else { $daily_purchase = json_decode($rechargerow['daily_purchase']); $found = false; foreach ($daily_purchase as $key => $dailyitem) { if ($dailyitem['id'] == $_REQUEST['goodsid']) { $found = true; $daily_purchase[$key]['time'] = $_REQUEST['timestamp']; } } if (!$found) { array_push($daily_purchase, array( 'id' => $_REQUEST['goodsid'], 'time' => $_REQUEST['timestamp'], )); } $ret = $conn->execScript( 'UPDATE recharge SET daily_purchase=:daily_purchase, modify_time=:modify_time' . ' WHERE accountid=:accountid;', array( ':accountid' => $account_id, ':daily_purchase' => json_encode($daily_purchase), ':modify_time' => time(), ) ); } } echo json_encode(array( 'errcode' => 0, 'errmsg' => '' )); } private function insertNewOrder($conn, $nowtime) { $ret = $conn->execScript( 'INSERT INTO orderinfo(accountid, orderid, goodsid, price, ' . ' create_time, modify_time)' . 'VALUES(:accountid, :orderid, :goodsid, :price, ' . ' :create_time, :modify_time);', array( ':accountid' => $_REQUEST['account_id'], ':orderid' => $_REQUEST['orderid'], ':price' => $_REQUEST['amount'], ':goodsid' => $_REQUEST['goodsid'], ':create_time' => $nowtime, ':modify_time' => $nowtime ) ); if (!$ret) { echo json_encode(array( 'errcode' => 2, 'errmsg' => '服务器内部错误' )); die(); } } private function updateUserTable($conn) { $ret = $conn->execScript( 'UPDATE user SET recharge_times_total=recharge_times_total + 1, ' . ' WHERE accountid=:accountid;', array( ':accountid' => $_REQUEST['account_id'], ) ); } private function addToBuyHis($conn, $nowtime) { $dayseconds = phpcommon\getdayseconds($nowtime); $conn->execScript( 'INSERT INTO buy_his(accountid, goodsid, sum_times, today_times, ' . ' last_buy_time, create_time, modify_time)' . 'VALUES(:accountid, :goodsid, 1, 1, ' . ' :last_buy_time, :create_time, :modify_time)' . 'ON DUPLICATE KEY UPDATE sum_times=sum_times + 1, ' . ' modify_time=:modify_time, last_buy_time=:last_buy_time,' . ' today_times=' . " CASE WHEN last_buy_time < $dayseconds THEN 1 ELSE today_times + 1 END;", array( ':accountid' => $_REQUEST['account_id'], ':goodsid' => $_REQUEST['goodsid'], ':last_buy_time' => $nowtime, ':create_time' => $nowtime, ':modify_time' => $nowtime, ) ); } public function activityInfo() { $account_id = $_REQUEST['account_id']; //登录校验 $login = loginVerify($account_id, $_REQUEST['session_id']); if (!$login) { phpcommon\sendError(ERR_USER_BASE + 1, 'session无效'); return; } $conn = $this->getMysql($account_id); if (!$conn) { phpcommon\sendError(ERR_USER_BASE + 1, '没有这个玩家'); return; } $rechargerow = $conn->execQueryOne( 'SELECT * FROM recharge WHERE accountid=:accountid;', array( ':accountid' => $account_id ) ); $firstrecharge = ''; if ($rechargerow) { $firstrecharge = $rechargerow['first_data']; } echo json_encode(array( 'errcode' => 0, 'errmsg' => '', 'first_recharge' => $firstrecharge, 'vip_info' => array(), 'daily_purchase' => array(), 'daily_recharge' => array(), 'total_recharge' => array(), 'total_consume' => array(), )); } }