微信支付H5调用支付详解(java版)
程序员文章站
2024-03-11 21:15:25
最近项目需要微信支付,然后看了下微信公众号支付,,虽然不难,但是细节还是需要注意的,用了大半天时间写了个demo,并且完整的测试了一下支付流程,下面分享一下微信公众号支付的...
最近项目需要微信支付,然后看了下微信公众号支付,,虽然不难,但是细节还是需要注意的,用了大半天时间写了个demo,并且完整的测试了一下支付流程,下面分享一下微信公众号支付的经验。
一、配置公众号微信支付
需要我们配置微信公众号支付地址和测试白名单。
比如:支付js页面的地址为 http://www.xxx.com/shop/pay/
那此处配置www.xxx.com/shop/pay/
二、开发流程
借用微信公众号支付api(地址 http://pay.weixin.qq.com/wiki/doc/api/index.php?chapter=7_4),我们需要开发的为红色标记出的。如下:
三、向微信服务器端下订单
调用统一下单接口,这样就能获取微信支付的prepay_id()。
在调用该接口前有几个字段是h5支付必须填写的openid
3.1 获取openid
可以通过网页授权形式()
在微信中发送如下链接
=要跳转的下订单的url&response_type=code&scope=snsapi_base&state=123#wechat_redirect
3.2 下订单获取prepay_id
代码如下,实际上是通过post发送一个xml 文件,获取微信服务器端发送过来的prepay_id。
import java.io.bytearrayinputstream; import javaioioexception; import javaioinputstream; import javaiounsupportedencodingexception; import javautildate; import javautilhashmap; import javautiliterator; import javautilmap; import javautilmapentry; import javautilrandom; import javaxservlethttphttpservletrequest; import javaxservlethttphttpservletresponse; import orgapachecommonscodecdigestdigestutils; import orgspringframeworkstereotypecontroller; import orgspringframeworkwebbindannotationrequestmapping; import orgxmlpullvxmlpullparser; import orgxmlpullvxmlpullparserexception; import orgxmlpullvxmlpullparserfactory; import comfasterxmljacksondatabindjsonnode; import comgsonoauthoauth; import comgsonoauthpay; import comgsonutilhttpkit; import comsyutildatetimeutil; import comsyutiljsonutil; @controller @requestmapping("/pay") public class wxpaycontroller { @requestmapping(value = "wxprepaydo") public void jspay(httpservletrequest request, httpservletresponse response, string callback) throws exception { // 获取openid string openid = sessionutilgetatt(request, "openid"); if (openid == null) { openid = getuseropenid(request); } string appid = "wx16691fcb0523c1a4"; string paternerkey = "iningfeng1234567fdfwfdfd1ss234567"; string out_trade_no = gettradeno(); map<string, string> paramap = new hashmap<string, string>(); paramapput("appid", appid); paramapput("attach", "测试"); paramapput("body", "测试购买支付"); paramapput("mch_id", "10283271"); paramapput("nonce_str", create_nonce_str()); paramapput("openid", openid); paramapput("out_trade_no", out_trade_no); paramapput("spbill_create_ip", getaddrip(request)); paramapput("total_fee", "1"); paramapput("trade_type", "jsapi"); paramapput("notify_url", "http://wwwxxxco/bank/page/wxnotify"); string sign = getsign(paramap, paternerkey); paramapput("sign", sign); // 统一下单 https://apimchweixinqqcom/pay/unifiedorder string url = "https://apimchweixinqqcom/pay/unifiedorder"; string xml = arraytoxml(paramap); string xmlstr = httpkitpost(url, xml); // 预付商品id string prepay_id = ""; if (xmlstrindexof("success") != -1) { map<string, string> map = doxmlparse(xmlstr); prepay_id = (string) mapget("prepay_id"); } map<string, string> paymap = new hashmap<string, string>(); paymapput("appid", appid); paymapput("timestamp", create_timestamp()); paymapput("noncestr", create_nonce_str()); paymapput("signtype", "md5"); paymapput("package", "prepay_id=" + prepay_id); string paysign = getsign(paymap, paternerkey); paymapput("pg", prepay_id); paymapput("paysign", paysign); webutilresponse(response, webutilpackjsonp(callback, jsonutilwarpjsonnoderesponse(jsonutilobjecttojsonnode(paymap))tostring())); } /** * map转成xml * * @param arr * @return */ public string arraytoxml(map<string, string> arr) { string xml = "<xml>"; iterator<entry<string, string>> iter = arrentryset()iterator(); while (iterhasnext()) { entry<string, string> entry = iternext(); string key = entrygetkey(); string val = entrygetvalue(); xml += "<" + key + ">" + val + "</" + key + ">"; } xml += "</xml>"; return xml; } // 获取openid private string getuseropenid(httpservletrequest request) throws exception { string code = requestgetparameter("code"); if (code == null) { string openid = requestgetparameter("openid"); return openid; } oauth o = new oauth(); string token = ogettoken(code); jsonnode node = jsonutilstringtojsonnode(token); string openid = nodeget("openid")astext(); return openid; } private string create_nonce_str() { string chars = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789"; string res = ""; for (int i = 0; i < 16; i++) { random rd = new random(); res += charscharat(rdnextint(charslength() - 1)); } return res; } private string getaddrip(httpservletrequest request){ return requestgetremoteaddr(); } private string create_timestamp() { return longtostring(systemcurrenttimemillis() / 1000); } private string gettradeno(){ string timestamp = datetimeutilformatdate(new date(), datetimeutildatetime_pattern); return "hzno" + timestamp; } private string getsign(map<string, string> params, string paternerkey ) throws unsupportedencodingexception { string string1 = paycreatesign(params, false); string stringsigntemp = string1 + "&key=" + paternerkey; string signvalue = digestutilsmd5hex(stringsigntemp)touppercase(); return signvalue; } private map<string, string> doxmlparse(string xml) throws xmlpullparserexception, ioexception { inputstream inputstream = new bytearrayinputstream(xmlgetbytes()); map<string, string> map = null; xmlpullparser pullparser = xmlpullparserfactorynewinstance() newpullparser(); pullparsersetinput(inputstream, "utf-8"); // 为xml设置要解析的xml数据 int eventtype = pullparsergeteventtype(); while (eventtype != xmlpullparserend_document) { switch (eventtype) { case xmlpullparserstart_document: map = new hashmap<string, string>(); break; case xmlpullparserstart_tag: string key = pullparsergetname(); if (keyequals("xml")) break; string value = pullparsernexttext(); mapput(key, value); break; case xmlpullparserend_tag: break; } eventtype = pullparsernext(); } return map; } }
四、h5支付
h5支付其实很简单,只需要调用微信内嵌浏览器的js方法就行()
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <%@ taglib prefix="spring" uri="http://wwwspringframeworkorg/tags" %> <% string path = requestgetcontextpath(); string basepath = requestgetscheme() + "://" + requestgetservername() + ":" + requestgetserverport() + path + "/"; %> <!doctype html public "-//w3c//dtd html 01 transitional//en" "http://wwwworg/tr/html4/loosedtd"> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=0, maximum-scale=0, user-scalable=0" /> <meta name="apple-mobile-web-app-capable" content="yes" /> <meta name="apple-mobile-web-app-status-bar-style" content="black" /> <meta name="format-detection" content="telephone=no" /> <title>测试支付</title> <link href="/css/csscss?v=0" rel="stylesheet" type="text/css"> </head> <body> <div class="index_box"> <div class="apply_name">微信js支付测试</div> <div class="branch_con"> <ul> <li><span class="name">测试支付信息</span></li> </ul> <p class="cz_btn"><a href="javascript:pay();" class="btn_1">立即支付</a></p> </div> </div> <script type="text/javascript" src="/js/zeptominjs"></script> <script type="text/javascript" src="/js/commonjs"></script> <script type="text/javascript"> var appid = urlparameter("appid"); var timestamp = urlparameter("timestamp"); var noncestr = urlparameter("noncestr"); var pg = urlparameter("pg"); var signtype = urlparameter("signtype"); var paysign = urlparameter("paysign"); function onbridgeready(){ weixinjsbridgeinvoke( 'getbrandwcpayrequest', { "appid" : appid, //公众号名称,由商户传入 "timestamp": timestamp, //时间戳,自1970年以来的秒数 "noncestr" : noncestr, //随机串 "package" : "prepay_id=" + pg, "signtype" : signtype, //微信签名方式: "paysign" : paysign //微信签名 }, function(res){ if(reserr_msg == "get_brand_wcpay_request:ok" ) { alert("支付成功"); } // 使用以上方式判断前端返回,微信团队郑重提示:reserr_msg将在用户支付成功后返回 ok,但并不保证它绝对可靠。 } ); } function pay(){ if (typeof weixinjsbridge == "undefined"){ if( documentaddeventlistener ){ documentaddeventlistener('weixinjsbridgeready', onbridgeready, false); }else if (documentattachevent){ documentattachevent('weixinjsbridgeready', onbridgeready); documentattachevent('onweixinjsbridgeready', onbridgeready); } }else{ onbridgeready(); } } </script> </body> </html>
效果如下
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。