本文主要是介绍Java结合微信支付 weixin-java-pay V3接口,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Java整合微信支付
1.起先的打算是根据微信支付的官方文档来一步步的去操作,但是因为时间与效率的问题还是使用了集成的框架,话不多说,直接开干。
1.1 maven依赖
<properties><weixin-java.version>4.3.0</weixin-java.version></properties><dependencies><dependency><groupId>com.github.binarywang</groupId><artifactId>weixin-java-miniapp</artifactId><version>${weixin-java.version}</version></dependency><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.8</version></dependency><dependency><groupId>com.github.binarywang</groupId><artifactId>weixin-java-pay</artifactId><version>${weixin-java.version}</version></dependency></dependencies>
1.2 配置文件 (写不写都行,可以直接写在类中)
properties格式
#微信支付相关参数 wxpay.mch-id= #商户API证书序列号 wxpay.mch-serial-no= #私钥 wxpay.private-key-path=apiclient_key.pem wxpay.key-path=apiclient_cert.p12 wxpay.private-cert-path=apiclient_cert.pem #APIv3密钥 wxpay.api-v3-key= wxpay.appid= #微信服务器地址 wxpay.domain=https://api.mch.weixin.qq.com #接收结果通知地址 wxpay.notify-domain=
yml格式
#微信支付相关参数 wechat:pay:mchid: #商户API证书序列号mch-serial-no: #私钥 证书 private-key-path=apiclient_key.pemkey-path=apiclient_cert.p12private-cert-path=apiclient_cert.pem#APIv3密钥api-v3-key: appid: #微信服务器地址domain: https://api.mch.weixin.qq.com#接收结果通知地址notify-domain:
工具类
import com.github.binarywang.wxpay.config.WxPayConfig; import com.github.binarywang.wxpay.service.WxPayService; import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;public class WxPayUtil {public static WxPayService getWxPayService() {WxPayService payService = new WxPayServiceImpl();WxPayConfig payConfig = new WxPayConfig();payConfig.setAppId("");payConfig.setMchId("");payConfig.setApiV3Key("");payConfig.setKeyPath("apiclient_cert.p12");//p12payConfig.setPrivateKeyPath("apiclient_key.pem");//私钥payConfig.setPrivateCertPath("apiclient_cert.pem");//证书payConfig.setUseSandboxEnv(false);payService.setConfig(payConfig);return payService;} }
1.3下单
//explain:所需参数 Payer、Amount、Description、OutTradeNo、TimeExpire、NotifyUrl。//explain:/*Payer:openid 支付人idAmount:Total:总额 单位分Currency :货币 CNY人民币Description:商品描述OutTradeNo:商品单号TimeExpire:过期时间 NotifyUrl:回调函数 外网可以访问的地址,跳过权限验证payService.createOrderV3(TradeTypeEnum.JSAPI, request);*/public Map payOrder(){Map map = new HashMap<>();WxPayService payService = WxPayUtil.getWxPayService();try {WxPayUnifiedOrderV3Request request = new WxPayUnifiedOrderV3Request();WxPayUnifiedOrderV3Request.Payer payer = new WxPayUnifiedOrderV3Request.Payer();payer.setOpenid();WxPayUnifiedOrderV3Request.Amount amount = new WxPayUnifiedOrderV3Request.Amount();amount.setTotal();amount.setCurrency("CNY");request.setDescription();request.setOutTradeNo();Date nowDate = new Date();Date dateAfter = new Date(nowDate.getTime() + 300000);String format = DateUtil.format(dateAfter, "yyyy-MM-dd'T'HH:mm:ssXXX");request.setTimeExpire(format);request.setNotifyUrl("");request.setAmount();request.setPayer();map.put("data", payService.createOrderV3(TradeTypeEnum.JSAPI, request));return R.data(map);} catch (Exception e) {System.out.println("微信支付失败!订单号:{} " + feeOrder.getOrderNo() + ",原因:{}" + e.getMessage());e.getStackTrace();map.put("data", "支付失败,请稍后重试!");}return R.data(map); }
1.4退款
public Map refund(){Map map = new HashMap<>();try {WxPayService wxPayService = WxPayUtil.getWxPayService();WxPayRefundV3Request request = new WxPayRefundV3Request();com.github.binarywang.wxpay.bean.request.WxPayRefundV3Request.Amount amount = new WxPayRefundV3Request.Amount();//退款单号 自己生成的单号request.setOutTradeNo();//使用内部单号request.setOutRefundNo();amount.setCurrency("CNY");amount.setRefund();amount.setTotal();request.setAmount(amount);//退款原因request.setReason();map.put("data", wxPayService.refundV3(request));return map;} catch (WxPayException e) {e.getMessage();map.put("data", "退款失败,请稍后重试!");}return map; }
1.5微信支付回调
@ApiOperation(value = "微信支付回调")@PostMapping("notify")public R notify() {WxPayService payService = WxPayUtil.getWxPayService();HttpServletRequest request = WebUtil.getRequest();SignatureHeader header = new SignatureHeader();header.setTimeStamp(request.getHeader("Wechatpay-Timestamp"));header.setNonce(request.getHeader("Wechatpay-Nonce"));header.setSignature(request.getHeader("Wechatpay-Signature"));header.setSerial(request.getHeader("Wechatpay-Serial"));Map map = new HashMap();try {String requestBody = WebUtil.getRequestBody(request.getInputStream());map.put("data", payService.parseOrderNotifyV3Result(requestBody, header));return R.data(map);} catch (Exception e) {e.printStackTrace();}return R.data(map);}
这篇关于Java结合微信支付 weixin-java-pay V3接口的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!