//操蛋的微信demo 和文档不对应无法跑起来,文档写的就是一拖狗屎。不想喷了,直接看代码。
RequestHandler requestHandler = new RequestHandler(super.getRequest(), super.getResponse());
//获取token //两小时内有效,两小时后重新获取
Token =requestHandler.GetToken();//更新token 到应用中requestHandler.getTokenReal();System.out.println("微信支付获取token=======================:"+Token);//requestHandler 初始化requestHandler.init();requestHandler.init(appid,appsecret,appkey,partnerkey,key);
// --------------------------------本地系统生成订单-------------------------------------
// 设置package订单参数
SortedMappackageParams = new TreeMap (); packageParams.put("bank_type","WX"); // 支付类型 packageParams.put("body","xxxx"); // 商品描述 packageParams.put("fee_type","1"); // 银行币种 packageParams.put("input_charset","UTF-8"); // 字符集 packageParams.put("notify_url","http://xxxx.com/xxxx/wxcallback"); // 通知地址 这里的通知地址使用外网地址测试,注意80端口是否打开。 packageParams.put("out_trade_no",no); // 商户订单号 packageParams.put("partner",partenerid); // 设置商户号 packageParams.put("spbill_create_ip",super.getRequest().getRemoteHost()); // 订单生成的机器IP,指用户浏览器端IP packageParams.put("total_fee",String.valueOf(rstotal)); // 商品总金额,以分为单位
// 设置支付参数
SortedMapsignParams = new TreeMap (); signParams.put("appid",appid); signParams.put("noncestr",noncestr); signParams.put("traceid",PropertiesUtils.getOrderNO()); signParams.put("timestamp",timestamp); signParams.put("package",packageValue); signParams.put("appkey",this.appkey);// 生成支付签名,要采用URLENCODER的原始值进行SHA1算法!String sign = ""; try{ sign = Sha1Util.createSHA1Sign(signParams);} catch(Exception e){ e.printStackTrace();}// 增加非参与签名的额外参数signParams.put("sign_method","sha1"); signParams.put("app_signature",sign);// api支付拼包结束------------------------------------//获取prepayidString prepayid = requestHandler.sendPrepay(signParams); System.out.println("prepayid :"+prepayid);
// --------------------------------生成完成---------------------------------------------
//生成预付快订单完成,返回给android,ios 掉起微信所需要的参数。SortedMappayParams = new TreeMap (); payParams.put("appid",appid); payParams.put("noncestr",noncestr); payParams.put("package","Sign=WXPay"); payParams.put("partnerid",partenerid); payParams.put("prepayid",prepayid); payParams.put("appkey",this.appkey);//这里除1000 是因为参数长度限制。int time = (int) (System.currentTimeMillis() / 1000); payParams.put("timestamp",String.valueOf(time)); System.out.println("timestamp:"+time);//签名String paysign = "";try{ paysign = Sha1Util.createSHA1Sign(payParams);} catch(Exception e){ e.printStackTrace();} payParams.put("sign",paysign);//拼json 数据返回给客户端BasicDBObject backObject = new BasicDBObject(); backObject.put("appid",appid); backObject.put("noncestr",payParams.get("noncestr")); backObject.put("package","Sign=WXPay"); backObject.put("partnerid",payParams.get("partnerid")); backObject.put("prepayid",payParams.get("prepayid")); backObject.put("appkey",this.appkey); backObject.put("timestamp",payParams.get("timestamp")); backObject.put("sign",payParams.get("sign")); String backstr = dataObject.toString(); System.out.println("backstr:"+backstr);return backstr;
====================到此为止,预付款订单已生成,并且已返回客户端====================
//坐等微信服务器通知,通知的地址就是生成预付款订单的notify_urlResponseHandler resHandler = new ResponseHandler(request, response);resHandler.setKey(partnerkey);//创建请求对象//RequestHandler queryReq = new RequestHandler(request, response);//queryReq.init();if(resHandler.isTenpaySign()==true){ //商户订单号 String out_trade_no = resHandler.getParameter("out_trade_no"); System.out.println("out_trade_no:" + out_trade_no); //财付通订单号 String transaction_id = resHandler.getParameter("transaction_id"); System.out.println("transaction_id:" + transaction_id); //金额,以分为单位 String total_fee = resHandler.getParameter("total_fee"); //如果有使用折扣券,discount有值,total_fee+discount=原请求的total_fee String discount = resHandler.getParameter("discount"); //支付结果 String trade_state = resHandler.getParameter("trade_state"); //判断签名及结果 if ("0".equals(trade_state)) { //------------------------------ //即时到账处理业务开始 //------------------------------ System.out.println("----------------业务逻辑执行-----------------"); //——请根据您的业务逻辑来编写程序(以上代码仅作参考)—— System.out.println("----------------业务逻辑执行完毕-----------------"); System.out.println("success"); // 请不要修改或删除 System.out.println("即时到账支付成功"); //给财付通系统发送成功信息,财付通系统收到此结果后不再进行后续通知 resHandler.sendToCFT("success"); //给微信服务器返回success 否则30分钟通知8次 return "success"; } else { System.out.println("通知签名验证失败"); resHandler.sendToCFT("fail"); response.setCharacterEncoding("utf-8"); }}else{ System.out.println("fail -Md5 failed");}