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 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 UChannel getChannelByID(Integer id) {

    if (id == null) {
      return null;
    }

    for (UChannel c : this.channels.values()) {
      if (c.getId() == id) {
        return c;
      }
    }
    return null;
  }
  // 添加或者修改渠道
  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 boolean isValid(UChannel channel) {

    StringBuilder sb = new StringBuilder();
    sb.append("dealseq=")
        .append(dealseq)
        .append("&")
        .append("notify_data=")
        .append(notify_data)
        .append("&")
        .append("orderid=")
        .append(orderid)
        .append("&")
        .append("subject=")
        .append(subject)
        .append("&")
        .append("uid=")
        .append(uid)
        .append("&")
        .append("v=")
        .append(v);

    return RSASignature.doCheck(sb.toString(), sign, channel.getCpPayKey(), "utf-8");
  }
  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());
  }
Пример #7
0
  @Override
  public void verify(final UChannel channel, String extension, final ISDKVerifyListener callback) {

    try {

      JSONObject json = JSONObject.fromObject(extension);
      final String uid = json.getString("uid");
      final String username = json.getString("nickname");

      Map<String, String> params = new HashMap<String, String>();
      params.put("appid", channel.getCpAppID());
      params.put("uid", uid);

      StringBuilder sb = new StringBuilder();
      sb.append(channel.getCpAppID()).append(channel.getCpAppKey()).append(uid);
      String sign = EncryptUtils.sha1(sb.toString());

      params.put("sign", sign.toUpperCase());

      String url = channel.getChannelAuthUrl();

      UHttpAgent.getInstance()
          .get(
              url,
              params,
              new UHttpFutureCallback() {
                @Override
                public void completed(String result) {

                  try {

                    Log.e("The auth result is " + result);

                    if ("SUCCESS".equals(result)) {

                      callback.onSuccess(new SDKVerifyResult(true, uid, username, ""));
                      return;
                    }

                  } catch (Exception e) {
                    e.printStackTrace();
                  }

                  callback.onFailed(
                      channel.getMaster().getSdkName()
                          + " verify failed. the post result is "
                          + result);
                }

                @Override
                public void failed(String e) {

                  callback.onFailed(channel.getMaster().getSdkName() + " verify failed. " + e);
                }
              });

    } catch (Exception e) {
      e.printStackTrace();
      callback.onFailed(
          channel.getMaster().getSdkName()
              + " verify execute failed. the exception is "
              + e.getMessage());
    }
  }
  @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();
    }
  }
Пример #9
0
  @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);
    }
  }
  private boolean isValid(UChannel channel) {

    return RSAUtils.verify(this.content, this.sign, channel.getCpPayKey(), "UTF-8");
  }