imLWY 通过本文主要向大家介绍了java接口详解,java集合类详解,java io详解,java反射机制详解,java泛型详解等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
一、微信APP支付接入商户服务中心
[申请流程指引] (https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419317780&token=84f23b4e9746c5963128711f225476cfd49ccf8c&lang=zh_CN)
二、开始开发
1、配置相关的配置信息
1.1、配置appid(Android)、mch_id(ios)、微信支付后的回调地址
sys.properties配置文件: appid=wx***************1 mch_id=1********2 notify_url=http://6*.***.***.**/returnmsg.do //回调通知的地址,一定是要可以直接访问的地址</div>
2、微信支付–下单
@ResponseBody
@RequestMapping(value = "/weixinpay.do", produces = "text/html;charset=UTF-8",method={RequestMethod.POST})
public static String weixinpay(String body, //商品描述
String detail, //商品详情
String attach, //附加数据,在查询API和支付通知中原样返回,该字段主要用于商户携带订单的自定义数据
String out_trade_no, //商户系统内部的订单号,32个字符内、可包含字母, 其他说明见商户订单号
String total_price, //订单总金额,单位为分,详见支付金额
String spbill_create_ip //用户端实际ip
) throws Exception {
WeixinConfigUtils config = new WeixinConfigUtils();
//参数组
String appid = config.appid;//微信开放平台审核通过的应用APPID
System.out.println("appid是:"+appid);
String mch_id = config.mch_id;
System.out.println("mch_id是:"+mch_id);
String nonce_str = RandCharsUtils.getRandomString(16);
System.out.println("随机字符串是:"+nonce_str);
body = body;//"测试微信支付0.01_2";
detail = detail;//"0.01_元测试开始";
//attach = attach;//"备用参数,先留着,后面会有用的";
//String out_trade_no = OrderUtil.getOrderNo();//"2015112500001000811017342394";
double totalfee =0;
try{
totalfee=Double.parseDouble(total_price);////单位是分,即是0.01元
}catch (Exception e) {
totalfee=0;
}
int total_fee=(int) (totalfee*100);
spbill_create_ip = spbill_create_ip;//"127.0.0.1";
String time_start = RandCharsUtils.timeStart();
System.out.println(time_start);
String time_expire = RandCharsUtils.timeExpire();
System.out.println(time_expire);
String notify_url = config.notify_url;
System.out.println("notify_url是:"+notify_url);
String trade_type = "APP";
//参数:开始生成签名
SortedMap<Object,Object> parameters = new TreeMap<Object,Object>();
parameters.put("appid", appid);
parameters.put("mch_id", mch_id);
parameters.put("nonce_str", nonce_str);
parameters.put("body", body);
//parameters.put("nonce_str", nonce_str);
parameters.put("detail", detail);
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);
String sign = WXSignUtils.createSign("UTF-8", parameters);
System.out.println("签名是:"+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);
System.out.println(MD5Utils.md5("fenxiangzhuyi") + "========================");
//构造xml参数
String xmlInfo = HttpXmlUtils.xmlInfo(unifiedorder);
String wxUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder";
String method = "POST";
String weixinPost = HttpXmlUtils.httpsRequest(wxUrl, method, xmlInfo).toString();
System.out.println(weixinPost);
ParseXMLUtils.jdomParseXml(weixinPost);
String json = JsonUtil.xml2jsonString(weixinPost);
System.out.println("=========================================================");
Bean b = JsonUtil.getSingleBean(json, Bean.class);
if(null!=b){
WeixinOrder weixin = b.getXml();
//参数:开始生成签名
SortedMap<Object,Object> par = new TreeMap<Object,Object>();
par.put("appid", weixin.getAppid());
par.put("partnerid", weixin.getMch_id());
par.put("prepayid", weixin.getPrepay_id());
par.put("package", "Sign=WXPay");
par.put("noncestr", weixin.getNonce_str());
//时间戳
Date date = new Date();
long time = date.getTime();
//mysq 时间戳只有10位 要做处理
String dateline = time + "";
dateline = dateline.substring(0, 10);
par.put("timestamp", dateline);
String signnew = WXSignUtils.createSign("UTF-8", par);
System.out.println("再次签名是:"+signnew);
SetPay setPay = new SetPay();
setPay.setAppid(weixin.getAppid());
setPay.setPartnerid(weixin.getMch_id());
setPay.setPrepayid(weixin.getPrepay_id());
setPay.setNoncestr(weixin.getNonce_str());
setPay.setTimestamp(dateline);
setPay.setSign(signnew);
setPay.setPack("Sign=WXPay");
JSONObject js = JSONObject.fromObject(setPay);
StringBuilder msg = new StringBuilder();
msg.append("{\"code\":\"1\",");
msg.append("\"msg\":\"查询成功!\",");
msg.append("\"datas\":");
msg.append(js.toString());
msg.append("}");
System.out.println(js);
return msg.toString();
}
StringBuilder msg = new StringBuilder();
msg.append("{\"code\":\"1\",");
msg.append("\"msg\":\"查询成功!\",");
msg.append("\"datas\":");
msg.append("支付失败!");
msg.append("}");
return msg.toString();
}
</div>
2.1、微信支付签名算法sign
package com.wx.weixincontroller.pay.weixin.Utils;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import com.wx.weixin.utils.MD5Utils;
/**
* 微信支付签名
* @author iYjrg_xiebin
* @date 2016年10月25日下午4:47:07
*/
public class WXSignUtils {
//http://mch.weixin.qq.com/wiki/doc/api/index.php?chapter=4_3
//商户Key:改成公司申请的即可
//32位密码设置地址:http://www.sexauth.com/ jdex1hvufnm1sdcb0e81t36k0d0f15nc
private static String Key = "***cb**e**ef**c*e*d***e*fd***cb*";
/**
* 微信支付签名算法sign
* @param characterEncoding
* @param parameters
* @return
*/
@SuppressWarnings("rawtypes")
public static String createSign(String characterEncoding,SortedMap<Object,Object> parameters){
StringBuffer sb = new StringBuffer();
Set es = parameters.entrySet();//所有参与传参的参数按照accsii排序(升序)
Iterator it = es.iterator();
while(it.hasNext()) {
Map.Entry entry = (Map.Entry)it.next();
String k = (String)entry.getKey();
Object v = entry.getValue();
if(null != v && !"".equals(v)
&& !"sign".equals(k) && !"key".equals(k)) {
sb.appe

