最近公司需要开发微信商城,然后各种作微信的东西啊,各种看API,头大,这里先记录下来以免后面忘记了~
首先是微信提供的api地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7&index=6
效果图:
还是那句话,第三方的东西做一遍跟做一百遍效果是一样的,直接上代码咯~
=================================老司机开车咯==============================
order.jsp页面的关键代码:
var appId = ""; var timeStamp = ""; var nonceStr = ""; var pg = ""; var signType = ""; var paySign = ""; //开始支付 function paymentOrder(){ var sta = $("#sta").val(); if(sta==0){ var addressId = $("#addressId").val(); if(addressId==''||addressId==null){ alert("请先选择收货地址"); return; } var pm = $("#selPayWays").val(); if(pm==0){ //账户余额 var userAccount = $("#userAccount").val(); var main_price = $(".main-price").html(); if(parseInt(userAccount)<parseInt(main_price)){ alert("账户余额不足,请选择其它的支付方式!"); return false; } $("#form1").submit(); }else if(pm==2){ //微信支付 var oids = '${oids}'; //ajax获取到相关参数 $.ajax({ url: '/***/***/wxpayOrder?addressId='+addressId+"&oids="+oids, type: 'GET', cache:false, async:false, dataType: 'JSON', timeout: 5000, error: function(textStatus){alert('系统错误~');}, success: function(msg){ if(msg!=null){ appId = msg.appId; timeStamp = msg.timeStamp; nonceStr = msg.nonceStr; pg = msg.prepay_id; signType = msg.signType; paySign = msg.sign; } } }); //唤起微信支付 if(appId!=''){ pay(); } } }else{ alert("订单已关闭"); } } //唤起微信支付 function pay(){ if (typeof WeixinJSBridge == "undefined"){ if( document.addEventListener ){ document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false); }else if (document.attachEvent){ document.attachEvent('WeixinJSBridgeReady', onBridgeReady); document.attachEvent('onWeixinJSBridgeReady', onBridgeReady); } }else{ onBridgeReady(); } } //开始支付 function onBridgeReady(){ WeixinJSBridge.invoke( 'getBrandWCPayRequest', { "appId" : appId, //公众号名称,由商户传入 "timeStamp": timeStamp+"", //时间戳,自1970年以来的秒数 "nonceStr" : nonceStr, //随机串 "package" : "prepay_id=" + pg, "signType" : signType, //微信签名方式: "paySign" : paySign //微信签名 }, function(res){ if(res.err_msg == "get_brand_wcpay_request:ok" ) { alert("支付成功"); // 使用以上方式判断前端返回,微信团队郑重提示:res.err_msg将在用户支付成功后返回 ok,但并不保证它绝对可靠。 //回到用户订单列表 window.location.href="http://wx.ooklady.com/wechat/order/orderlist"; }else if (res.err_msg == "get_brand_wcpay_request:cancel") { alert("支付过程中用户取消"); }else{ //支付失败 alert(res.err_msg) } } ); }后台生成预支付id的关键代码:
UnifiedorderResult unifiedorderResult = null; //getSumTotal 微信的价格单位是分(x100),所以intValue 不存在精度丢失的问题 int total_fee = (trans.getTotal().intValue() - userBonus - userYqh)*100; total_fee = 1;//一分钱测试 unifiedorderResult = WxPay.getWxPayUnifiedResult(body, detail, attach,trans.getId()+CommonUtils.getRandByNum(6), total_fee, spbill_create_ip,loginUser.getThirdLoginWXId()); String timestamp = String.valueOf(new Date().getTime()/1000); String nonceStr = unifiedorderResult.getNonce_str(); String prepay_id = unifiedorderResult.getPrepay_id(); jsonObject.put("appId", WxConfig.appid); jsonObject.put("timeStamp", timestamp); jsonObject.put("signType", "MD5"); jsonObject.put("nonceStr", nonceStr); jsonObject.put("package", "prepay_id="+prepay_id); //参数:开始生成签名 SortedMap<Object,Object> parameters = new TreeMap<Object,Object>(); parameters.put("appId", WxConfig.appid); parameters.put("timeStamp", timestamp); parameters.put("signType", "MD5"); parameters.put("nonceStr", nonceStr); parameters.put("package", "prepay_id="+prepay_id); String sign = WXSignUtils.createSign("UTF-8", parameters); jsonObject.put("sign", sign); jsonObject.put("prepay_id", prepay_id); writer.write(jsonObject.toString()); System.out.println("预支付id相关参数是:"+jsonObject.toString()); if(writer!=null){ writer.close(); }total=1;//1分钱测试,这里设置了后所有的商品都是1分钱的东西了,切记上线之前要注释掉!!否则你老板就要叫你去他办公室聊人生,谈理想了~
/** * 统一下单得到prepay_id(预支付id) * @param body * @param detail * @param attach * @param out_trade_no * @param total_fee * @param spbill_create_ip * @return */ public static UnifiedorderResult getWxPayUnifiedResult(String body,String detail,String attach,String out_trade_no,int total_fee,String spbill_create_ip,String openid){ try{ //应用APPID【appid】 String appid = WxConfig.appid; //System.out.println("appid是:"+appid); //微信支付商户号【mch_id】 String mch_id = WxConfig.mch_id; //System.out.println("mch_id是:"+mch_id); //回调地址 String notify_url = WxConfig.notify_url; //随机字符串 String nonce_str = RandCharsUtils.getRandomString(16); //System.out.println("随机字符串是:"+nonce_str); //交易起始时间 String time_start = RandCharsUtils.timeStart(); //System.out.println(time_start); //交易结束时间 String time_expire = RandCharsUtils.timeExpire(); //System.out.println(time_expire); //交易类型 String trade_type = "JSAPI";//JSAPI--公众号支付、NATIVE--原生扫码支付、APP--app支付,统一下单接口trade_type的传参可参考这里 //body = "黑色手拿包"; //参数:开始生成签名 SortedMap<Object,Object> parameters = new TreeMap<Object,Object>(); parameters.put("appid", appid); parameters.put("mch_id", mch_id); parameters.put("nonce_str", nonce_str); //body = Utf8EncodeUtils.getUTF8XMLString(body); //System.out.println("body是:"+body); parameters.put("body", body.toString()); //detail = Utf8EncodeUtils.getUTF8XMLString(detail); //System.out.println("detail是:"+detail); parameters.put("detail", detail.toString()); parameters.put("attach", attach); parameters.put("out_trade_no", out_trade_no); parameters.put("total_fee", total_fee); parameters.put("time_start", time_start); parameters.put("time_expire", time_expire); parameters.put("notify_url", notify_url); parameters.put("trade_type", trade_type); parameters.put("spbill_create_ip", spbill_create_ip); parameters.put("openid", openid); //开始签名 String sign = WXSignUtils.createSign("UTF-8", parameters); System.out.println("签名是:"+sign); log.debug("微信支付签名:"+sign); Unifiedorder unifiedorder = new Unifiedorder(); unifiedorder.setAppid(appid); unifiedorder.setMch_id(mch_id); unifiedorder.setNonce_str(nonce_str); unifiedorder.setSign(sign); unifiedorder.setBody(body); unifiedorder.setDetail(detail); unifiedorder.setAttach(attach); unifiedorder.setOut_trade_no(out_trade_no); unifiedorder.setTotal_fee(total_fee); unifiedorder.setSpbill_create_ip(spbill_create_ip); unifiedorder.setTime_start(time_start); unifiedorder.setTime_expire(time_expire); unifiedorder.setNotify_url(notify_url); unifiedorder.setTrade_type(trade_type); unifiedorder.setOpenid(openid); //构造xml参数 String xmlInfo = HttpXmlUtils.xmlInfo(unifiedorder); System.out.println(xmlInfo); String wxUrl = UnifiedorderUrl; String method = "POST"; String weixinPost = HttpXmlUtils.httpsRequest(wxUrl, method, xmlInfo).toString(); System.out.println(weixinPost); log.debug("微信支付返回:"+weixinPost); return JdomParseXmlUtils.getUnifiedorderResult(weixinPost); }catch(Exception ex){ ex.printStackTrace(); log.debug("微信支付:"+ex.getMessage()); } return null; }
拿到与支付id后再次签名的时候由于没有标准的文档格式,只能靠自己仔细读那陀代码了
额,到此为止,js支付应该没什么问题了,good luck~