微信支付之h5網頁支付
來自: http://my.oschina.net/u/2385858/blog/607663
先簡單說下微信公眾號支付的流程首先獲得code,然后獲得openid,然后根據openid獲取到預支付ID(prepay_id),獲取到prepay_id,自然得到packages ,這個最難獲得的參數獲得之后,基本上就做好一半了,剩余的參數 appId 公眾號id,timeStamp時間戳,nonceStr隨機字符串,signType簽名方式,paySign。
獲取到這些參數后,新建一個頁面,復制微信的代碼 :
function onBridgeReady(){
WeixinJSBridge.invoke(
'getBrandWCPayRequest', {
"appId" : "wx2421b1c4370ec43b", //公眾號名稱,由商戶傳入
"timeStamp":" 1395712654", //時間戳,自1970年以來的秒數
"nonceStr" : "e61463f8efa94090b1f366cccfbbb444", //隨機串
"package" : "prepay_id=u802345jgfjsdfgsdg888",
"signType" : "MD5", //微信簽名方式:
"paySign" : "70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信簽名
},
function(res){
if(res.err_msg == "get_brand_wcpay_request:ok" ) {} // 使用
以上方式判斷前端返回,微信團隊鄭重提示:res.err_msg將在用戶支付成功后返回 ok,但并不保證它絕對可靠。
}
);
}
if (typeof WeixinJSBridge == "undefined"){
if( document.addEventListener ){
document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
}else if (document.attachEvent){
document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
}
}else{
onBridgeReady();
}
聲明:以上代碼為微信官方文檔中,如使用,請到官方文檔中復制
這樣微信就會自動調用支付
1、調用微信支付一閃而過
微信的測試路徑不對,進入公眾號平臺,檢查測試路徑,
如果使用的是測試路徑,則必須將自己的公眾號加入到測試白名單中
2、微信支付所需參數
appid 微信公眾平臺中的 基本配置中
appsecret 微信公眾平臺中的 基本配置中
partner 商戶號
key 在商戶平臺下的賬戶設置api安全中。
3、在微信支付頁面最好只要一些必要的東西,如果多了,可能會引起沖突。
4、開發前需要修改接口權限下的網頁授權基本信息,填寫測試的域名
2016-01-22
微信支付用到的方法
/**
* 元轉換成分
* @param money
* @return
*/
public static String getMoney(String amount) {
if(amount==null){
return "";
}
// 金額轉化為分為單位
String currency = amount.replaceAll("\\$|\\¥|\\,", ""); //處理包含, ¥ 或者$的金額
int index = currency.indexOf(".");
int length = currency.length();
Long amLong = 0l;
if(index == -1){
amLong = Long.valueOf(currency+"00");
}else if(length - index >= 3){
amLong = Long.valueOf((currency.substring(0, index+3)).replace(".", ""));
}else if(length - index == 2){
amLong = Long.valueOf((currency.substring(0, index+2)).replace(".", "")+0);
}else{
amLong = Long.valueOf((currency.substring(0, index+1)).replace(".", "")+"00");
}
return amLong.toString();
}
public String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}/**
* 獲取隨機字符串
* @return
*/
public static String getNonceStr() {
// 隨機數
String currTime = TenpayUtil.getCurrTime();
// 8位日期
String strTime = currTime.substring(8, currTime.length());
// 四位隨機數
String strRandom = TenpayUtil.buildRandom(4) + "";
// 10位序列號,可以自行調整。
return strTime + strRandom;
}//獲取ip地址
public String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
本文由用戶 zcwxtd 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!