package cc.mrbird.febs.pay.util;
|
|
import cc.mrbird.febs.common.exception.FebsException;
|
import cc.mrbird.febs.common.properties.XcxProperties;
|
import cc.mrbird.febs.common.utils.SpringContextHolder;
|
import cc.mrbird.febs.pay.model.*;
|
import cc.mrbird.febs.pay.service.impl.JsApiPayComService;
|
import cc.mrbird.febs.pay.service.impl.RefundService;
|
import cn.hutool.core.util.StrUtil;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.Service;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
/**微信工具类
|
* @author jiangyouyao
|
*
|
*/
|
@Slf4j
|
@Service(value="weixinServiceUtil")
|
public class WeixinServiceUtil {
|
|
private final XcxProperties xcxProperties = SpringContextHolder.getBean(XcxProperties.class);
|
|
/**
|
* 支付"175.9.82.254"
|
* WebUtil.getRequest().getRemoteAddr()
|
* getRemortIP(WebUtil.getRequest())
|
* @Description 公众号支付,生成预支付订单
|
@date 2017年6月27日
|
@atuhor jiangyouyao
|
* @param desc 描述信息
|
* @param outTradeNo 订单id
|
* @param price 订单价格,类型为整型,单位是分,不是元,等于你的实际价格*100
|
* @param openId 支付者的微信openId
|
* @return 预支付订单返回的结果对象(该结果对象已封装),在H5页面使用该对象信息
|
* @throws Exception
|
*/
|
public BrandWCPayRequestData createOrder(String desc, String outTradeNo, int price, String openId, String attach) throws Exception {
|
String wecharPaynotifyUrl = xcxProperties.getWecharPaynotifyUrl();
|
return buildBrandWCPayRequestData(desc, outTradeNo, price, openId, attach, wecharPaynotifyUrl);
|
}
|
|
public BrandWCPayRequestData createRechargeWallet(String desc, String outTradeNo, int price, String openId, String attach) throws Exception {
|
String wecharRechargePaynotifyUrl = xcxProperties.getWecharRechargePaynotifyUrl();
|
return buildBrandWCPayRequestData(desc, outTradeNo, price, openId, attach, wecharRechargePaynotifyUrl);
|
}
|
|
private BrandWCPayRequestData buildBrandWCPayRequestData(String desc, String outTradeNo, int price, String openId, String attach, String notifyUrl) throws Exception {
|
// 创建微信支付预付接口
|
JsApiPayBusiness jsApiPayBusiness = new JsApiPayBusiness();
|
String idAddr = getIpAddr(WebUtil.getRequest());
|
|
String mchID = xcxProperties.getWecharpayMchid();
|
String paySecret = xcxProperties.getWecharpaySecret();
|
String appId = xcxProperties.getXcxAppid();
|
|
JsApiPayReqData jsApiPayReqData = new JsApiPayReqData(
|
appId, mchID, paySecret, notifyUrl, desc,
|
outTradeNo, price, idAddr, openId, attach);
|
|
// 创建预付订单并返回请求结果
|
JsApiPayResData result = jsApiPayBusiness.createPrapareOrder(jsApiPayReqData);
|
// 把预付订单的信息存放在request域中
|
WebUtil.getRequest().setAttribute("msg", result.toString());
|
log.info("#---返回码:return_code = {}" , result.getReturn_code());
|
log.info("#---签名信息:return_msg = {}" , result.getReturn_msg());
|
if (result.getReturn_code().equals("SUCCESS")) {
|
// 请求成功, 构建BrandWCPayRequest发起支付需要的参数
|
BrandWCPayRequestData payData = new BrandWCPayRequestData(result.getPrepay_id(),appId,paySecret);
|
WebUtil.getRequest().setAttribute("payData", payData);
|
return payData;
|
}else{
|
log.error("创建微信支付订单失败msg={}",result.getReturn_msg());
|
throw new FebsException("创建微信支付订单失败,请检查程序配置");
|
}
|
}
|
|
/**@Description 退款,企业付款到个人
|
@date 2017年5月26日
|
@atuhor jiangyouyao
|
* @param outTradeNo 商户订单编号(原订单编号)
|
* @param outRefundNo 退款编号
|
* @param totalFee 原订单金额(分)
|
* @param refundFee 退款金额(分)
|
* @param opUserID 操作员id,默认为商户号
|
* @return
|
*/
|
public boolean comRefund(String outTradeNo, String outRefundNo,int totalFee,
|
int refundFee, String opUserID){
|
log.info("#---outTradeNo:{}#---outRefundNo:{}#---totalFee:{}#---refundFee:{}",
|
outTradeNo,outRefundNo,totalFee,refundFee);
|
|
String mchID = xcxProperties.getWecharpayMchid();
|
if(opUserID==null){
|
opUserID=mchID;
|
}
|
RefundResData result=null;
|
boolean flag=false;
|
try {
|
JsApiPayBusiness jsApiPayBusiness = new JsApiPayBusiness();
|
String paySecret = xcxProperties.getWecharpaySecret();
|
String appId = xcxProperties.getXcxAppid();
|
String certLocalPath = xcxProperties.getCertLocalPath();
|
|
RefundReqData refundReqData=new RefundReqData(mchID,appId,paySecret, outTradeNo, outRefundNo, totalFee, refundFee,opUserID);
|
RefundService refundService=new RefundService();
|
HttpsRequest2 request2= (HttpsRequest2) refundService.getServiceRequest();
|
request2.setCertLocalPath(certLocalPath);
|
request2.setMchId(mchID);
|
|
result = jsApiPayBusiness.refundComOrder(refundService, refundReqData);
|
log.info("#退款,企业付款到个人---result:{}",result);
|
//如果返回余额不足时,商户余额不足时微信返回状态码(退款失败时)
|
if (StrUtil.equals("NOTENOUGH", result.getErr_code())) {
|
log.info("#退款失败,{}!", "商户余额不足");
|
flag=false;
|
}
|
} catch (Exception e) {
|
log.error("#企业付款到个人异常#退款#outTradeNo:{}#opUserID:{}", e, outTradeNo,opUserID);
|
flag=false;
|
}
|
|
if (result.getResult_code().equals("SUCCESS")) {
|
flag=true;
|
}else{
|
flag=false;
|
}
|
return flag;
|
}
|
|
/**@Description 用户提现提现审核,企业付款到个人
|
@date 2017年5月26日
|
@atuhor jiangyouyao
|
* @param desc 描述
|
* @param outTradeNo 提现申请编号
|
* @param totalFee 提现金额(分)
|
* @param openid
|
* @return
|
*/
|
public boolean comPay(String desc, String outTradeNo,int totalFee, String openid){
|
boolean flag=false;
|
try {
|
JsApiPayBusiness jsApiPayBusiness = new JsApiPayBusiness();
|
String paySecret = xcxProperties.getWecharpaySecret();
|
String appId = xcxProperties.getXcxAppid();
|
String certLocalPath = xcxProperties.getCertLocalPath();
|
String mchID = xcxProperties.getWecharpayMchid();
|
|
JsApiPayComReqData jsApiPayComReqData=new JsApiPayComReqData(mchID,
|
appId,
|
paySecret,
|
desc,outTradeNo,totalFee,openid,"NO_CHECK","");
|
JsApiPayComService jsApiPayComService=new JsApiPayComService();
|
|
HttpsRequest2 request2= (HttpsRequest2) jsApiPayComService.getServiceRequest();
|
request2.setCertLocalPath(certLocalPath);
|
request2.setMchId(mchID);
|
|
JsApiPayComResData result = jsApiPayBusiness.payComOrder(jsApiPayComService, jsApiPayComReqData);
|
|
log.info("#提现,企业付款到个人---result:{}",result);
|
if (result.getResult_code().equals("SUCCESS")) {
|
flag=true;
|
}else{
|
log.info("#提现,企业付款到个人失败,{}!", "商户余额不足");
|
flag=false;
|
}
|
} catch (Exception e) {
|
log.error("#企业付款到个人异常#提现#outTradeNo:{}#opUserID:{}", e, outTradeNo,openid);
|
flag=false;
|
}
|
return flag;
|
}
|
|
/**@Description 支付获取远程设备的ip
|
@date 2017年6月27日
|
@atuhor jiangyouyao
|
* @param request
|
* @return
|
*/
|
public String getIpAddr(HttpServletRequest request) {
|
String ip = request.getHeader("x-forwarded-for");
|
/* if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getHeader("Proxy-Client-IP");
|
}
|
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getHeader("WL-Proxy-Client-IP");
|
} */
|
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getRemoteAddr();
|
}
|
if(ip!=null){
|
ip=ip.split(",")[0];
|
}
|
return ip;
|
}
|
|
|
}
|