package com.yeshi.location.app.controller;
|
|
import com.alipay.api.AlipayApiException;
|
import com.alipay.api.internal.util.AlipaySignature;
|
import com.google.gson.Gson;
|
import com.yeshi.location.app.service.inter.vip.OrderService;
|
import com.yeshi.location.app.utils.vip.VIPOrderUtil;
|
import com.yeshi.location.app.utils.vip.VipUtil;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.stereotype.Controller;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.yeshi.utils.StringUtil;
|
import org.yeshi.utils.entity.alipay.AlipayAppInfo;
|
|
import javax.annotation.Resource;
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.IOException;
|
import java.io.UnsupportedEncodingException;
|
import java.util.HashMap;
|
import java.util.Iterator;
|
import java.util.Map;
|
|
@Controller
|
@RequestMapping("alipay")
|
public class AlipayController {
|
Logger logger = LoggerFactory.getLogger(AlipayController.class);
|
|
@Resource
|
private RedisTemplate<String, String> redisTemplate;
|
|
@Resource
|
private OrderService orderService;
|
|
@RequestMapping("printPayForm")
|
public void printPayForm(String formId, HttpServletResponse response) {
|
String form = redisTemplate.opsForValue().get(formId);
|
if (StringUtil.isNullOrEmpty(form)) {
|
form = "出错了,请稍后再试";
|
}
|
|
response.setContentType("text/html;charset=GBK");
|
try {
|
response.getWriter().write(form);//直接将完整的表单html输出到页面
|
response.getWriter().flush();
|
response.getWriter().close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
|
|
@RequestMapping("pay")
|
public void pay(HttpServletRequest request, HttpServletResponse response) {
|
try {
|
request.setCharacterEncoding("GBK");
|
response.setCharacterEncoding("GBK");
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
|
Map<String, String[]> params = request.getParameterMap();
|
Map<String, String> map = new HashMap<>();
|
for (Iterator<String> its = params.keySet().iterator(); its.hasNext(); ) {
|
String key = its.next();
|
map.put(key, params.get(key)[0]);
|
}
|
String outTradeNo = map.get("out_trade_no") + "";
|
String tradeStatus = map.get("trade_status") + "";
|
|
|
logger.info("支付回调:" + new Gson().toJson(map));
|
AlipayAppInfo app = VipUtil.getAlipayApp();
|
|
try {
|
boolean right = AlipaySignature.rsaCheckV1(map, app.getAlipayPublicKey(), "GBK", map.get("sign_type"));
|
if (right) {
|
//支付成功
|
if ("TRADE_SUCCESS".equalsIgnoreCase(tradeStatus)) {
|
Long id = VIPOrderUtil.getIdFromOutOrderNo(outTradeNo);
|
logger.info("订单ID:{}", id);
|
orderService.checkOrderPayState(id);
|
}
|
response.getWriter().print("success");
|
response.getWriter().close();
|
}
|
logger.info("签名是否正确:" + right);
|
} catch (AlipayApiException | IOException e) {
|
e.printStackTrace();
|
}
|
|
|
}
|
|
|
}
|