1.1 小程序支付说明
1.2 小程序轻量级支付模板(引用码云作者egan)引入jar包
com.egzosn pay-java-common 2.12.1com.egzosn pay-java-wx 2.12.1
1.3 初始化微信支付配置存储类
private WxPayService wxUserPayService;/** * 微信支付必要信息初始化 * @param appId */ private void wxUserPayServiceInit(String appId) { if (wxUserPayService == null) { WxPayConfigStorage wxPayConfigStorage = new WxPayConfigStorage(); wxPayConfigStorage.setMchId(mchId);//支付商户号 wxPayConfigStorage.setAppid(appId);//小程序appid // wxPayConfigStorage.setKeyPublic("转账公钥,转账时必填"); wxPayConfigStorage.setSecretKey(appKey);// 商户支付密钥 wxPayConfigStorage.setNotifyUrl(domain + "/wx/userpaycallback");//支付结果回调接口地址,必须外网可访问的完整url地址 wxPayConfigStorage.setSignType("MD5"); wxPayConfigStorage.setInputCharset("utf-8"); // https证书设置,退款必须 方式一 /*HttpConfigStorage httpConfigStorage = new HttpConfigStorage(); httpConfigStorage.setKeystorePath(keystorePath);//apiclient_cert.p12证书存放路径,填写绝对路径 httpConfigStorage.setStorePassword(mchId); httpConfigStorage.setPath(true); httpConfigStorage.setMaxTotal(20); httpConfigStorage.setDefaultMaxPerRoute(10); wxUserPayService = new WxPayService(wxPayConfigStorage, httpConfigStorage);*/ wxUserPayService = new WxPayService(wxPayConfigStorage); } }
1.4 付款前生成微信预支付交易单,返回正确的预支付交易参数信息,供给微信小程序前端开发者调用付款API
String title = "开通会员";//支付下单标题 double total_fee = vipConfigInfoService.getInfo().getPrice();//实际支付金额 // 测试默认都是// if (settingActive.equals("test")) {// total_fee = 0.01;// } // 添加支付记录信息,生成商户订单号,这里用支付记录id String out_trade_no = payService.representVip(type, appId, openId, userId, total_fee, title); wxUserPayServiceInit(appId);//初始化微信支付配置存储类 PayOrder order = new PayOrder(title, type, new BigDecimal(total_fee), out_trade_no); order.setTransactionType(WxTransactionType.JSAPI); order.setOpenid(openId); order.setSpbillCreateIp(spbill_create_ip);//终端ip,调用微信支付API的机器IP order.setAddition(type); JSONObject result = wxUserPayService.unifiedOrder(order);//微信统一下单接口,此方法在引入的jar中,直接传入必填的信息调用即可 System.out.println("请求微信支付统一下单结果:" + result.toString()); String timeStamp = System.currentTimeMillis() / 1000 + ""; JSONObject json = new JSONObject(); json.put("nonceStr