您的位置:首页 > 移动开发 > 微信开发

微信支付之H5页面WAP端接入

2015-10-10 13:09 706 查看

更新:2015年12月3日微信提供 Wap 支付, 开发者文档:【微信支付】开发者文档

1.前言

公司是通过支付宝和微信支付那块内容获取收入,app端已经接入成功,现在要做WAP端。需要页面和后台接口一起来实现。

2.接口接入

因为微信支付版本更新了,网上下的demo是V2.5版的,用不了了。所以去网上找资料,看到最新版的V3。

这里我找到了一个统一下单接口,文档入口.

他的接口地址为:https://api.mch.weixin.qq.com/pay/unifiedorder

因此,开始接入我所需要的wap端参数。

这里需要的参数关键有Appid,mch_id,key。

appid和mch_id是在公众平台那边获取。key值是在商户平台(pay.weixin.qq.com)-->账户设置-->API安全-->密钥设置 这边自己设置的。

坑一:若key值设置不对,会出现错误“支付权限查询失败” 。这时候请检查 appid,mch_id所在的公众号 对应 商户号的key值是否正确。

坑二:我在开发中还遇到“您没有WAP支付权限” 这么个错误。百了很久都没人遇到这个坑。于是,发送邮件给微信支付(weixinpay@tencent.com, wepayTS@tencent.com)这两个邮件我都发了,结果还是漫无回应啊。于是,打通了商户平台的客服(0755-86018333),客服是MM,估计不懂技术问题,叫我去提问平台提交问题(http://kf.qq.com//bills/150821samab01c976f2a.html),说是技术人员看到会回复的,我问是不是 马上回复,MM不说,就说会回复的,唉,毕竟人家客服不懂,就没继续问下去了。打开客服给的网址,填写的时候,发现没有WAP端,也没有统一下单这说法,那我只好填写了 网页(JSAPI)支付
,下面在详细说明,提交后,出现了个提示,说是七天内给个回应。我去,那还不是白忙活,要7天 业务紧急啊。。

3.代码编写

(1).获取统一下单参数

public String CreateWapUrl(String outTradeNo, String ip) throws SDKRuntimeException {
HashMap<String, Object> param = new HashMap<String, Object>();
param.put("appid", WxPayConfig.APPID);
param.put("mch_id", WxPayConfig.MCHID);
param.put("nonce_str", CommonUtil.CreateNoncestr());
param.put("body", "产品测试");
param.put("out_trade_no", outTradeNo);
param.put("total_fee", 1);
param.put("spbill_create_ip", ip);
param.put("notify_url", WxPayConfig.NOTIFYURL);
param.put("trade_type", "WAP");
param.put("sign", getSign(param));
return CommonUtil.MapToXml(param);
}

(2).获取签名值

public String getSign(HashMap<String, Object> param) throws SDKRuntimeException {
String sign="";
String content = CommonUtil.FormatParamMap(param);
sign =  Sign(content, WxPayConfig.KEY);
return sign;
}

public static String Sign(String content, String key) throws SDKRuntimeException {
String signStr = "";
if ("" == key) {
throw new SDKRuntimeException("财付通签名key不能为空!");
}
if ("" == content) {
throw new SDKRuntimeException("财付通签名内容不能为空");
}
signStr = content + "&key=" + key;
return MD5Util.MD5(signStr).toUpperCase();
}

(3).工具类方法

public static boolean IsNumeric(String str) {
if (str.matches("\\d *")) {
return true;
} else {
return false;
}
}

//map转成xml
public static String MapToXml(HashMap<String, Object> arr) {
String xml = "<xml>";

Iterator<Entry<String, Object>> iter = arr.entrySet().iterator();
while (iter.hasNext()) {
Entry<String, Object> entry = iter.next();
String key = entry.getKey();
String val = entry.getValue()+"";
if (IsNumeric(val)) {
xml += "<" + key + ">" + val + "</" + key + ">";

} else
xml += "<" + key + "><![CDATA[" + val + "]]></" + key + ">";
}

xml += "</xml>";
return xml;
}

//xml转成map
@SuppressWarnings("unchecked")
public static Map<String, String> parseXml(String xml) throws Exception {
Map<String, String> map = new HashMap<String, String>();
Document document = DocumentHelper.parseText(xml);
Element root = document.getRootElement();
List<Element> elementList = root.elements();
for (Element e : elementList) {
map.put(e.getName(), e.getText());
}
return map;
}

public static String FormatParamMap(HashMap<String, Object> parameters) throws SDKRuntimeException {
String buff = "";
try {
List<Map.Entry<String, Object>> infoIds = new ArrayList<Map.Entry<String, Object>>(
parameters.entrySet());
Collections.sort(infoIds,
new Comparator<Map.Entry<String, Object>>() {
public int compare(Map.Entry<String, Object> o1,
Map.Entry<String, Object> o2) {
return (o1.getKey()).toString().compareTo(
o2.getKey());
}
});

for (int i = 0; i < infoIds.size(); i++) {
Map.Entry<String, Object> item = infoIds.get(i);
if (item.getKey() != "") {
buff += item.getKey() + "="
+ URLEncoder.encode(item.getValue()+"", "utf-8") + "&";
}
}
if (buff.isEmpty() == false) {
buff = buff.substring(0, buff.length() - 1);
}
} catch (Exception e) {
throw new SDKRuntimeException(e.getMessage());
}
return buff;
}

public static String CreateNoncestr() {
String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
String res = "";
for (int i = 0; i < 16; i++) {
Random rd = new Random();
res += chars.charAt(rd.nextInt(chars.length() - 1));
}
return res;
}

(4).发送请求方法

public static String sendPost(String url, String param,String charset) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(new String(param.getBytes(),charset));
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), charset));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return result;
}

(5).执行接口

//网页版微信支付接口
public String wxWapPay() throws Exception {
String result = SUCCESS;
String message = "";
int code = 0;
try {
String ip = getIpAddr(request);
String outTradeNo = new SimpleDateFormat("YYYYMMDDHHmmssSSS").format(new Date())+"-wap";
String param = new WxPayHelper().CreateWapUrl(outTradeNo, ip);
String resp = HttpRequest.sendPost(WxPayConfig.UNIFIEDORDER_INTERFACE, param, "utf-8");
Map<String, String> res = CommonUtil.parseXml(resp);

if(res.get("return_code") == "SUCCESS") {
if(res.get("result_code") == "SUCCESS") {
message = res.get("code_url");
}else {
code = -1;
message = res.get("err_code_des");
logger.error("wxWapPay error code"+res.get("err_code")+", reason is "+res.get("err_code_des"));
}
}else {
code = -1;
message = res.get("return_msg");
logger.error("wxWapPay error reason is "+res.get("return_msg"));
}
} catch (Exception e) {
code = -1;
logger.error("wxWapPay Exception reason is "+ e);
e.printStackTrace();
}
dataMap = new HashMap<String, Object>();
dataMap.put("code", code);
dataMap.put("message", message);

return result;
}


更多参考

https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: