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