微信红包java接入代码 java微信红包实现( 三 )


e.printStackTrace();
}
// 增加非参与签名的额外参数
signParams.put("sign_method", "sha1");
signParams.put("app_signature", sign);
// api支付拼包结束------------------------------------
//获取prepayid
String prepayid = requestHandler.sendPrepay(signParams);
System.out.println("prepayid :" + prepayid);
// --------------------------------生成完成---------------------------------------------
//生成预付快订单完成 , 返回给android,ios 掉起微信所需要的参数 。
SortedMapString, String payParams = new TreeMapString, String();
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_url
ResponseHandler 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");

推荐阅读