Commit 8199ffe7 authored by hezhen's avatar hezhen

添加退款功能

parent 0dec6e30
...@@ -35,4 +35,4 @@ APP_TRADE_TYPE=APP ...@@ -35,4 +35,4 @@ APP_TRADE_TYPE=APP
#支付回调 #支付回调
weixinHost=xxtest.upyuns.com weixinHost=xxtest.upyuns.com
#证书存放目录 #证书存放目录
APICLIENT_CERT=D:\\server\\cert\\apiclient_cert.p12 APICLIENT_CERT=D:\\cert\\1514583081_20181017_cert.p12
...@@ -23,10 +23,6 @@ public class OrderRefundVo{ ...@@ -23,10 +23,6 @@ public class OrderRefundVo{
//订单号 //订单号
@ApiModelProperty(value = "订单号") @ApiModelProperty(value = "订单号")
private String orderNo; private String orderNo;
//用户id
@ApiModelProperty(value = "用户id")
private Integer userId;
//支付金额 //支付金额
@ApiModelProperty(value = "支付金额") @ApiModelProperty(value = "支付金额")
...@@ -36,9 +32,6 @@ public class OrderRefundVo{ ...@@ -36,9 +32,6 @@ public class OrderRefundVo{
@ApiModelProperty(value = "退款金额") @ApiModelProperty(value = "退款金额")
private Integer refundAmount; private Integer refundAmount;
//支付接口返回的流水号
@ApiModelProperty(value = "支付接口返回的流水号")
private String serialNumber;
//退款描述 //退款描述
@ApiModelProperty(value = "退款描述") @ApiModelProperty(value = "退款描述")
......
...@@ -7,8 +7,6 @@ import java.util.HashMap; ...@@ -7,8 +7,6 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Random; import java.util.Random;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import com.github.wxiaoqi.security.common.util.process.SystemConfig;
import com.xxfc.platform.universal.weixin.util.Snowflake;
import com.xxfc.platform.universal.weixin.util.WeChatSignUtil; import com.xxfc.platform.universal.weixin.util.WeChatSignUtil;
import com.xxfc.platform.universal.weixin.util.XmlUtils; import com.xxfc.platform.universal.weixin.util.XmlUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -36,15 +34,15 @@ public class WxPayRefundUtils { ...@@ -36,15 +34,15 @@ public class WxPayRefundUtils {
// private static final String pay_appId = "wx81470220f10b266e"; // private static final String pay_appId = "wx81470220f10b266e";
// private static final String pay_mchId = "1423414902"; // private static final String pay_mchId = "1423414902";
// private static final String subMchId = "1511773781"; // private static final String subMchId = "1511773781";
private static final String APICLIENT_CERT = SystemConfig.APICLIENT_CERT; //private static final String APICLIENT_CERT = SystemConfig.APICLIENT_CERT;
// private static final String pay_partnerKey = // private static final String pay_partnerKey =
// "CNITR89201000CNITR89201000FUWUSH"; // "CNITR89201000CNITR89201000FUWUSH";
public static boolean refund(String pay_appId, String pay_mchId,String pay_partnerKey, public static boolean refund(String pay_appId, String pay_mchId,String pay_partnerKey,
String out_trade_no,String out_refund_no, String total_fee, String refund_fee, String refund_desc) { String out_trade_no,String out_refund_no, String total_fee, String refund_fee, String refund_desc,String apiclient_cert) {
String result = postXML(URL, genXML(pay_appId, pay_mchId,pay_partnerKey, out_trade_no,out_refund_no, total_fee, String result = postXML(URL, genXML(pay_appId, pay_mchId,pay_partnerKey, out_trade_no,out_refund_no, total_fee,
refund_fee, refund_desc), pay_mchId); refund_fee, refund_desc), pay_mchId,apiclient_cert);
System.out.println(result); log.info(result);
// 判定退款操作结果是否正确 // 判定退款操作结果是否正确
String return_msg = "<return_msg><![CDATA[OK]]></return_msg>"; String return_msg = "<return_msg><![CDATA[OK]]></return_msg>";
String result_code = "<result_code><![CDATA[SUCCESS]]></result_code>"; String result_code = "<result_code><![CDATA[SUCCESS]]></result_code>";
...@@ -52,8 +50,8 @@ public class WxPayRefundUtils { ...@@ -52,8 +50,8 @@ public class WxPayRefundUtils {
return result.contains(result_code) && result.contains(return_msg) && result.contains(return_code); return result.contains(result_code) && result.contains(return_msg) && result.contains(return_code);
} }
private static String postXML(String url, String xml, String pay_mchId) { private static String postXML(String url, String xml, String pay_mchId,String apiclient_cert) {
try (FileInputStream ips = new FileInputStream(new File(APICLIENT_CERT));) { try (FileInputStream ips = new FileInputStream(new File(apiclient_cert));) {
KeyStore keyStore = KeyStore.getInstance("PKCS12"); KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(ips, pay_mchId.toCharArray()); keyStore.load(ips, pay_mchId.toCharArray());
SSLContext sslContext = SSLContexts.custom().loadKeyMaterial(keyStore, pay_mchId.toCharArray()).build(); SSLContext sslContext = SSLContexts.custom().loadKeyMaterial(keyStore, pay_mchId.toCharArray()).build();
......
package com.xxfc.platform.universal.biz; package com.xxfc.platform.universal.biz;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.github.wxiaoqi.security.common.util.OrderUtil;
import com.github.wxiaoqi.security.common.biz.BaseBiz; import com.github.wxiaoqi.security.common.biz.BaseBiz;
import com.github.wxiaoqi.security.common.util.OrderUtil;
import com.github.wxiaoqi.security.common.util.process.ResultCode; import com.github.wxiaoqi.security.common.util.process.ResultCode;
import com.github.wxiaoqi.security.common.util.process.SystemConfig; import com.github.wxiaoqi.security.common.util.process.SystemConfig;
import com.github.wxiaoqi.security.common.util.result.JsonResultUtil; import com.github.wxiaoqi.security.common.util.result.JsonResultUtil;
import com.xxfc.platform.universal.entity.OrderPay;
import com.xxfc.platform.universal.entity.OrderRefund; import com.xxfc.platform.universal.entity.OrderRefund;
import com.xxfc.platform.universal.mapper.OrderRefundMapper; import com.xxfc.platform.universal.mapper.OrderRefundMapper;
import com.xxfc.platform.universal.vo.OrderRefundVo; import com.xxfc.platform.universal.vo.OrderRefundVo;
...@@ -15,7 +14,11 @@ import com.xxfc.platform.universal.weixin.util.Snowflake; ...@@ -15,7 +14,11 @@ import com.xxfc.platform.universal.weixin.util.Snowflake;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import java.util.List;
/** /**
* 订单支付退款表 * 订单支付退款表
...@@ -28,6 +31,10 @@ import org.springframework.stereotype.Service; ...@@ -28,6 +31,10 @@ import org.springframework.stereotype.Service;
@Slf4j @Slf4j
public class OrderRefundBiz extends BaseBiz<OrderRefundMapper,OrderRefund> { public class OrderRefundBiz extends BaseBiz<OrderRefundMapper,OrderRefund> {
@Autowired
private OrderPayBiz payBiz;
@Value("${universal.cert}")
private String APICLIENT_CERT ;
//申请退款 //申请退款
public JSONObject refund(OrderRefundVo orderRefundVo)throws Exception{ public JSONObject refund(OrderRefundVo orderRefundVo)throws Exception{
...@@ -35,7 +42,7 @@ public class OrderRefundBiz extends BaseBiz<OrderRefundMapper,OrderRefund> { ...@@ -35,7 +42,7 @@ public class OrderRefundBiz extends BaseBiz<OrderRefundMapper,OrderRefund> {
log.error("-----参数为空-----------"); log.error("-----参数为空-----------");
return JsonResultUtil.createFailedResult(ResultCode.NULL_CODE, "参数为空"); return JsonResultUtil.createFailedResult(ResultCode.NULL_CODE, "参数为空");
} }
String out_trade_no=orderRefundVo.getOrderNo(); String order_no=orderRefundVo.getOrderNo();
String appid= SystemConfig.APP_ID; String appid= SystemConfig.APP_ID;
String mchId=SystemConfig.APP_PARTNER; String mchId=SystemConfig.APP_PARTNER;
String partnerKey=SystemConfig.APP_PARTNER_KEY; String partnerKey=SystemConfig.APP_PARTNER_KEY;
...@@ -43,26 +50,36 @@ public class OrderRefundBiz extends BaseBiz<OrderRefundMapper,OrderRefund> { ...@@ -43,26 +50,36 @@ public class OrderRefundBiz extends BaseBiz<OrderRefundMapper,OrderRefund> {
Integer refundAmount=orderRefundVo.getRefundAmount(); Integer refundAmount=orderRefundVo.getRefundAmount();
String refundDesc =StringUtils.isNotBlank(orderRefundVo.getRefundDesc())?orderRefundVo.getRefundDesc(): "审核通过,退款"; String refundDesc =StringUtils.isNotBlank(orderRefundVo.getRefundDesc())?orderRefundVo.getRefundDesc(): "审核通过,退款";
String out_refund_no = Snowflake.build() + ""; String out_refund_no = Snowflake.build() + "";
if(StringUtils.isBlank(out_trade_no)||StringUtils.isBlank(appid)||StringUtils.isBlank(mchId)||StringUtils.isBlank(partnerKey) if(StringUtils.isBlank(order_no)||StringUtils.isBlank(appid)||StringUtils.isBlank(mchId)||StringUtils.isBlank(partnerKey)
||payAmount==null||payAmount==0||refundAmount==null||refundAmount==0){ ||payAmount==null||payAmount==0||refundAmount==null||refundAmount==0){
log.error("-----参数为空-----------"); log.error("-----参数为空-----------");
return JsonResultUtil.createFailedResult(ResultCode.NULL_CODE, "参数为空"); return JsonResultUtil.createFailedResult(ResultCode.NULL_CODE, "参数为空");
} }
Example example =new Example(OrderPay.class);
example.createCriteria().andEqualTo("orderNo", order_no).andEqualTo("isDel",0).andEqualTo("status",1);
List<OrderPay> list=payBiz.selectByExample(example);
if(list.size()==0){
log.error("---支付回调---trade_no====="+order_no+"----订单不存在或未支付");
return JsonResultUtil.createFailedResult(ResultCode.FAILED_CODE, order_no+"订单不存在或未支付");
}
OrderPay orderPay=list.get(0);
String out_trade_no=orderPay.getTradeNo();
boolean flag=WxPayRefundUtils.refund(appid,mchId,partnerKey,out_trade_no,out_refund_no,payAmount+"", boolean flag=WxPayRefundUtils.refund(appid,mchId,partnerKey,out_trade_no,out_refund_no,payAmount+"",
refundAmount+"",refundDesc); refundAmount+"",refundDesc,APICLIENT_CERT);
if(flag){ if(flag){
OrderRefund orderRefund= new OrderRefund(); OrderRefund orderRefund= new OrderRefund();
BeanUtils.copyProperties(orderRefund,orderRefundVo); BeanUtils.copyProperties(orderRefund,orderRefundVo);
if(StringUtils.isNotBlank(orderRefund.getRefundDesc())){ if(StringUtils.isNotBlank(orderRefund.getRefundDesc())){
orderRefund.setRefundDesc(refundDesc); orderRefund.setRefundDesc(refundDesc);
} }
orderRefund.setUserId(orderPay.getUserId());
orderRefund.setStatus(2); orderRefund.setStatus(2);
orderRefund.setFinishTime(System.currentTimeMillis()); orderRefund.setFinishTime(System.currentTimeMillis());
String trade_no = OrderUtil.GetOrderNumber(""); orderRefund.setRefundTradeNo(out_refund_no);
orderRefund.setRefundTradeNo(trade_no); orderRefund.setOutRefundNo(out_trade_no);
orderRefund.setOutRefundNo(out_refund_no); orderRefund.setSerialNumber(orderPay.getSerialNumber());
insertSelective(orderRefund); insertSelective(orderRefund);
return JsonResultUtil.createSuccessResultWithObj(trade_no); return JsonResultUtil.createSuccessResultWithObj(out_refund_no);
} }
return JsonResultUtil.createDefaultFail(); return JsonResultUtil.createDefaultFail();
} }
......
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