@RequestMapping(method = RequestMethod.POST, value = "/reward")
 @ResponseBody
 public String reward(HttpServletRequest request, HttpServletResponse response)
     throws IOException {
   JSONObject webhooks = Tools.getParams(request);
   logger.debug("webhooks info == " + webhooks);
   JSONObject charge = webhooks.getJSONObject("data").getJSONObject("object");
   logger.debug("charge info == " + charge);
   String dealId = charge.getString("order_no");
   logger.debug("deal id: " + dealId);
   if (charge.getBoolean("paid") == true) {
     Deal deal = new Deal();
     deal.setDealId(dealId);
     deal.setDealStatus(true);
     dealService.updateDealRecord(deal);
   }
   Event event = Webhooks.eventParse(webhooks.toString());
   if ("charge.succeeded".equals(event.getType())) {
     response.setStatus(200);
   } else if ("refund.succeeded".equals(event.getType())) {
     response.setStatus(200);
   } else {
     response.setStatus(500);
   }
   return "complete";
 }
  @RequestMapping(method = RequestMethod.POST, value = "/charge")
  @ResponseBody
  public Charge charge(HttpServletRequest request) {
    // 获取请求body中的参数
    JSONObject params = Tools.getParams(request);
    // 文章的标识
    String articleId = params.getString("articleId");
    // 付款读者的标识
    String wechat = params.getString("wgateid");
    // 付款的渠道
    String channel = params.getString("channel");
    // 当前页面的URL
    String url = params.getString("url");
    // 付款的金额
    int amount = params.getInt("amount");
    // 获取请求的IP地址
    String ip = Tools.getIP(request);

    Deal deal = new Deal(wechat, articleId);
    deal.setDealPayment(amount);
    deal.setClientIp(ip);
    if (dealService.queryDealRecord(deal).getResponseCode() == ResponseCode.RESPONSE_NULL) {
      ResultData createMessage = dealService.createDealRecord(deal);
      if (createMessage.getResponseCode() == ResponseCode.RESPONSE_OK) {
        logger.debug("Insert a deal record successfully.");
      } else {
        logger.debug("Fail to insert a deal record.");
        return null;
      }
    }

    // 设置订单的ID
    String orderNo = deal.getDealId();

    // 根据文章的标识查询文章的信息
    Article article = new Article();
    article.setArticleId(articleId);
    article = (Article) articleService.queryArticle(article).getData();

    Reader reader = new Reader();
    if (!StringUtils.isEmpty(wechat)) {
      reader.setReaderWechat(wechat);
      ResultData readerExistMessage = readerService.queryReader(reader);
      if (readerExistMessage.getResponseCode() == ResponseCode.RESPONSE_NULL) {
        readerService.createReader(reader);
      } else if (readerExistMessage.getResponseCode() == ResponseCode.RESPONSE_ERROR) {
        return null;
      }
    }

    ChargeForm form = new ChargeForm(orderNo, article, reader, channel, amount, ip, url);
    ResultData result = dealService.charge(form);

    Charge charge = (Charge) result.getData();
    logger.debug(charge.toString());
    return charge;
  }
  @Test
  public void testPriceProductShopLessBuyPrice() {
    shop.getProductSets().put(ProductName.CHEESE, new ProductInfo(5, 5));
    shop.getProductSets().put(ProductName.FISH, new ProductInfo(10, 10));

    buys.put(ProductName.CHEESE, new ProductInfo(15, 10));
    buys.put(ProductName.FISH, new ProductInfo(40, 15));

    transaction = deal.deal(buys);

    result.clear();
    assertTrue(shop.getProductSets().entrySet().containsAll(result.entrySet()));

    result.put(ProductName.CHEESE, new ProductInfo(5, 5));
    result.put(ProductName.FISH, new ProductInfo(10, 10));
    /*System.out.println("|transaction");
    for (Map.Entry<ProductName, ProductInfo> entry : transaction.getProductSets().entrySet()){
        System.out.print(entry.getKey() + " " + entry.getValue().getCount() + " " + entry.getValue().getPrice() + "; ");
    }
    System.out.println("\n|shop");
    for (Map.Entry<ProductName, ProductInfo> entry : shop.getProductSets().entrySet()){
        System.out.print(entry.getKey() + " " + entry.getValue().getCount() + " " + entry.getValue().getPrice() + "; ");
    }
    System.out.println("\n|result");
    for (Map.Entry<ProductName, ProductInfo> entry : result.entrySet()){
        System.out.print(entry.getKey() + " " + entry.getValue().getCount() + " " + entry.getValue().getPrice() + "; ");
    }
    System.out.println("|");*/
    assertTrue(transaction.getProductSets().entrySet().containsAll(result.entrySet()));
  }
  @Test
  public void testShopEmpty() {
    buys.put(ProductName.CHEESE, new ProductInfo(15, 2.5));
    buys.put(ProductName.FISH, new ProductInfo(20, 7.2));

    transaction = deal.deal(buys);

    assertTrue(shop.getProductSets().entrySet().containsAll(result.entrySet()));
  }
