赞
踩
一. 需要在pom.xml加入以下依赖!
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-pay</artifactId>
<version>4.2.8.B</version>
</dependency>
二. 将 application.yml 中修改自己商户平台的信息,以下 keyPath 是证书,可以在微信支付后台下载,指定相关目录即可
#微信相关配置 wx: #微信支付配置 pay: #微信公众号或者小程序等的appid (可以拆开) appId: #自己的appid #微信支付商户号 mchId: #自己的商户号 #微信支付商户密钥 mchKey: #自己的商户密钥 #.p12证书的位置,可以指定绝对路径,也可以指定类路径(以classpath:开头) keyPath: #自己的路径\apiclient_cert.p12 #JSAPI--公众号支付或小程序 NATIVE--原生扫码支付 APP--app支付 tradeType: JSAPI #支付回调地址 (自己定义的回调接口代码) paymentCallbackUrl: #自己的支付回调地址 #退款回调地址 (自己定义的回调接口代码) refundCallbackUrl: #自己的退款回调地址 #如果有其他需求可以自己加...
三.将application.yml定义的支付配置创建为常量类 代码如下(可以自行修改):
/** * 微信支付常量类 * @author bolong * @date 2022-03-16 */ public class WxPayOwnConstant { /** * 微信 公众号 或者 小程序 等 appid (可以拆开) */ public static String APP_ID = ""; /** * 微信支付商户号 */ public static String MCH_ID = ""; /** * 微信支付商户密钥 */ public static String MCH_KEY = ""; /** * .p12证书位置 */ public static String KEY_PATH = ""; /** * 支付回调地址 */ public static String PAYMENT_CALLBACK_URL = ""; /** * 退款回调地址 */ public static String REFUND_CALLBACK_URL = ""; }
四.启动时加载application.yml 配置 代码如下:
import com.bolong.websocket.websocketdemo.wx.pay.constant.WxPayOwnConstant; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; import javax.annotation.PostConstruct; /** * 微信支付启动时配置加载 * * @author bolong * @date 2022-03-16 */ @Configuration public class WxPayOwnInitiate { @Autowired private Environment environment; /** * 启动时加载配置 */ @PostConstruct public void initWxPay() { //微信 公众号 或者 小程序 等 appid WxPayOwnConstant.APP_ID = environment.getProperty("wx.pay.appId"); //微信支付商户号 WxPayOwnConstant.MCH_ID = environment.getProperty("wx.pay.mchId"); //微信支付商户密钥 WxPayOwnConstant.MCH_KEY = environment.getProperty("wx.pay.mchKey"); //.p12证书的位置 WxPayOwnConstant.KEY_PATH = environment.getProperty("wx.pay.keyPath"); //支付回调地址 WxPayOwnConstant.PAYMENT_CALLBACK_URL = environment.getProperty("wx.pay.paymentCallbackUrl"); //退款回调地址 WxPayOwnConstant.REFUND_CALLBACK_URL = environment.getProperty("wx.pay.refundCallbackUrl"); } }
五.写WxPayService配置类 代码如下(可自行修改):
import com.bolong.websocket.websocketdemo.wx.pay.constant.WxPayOwnConstant; import com.github.binarywang.wxpay.config.WxPayConfig; import com.github.binarywang.wxpay.service.WxPayService; import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl; import com.google.common.collect.Maps; import org.springframework.context.annotation.Configuration; import java.util.Map; /** * 微信支付配置 * @author bolong * @date 2022-03-16 * */ @Configuration public class WxPayOwnConfiguration { //微信支付service map集合 private static Map<String, WxPayService> wxPayServicesMap = Maps.newHashMap(); private static WxPayService wechatWxPayService; private static WxPayService miniAppWxPayService; /** * 获取支付类型 * @param wxPayServicesKey 生成类型 * @return WxPayService */ public synchronized static WxPayService getWxPayService(String wxPayServicesKey) { //获取集合中的 WxPayService WxPayService wxPayService = wxPayServicesMap.get(wxPayServicesKey); //集合中没有则创建 if (wxPayService == null) { WxPayConfig wxPayConfig = new WxPayConfig(); switch (wxPayServicesKey) { case "wechat": wxPayConfig.setAppId(WxPayOwnConstant.APP_ID); break; case "miniApp": wxPayConfig.setAppId(WxPayOwnConstant.APP_ID); break; } wxPayConfig.setMchId(WxPayOwnConstant.MCH_ID); wxPayConfig.setMchKey(WxPayOwnConstant.MCH_KEY); wxPayConfig.setKeyPath(WxPayOwnConstant.KEY_PATH); switch (wxPayServicesKey) { case "wechat": wechatWxPayService = new WxPayServiceImpl(); wechatWxPayService.setConfig(wxPayConfig); wxPayServicesMap.put("wechat", wechatWxPayService); return wechatWxPayService; case "miniApp": miniAppWxPayService = new WxPayServiceImpl(); miniAppWxPayService.setConfig(wxPayConfig); wxPayServicesMap.put("miniApp", miniAppWxPayService); return miniAppWxPayService; } } return wxPayService; } }
六.创建接口 代码文件如下(可自行修改):
import com.bolong.websocket.websocketdemo.wx.pay.dto.PaymentDto; import com.bolong.websocket.websocketdemo.wx.pay.dto.RefundDto; import com.bolong.websocket.websocketdemo.wx.pay.dto.WithdrawalDto; /** * * 微信支付接口 * @author bolong * @date 2022-03-16 * */ public interface WxPayOwnService { /** * 支付方法 * @return 结果 */ public Object payment(PaymentDto paymentDto); /** * 退款方法 * @return 结果 */ public Object refund(RefundDto refundDto); /** * 提现方法 (调用的就是企业付款到用户) * @return 结果 */ public Object withdrawal(WithdrawalDto withdrawalDto); }
import com.bolong.websocket.websocketdemo.wx.pay.config.WxPayOwnConfiguration; import com.bolong.websocket.websocketdemo.wx.pay.constant.WxPayOwnConstant; import com.bolong.websocket.websocketdemo.wx.pay.dto.PaymentDto; import com.bolong.websocket.websocketdemo.wx.pay.dto.RefundDto; import com.bolong.websocket.websocketdemo.wx.pay.dto.WithdrawalDto; import com.bolong.websocket.websocketdemo.wx.pay.service.WxPayOwnService; import com.bolong.websocket.websocketdemo.wx.pay.utils.WxPayOwnUtil; import com.github.binarywang.wxpay.bean.entpay.EntPayRequest; import com.github.binarywang.wxpay.bean.entpay.EntPayResult; import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult; import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest; import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest; import com.github.binarywang.wxpay.bean.result.WxPayRefundResult; import com.github.binarywang.wxpay.exception.WxPayException; import com.github.binarywang.wxpay.service.WxPayService; import org.springframework.stereotype.Service; import java.util.HashMap; import java.util.Map; @Service public class WxPayOwnServiceImpl implements WxPayOwnService { /** * 微信支付接口 * app支付返回值为WxPayAppOrderResult * @param paymentDto 微信支付所需参数 * @return 结果 */ @Override public Object payment(PaymentDto paymentDto) { WxPayService wxPayService = null; if (paymentDto.getPaymentType().equals("wechat")) { wxPayService = WxPayOwnConfiguration.getWxPayService("wechat"); } else if (paymentDto.getPaymentType().equals("miniApp")) { wxPayService = WxPayOwnConfiguration.getWxPayService("miniApp"); } WxPayUnifiedOrderRequest wxPayUnifiedOrderRequest = new WxPayUnifiedOrderRequest(); //商品描述 wxPayUnifiedOrderRequest.setBody(paymentDto.getTradeDesc()); //商户订单号 wxPayUnifiedOrderRequest.setOutTradeNo(paymentDto.getTradeNumber()); //标价金额 单位分 wxPayUnifiedOrderRequest.setTotalFee(WxPayOwnUtil.yuanToFee(paymentDto.getTotalFee())); //终端IP wxPayUnifiedOrderRequest.setSpbillCreateIp("127.0.0.1"); //回调地址 wxPayUnifiedOrderRequest.setNotifyUrl(WxPayOwnConstant.PAYMENT_CALLBACK_URL); //交易类型 wxPayUnifiedOrderRequest.setTradeType("JSAPI"); //交易起始时间 wxPayUnifiedOrderRequest.setTimeStart(WxPayOwnUtil.createTimestamp()); //用户openid wxPayUnifiedOrderRequest.setOpenid(paymentDto.getOpenid()); WxPayMpOrderResult wxPayMpOrderResult = null; try { wxPayMpOrderResult = (WxPayMpOrderResult) wxPayService.createOrder(wxPayUnifiedOrderRequest); } catch (WxPayException e) { System.err.println("支付时发生异常"); e.printStackTrace(); return null; } //成功获取参数 Map<String, String> returnPayParam = new HashMap<>(); if (wxPayMpOrderResult != null) { returnPayParam.put("appId", wxPayMpOrderResult.getAppId()); returnPayParam.put("timeStamp", wxPayMpOrderResult.getTimeStamp()); returnPayParam.put("nonceStr", wxPayMpOrderResult.getNonceStr()); returnPayParam.put("package", wxPayMpOrderResult.getPackageValue()); returnPayParam.put("signType", wxPayMpOrderResult.getSignType()); returnPayParam.put("paySign", wxPayMpOrderResult.getPaySign()); return returnPayParam; } return null; } /** * 微信退款接口 * @param refundDto 微信退款所需参数 * @return 结果 */ @Override public Object refund(RefundDto refundDto) { WxPayService wxPayService = null; if (refundDto.getPaymentType().equals("wechat")) { wxPayService = WxPayOwnConfiguration.getWxPayService("wechat"); } else if (refundDto.getPaymentType().equals("miniApp")) { wxPayService = WxPayOwnConfiguration.getWxPayService("miniApp"); } WxPayRefundRequest wxPayRefundRequest = new WxPayRefundRequest(); //商户订单号 wxPayRefundRequest.setOutTradeNo(refundDto.getTradeNumber()); //商户退款单号 wxPayRefundRequest.setOutRefundNo(refundDto.getRefundNumber()); //订单金额 wxPayRefundRequest.setTotalFee(WxPayOwnUtil.yuanToFee(refundDto.getTotalFee())); //退款金额 wxPayRefundRequest.setRefundFee(WxPayOwnUtil.yuanToFee(refundDto.getRefundFee())); //回调地址 wxPayRefundRequest.setNotifyUrl(WxPayOwnConstant.REFUND_CALLBACK_URL); WxPayRefundResult wxPayRefundResult = null; try { wxPayRefundResult = wxPayService.refund(wxPayRefundRequest); } catch (WxPayException e) { System.err.println("退款时发生异常"); e.printStackTrace(); return "fail"; } //退款返回 if (wxPayRefundResult != null) { if ("SUCCESS".equals(wxPayRefundResult.getReturnCode()) && "SUCCESS".equals(wxPayRefundResult.getResultCode())) { return "success"; } } return "fail"; } /** * 微信提现接口 (调用的就是企业付款到用户) * @param withdrawalDto 微信提现所需参数 * @return 结果 */ @Override public Object withdrawal(WithdrawalDto withdrawalDto) { WxPayService wxPayService = null; if (withdrawalDto.getPaymentType().equals("wechat")) { wxPayService = WxPayOwnConfiguration.getWxPayService("wechat"); } else if (withdrawalDto.getPaymentType().equals("miniApp")) { wxPayService = WxPayOwnConfiguration.getWxPayService("miniApp"); } EntPayRequest entPayRequest = new EntPayRequest(); //商户订单号 entPayRequest.setPartnerTradeNo(withdrawalDto.getTradeNumber()); //用户openid entPayRequest.setOpenid(withdrawalDto.getOpenid()); //校验用户姓名选项 //NO_CHECK:不校验真实姓名 //FORCE_CHECK:强校验真实姓名 entPayRequest.setCheckName("NO_CHECK"); //金额 entPayRequest.setAmount(WxPayOwnUtil.yuanToFee(withdrawalDto.getAmount())); //付款备注 entPayRequest.setDescription(withdrawalDto.getDesc()); //Ip地址 entPayRequest.setSpbillCreateIp("127.0.0.1"); EntPayResult entPayResult = null; try { entPayResult = wxPayService.getEntPayService().entPay(entPayRequest); } catch (WxPayException e) { System.err.println("打款时发生异常"); e.printStackTrace(); return "fail"; } if (entPayResult != null) { if ("SUCCESS".equals(entPayResult.getReturnCode()) && "SUCCESS".equals(entPayResult.getResultCode())) { return "success"; } } return "fail"; } }
import java.math.BigDecimal; public class PaymentDto { /** 支付类型 */ private String paymentType; /** 交易描述 */ private String tradeDesc; /** 交易编号 */ private String tradeNumber; /** 支付金额 */ private BigDecimal totalFee; /** 用户openid */ private String openid; public String getPaymentType() { return paymentType; } public void setPaymentType(String paymentType) { this.paymentType = paymentType; } public String getTradeDesc() { return tradeDesc; } public void setTradeDesc(String tradeDesc) { this.tradeDesc = tradeDesc; } public String getTradeNumber() { return tradeNumber; } public void setTradeNumber(String tradeNumber) { this.tradeNumber = tradeNumber; } public BigDecimal getTotalFee() { return totalFee; } public void setTotalFee(BigDecimal totalFee) { this.totalFee = totalFee; } public String getOpenid() { return openid; } public void setOpenid(String openid) { this.openid = openid; } @Override public String toString() { return "PaymentDto{" + "paymentType='" + paymentType + '\'' + ", tradeDesc='" + tradeDesc + '\'' + ", tradeNumber='" + tradeNumber + '\'' + ", totalFee=" + totalFee + ", openid='" + openid + '\'' + '}'; } }
import java.math.BigDecimal; public class RefundDto { /** 支付类型 */ private String paymentType; /** 退款交易编号 */ private String tradeNumber; /** 要退款的交易编号 */ private String refundNumber; /** 要退款的交易总金额 */ private BigDecimal totalFee; /** 退款金额 */ private BigDecimal refundFee; public String getPaymentType() { return paymentType; } public void setPaymentType(String paymentType) { this.paymentType = paymentType; } public String getTradeNumber() { return tradeNumber; } public void setTradeNumber(String tradeNumber) { this.tradeNumber = tradeNumber; } public String getRefundNumber() { return refundNumber; } public void setRefundNumber(String refundNumber) { this.refundNumber = refundNumber; } public BigDecimal getTotalFee() { return totalFee; } public void setTotalFee(BigDecimal totalFee) { this.totalFee = totalFee; } public BigDecimal getRefundFee() { return refundFee; } public void setRefundFee(BigDecimal refundFee) { this.refundFee = refundFee; } @Override public String toString() { return "RefundDto{" + "paymentType='" + paymentType + '\'' + ", tradeNumber='" + tradeNumber + '\'' + ", refundNumber='" + refundNumber + '\'' + ", totalFee=" + totalFee + ", refundFee=" + refundFee + '}'; } }
import java.math.BigDecimal; public class WithdrawalDto { /** 支付类型 */ private String paymentType; /** 企业付款交易单号 */ private String tradeNumber; /** 企业付款用户openid */ private String openid; /** 金额 */ private BigDecimal amount; /** 付款备注 */ private String desc; public String getPaymentType() { return paymentType; } public void setPaymentType(String paymentType) { this.paymentType = paymentType; } public String getTradeNumber() { return tradeNumber; } public void setTradeNumber(String tradeNumber) { this.tradeNumber = tradeNumber; } public String getOpenid() { return openid; } public void setOpenid(String openid) { this.openid = openid; } public BigDecimal getAmount() { return amount; } public void setAmount(BigDecimal amount) { this.amount = amount; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } @Override public String toString() { return "WithdrawalDto{" + "paymentType='" + paymentType + '\'' + ", tradeNumber='" + tradeNumber + '\'' + ", openid='" + openid + '\'' + ", amount=" + amount + ", desc='" + desc + '\'' + '}'; } }
import java.math.BigDecimal; public class WxPayOwnUtil { /** * 1 块钱转为 100 分 * 元转分 * * @param bigDecimal 钱数目 * @return 分 */ public static Integer yuanToFee(BigDecimal bigDecimal) { return bigDecimal.multiply(new BigDecimal(100)).intValue(); } /** * 时间 * * @return 时间戳 */ public static String createTimestamp() { return Long.toString(System.currentTimeMillis() / 1000); } }
七.创建控制器 代码文件如下(可自行修改):
import com.bolong.websocket.websocketdemo.wx.pay.service.WxPayOwnService; import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse; import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult; import com.github.binarywang.wxpay.bean.notify.WxPayRefundNotifyResult; import com.github.binarywang.wxpay.exception.WxPayException; import com.github.binarywang.wxpay.service.WxPayService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * 微信支付请求接口 * @author bolong * @date 2022-03-16 */ @RestController @RequestMapping("/wx/pay") public class WxPayOwnController { @Autowired(required = false) private WxPayService wxPayService; @Autowired private WxPayOwnService wxPayOwnService; /** * 微信支付回调 * @param xmlData 微信服务器请求参数 * @return 响应结果 */ @PostMapping("/paymentCallback") public String paymentCallback(@RequestBody String xmlData) { WxPayOrderNotifyResult wxPayOrderNotifyResult = null; try { wxPayOrderNotifyResult = wxPayService.parseOrderNotifyResult(xmlData); } catch (WxPayException e) { System.err.println("微信支付回调失败"); e.printStackTrace(); return WxPayNotifyResponse.fail(e.getMessage()); } if(wxPayOrderNotifyResult != null) { if ("SUCCESS".equals(wxPayOrderNotifyResult.getReturnCode()) && "SUCCESS".equals(wxPayOrderNotifyResult.getResultCode())) { //TODO 自己的业务逻辑 //交易单号 (订单号) String tradeNumber = wxPayOrderNotifyResult.getOutTradeNo(); return WxPayNotifyResponse.success("支付后调成功!"); } } return WxPayNotifyResponse.fail("支付后调失败!"); } /** * 微信退款回调 * @param xmlData 微信服务器请求参数 * @return 响应结果 */ @PostMapping("/refundCallback") public String refundCallback(@RequestBody String xmlData) { WxPayRefundNotifyResult wxPayRefundNotifyResult = null; try { wxPayRefundNotifyResult = wxPayService.parseRefundNotifyResult(xmlData); } catch (WxPayException e) { System.err.println("微信退款回调失败"); e.printStackTrace(); return WxPayNotifyResponse.fail(e.getMessage()); } if(wxPayRefundNotifyResult != null) { if ("SUCCESS".equals(wxPayRefundNotifyResult.getReturnCode())) { //TODO 自己的业务逻辑 //交易单号 (订单号) String tradeNumber = wxPayRefundNotifyResult.getReqInfo().getOutRefundNo(); return WxPayNotifyResponse.success("退款后调成功!"); } } return WxPayNotifyResponse.fail("退款后调失败!"); } }
希望可以帮助到您 有错误和不足的地方可以评论出来 我也是一个菜鸟 欢迎大神指点!!! 嘻嘻
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。