Commit 4de105ca authored by zhangyong's avatar zhangyong

1

parent c5abb58b
......@@ -39,214 +39,7 @@ public class WxApiController {
@Autowired
private WxChatService wxChatService;
/**
*
* <p>Title: 微信統一下单接口 </p>
* <p>Description: https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_1&index=1 </p>
* 传参请按微信API的参数属性名传值
* @param request
* @param response
* @return
*/
@ApiOperation("微信統一下单接口")
@ApiImplicitParams({
@ApiImplicitParam(paramType="query", name = "terminal", dataType = "String", required = false, value = "终端标识", defaultValue = "wechat"),
@ApiImplicitParam(paramType="query", name = "langID", dataType = "String", required = false, value = "语言", defaultValue = "936"),
@ApiImplicitParam(paramType="query",name="body",dataType="String",required=true,value="商品描述",defaultValue=""),
@ApiImplicitParam(paramType="query",name="detail",dataType="String",required=false,value="商品详情",defaultValue=""),
@ApiImplicitParam(paramType="query",name="out_trade_no",dataType="String",required=true,value="商户订单号",defaultValue=""),
@ApiImplicitParam(paramType="query",name="total_fee",dataType="String",required=true,value="总金额(分)",defaultValue=""),
@ApiImplicitParam(paramType="query",name="openid",dataType="String",required=false,value="用户标识",defaultValue="")
})
@RequestMapping(value="/unifiedorder",method=RequestMethod.GET)
public String unifiedorder(HttpServletRequest request, HttpServletResponse response){
try {
String terminal = request.getParameter("terminal");
String langID = request.getParameter("langID");
SortedMap<Object,Object> params = new TreeMap<Object,Object>();
String appid = "";
String md5Key = "";
TSystemCtrl tSystemCtrl = wxChatService.loadAppID(PayContstrant.WX_MINI_APP_KEY,terminal, langID);
if(tSystemCtrl != null) {
appid = tSystemCtrl.getsValue1();
String mch_id = tSystemCtrl.getsValue3();
String notify_url = tSystemCtrl.getsValue4();
md5Key = tSystemCtrl.getsValue5();
params.put("appid",appid);
params.put("mch_id",mch_id);
params.put("notify_url", notify_url);
params.put("md5Key", md5Key);
if(!StringUtil.isNotEmpty(appid,mch_id,notify_url,md5Key)) {
return Rjx.jsonErr().setCode(-100).setMessage("支付失败").set("errorMsg", "数据字典参数配置不完整").toJson();
}
}else {
return Rjx.jsonErr().setCode(-100).setMessage("支付失败").set("errorMsg", "数据字典参数未配置").toJson();
}
params.put("nonce_str",StringUtil.getNonceStr());
params.put("sign_type","MD5");
params.put("fee_type","CNY");
params.put("spbill_create_ip",ServletUtil.getLocalIP());
params.put("trade_type", "JSAPI");
params.put("body", request.getParameter("body")==null?"":request.getParameter("body"));
params.put("detail", request.getParameter("detail")==null?"":request.getParameter("detail"));
params.put("out_trade_no", request.getParameter("out_trade_no")==null?"":request.getParameter("out_trade_no"));
params.put("total_fee", request.getParameter("total_fee")==null?"":request.getParameter("total_fee"));
params.put("openid", request.getParameter("openid")==null?"":request.getParameter("openid"));
Map<String,String> resultMap = wxChatService.unifiedorder(params);
String return_code = resultMap.get("return_code");
String result_code = resultMap.get("result_code");
if(return_code.equals("SUCCESS") && result_code.equals("SUCCESS")){ //返回前端支付需要的参数
String return_sign = resultMap.get("sign"); // 返回的签名
resultMap.put("md5Key", md5Key);
if(!WxSignUtil.checkSign( resultMap, return_sign)){
return Rjx.jsonErr().setCode(-100).setMessage("微信下单失败").set("errorMsg", "返回结果签名不一致").toJson();
}
String prepay_id = resultMap.get("prepay_id");
String trade_type = resultMap.get("trade_type");
String packageStr = "prepay_id="+prepay_id;
String payNonceStr = StringUtil.getNonceStr();
String timeStampStr = DateUtil.getTimeStamp();
SortedMap<Object,Object> payParams = new TreeMap<Object,Object>();
payParams.put("appId", appid);
payParams.put("timeStamp", timeStampStr);
payParams.put("nonceStr", payNonceStr);
payParams.put("package", packageStr);
payParams.put("signType", "MD5");
payParams.put("md5Key", md5Key);
String paySign = WxSignUtil.createSign(payParams);
return Rjx.jsonOk().set("package", packageStr).set("trade_type", trade_type).set("nonceStr", payNonceStr).set("timeStamp", timeStampStr).set("paySign", paySign).toJson();
}
if(return_code.equals("FAIL")){
String return_msg = resultMap.get("return_msg");
return Rjx.jsonErr().setCode(-100).setMessage("微信下单失败").set("errorMsg", return_msg).toJson();
}
if(return_code.equals("SUCCESS") && result_code.equals("FAIL")){
String err_code = resultMap.get("err_code");
String err_code_des = resultMap.get("err_code_des");
return Rjx.jsonErr().setCode(-100).setMessage("微信下单失败").set("errorMsg", err_code).set("err_code_des", err_code_des).toJson();
}
} catch (Exception e) {
e.printStackTrace();
return Rjx.jsonErr().setCode(-100).setMessage("微信下单失败").set("errorMsg","生成预支付订单异常").toJson();
}
return Rjx.jsonErr().setCode(-100).setMessage("微信下单失败").toJson();
}
/**
* 申请退款
* <p>Title: https://api.mch.weixin.qq.com/secapi/pay/refund</p>
* <p>Description: </p>
* @param request
* @param response
* @return
*/
@ApiOperation("申请退款")
@ApiImplicitParams({
@ApiImplicitParam(paramType="query", name = "terminal", dataType = "String", required = false, value = "终端标识", defaultValue = "wechat"),
@ApiImplicitParam(paramType="query", name = "langID", dataType = "String", required = false, value = "语言", defaultValue = "936"),
@ApiImplicitParam(paramType="query",name="transaction_id",dataType="String",required=true,value="微信订单号",defaultValue=""),
@ApiImplicitParam(paramType="query",name="out_trade_no",dataType="String",required=false,value="商户订单号",defaultValue=""),
@ApiImplicitParam(paramType="query",name="out_refund_no",dataType="String",required=true,value="商户退款单号",defaultValue=""),
@ApiImplicitParam(paramType="query",name="total_fee",dataType="String",required=true,value="订单金额(分)",defaultValue=""),
@ApiImplicitParam(paramType="query",name="refund_fee",dataType="String",required=false,value="申请退款金额(分)",defaultValue=""),
})
@RequestMapping(value="/refund",method=RequestMethod.GET)
public String refund(HttpServletRequest request, HttpServletResponse response){
try {
String terminal = request.getParameter("terminal");
String langID = request.getParameter("langID");
SortedMap<Object,Object> params = new TreeMap<Object,Object>();
String appid = "";
String mch_id ="";
String md5Key = "";
TSystemCtrl tSystemCtrl = wxChatService.loadAppID(PayContstrant.WX_MINI_APP_KEY,terminal, langID);
if(tSystemCtrl != null) {
appid = tSystemCtrl.getsValue1();
mch_id =tSystemCtrl.getsValue3();
md5Key = tSystemCtrl.getsValue5();
if(!StringUtil.isNotEmpty(appid,mch_id,md5Key)) {
return Rjx.jsonErr().setCode(-100).setMessage("支付失败").set("errorMsg", "数据字典参数配置不完整").toJson();
}
}else {
return Rjx.jsonErr().setCode(-100).setMessage("支付失败").set("errorMsg", "数据字典参数未配置").toJson();
}
params.put("appid", appid);
params.put("mch_id", mch_id);
params.put("md5Key", md5Key);
params.put("nonce_str", StringUtil.getNonceStr()); //随机字符串
params.put("sign_type", "MD5");
params.put("transaction_id", request.getParameter("transaction_id")==null?"":request.getParameter("transaction_id"));
params.put("out_trade_no", request.getParameter("out_trade_no")==null?"":request.getParameter("out_trade_no"));
params.put("out_refund_no", request.getParameter("out_refund_no")==null?"":request.getParameter("out_refund_no"));
params.put("total_fee", request.getParameter("total_fee")==null?"":request.getParameter("total_fee"));
params.put("refund_fee", request.getParameter("refund_fee")==null?"":request.getParameter("refund_fee"));
Map<String,String> resultMap = wxChatService.refund(params);
String return_code = resultMap.get("return_code");
String result_code = resultMap.get("result_code");
if(return_code.equals("SUCCESS") && result_code.equals("SUCCESS")){ //返回前端支付需要的参数
String return_sign = resultMap.get("sign"); // 返回的签名
resultMap.put("md5Key", md5Key);
if(!WxSignUtil.checkSign( resultMap, return_sign)){
return Rjx.jsonErr().setCode(-100).setMessage("退款失败").set("errorMsg","返回结果签名不一致").toJson();
}
return Rjx.jsonOk().setMessage("微信退款成功").toJson();
}
if(return_code.equals("FAIL")){
String return_msg = resultMap.get("return_msg");
return Rjx.jsonErr().setCode(-100).setMessage("退款失败").set("errorMsg",return_msg).toJson();
}
if(return_code.equals("SUCCESS") && result_code.equals("FAIL")){
String err_code = resultMap.get("err_code");
String err_code_des = resultMap.get("err_code_des");
return Rjx.jsonErr().setCode(-100).setMessage("退款失败").set("err_code", err_code).set("errorMsg", err_code_des).toJson();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
......
......@@ -2,6 +2,8 @@ package com.egolm.payment.controller;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
......@@ -14,11 +16,14 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.egolm.common.DateUtil;
import com.egolm.common.StringUtil;
import com.egolm.common.bean.Rjx;
import com.egolm.common.web.ServletUtil;
import com.egolm.payment.pojo.TSystemCtrl;
import com.egolm.payment.service.WxChatService;
import com.egolm.payment.util.PayContstrant;
import com.egolm.payment.util.WxSignUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
......@@ -85,6 +90,213 @@ public class WxMiniApiController {
return Rjx.jsonErr().set("errcode", errcode).set("errmsg", errmsg).toJson();
}
/**
*
* <p>Title: 微信統一下单接口 </p>
* <p>Description: https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_1&index=1 </p>
* 传参请按微信API的参数属性名传值
* @param request
* @param response
* @return
*/
@ApiOperation("微信統一下单接口")
@ApiImplicitParams({
@ApiImplicitParam(paramType="query", name = "terminal", dataType = "String", required = false, value = "终端标识", defaultValue = "wechat"),
@ApiImplicitParam(paramType="query", name = "langID", dataType = "String", required = false, value = "语言", defaultValue = "936"),
@ApiImplicitParam(paramType="query",name="body",dataType="String",required=true,value="商品描述",defaultValue=""),
@ApiImplicitParam(paramType="query",name="detail",dataType="String",required=false,value="商品详情",defaultValue=""),
@ApiImplicitParam(paramType="query",name="out_trade_no",dataType="String",required=true,value="商户订单号",defaultValue=""),
@ApiImplicitParam(paramType="query",name="total_fee",dataType="String",required=true,value="总金额(分)",defaultValue=""),
@ApiImplicitParam(paramType="query",name="openid",dataType="String",required=false,value="用户标识",defaultValue="")
})
@RequestMapping(value="/unifiedorder",method=RequestMethod.GET)
public String unifiedorder(HttpServletRequest request, HttpServletResponse response){
try {
String terminal = request.getParameter("terminal");
String langID = request.getParameter("langID");
SortedMap<Object,Object> params = new TreeMap<Object,Object>();
String appid = "";
String md5Key = "";
TSystemCtrl tSystemCtrl = wxChatService.loadAppID(PayContstrant.WX_MINI_APP_KEY,terminal, langID);
if(tSystemCtrl != null) {
appid = tSystemCtrl.getsValue1();
String mch_id = tSystemCtrl.getsValue3();
String notify_url = tSystemCtrl.getsValue4();
md5Key = tSystemCtrl.getsValue5();
params.put("appid",appid);
params.put("mch_id",mch_id);
params.put("notify_url", notify_url);
params.put("md5Key", md5Key);
if(!StringUtil.isNotEmpty(appid,mch_id,notify_url,md5Key)) {
return Rjx.jsonErr().setCode(-100).setMessage("支付失败").set("errorMsg", "数据字典参数配置不完整").toJson();
}
}else {
return Rjx.jsonErr().setCode(-100).setMessage("支付失败").set("errorMsg", "数据字典参数未配置").toJson();
}
params.put("nonce_str",StringUtil.getNonceStr());
params.put("sign_type","MD5");
params.put("fee_type","CNY");
params.put("spbill_create_ip",ServletUtil.getLocalIP());
params.put("trade_type", "JSAPI");
params.put("body", request.getParameter("body")==null?"":request.getParameter("body"));
params.put("detail", request.getParameter("detail")==null?"":request.getParameter("detail"));
params.put("out_trade_no", request.getParameter("out_trade_no")==null?"":request.getParameter("out_trade_no"));
params.put("total_fee", request.getParameter("total_fee")==null?"":request.getParameter("total_fee"));
params.put("openid", request.getParameter("openid")==null?"":request.getParameter("openid"));
Map<String,String> resultMap = wxChatService.unifiedorder(params);
String return_code = resultMap.get("return_code");
String result_code = resultMap.get("result_code");
if(return_code.equals("SUCCESS") && result_code.equals("SUCCESS")){ //返回前端支付需要的参数
String return_sign = resultMap.get("sign"); // 返回的签名
resultMap.put("md5Key", md5Key);
if(!WxSignUtil.checkSign( resultMap, return_sign)){
return Rjx.jsonErr().setCode(-100).setMessage("微信下单失败").set("errorMsg", "返回结果签名不一致").toJson();
}
String prepay_id = resultMap.get("prepay_id");
String trade_type = resultMap.get("trade_type");
String packageStr = "prepay_id="+prepay_id;
String payNonceStr = StringUtil.getNonceStr();
String timeStampStr = DateUtil.getTimeStamp();
SortedMap<Object,Object> payParams = new TreeMap<Object,Object>();
payParams.put("appId", appid);
payParams.put("timeStamp", timeStampStr);
payParams.put("nonceStr", payNonceStr);
payParams.put("package", packageStr);
payParams.put("signType", "MD5");
payParams.put("md5Key", md5Key);
String paySign = WxSignUtil.createSign(payParams);
return Rjx.jsonOk().set("package", packageStr).set("trade_type", trade_type).set("nonceStr", payNonceStr).set("timeStamp", timeStampStr).set("paySign", paySign).toJson();
}
if(return_code.equals("FAIL")){
String return_msg = resultMap.get("return_msg");
return Rjx.jsonErr().setCode(-100).setMessage("微信下单失败").set("errorMsg", return_msg).toJson();
}
if(return_code.equals("SUCCESS") && result_code.equals("FAIL")){
String err_code = resultMap.get("err_code");
String err_code_des = resultMap.get("err_code_des");
return Rjx.jsonErr().setCode(-100).setMessage("微信下单失败").set("errorMsg", err_code).set("err_code_des", err_code_des).toJson();
}
} catch (Exception e) {
e.printStackTrace();
return Rjx.jsonErr().setCode(-100).setMessage("微信下单失败").set("errorMsg","生成预支付订单异常").toJson();
}
return Rjx.jsonErr().setCode(-100).setMessage("微信下单失败").toJson();
}
/**
* 申请退款
* <p>Title: https://api.mch.weixin.qq.com/secapi/pay/refund</p>
* <p>Description: </p>
* @param request
* @param response
* @return
*/
@ApiOperation("申请退款")
@ApiImplicitParams({
@ApiImplicitParam(paramType="query", name = "terminal", dataType = "String", required = false, value = "终端标识", defaultValue = "wechat"),
@ApiImplicitParam(paramType="query", name = "langID", dataType = "String", required = false, value = "语言", defaultValue = "936"),
@ApiImplicitParam(paramType="query",name="transaction_id",dataType="String",required=true,value="微信订单号",defaultValue=""),
@ApiImplicitParam(paramType="query",name="out_trade_no",dataType="String",required=false,value="商户订单号",defaultValue=""),
@ApiImplicitParam(paramType="query",name="out_refund_no",dataType="String",required=true,value="商户退款单号",defaultValue=""),
@ApiImplicitParam(paramType="query",name="total_fee",dataType="String",required=true,value="订单金额(分)",defaultValue=""),
@ApiImplicitParam(paramType="query",name="refund_fee",dataType="String",required=false,value="申请退款金额(分)",defaultValue=""),
})
@RequestMapping(value="/refund",method=RequestMethod.GET)
public String refund(HttpServletRequest request, HttpServletResponse response){
try {
String terminal = request.getParameter("terminal");
String langID = request.getParameter("langID");
SortedMap<Object,Object> params = new TreeMap<Object,Object>();
String appid = "";
String mch_id ="";
String md5Key = "";
TSystemCtrl tSystemCtrl = wxChatService.loadAppID(PayContstrant.WX_MINI_APP_KEY,terminal, langID);
if(tSystemCtrl != null) {
appid = tSystemCtrl.getsValue1();
mch_id =tSystemCtrl.getsValue3();
md5Key = tSystemCtrl.getsValue5();
if(!StringUtil.isNotEmpty(appid,mch_id,md5Key)) {
return Rjx.jsonErr().setCode(-100).setMessage("支付失败").set("errorMsg", "数据字典参数配置不完整").toJson();
}
}else {
return Rjx.jsonErr().setCode(-100).setMessage("支付失败").set("errorMsg", "数据字典参数未配置").toJson();
}
params.put("appid", appid);
params.put("mch_id", mch_id);
params.put("md5Key", md5Key);
params.put("nonce_str", StringUtil.getNonceStr()); //随机字符串
params.put("sign_type", "MD5");
params.put("transaction_id", request.getParameter("transaction_id")==null?"":request.getParameter("transaction_id"));
params.put("out_trade_no", request.getParameter("out_trade_no")==null?"":request.getParameter("out_trade_no"));
params.put("out_refund_no", request.getParameter("out_refund_no")==null?"":request.getParameter("out_refund_no"));
params.put("total_fee", request.getParameter("total_fee")==null?"":request.getParameter("total_fee"));
params.put("refund_fee", request.getParameter("refund_fee")==null?"":request.getParameter("refund_fee"));
Map<String,String> resultMap = wxChatService.refund(params);
String return_code = resultMap.get("return_code");
String result_code = resultMap.get("result_code");
if(return_code.equals("SUCCESS") && result_code.equals("SUCCESS")){ //返回前端支付需要的参数
String return_sign = resultMap.get("sign"); // 返回的签名
resultMap.put("md5Key", md5Key);
if(!WxSignUtil.checkSign( resultMap, return_sign)){
return Rjx.jsonErr().setCode(-100).setMessage("退款失败").set("errorMsg","返回结果签名不一致").toJson();
}
return Rjx.jsonOk().setMessage("微信退款成功").toJson();
}
if(return_code.equals("FAIL")){
String return_msg = resultMap.get("return_msg");
return Rjx.jsonErr().setCode(-100).setMessage("退款失败").set("errorMsg",return_msg).toJson();
}
if(return_code.equals("SUCCESS") && result_code.equals("FAIL")){
String err_code = resultMap.get("err_code");
String err_code_des = resultMap.get("err_code_des");
return Rjx.jsonErr().setCode(-100).setMessage("退款失败").set("err_code", err_code).set("errorMsg", err_code_des).toJson();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment