小程序微信支付完整demo,包含退款

最近刚完成一个商场小程序(http://market.zhenzikj.com/detail/121.html), 使用到了微信支付功能,其中遇到了很多的抗,所以,我把支付这块摘出来,以免大家少走弯路。

demo小程序端很简单,就是一个页面:

技术图片

 

js代码:

//支付 pay: function(e){ var that = this; if(that.data.number == ‘‘){ wx.showToast({ title: ‘请填写支付金额!‘, icon: ‘none‘, duration: 2000 }) return; } util.getOpenid(function(openid){ wx.request({ url: app.globalData.baseUrl + ‘/pay/pay.html‘, header: { ‘content-type‘: ‘application/x-www-form-urlencoded‘ }, method: ‘POST‘, data: { totalFee: that.data.number, openid: openid }, success (res) { var data = res.data.data; wx.requestPayment( { ‘timeStamp‘: data.timeStamp, ‘nonceStr‘: data.nonce_str, ‘package‘: ‘prepay_id=‘+data.prepay_id, ‘signType‘: ‘MD5‘, ‘paySign‘: data.sign, ‘success‘:function(res){ wx.showToast({ title: ‘支付成功‘, icon: ‘none‘, duration: 2000 }) }, ‘fail‘:function(res){ wx.showToast({ title: ‘支付失败‘, icon: ‘none‘, duration: 2000 }) } }) } }) });

  

java代码:

统一下单接口

/** * 支付(api) */ @RequestMapping(value="/pay") @ResponseBody public ResultInfo<Object> pay( HttpServletRequest request, double totalFee, String openid){ try { final String appId = CustomPropertyConfigurer.getProperty("weixin.mini.appid"); final String key = CustomPropertyConfigurer.getProperty("weixin.mini.key"); final String mch_id = CustomPropertyConfigurer.getProperty("weixin.mini.mch_id"); final String notify_url = CustomPropertyConfigurer.getProperty("weixin.mini.notify_url"); final String ip = CustomPropertyConfigurer.getProperty("ip"); //out_trade_no是自定义的系统内部订单号 String out_trade_no = new DateTime().toString("yyyyMMddHHmmss") + String.valueOf((int)((Math.random()*9+1)*1000)); Order order = new Order(); order.setAppid(appId); order.setMch_id(mch_id); order.setNonce_str(RandomStringGenerator.getRandomStringByLength(32)); order.setBody("下单-支付"); order.setOut_trade_no(out_trade_no); order.setTotal_fee((int)(totalFee * 100));//支付金额(分) order.setSpbill_create_ip(ip); order.setNotify_url(notify_url); order.setTrade_type("JSAPI"); order.setSign_type("MD5"); order.setOpenid(openid); //生成签名 String sign = Signature.getSign(order, key); order.setSign(sign); String result = HttpRequest.sendPost("https://api.mch.weixin.qq.com/pay/unifiedorder", order, false, null, null); System.out.println(result); L.info("---------下单返回:"+result); XStream xStream = new XStream(); xStream.ignoreUnknownElements(); xStream.alias("xml", OrderReturnInfo.class); OrderReturnInfo returnInfo = (OrderReturnInfo)xStream.fromXML(result); if(returnInfo.getResult_code().equals("SUCCESS") && returnInfo.getReturn_code().equals("SUCCESS")){ //下单成功,此处是你的业务代码 System.out.print("下单成功"); } //生成签名 final String timeStamp = String.valueOf(System.currentTimeMillis()/1000); sign = "appId="+appId+"&nonceStr="+order.getNonce_str()+"&package=prepay_id="+returnInfo.getPrepay_id()+"&signType=MD5&timeStamp="+timeStamp+"&key="+key; sign = MD5.MD5Encode(sign).toUpperCase(); JSONObject jsonObject = new JSONObject(); jsonObject.put("nonce_str", order.getNonce_str()); jsonObject.put("prepay_id", returnInfo.getPrepay_id()); jsonObject.put("sign", sign); jsonObject.put("timeStamp", timeStamp); return new ResultInfo<Object>(0, jsonObject); } catch (Exception e) { e.printStackTrace(); L.error("-------------", e); } return new ResultInfo<Object>(1000, "未知错误"); }

  

申请退款接口

 

/** * 申请退款 */ @RequestMapping(value="/apply") @ResponseBody public Object apply( HttpServletRequest request, double refundFee, String out_trade_no){ try { final String appId = CustomPropertyConfigurer.getProperty("weixin.mini.appid"); final String mch_id = CustomPropertyConfigurer.getProperty("weixin.mini.mch_id"); final String refund_notify_url = CustomPropertyConfigurer.getProperty("weixin.mini.refund_notify_url"); final String key = CustomPropertyConfigurer.getProperty("weixin.mini.key"); WxRefund wxRefund = new WxRefund(); wxRefund.setAppid(appId); wxRefund.setMch_id(mch_id); wxRefund.setNonce_str(RandomStringGenerator.getRandomStringByLength(32)); wxRefund.setOut_trade_no(out_trade_no); wxRefund.setOut_refund_no(out_trade_no); wxRefund.setRefund_fee((int)(refundFee*100)); wxRefund.setTotal_fee((int)(refundFee*100)); wxRefund.setNotify_url(refund_notify_url); //生成签名 String sign = Signature.getSign(wxRefund, key); wxRefund.setSign(sign); //获取证书 String certFile = request.getServletContext().getRealPath("/") + "cert/apiclient_cert.p12"; String result = HttpRequest.sendPost("https://api.mch.weixin.qq.com/secapi/pay/refund", wxRefund, true, mch_id, certFile); L.info("---------退款返回:"+result); System.out.println(result); XStream xStream = new XStream(); xStream.ignoreUnknownElements(); xStream.alias("xml", RefundReturnInfo.class); RefundReturnInfo returnInfo = (RefundReturnInfo)xStream.fromXML(result); if(returnInfo.getResult_code().equals("SUCCESS") && returnInfo.getReturn_code().equals("SUCCESS")){ return true; } return new ResultInfo<Object>(0, null); } catch (Exception e) { e.printStackTrace(); L.error("------------------------", e); } return new ResultInfo<Object>(1000, "未知错误"); }

  

完整代码下载: http://market.zhenzikj.com/detail/123.html

 

相关文章