微信支付(APP内支付)

2017-12-21  本文已影响339人  白鲁宁

支付开通就不多说了。直接上代码过程 微信是以分为单位

注意:微信的key要严格按照他的来 谨记

1.先拼装请求参数

SortedMap parameterMap =newTreeMap<>();

parameterMap.put("appid", wx123456789);//应用APPID

parameterMap.put("mch_id", 148123456);//商户号

parameterMap.put("nonce_str",uuid());//随机字符串

parameterMap.put("body",”充值“);//商品名称

parameterMap.put("out_trade_no","CZ123456789");//订单号

parameterMap.put("total_fee",newBigDecimal("100").movePointRight(2));//100元订单金额以分为单位就是1元

parameterMap.put("spbill_create_ip", request.getRemoteAddr());//客户端本地ip

parameterMap.put("notify_url", localcallback.hhl.com);//支付回调地址

parameterMap.put("trade_type","APP");//如果是app支付就是app.公众号支付是JSAPI

String sign =WxpayMD5Util.createSign(parameterMap);// MD5进行签名,必须为UTF-8编码

parameterMap.put("sign", sign);

String requestXML = WxpayXMLUtil.getRequestXml(parameterMap);//生成xml结构的数据,用于统一下单接口的请求

logger.info("requestXML生成XML报文数据:");

logger.info(requestXML);

HttpClient client =newHttpClient();

PostMethod myPost =newPostMethod(WxpayConfigUtil.getWxpayUniurl());//开始请求统一下单接口,获取预支付prepay_id

client.getParams().setSoTimeout(300*1000);

String result = StringUtils.EMPTY;

try{

myPost.setRequestEntity(newStringRequestEntity(requestXML,"text/xml","utf-8"));

intstatusCode = client.executeMethod(myPost);

if(statusCode == HttpStatus.SC_OK) {

BufferedInputStream bis =newBufferedInputStream(myPost.getResponseBodyAsStream());

byte[] bytes =new byte[1024];

ByteArrayOutputStream bos =newByteArrayOutputStream();

intcount;

while((count = bis.read(bytes)) != -1) {

bos.write(bytes,0, count);

}

byte[] strByte = bos.toByteArray();

result =newString(strByte,0, strByte.length,"utf-8");

bos.close();

bis.close();

}

}catch(Exception e) {

e.printStackTrace();

}

/**需要释放掉、关闭连接*/

myPost.releaseConnection();

client.getHttpConnectionManager().closeIdleConnections(0);

logger.info("请求统一支付接口的返回XML报文结果:");

logger.info(result);

result成功后的结果如下:

转换成map后

0 = {HashMap$Node@10582} "nonce_str" -> "rwXiKn26tXzNeHt7"

1 = {HashMap$Node@10583} "appid" -> "wx123456789"

2 = {HashMap$Node@10584} "sign" -> "FQQQQQQAAAAACCCCCCDDDDDD"

3 = {HashMap$Node@10585} "trade_type" -> "APP"

4 = {HashMap$Node@10586} "return_msg" -> "OK"

5 = {HashMap$Node@10587} "result_code" -> "SUCCESS"

6 = {HashMap$Node@10588} "mch_id" -> "148123456"

7 = {HashMap$Node@10589} "return_code" -> "SUCCESS"

8 = {HashMap$Node@10590} "prepay_id" -> "wx201712201234567890123445"

try{

/**解析微信返回的信息,以Map形式存储便于取值*/

Map map = WxpayXMLUtil.doXMLParse(result);

SortedMap params =newTreeMap<>();

params.put("appid", WxpayConfigUtil.getWxpayAppId());

params.put("partnerid", WxpayConfigUtil.getWxpayMchId());//微信商户账号

params.put("prepayid", map.get("prepay_id"));//预付订单id

params.put("package","Sign=WXPay");//默认sign=WXPay

params.put("noncestr", IdGen.uuid());//自定义不重复的长度不长于32位

params.put("timestamp",newDate().getTime());

String paySign = WxpayMD5Util.createSign(params);

params.put("paySign", paySign);

String json = JSONArray.fromObject(params).toString();

}catch(JDOMException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}

-----------------------

以上就是微信支付交互步骤了。直接 return json就可以了。

注意:微信的key要严格按照他的来 谨记

这是交互中用到的工具类

WxpayMD5Util    WxpayXMLUtil

支付后的回调,明天更新

文档参考链接如下

https://www.cnblogs.com/xu-xiang/p/5797575.html

上一篇下一篇

猜你喜欢

热点阅读