赞
踩
1.起先的打算是根据微信支付的官方文档来一步步的去操作,但是因为时间与效率的问题还是使用了集成的框架,话不多说,直接开干。
<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>
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.pem key-path=apiclient_cert.p12 private-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");//p12 payConfig.setPrivateKeyPath("apiclient_key.pem");//私钥 payConfig.setPrivateCertPath("apiclient_cert.pem");//证书 payConfig.setUseSandboxEnv(false); payService.setConfig(payConfig); return payService; } }
//explain:所需参数 Payer、Amount、Description、OutTradeNo、TimeExpire、NotifyUrl。 //explain: /* Payer:openid 支付人id Amount: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); }
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; }
@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); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。