public void loadChannelData(List<UChannel> channelLst) { channels = new HashMap<Integer, UChannel>(); for (UChannel channel : channelLst) { channels.put(channel.getChannelID(), channel); } Log.i("Load channels:" + channels.size()); }
public void loadGameData(List<UGame> gameLst) { games = new HashMap<Integer, UGame>(); for (UGame game : gameLst) { games.put(game.getAppID(), game); } Log.i("Load games :" + games.size()); }
public void addGame(UGame game) { if (games.containsKey(game.getAppID())) { Log.e("The appID is already is exists. add game failed." + game.getAppID()); return; } games.put(game.getAppID(), game); }
public void loadMasterData(List<UChannelMaster> masterLst) { masters = new HashMap<Integer, UChannelMaster>(); for (UChannelMaster master : masterLst) { masters.put(master.getMasterID(), master); } Log.i("Load masters:" + masters.size()); }
public void addChannel(UChannel channel) { if (channels.containsKey(channel.getChannelID())) { Log.e("The channelID is already is exists. add channel faild." + channel.getChannelID()); return; } channels.put(channel.getChannelID(), channel); }
public void addMaster(UChannelMaster master) { if (masters.containsKey(master.getMasterID())) { Log.e( "The channel master ID is already is exists. add channel master faild." + master.getMasterID()); return; } masters.put(master.getMasterID(), master); }
// 添加或者修改渠道 public void saveChannel(UChannel channel) { if (channels.containsKey(channel.getChannelID())) { channels.remove(channel.getChannelID()); } Log.d("the channel is " + channel); UChannel c = getChannelByID(channel.getId()); if (c != null) { channels.remove(c.getChannelID()); } channels.put(channel.getChannelID(), channel); }
private void renderState(int state, JSONObject data) { try { JSONObject json = new JSONObject(); json.put("state", state); json.put("data", data); super.renderJson(json.toString()); } catch (Exception e) { e.printStackTrace(); Log.e(e.getMessage()); } }
private boolean isSignOK(UChannel channel, LehaihaiPayResult rsp) { StringBuilder sb = new StringBuilder(); sb.append("amount=") .append(rsp.getAmount()) .append("gameid=") .append(rsp.getGameid()) .append("orderid=") .append(rsp.getOrderid()) .append("serverid=") .append(rsp.getServerid()) .append("time=") .append(rsp.getTime()) .append("uid=") .append(rsp.getUid()) .append(channel.getCpAppSecret()); Log.d("sign txt:" + sb.toString()); String md5 = EncryptUtils.md5(sb.toString()).toLowerCase(); Log.d("md5:" + md5); return md5.equals(rsp.getSign()); }
/** * * 上面协议返回客户端之后,开始连接登录游戏服。游戏服可以调用该协议进行再次登录认证。 但是,该步骤是可选的。游戏服务器也可以自己验证token以及token的时效性,这样就不用来 * U8Server进行再次登录认证了。 * * <p>服务器自己验证token,根据U8Server分配给每个游戏参数中的AppSecret,按照生成token的 规则,进行验证。同时,需要验证timestamp的时效性 */ @Action("verifyAccount") public void loginVerify() { try { UUser user = userManager.getUser(this.userID); if (user == null) { renderState(StateCode.CODE_USER_NONE, null); return; } if (StringUtils.isEmpty(this.token)) { renderState(StateCode.CODE_VERIFY_FAILED, null); return; } StringBuilder sb = new StringBuilder(); sb.append("userID=") .append(this.userID) .append("token=") .append(this.token) .append(user.getGame().getAppkey()); if (!userManager.isSignOK(sb.toString(), sign)) { renderState(StateCode.CODE_SIGN_ERROR, null); return; } long now = System.currentTimeMillis(); if (!userManager.checkUser(user, token)) { renderState(StateCode.CODE_TOKEN_ERROR, null); return; } JSONObject data = new JSONObject(); data.put("userID", user.getId()); data.put("username", user.getName()); renderState(StateCode.CODE_SUCCESS, data); return; } catch (Exception e) { Log.e(e.getMessage()); } renderState(StateCode.CODE_VERIFY_FAILED, null); }
@Action("payCallback") public void payCallback() { try { long localOrderID = Long.parseLong(dealseq); UOrder order = orderManager.getOrder(localOrderID); if (order == null || order.getChannel() == null) { Log.d("The order is null or the channel is null.orderID:" + dealseq); this.renderState(false, "notifyId 错误"); return; } if (order.getState() > PayState.STATE_PAYING) { Log.d("The order is already completed.local orderID:%s; orderID:%s", localOrderID, orderid); this.renderState(true, "该订单已经被处理,或者CP订单号重复"); return; } if (!isValid(order.getChannel())) { Log.d("The sign is not valid, sign:%s; orderID:%s", sign, dealseq); this.renderState(false, "签名错误"); return; } RSAEncrypt encrypt = new RSAEncrypt(); encrypt.loadPublicKey(order.getChannel().getCpPayKey()); byte[] dcDataStr = Base64.decode(notify_data); byte[] plainData = encrypt.decrypt(encrypt.getPublicKey(), dcDataStr); // 获取到加密通告信息 String notifyDataStr = new String(plainData, "UTF-8"); Map<String, String> notifyData = HttpUtils.parseUrlParams(notifyDataStr); String feeStr = notifyData.containsKey("fee") ? notifyData.get("fee") : null; String payresultStr = notifyData.containsKey("payresult") ? notifyData.get("payresult") : null; int fee = (int) (Float.valueOf(feeStr) * 100); int payresult = Integer.valueOf(payresultStr); if (order.getMoney() != fee) { Log.e( "订单金额不一致, local orderID:%s;local money:%s;money:%s", localOrderID, fee, order.getMoney()); } if (payresult != 0) { Log.e("平台支付失败 local orderID:%s; pay result:%s", localOrderID, payresult); this.renderState(false, "联想支付中心返回的结果是支付失败"); return; } order.setChannelOrderID(orderid); order.setRealMoney(fee); order.setSdkOrderTime(""); order.setCompleteTime(new Date()); order.setState(PayState.STATE_SUC); orderManager.saveOrder(order); SendAgent.sendCallbackToServer(this.orderManager, order); this.renderState(true, ""); } catch (Exception e) { e.printStackTrace(); try { this.renderState(false, "未知错误"); } catch (IOException e1) { e1.printStackTrace(); } } }
@Action("payCallback") public void payCallback() { try { LehaihaiPayResult rsp = (LehaihaiPayResult) JsonUtils.decodeJson(data, LehaihaiPayResult.class); if (rsp == null) { Log.e("the data parse failed. data:" + data); return; } long orderID = Long.parseLong(rsp.getExtendsInfo()); UOrder order = orderManager.getOrder(orderID); if (order == null) { Log.d("The order is null"); this.renderState(false); return; } UChannel channel = order.getChannel(); if (channel == null) { Log.d("the channel is null."); this.renderState(false); return; } if (order.getState() > PayState.STATE_PAYING) { Log.d("The state of the order is complete. The state is " + order.getState()); this.renderState(true); return; } if (!isSignOK(channel, rsp)) { Log.d( "The sign verify failed.sign:%s;appKey:%s;orderID:%s", rsp.getSign(), channel.getCpPayKey(), rsp.getExtendsInfo()); this.renderState(false); return; } int moneyInt = (int) (Float.valueOf(rsp.getAmount()) * 100); // 以分为单位 order.setRealMoney(moneyInt); order.setSdkOrderTime(rsp.getTime()); order.setCompleteTime(new Date()); order.setChannelOrderID(rsp.getOrderid()); order.setState(PayState.STATE_SUC); orderManager.saveOrder(order); SendAgent.sendCallbackToServer(this.orderManager, order); renderState(true); } catch (Exception e) { try { renderState(false); } catch (IOException e1) { e1.printStackTrace(); } e.printStackTrace(); } }
@Action("getToken") public void getLoginToken() { Log.d("getToken..."); try { final UGame game = gameManager.queryGame(this.appID); if (game == null) { renderState(StateCode.CODE_GAME_NONE, null); return; } final UChannel channel = channelManager.queryChannel(this.channelID); if (channel == null) { renderState(StateCode.CODE_CHANNEL_NONE, null); return; } if (channel.getAppID() != this.appID) { renderState(StateCode.CODE_CHANNEL_NOT_MATCH, null); return; } UChannelMaster master = channel.getMaster(); if (master == null) { renderState(StateCode.CODE_CHANNEL_NONE, null); return; } StringBuilder sb = new StringBuilder(); sb.append("appID=") .append(this.appID) .append("channelID=") .append(this.channelID) .append("extension=") .append(this.extension) .append(game.getAppkey()); if (!userManager.isSignOK(sb.toString(), sign)) { Log.e("the sign is invalid. sign:" + sign); renderState(StateCode.CODE_SIGN_ERROR, null); return; } ISDKScript verifier = SDKCacheManager.getInstance().getSDKScript(channel); if (verifier == null) { Log.e("the ISDKScript is not found . channelID:" + channelID); renderState(StateCode.CODE_VERIFY_FAILED, null); return; } Log.d("The auth url is " + channel.getChannelAuthUrl()); Log.d("channel is " + channel.getChannelID() + ";extension is " + extension); verifier.verify( channel, extension, new ISDKVerifyListener() { @Override public void onSuccess(SDKVerifyResult sdkResult) { try { Log.d("user verify success. result:" + sdkResult.getUserID()); if (sdkResult.isSuccess() && !StringUtils.isEmpty(sdkResult.getUserID())) { UUser user = userManager.getUserByCpID( channel.getAppID(), channel.getChannelID(), sdkResult.getUserID()); if (user == null) { user = userManager.generateUser(channel, sdkResult); } else { user.setChannelUserName( sdkResult.getUserName() == null ? "" : sdkResult.getUserName()); user.setChannelUserNick( sdkResult.getNickName() == null ? "" : sdkResult.getNickName()); user.setLastLoginTime(new Date().getTime() + ""); } user.setToken(UGenerator.generateToken(user, game.getAppSecret())); userManager.saveUser(user); JSONObject data = new JSONObject(); data.put("userID", user.getId()); data.put("sdkUserID", user.getChannelUserID()); data.put("username", user.getName()); data.put("sdkUserName", user.getChannelUserName()); data.put("token", user.getToken()); data.put("extension", sdkResult.getExtension()); data.put("timestamp", user.getLastLoginTime()); renderState(StateCode.CODE_SUCCESS, data); } else { renderState(StateCode.CODE_AUTH_FAILED, null); } } catch (Exception e) { renderState(StateCode.CODE_AUTH_FAILED, null); e.printStackTrace(); } } @Override public void onFailed(String errorMsg) { Log.e("The user verify failed. errorMsg:" + errorMsg); renderState(StateCode.CODE_AUTH_FAILED, null); } }); } catch (Exception e) { Log.e(e.getMessage()); renderState(StateCode.CODE_AUTH_FAILED, null); } }
@Action("payCallback") public void payCallback() { try { WDJPayCallbackContent data = (WDJPayCallbackContent) JsonUtils.decodeJson(content, WDJPayCallbackContent.class); if (data == null) { Log.e("The content parse error..."); return; } long localOrderID = Long.parseLong(data.getOut_trade_no()); UOrder order = orderManager.getOrder(localOrderID); if (order == null || order.getChannel() == null) { Log.d("The order is null or the channel is null."); this.renderState(false, "notifyId 错误"); return; } if (order.getState() == PayState.STATE_COMPLETE) { Log.d("The state of the order is complete. The state is " + order.getState()); this.renderState(true, "该订单已经被处理,或者CP订单号重复"); return; } int orderMoney = Integer.valueOf(data.getMoney()); // 转换为分 if (order.getMoney() != orderMoney) { Log.e( "订单金额不一致! local orderID:" + localOrderID + "; money returned:" + data.getMoney() + "; order money:" + order.getMoney()); this.renderState(false, "订单金额不一致"); return; } if (isValid(order.getChannel())) { order.setChannelOrderID(data.getOrderId()); order.setState(PayState.STATE_SUC); orderManager.saveOrder(order); SendAgent.sendCallbackToServer(this.orderManager, order); this.renderState(true, ""); } else { order.setChannelOrderID(data.getOrderId()); order.setState(PayState.STATE_FAILED); orderManager.saveOrder(order); this.renderState(false, "sign 错误"); } } catch (Exception e) { e.printStackTrace(); try { this.renderState(false, "未知错误"); } catch (IOException e1) { e1.printStackTrace(); } } }