package com.panzhihua.applets.api;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.panzhihua.applets.umf.MyAESUtil;
import com.panzhihua.applets.umf.UmfPayUtil;
import com.panzhihua.applets.unionpay.Query;
import com.panzhihua.applets.unionpay.QueryEntiy;
import com.panzhihua.applets.unionpay.Refund;
import com.panzhihua.common.model.dtos.wx.UnionpayCarWash;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.service.user.UserService;
import com.panzhihua.common.utlis.DateUtils;
import com.panzhihua.common.utlis.HttpClientUtil;
import com.umf.api.service.UmfService;
import com.umf.api.service.UmfServiceImpl;
import io.swagger.annotations.ApiOperation;
import com.panzhihua.common.service.community.ComBatteryCommodityOrderFeign;
import org.json.XML;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.panzhihua.common.model.dtos.shop.WxPayNotifyOrderDTO;
import com.panzhihua.common.service.community.CommunityService;
import com.panzhihua.common.utlis.StringUtils;
import lombok.extern.slf4j.Slf4j;
import static com.umf.api.service.UmfServiceImpl.printResult;
/**
* @auther lyq
* @create 2021-04-14 15:02:49
* @describe 微信支付回到函数
*/
@Slf4j
@RestController
@RequestMapping("/wx/")
public class WxCallbackApi {
private static final String SUCCESS="SUCCESS";
@Resource
private CommunityService communityService;
@Resource
private UmfPayUtil umfPayUtil;
@Value("${umf.file}")
private String file;
@Resource
private UserService userService;
@Resource
private ComBatteryCommodityOrderFeign commodityOrderFeign;
@PostMapping("wxNotify")
public void payCallback(HttpServletRequest request, HttpServletResponse response) {
log.error("微信支付回调start");
String inputLine = "";
String notityXml = "";
try {
while ((inputLine = request.getReader().readLine()) != null) {
notityXml += inputLine;
}
// 关闭流
request.getReader().close();
log.error("微信回调内容信息:" + notityXml);
// 解析成Json
org.json.JSONObject xmlJson = XML.toJSONObject(notityXml);
if (StringUtils.isNotEmpty(xmlJson.toString())) {
JSONObject resultData = JSON.parseObject(xmlJson.get("xml").toString());
// 成功支付订单
if (resultData.getString("result_code").equals("SUCCESS")
&& resultData.getString("return_code").equals("SUCCESS")) {
WxPayNotifyOrderDTO notifyOrderDTO = new WxPayNotifyOrderDTO();
// 订单号
String orderTradeNo = resultData.getString("out_trade_no");
notifyOrderDTO.setOrderTradeNo(orderTradeNo);
// 微信支付订单号
String wxTradeNo = resultData.getString("transaction_id");
notifyOrderDTO.setWxTradeNo(wxTradeNo);
// 订单金额
String totalFee = resultData.getString("total_fee");
notifyOrderDTO.setTotalFee(totalFee);
// 支付完成时间
String payTime = resultData.getString("time_end");
notifyOrderDTO.setPayTime(payTime);
// 现金支付金额
String cashFee = resultData.getString("cash_fee");
notifyOrderDTO.setCashFee(cashFee);
// 根据订单号修改订单信息
communityService.wxOrderPayNotify(notifyOrderDTO);
// 封装 返回值
StringBuffer buffer = new StringBuffer();
buffer.append("");
buffer.append("SUCCESS");
buffer.append("OK");
buffer.append("");
// 给微信服务器返回 成功标示 否则会一直询问 咱们服务器 是否回调成功
PrintWriter writer = response.getWriter();
// 返回
writer.print(buffer.toString());
} else {// 未成功支付订单
// 封装 返回值
StringBuffer buffer = new StringBuffer();
buffer.append("");
buffer.append("FAIL");
buffer.append("FAIL");
buffer.append("");
// 给微信服务器返回 成功标示 否则会一直询问 咱们服务器 是否回调成功
PrintWriter writer = response.getWriter();
// 返回
writer.print(buffer.toString());
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
@GetMapping("wxNotifyAll")
public void payCallbackAll(HttpServletRequest request, HttpServletResponse response) {
//获取联动发送请求的参数
String requestParam = request.getQueryString();
System.out.println("花城洗车请求参数 :" + requestParam);
//调用异步通知解析方法
UmfService service = new UmfServiceImpl("53461",file);
Map respMap = null;
try {
respMap = service.notifyDataParserMap(requestParam);
} catch (Exception e) {
e.printStackTrace();
}
if(respMap.get("error_code").equals("0000")){
//调用uu洗车支付成功回调
Map map=new HashMap();
map.put("orderId",respMap.get("order_id"));
HttpClientUtil.sendPostByJson("https://uu-api.imashuo.com/notify/wx/pzh-pay",JSONObject.toJSONString(map),
1,"appid","10000");
log.error("花城洗车回调成功 :" + respMap.get("order_id"));
}
//调用SDK生成返回联动平台字符串,加到CONTENT中
String resMetaData = service.responseUMFMap(respMap);
response.setContentType("text/html;charset=utf-8");
PrintWriter out = null;
try {
out = response.getWriter();
} catch (IOException e) {
e.printStackTrace();
}
out.println("");
out.println("");
out.println("
");
out.println("");
out.println("");
out.println("");
out.flush();
out.close();
}
/**
* 银联支付回调专用 银联方面只能是有个post
* @param response
*/
@PostMapping("wxNotifyAllUnionpay")
public void payCallbackAllUnionpay(@RequestParam("merOrderId") String merOrderId, HttpServletResponse response)
{
//获取联动发送请求的参数
log.error("花城洗车请求参数 :" + merOrderId);
log.error("花城洗车请求参数response :" + response.toString());
//调用异步通知解析方法
String content=Query.query(merOrderId);
QueryEntiy queryEntiy=JSONObject.parseObject(content,QueryEntiy.class);
log.error("花城洗车请求参数queryEntiy :" + queryEntiy.toString());
if(queryEntiy.getErrCode().equals("SUCCESS"))
{
//查询银联单号对应的洗车单号
R r=communityService.carWashById(merOrderId);
if(r.getCode()==200)
{
HashMap unionpayCarWash= (HashMap) r.getData();
//调用uu洗车支付成功回调
Map map=new HashMap();
map.put("orderId",unionpayCarWash.get("carWashId"));
HttpClientUtil.sendPostByJson("https://uu-api.imashuo.com/notify/wx/pzh-pay",JSONObject.toJSONString(map),
1,"appid","10000");
log.error("花城洗车回调成功 :" + queryEntiy.getMerOrderId()+" : 洗车号"
+unionpayCarWash.get("carWashId")+" : 银联号"+unionpayCarWash.get("unionpayOrderId"));
}
}
//调用SDK生成返回联动平台字符串,加到CONTENT中
response.setContentType("text/html;charset=utf-8");
PrintWriter out = null;
try {
out = response.getWriter();
} catch (IOException e) {
e.printStackTrace();
}
out.println("");
out.println("");
out.println("");
out.println("");
out.println("");
out.println("");
out.flush();
out.close();
}
@ApiOperation("uu洗车退款接口")
@PostMapping("/uuRepay")
public R uuRepay(@RequestBody String aesString)
{
try {
Map aesMap= (Map) JSON.parse(aesString);
String desString=MyAESUtil.Decrypt(aesMap.get("aesString"),"Ryo7M3n8loC5Abcd");
Map desMap= (Map) JSON.parse(desString);
Map map=umfPayUtil.repay(desMap.get("orderId"),desMap.get("merdate"), DateUtils.getDateFormatString(new Date(),
"yyMMddHHmmss")+"0001",desMap.get("refundAmount"),desMap.get("orgAmount"));
if(desMap.get("orderId")!=null)
{
R r=communityService.queryById(desMap.get("orderId"));
if(r.getCode()==200)
{
HashMap wash= (HashMap) r.getData();
if(wash!=null)
{
String re=Refund.sendOrder(wash.get("money").toString(),
wash.get("unionpayOrderId").toString());
map.put("unionpay","退款成功");
log.error("花城洗车银联退款成功 :" + re);
}
}
}
return R.ok(map);
} catch (Exception e) {
e.printStackTrace();
return R.fail("加密验证失败");
}
}
@ApiOperation("uu洗车推送")
@PostMapping("/uuPush")
public R uuPush(@RequestBody String aesString){
try {
Map aesMap= (Map) JSON.parse(aesString);
String desString=MyAESUtil.Decrypt(aesMap.get("aesString"),"Ryo7M3n8loC5Abcd");
Map desMap= (Map) JSON.parse(desString);
return userService.uuPush(desMap.get("washer_mobile"),desMap.get("phone"),desMap.get("washer_name"),Integer.parseInt(desMap.get("order_status")));
} catch (Exception e) {
e.printStackTrace();
return R.fail("推送失败");
}
}
@ApiOperation("ws洗车推送")
@PostMapping("/wsPush")
public R wsPush(@RequestBody String aesString){
try {
Map aesMap= (Map) JSON.parse(aesString);
String desString=MyAESUtil.Decrypt(aesMap.get("aesString"),"Ryo7M3n8loC5Abcd");
Map desMap= (Map) JSON.parse(desString);
return userService.wsPush(desMap.get("washer_mobile"),desMap.get("phone"),desMap.get("washer_name"),Integer.parseInt(desMap.get("order_status")));
} catch (Exception e) {
e.printStackTrace();
return R.fail("推送失败");
}
}
/**
* 电动车商城商品购买支付回调
* @param request 请求参数
* @param response 返回参数
*/
@PostMapping("/batteryPayNotify")
public void batteryPayNotify(HttpServletRequest request, HttpServletResponse response) {
log.info("钱包充值微信支付回调start");
String inputLine = "";
String notityXml = "";
try {
while ((inputLine = request.getReader().readLine()) != null) {
notityXml += inputLine;
}
// 关闭流
request.getReader().close();
log.info("钱包充值微信回调内容信息:" + notityXml);
// 解析成Json
org.json.JSONObject xmlJson = XML.toJSONObject(notityXml);
if (StringUtils.isNotEmpty(xmlJson.toString())) {
JSONObject resultData = JSON.parseObject(xmlJson.get("xml").toString());
// 成功支付订单
if (SUCCESS.equals(resultData.getString("result_code")) && SUCCESS.equals(resultData.getString("result_code"))){
WxPayNotifyOrderDTO notifyOrderDTO = getWxNotify(resultData);
// 根据订单号修改订单信息
commodityOrderFeign.batteryPayNotify(notifyOrderDTO);
// 给微信服务器返回 成功标示 否则会一直询问 咱们服务器 是否回调成功
PrintWriter writer = response.getWriter();
// 返回
writer.print(getWxSuccessString());
} else {// 未成功支付订单
// 给微信服务器返回 成功标示 否则会一直询问 咱们服务器 是否回调成功
PrintWriter writer = response.getWriter();
// 返回
writer.print(getWxFailString());
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
@PostMapping("/batteryActivityPayNotify")
public void batteryActivityPayNotify(HttpServletRequest request, HttpServletResponse response) {
log.info("购买活动商品微信支付回调start");
String inputLine = "";
String notityXml = "";
try {
while ((inputLine = request.getReader().readLine()) != null) {
notityXml += inputLine;
}
// 关闭流
request.getReader().close();
log.info("购买活动商品微信回调内容信息:" + notityXml);
// 解析成Json
org.json.JSONObject xmlJson = XML.toJSONObject(notityXml);
if (StringUtils.isNotEmpty(xmlJson.toString())) {
JSONObject resultData = JSON.parseObject(xmlJson.get("xml").toString());
// 成功支付订单
if (SUCCESS.equals(resultData.getString("result_code")) && SUCCESS.equals(resultData.getString("result_code"))){
WxPayNotifyOrderDTO notifyOrderDTO = getWxNotify(resultData);
// 根据订单号修改订单信息
commodityOrderFeign.batteryActivityPayNotify(notifyOrderDTO);
// 给微信服务器返回 成功标示 否则会一直询问 咱们服务器 是否回调成功
PrintWriter writer = response.getWriter();
// 返回
writer.print(getWxSuccessString());
} else {// 未成功支付订单
// 给微信服务器返回 成功标示 否则会一直询问 咱们服务器 是否回调成功
PrintWriter writer = response.getWriter();
// 返回
writer.print(getWxFailString());
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 将微信支付回调的请求参数转义为对象
* @param resultData 微信支付回调请求参数
* @return 转义为支付请求参数
*/
private WxPayNotifyOrderDTO getWxNotify(JSONObject resultData){
WxPayNotifyOrderDTO notifyOrderDTO = new WxPayNotifyOrderDTO();
notifyOrderDTO.setResult(resultData.toJSONString());
// 订单号
String orderTradeNo = resultData.getString("out_trade_no");
notifyOrderDTO.setOrderTradeNo(orderTradeNo);
// 微信支付订单号
String wxTradeNo = resultData.getString("transaction_id");
notifyOrderDTO.setWxTradeNo(wxTradeNo);
// 订单金额
String totalFee = resultData.getString("total_fee");
notifyOrderDTO.setTotalFee(totalFee);
// 支付完成时间
String payTime = resultData.getString("time_end");
notifyOrderDTO.setPayTime(payTime);
// 现金支付金额
String cashFee = resultData.getString("cash_fee");
notifyOrderDTO.setCashFee(cashFee);
//附加数据
String attach = resultData.getString("attach");
notifyOrderDTO.setAttach(attach);
return notifyOrderDTO;
}
/**
* 封装微信成功返回值
* @return 成功返回值
*/
private String getWxSuccessString(){
StringBuffer buffer = new StringBuffer();
buffer.append("");
buffer.append("SUCCESS");
buffer.append("OK");
buffer.append("");
return buffer.toString();
}
/**
* 封装微信失败返回值
* @return 失败返回值
*/
private String getWxFailString(){
StringBuffer buffer = new StringBuffer();
buffer.append("");
buffer.append("FAIL");
buffer.append("FAIL");
buffer.append("");
return buffer.toString();
}
@GetMapping("/settle")
public R settle(String settleDate){
return R.ok(umfPayUtil.settle(settleDate));
}
@PostMapping("/flower/wxNotify")
@ApiOperation("花城支付回调")
public void flowerWxNotify(HttpServletRequest request, HttpServletResponse response) {
log.error("微信支付回调start");
String inputLine = "";
String notityXml = "";
try {
while ((inputLine = request.getReader().readLine()) != null) {
notityXml += inputLine;
}
// 关闭流
request.getReader().close();
log.error("微信回调内容信息:" + notityXml);
// 解析成Json
org.json.JSONObject xmlJson = XML.toJSONObject(notityXml);
if (StringUtils.isNotEmpty(xmlJson.toString())) {
JSONObject resultData = JSON.parseObject(xmlJson.get("xml").toString());
// 成功支付订单
if (resultData.getString("result_code").equals("SUCCESS")
&& resultData.getString("return_code").equals("SUCCESS")) {
WxPayNotifyOrderDTO notifyOrderDTO = new WxPayNotifyOrderDTO();
// 订单号
String orderTradeNo = resultData.getString("out_trade_no");
notifyOrderDTO.setOrderTradeNo(orderTradeNo);
// 微信支付订单号
String wxTradeNo = resultData.getString("transaction_id");
notifyOrderDTO.setWxTradeNo(wxTradeNo);
// 订单金额
String totalFee = resultData.getString("total_fee");
notifyOrderDTO.setTotalFee(totalFee);
// 支付完成时间
String payTime = resultData.getString("time_end");
notifyOrderDTO.setPayTime(payTime);
// 现金支付金额
String cashFee = resultData.getString("cash_fee");
notifyOrderDTO.setCashFee(cashFee);
// 根据订单号修改订单信息
communityService.wxOrderPayNotifyFlower(notifyOrderDTO);
// 封装 返回值
StringBuffer buffer = new StringBuffer();
buffer.append("");
buffer.append("SUCCESS");
buffer.append("OK");
buffer.append("");
// 给微信服务器返回 成功标示 否则会一直询问 咱们服务器 是否回调成功
PrintWriter writer = response.getWriter();
// 返回
writer.print(buffer.toString());
} else {// 未成功支付订单
// 封装 返回值
StringBuffer buffer = new StringBuffer();
buffer.append("");
buffer.append("FAIL");
buffer.append("FAIL");
buffer.append("");
// 给微信服务器返回 成功标示 否则会一直询问 咱们服务器 是否回调成功
PrintWriter writer = response.getWriter();
// 返回
writer.print(buffer.toString());
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}