示例#5
0
 /**
  * 响应页面提交
  *
  * @param actionMapping ActionMapping 这个 Action 的配置信息
  * @param actionForm ActionForm 用户提交的表单数据
  * @param request HttpServletRequest 当前的 HTTP 请求对象
  * @param response HttpServletResponse 当前的 HTTP 响应对象
  * @return ActionForward 请求转发路径
  * @throws Exception
  */
 public ActionForward execute(
     ActionMapping actionMapping,
     ActionForm actionForm,
     HttpServletRequest request,
     HttpServletResponse response)
     throws Exception {
   QueryForm aWebForm = (QueryForm) actionForm;
   Deal.doQuery(aWebForm, request, response); // 查询处理
   return actionMapping.getInputForward();
 }
示例#6
0
  /**
   * 响应页面提交
   *
   * @param actionMapping ActionMapping这个 Action 的配置信息
   * @param actionForm ActionForm 用户提交的表单数据
   * @param request HttpServletRequest当前的 HTTP 请求对象
   * @param response HttpServletResponse当前的 HTTP 响应对象
   * @return ActionForward 提交到查询页面
   * @throws Exception
   */
  public ActionForward execute(
      ActionMapping actionMapping,
      ActionForm actionForm,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {
    EditForm aWebForm = (EditForm) actionForm;
    if (aWebForm.getMethod() == null) { // 初次进行页面,需要取初始值
      Deal.setEditDefault(aWebForm, request, response); // 设置进入增加页面的初始值
      return actionMapping.getInputForward();
    } else {
      String sMessage = "未能找到 " + aWebForm.getMethod() + " 对应的处理方法";
      String[] button; // 按钮数组
      String[] onclick; // 按钮响应事件

      if (aWebForm.getMethod().equals("edit")) { // 修改
        Deal.doDHHFEdit(aWebForm, request, response); // 处理修改
        sMessage = mModuleName + "修改处理成功";
        button = new String[] {"关闭"}; // 按钮数组
        onclick = new String[] {"parent.window.close()"}; // 按钮响应事件
      } else if (aWebForm.getMethod().equals("add")) { // 修改
        Deal.doDHHFAdd(aWebForm, request, response); // 处理修改
        sMessage = mModuleName + "增加处理成功";
        button = new String[] {"关闭"}; // 按钮数组
        onclick = new String[] {"parent.window.close()"}; // 按钮响应事件
      } else if (aWebForm.getMethod().equals("del")) { // 删除
        Deal.doDHHFDelete(aWebForm, request, response); // 处理删除
        sMessage = mModuleName + "删除处理成功";
        button = new String[] {"关闭"}; // 按钮数组
        onclick = new String[] {"parent.window.close()"}; // 按钮响应事件
      } else { // 未能找到 " + aWebForm.getMethod() + " 对应的处理方法
        button = new String[] {"关闭"}; // 按钮数组
        onclick = new String[] {"parent.window.close()"}; // 按钮响应事件
      }
      CCommonMessage mMessage = new CCommonMessage(); // 统一提示页面
      mMessage.setMessage(sMessage); // 默认为"处理成功!"
      mMessage.setButtonName(button);
      mMessage.setOnClickFunc(onclick);
      request.setAttribute(CConstants.MESSAGE_OBJECT, mMessage);
      // 在处理页面最后调用(事先要配置名字为success的forward,在全局配置里有 message)
      return actionMapping.findForward("message");
    }
  }
示例#7
0
  @Override
  public List<DealBean> loadInBackground() {
    Map<String, DealBean> map = new HashMap<String, DealBean>();
    List<DealBean> list = new ArrayList<DealBean>();
    JSONArray ret = null;
    try {
      HttpPostHelper hpe = new HttpPostHelper(63);
      hpe.addSpParameter("lat", 37.76515);
      hpe.addSpParameter("lon", -122.481);
      hpe.addSpParameter("dist", 45);
      ret = hpe.post();
      for (int i = 0; i < ret.length(); i++) {
        JSONObject obj = ret.getJSONObject(i);
        String bizId = obj.getJSONObject("nodes").getString("biz_id");
        if (!map.containsKey(bizId)) {
          DealBean db = new DealBean();
          db.setBiz(obj.getJSONObject("nodes").getString("biz_name"));
          db.setPhone(obj.getJSONObject("nodes").getString("phone"));
          db.setStreet(obj.getJSONObject("nodes").getString("street"));
          db.setCity(obj.getJSONObject("nodes").getString("city"));
          db.setState(obj.getJSONObject("nodes").getString("state"));
          db.setZipcode(obj.getJSONObject("nodes").getString("zipcode"));

          map.put(bizId, db);
        }
        Deal deal = new Deal();
        String expr = obj.getJSONObject("nodes").getString("expiration");
        SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy h:mm a");
        Date expDate = df.parse(expr);
        deal.setExpiration(expDate);
        deal.setName(obj.getJSONObject("nodes").getString("name"));
        map.get(bizId).getDeals().add(deal);
      }
    } catch (Exception e) {

    }
    for (DealBean db : map.values()) list.add(db);

    return list;
  }
  @Test
  public void testBuysEmpty() {
    shop.getProductSets().put(ProductName.CHEESE, new ProductInfo(5, 2.5));
    shop.getProductSets().put(ProductName.FISH, new ProductInfo(10, 7.2));

    transaction = deal.deal(buys);

    result.put(ProductName.CHEESE, new ProductInfo(5, 2.5));
    result.put(ProductName.FISH, new ProductInfo(10, 7.2));
    assertTrue(shop.getProductSets().entrySet().containsAll(result.entrySet()));
    result.clear();
    assertTrue(transaction.getProductSets().entrySet().containsAll(result.entrySet()));
  }
示例#9
0
  /**
   * Only execution point for this application.
   *
   * @param ignored not used.
   * @throws Exception if something goes wrong.
   */
  public static void main(final String[] ignored) throws Exception {
    // Create a new remote client invoker
    String port = System.getProperty("org.switchyard.component.sca.client.port", "8080");
    RemoteInvoker invoker = new HttpInvoker("http://localhost:" + port + "/switchyard-remote");

    // Create request payload
    Offer offer = createOffer(true);

    // Create the request message
    RemoteMessage message = new RemoteMessage();
    message.setService(SERVICE).setOperation("offer").setContent(offer);

    // Invoke the service
    RemoteMessage reply = invoker.invoke(message);
    if (reply.isFault()) {
      System.err.println("Oops ... something bad happened.  " + reply.getContent());
    } else {
      Deal deal = (Deal) reply.getContent();
      out.println("==================================");
      out.println("Was the offer accepted? " + deal.isAccepted());
      out.println("==================================");
    }
  }
  @Test
  public void testPriceProductShopMoreBuyPrice() {
    shop.getProductSets().put(ProductName.CHEESE, new ProductInfo(5, 10));
    shop.getProductSets().put(ProductName.FISH, new ProductInfo(10, 15));

    buys.put(ProductName.CHEESE, new ProductInfo(15, 5));
    buys.put(ProductName.FISH, new ProductInfo(40, 10));

    transaction = deal.deal(buys);

    result.put(ProductName.CHEESE, new ProductInfo(5, 10));
    result.put(ProductName.FISH, new ProductInfo(10, 15));
    assertTrue(shop.getProductSets().entrySet().containsAll(result.entrySet()));

    result.clear();
    assertTrue(transaction.getProductSets().entrySet().containsAll(result.entrySet()));
  }
示例#11
0
 public void startGame() {
   /*This is the method which is called by goButtonActionPerformed.
   It calls for the consturction of the Deal and Mail decks, along with the
   Board and StatWindow. It also contructs the Players, one after another,
   using a for loop to determine how many are to be created. Finally, it
   runs the game, calling takeYourTurn in player.*/
   devMode = false;
   toDeal = new Deal(this);
   toMail = new Mail(this);
   toBoard = new Board();
   // toStatWindow = new StatWindow(noOfPlayers, noOfMonths);
   System.out.println("Setting up the game...");
   toPlayer = new Player[noOfPlayers];
   for (int index = 0; index < noOfPlayers; index++) {
     toPlayer[index] = new Player(this, toBoard, index, toMail, toDeal);
   }
   toGameUI = new GameUI(noOfPlayers, this, toDeal, toMail, toBoard);
   for (int index = 0; index < noOfPlayers; index++) {
     toPlayer[index].setGameUI(toGameUI);
   }
   toDeal.updateCards(this);
   toMail.updateCards(this);
   toUIThread.setToGameUI(toGameUI);
   finished = false;
   toUIThreadsThread.start();
   toGameUI.distributeStartingMoney();
   this.passString("Setting up the game");
   index++;
   jackpot = 0;
   ready = true;
   while (!finished) {
     for (int index = 0; index < noOfPlayers; index++) {
       if (!toPlayer[index].finished) {
         toPlayer[index].takeYourTurn();
       }
     }
   }
 }