java微信H5支付中遇到的問題都已解決 java程式碼 和html程式碼
1,開通賬號,申請商戶賬號 開啟連線https://pay.weixin.qq.com/index.php/partner/public/home (如圖1)
(圖1)
2,完成註冊以後登入 進入(圖2)
(圖2)
點選H5支付開通
3,如果已開通 (如圖3)
(圖3)
4點選開發配置選單(如圖4)新增域名,域名必須是備案過的
完了就可以開發了
下面是我的程式碼很簡單
package controllers.payment.wechat;
import com.jpay.weixin.api.WxPayApi;
import com.jpay.weixin.api.WxPayApiConfig;
import constants.payment.MerchantAccountConstants;
import org.jdom.JDOMException;
import play.Logger;
import play.Play;
import playecsp.constants.EcspConstants;
import playecsp.controllers.EcspController;
import service.payment.IAliPayService;
import util.payment.H5ScencInfo;
import util.payment.HttpUtils;
import util.payment.PayCommonUtils;
import java.math.BigDecimal;
import javax.inject.Inject;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* 微信支付服務
* Created by on 2016/11/24.
*/
@playecsp.annotations.EcspController(name = "微信支付")
public class H5WechatPayment extends EcspController {
@Inject
private static IAliPayService aliPayService;
private static final String appID="xxxxx";//此處填寫申請的appID
private static final String mchID="1489896702";//此處填寫申請商戶號
private static final String partnerKey="OYLBnUzRJw1qKgNXjY7akBQabbwgUFlO";//此處填寫key
private static String notify_url = "http://ftrain.yladc.com/fsp/wxpay/wxPayRecallForH5";//次處填寫你的回撥地址域名必須和上面圖4出一樣
private static String name = "";
public static WxPayApiConfig getApiConfig() {
return WxPayApiConfig.New()
.setAppId(appID)
.setMchId(mchID)
.setPaternerKey(partnerKey)
.setPayModel(WxPayApiConfig.PayModel.BUSINESSMODEL);
}
public static void wapPayH5(BigDecimal totalPriceReal) throws Exception, IOException{
System.out.println("--pay start--");
String ip = PayCommonUtils.getHostIP();//終端Ip必須和域名繫結的機器上的外網Ip一致,網路環境未能通過安全驗證,請稍後再試的錯誤
// 獲取終端 IP
if (ip.isEmpty()) {
ip = "127.0.0.1";
}
Integer totalPriceRealTurn=0;
if(totalPriceReal!=null && !totalPriceReal.equals("")){
totalPriceRealTurn= totalPriceReal.multiply(BigDecimal.valueOf(100)).intValue();
}else{
renderEcspJson("", "wechatPayment", EcspConstants.FAIL, "請輸入金額");
}
//組裝資訊
H5ScencInfo sceneInfo = new H5ScencInfo();
H5ScencInfo.H5 h5_info = new H5ScencInfo.H5();
//場景型別
h5_info.setType("Wap");
//WAP網站URL地址
h5_info.setWap_url(notify_url);
//WAP網站名
h5_info.setWap_name(name);
sceneInfo.setH5_info(h5_info);
//WxPayApiConfig wxPayApiConfig=getApiConfig();
Map<String, String> params=WxPayApiConfig.New()
.setAppId(appID)//支付appId
.setMchId(mchID)//商戶號
.setBody("H5支付測試")//商品描述
.setSpbillCreateIp(ip)//終端IP
.setTotalFee(totalPriceRealTurn.toString())//總金額
.setTradeType(WxPayApi.TradeType.MWEB)//交易型別
.setNotifyUrl(notify_url)//回撥地址
.setPaternerKey(partnerKey)//訂單號
.setOutTradeNo(String.valueOf(System.currentTimeMillis()))//訂單號
.setSceneInfo(sceneInfo.toString())//場景資訊
.setAttach("H5支付測試")//附加資料
.build();
String xml= mapToXml(params);
//呼叫微信統一下單介面
String resData = HttpUtils.sendReqForPost(xml.toString());
//解析返回xml成map
Map<String, String> map = new HashMap<String, String>();
map = PayCommonUtils.doXMLParse(resData);
if ("SUCCESS".equals(map.get("return_code")) && "SUCCESS".equals(map.get("result_code"))) {
Logger.info(" order of weixin is successful :::" + map.get("code_url"));
//嚮應用返回喚起連結mweb_url
Map<String, String> retData = new HashMap<String, String>();
retData.put("mweb_url", map.get("mweb_url"));
retData.put("prepay_id", map.get("prepay_id"));
renderEcspJson(retData, "wechatPayment", EcspConstants.SUCESS, "");
} else {
renderEcspJson("", "wechatPayment", EcspConstants.FAIL, map.get("return_msg"));
}
}
/**
* map 轉 xml
*
* @param map
* @return
*/
private static String mapToXml(Map<String, String> map) {
StringBuffer sf = new StringBuffer("<xml>");
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
String key = entry.getKey();
String value = entry.getValue();
sf.append("<" + key + ">" + value + "</" + key + ">");
}
sf.append("</xml>");
return sf.toString();
}
/**
* h5信支付非同步回撥方法
* Created by lpp on 2018/6/1.
*/
public static void wxPayRecallForH5() throws IOException, JDOMException, InterruptedException {
Logger.info(":::weixin pay of wxPayRecallForH5 recall :::");
}
}
html程式碼
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>h5支付</title>
<link href="${ctx}/admin/css/common/bootstrap.css" rel="stylesheet">
<link href="${ctx}/admin/css/common/font-awesome.min.css" rel="stylesheet">
<!-- Expand styles -->
<link href="${ctx}/admin/css/common/ecsp-ext.css" rel="stylesheet">
<link href="${ctx}/admin/css/common/ecsp-size.css" rel="stylesheet">
<link href="${ctx}/admin/css/common/ecsp-plugin.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link rel="stylesheet" type="text/css" href="${ctx}/admin/css/theme/default_theme.css">
<link href="${ctx}/lib/layer/skin/layer.css" rel="stylesheet">
</head>
<body>
<div class="login-container animated fadeInDown" style="text-align: center">
請輸入金額:<input type="text" id="totalPriceReal"><br/>
<br/>
<input type="button" value="去支付" onclick="submintPay()">
</div>
<script src="${ctx}/js/jquery.min.js"></script>
<script src="${ctx}/js/bootstrap.min.js"></script>
<script>
function submintPay(){
var totalPriceReal=$("#totalPriceReal").val();//商品金額
var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
if (reg.test(totalPriceReal)) {
var orderIofn={
"totalPriceReal":totalPriceReal
};
$.ajax({
url:"/fsp/wxpay/wapPayH5",
data:orderIofn,
type: "POST",
async: false,
success:function(data){
if(data.header.retCode=='000000'){
// alert(JSON.stringify(data));
//支付喚起微信連線
var mweb_url=data.body.mweb_url;
//支付完成連線
var mweb_complete_url="http://ftrain.yladc.com/fsp/wechatpay/gotoPayCompletion";
self.location.href=mweb_url+"&redirect_url="+mweb_complete_url;
}else{
//alert(JSON.stringify(data));
}
},
error:function(){
layer.close(load_index);
layer.msg("刪除失敗");
}
});
}else{
alert("請輸入合法金額");
};
}
</script>
</body>
</html>