码迷,mamicode.com
首页 > 微信 > 详细

获取微信支付所需签名等

时间:2017-07-27 10:45:51      阅读:367      评论:0      收藏:0      [点我收藏+]

标签:parse   tar   机器   puts   input   exce   http   cat   money   

@RequestMapping(value = "/toPay", method = RequestMethod.POST)
@ResponseBody
public String getShowCode(@RequestParam(value = "openid", required = false) String openid,
@RequestParam(value = "shouprices", required = false) String total_fee,
@RequestParam(value = "xiaid", required = false) String xiaId,
@RequestParam(value = "recharge", required = false) String recharge,
@RequestParam(value = "kuaikeId", required = false) String kuaikeId, HttpServletRequest request,
ModelMap model) throws IOException {

// 网页授权后获取传递的参数
String orderNo = System.currentTimeMillis() + getRandomString(5);
// String money = "0.01";
// 金额转化为分为单位
// float sessionmoney = Float.parseFloat(money);
// String finalmoney = String.format("%.2f", sessionmoney);
// finalmoney = finalmoney.replace(".", "");

// 商户相关资料
Properties p = new Properties();
InputStream input = R_apppayController.class.getResourceAsStream("/payConfig.properties");
p.load(input);
String appid = p.get("APP_ID").toString();
String appsecret = p.get("APPSECRET").toString();
String mch_id = p.get("MCH_ID").toString();// 邮件里的MCHID
String partnerkey = p.get("API_KEY").toString();// 在微信商户平台pay.weixin.com里自己生成的那个key
String spbill_create_ip = p.get("CREATE_IP").toString();
double fee = Double.parseDouble(total_fee) * 100;
int a = (int) fee;
String total = String.valueOf(a);

// String openId = "oX0yLxLnmsLtK2POMWioeBSRFBFc";//用oath授权得到的openid

// 获取openId后调用统一支付接口https://api.mch.weixin.qq.com/pay/unifiedorder
String currTime = TenpayUtil.getCurrTime();
// 8位日期
String strTime = currTime.substring(8, currTime.length());
// 四位随机数
String strRandom = TenpayUtil.buildRandom(4) + "";
// 10位序列号,可以自行调整。
String strReq = strTime + strRandom;

// 子商户号 非必输
// String sub_mch_id="";
// 设备号 非必输
String device_info = "";
// 随机数
String nonce_str = strReq;
// 商品描述
// String body = describe;

// 商品描述根据情况修改
String body = "支付";
// 附加数据
// String attach = userId;
// 商户订单号
String out_trade_no = orderNo;
// int intMoney = Integer.parseInt(finalmoney);

// 总金额以分为单位,不带小数点
// int total_fee = intMoney;
// 订单生成的机器 IP

// 订 单 生 成 时 间 非必输
// String time_start ="";
// 订单失效时间 非必输
// String time_expire = "";
// 商品标记 非必输
// String goods_tag = "";


String attach = "";

// 这里notify_url是 支付完成后微信发给该链接信息,可以判断会员是否支付成功,改变订单状态等。
String notify_url = p.get("RECHARGE_URL").toString();

String trade_type = "JSAPI";//微信支付
// 非必输
// String product_id = "";
SortedMap<String, String> packageParams = new TreeMap<String, String>();
packageParams.put("appid", appid);
packageParams.put("mch_id", mch_id);
packageParams.put("nonce_str", nonce_str);
packageParams.put("body", body);
// packageParams.put("attach", attach);
packageParams.put("out_trade_no", out_trade_no);
packageParams.put("attach", attach);
packageParams.put("total_fee", total);
// packageParams.put("total_fee", "finalmoney");
packageParams.put("spbill_create_ip", spbill_create_ip);
packageParams.put("notify_url", notify_url);

packageParams.put("trade_type", trade_type);
packageParams.put("openid", openid);

RequestHandler reqHandler = new RequestHandler(null, null);
reqHandler.init(appid, appsecret, partnerkey);

String sign = reqHandler.createSign(packageParams);
String xml = "<xml>" + "<appid>" + appid + "</appid>" + "<mch_id>" + mch_id + "</mch_id>" + "<nonce_str>"
+ nonce_str + "</nonce_str>" + "<sign><![CDATA[" + sign + "]]></sign>" + "<body><![CDATA[" + body
+ "]]></body>" + "<out_trade_no>" + out_trade_no + "</out_trade_no>" + "<total_fee>" + total
+ "</total_fee>" + "<attach>" + attach + "</attach>" + "<spbill_create_ip>" + spbill_create_ip
+ "</spbill_create_ip>" + "<notify_url>" + notify_url + "</notify_url>" + "<trade_type>" + trade_type
+ "</trade_type>" + "<openid>" + openid + "</openid>" + "</xml>";
System.out.println(xml);
String createOrderURL = p.get("UFDODER_URL").toString();
String prepay_id = "";
try {
prepay_id = new GetWxOrderno().getPayNo(createOrderURL, xml);
if (prepay_id.equals("")) {
System.out.println("统一支付接口获取预支付订单出错");
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
SortedMap<String, String> finalpackage = new TreeMap<String, String>();
String appid2 = appid;
String timestamp = Sha1Util.getTimeStamp();
String nonceStr2 = nonce_str;
String packages = "prepay_id=" + prepay_id;
finalpackage.put("appId", appid2);
finalpackage.put("timeStamp", timestamp);
finalpackage.put("nonceStr", nonceStr2);
finalpackage.put("package", packages);
finalpackage.put("signType", "MD5");
String finalsign = reqHandler.createSign(finalpackage);
Date date = new Date();
String product_id = getRandomString(5) + System.currentTimeMillis();
String order_no = System.currentTimeMillis() + getRandomString(3);

// return total;
return "\‘timestamp‘:\"" + timestamp // 这里的也是小写~~
+ "\",‘nonceStr‘:\"" + nonceStr2 + "\",‘paySign‘:\"" + finalsign + "\",‘package‘:\"" + packages + "\"";

}

获取微信支付所需签名等

标签:parse   tar   机器   puts   input   exce   http   cat   money   

原文地址:http://www.cnblogs.com/tianhao2017/p/7243231.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!