微信支付
微信支付商户号配置
-
前端代码
<div class="weixinpay">
<a class="weui-btn" id="confirm" type="submit" style="width:70%; border-radius: 0px; font-size: 16px;" onclick="pay();">去付款</a>
</div>
js代码
1、当支付页面为非微信打开时,强制关闭页面
<script type="text/javascript">
// 对浏览器的UserAgent进行正则匹配,不含有微信独有标识的则为其他浏览器
var useragent = navigator.userAgent;
if (useragent.match(/MicroMessenger/i) != 'MicroMessenger') {
// 这里警告框会阻塞当前页面继续加载
alert('已禁止本次访问:您必须使用微信内置浏览器访问本页面!');
// 以下代码是用javascript强行关闭当前页面
var opened = window.open('about:blank', '_self');
opened.opener = null;
opened.close();
}
</script>
2、H5调起支付接口
<script type="text/javascript">
function pay() {
if (typeof WeixinJSBridge == "undefined") {
if (document.addEventListener) {
document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
} else if (document.attachEvent) {
document.attachEvent('WeixinJSBridgeReady', jsApiCall);
document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
}
} else {
//调用预付单生成接口
toggleLoadingToast(true, '正在获取支付数据');
$.getJSON("debtWxpay.action?saleno=${param.saleno}&vipcardno=${param.vipcardno}&xjtotal=${debtlist.zttotal}", function(data, status) {
if (data.code == '1') {
toggleLoadingToast(false);
invokeBridge(data.data);
} else {
alert(data.msg);
}
})
}
}
//调用微信支付接口
function invokeBridge(data) {
WeixinJSBridge.invoke('getBrandWCPayRequest', {
"appId" : data.appId, //公众号名称,由商户传入
"timeStamp" : data.timeStamp, //时间戳,自1970年以来的秒数
"nonceStr" : data.nonceStr, //随机串
"package" : data.package,
"signType" : "MD5", //微信签名方式:
"paySign" : data.paySign
//微信签名
}, function(res) {
WeixinJSBridge.log(res.err_msg);
if (res.err_msg == "get_brand_wcpay_request:cancel") {
window.location.href = 'paycancel.jsp';
}
if (res.err_msg == "get_brand_wcpay_request:ok") {
window.location.href = 'success.jsp';
}
});
}
//微信toast提示
function toggleLoadingToast(show, content) {
var $loadingToast = $('#loadingToast');
if (content !== undefined) {
$loadingToast.find('.weui-toast__content').text(content);
}
if (show) {
if ($loadingToast.css('display') != 'none')
return;
$loadingToast.fadeIn(0);
} else {
if ($loadingToast.css('display') == 'none')
return;
$loadingToast.fadeOut(0);
}
}
</script>
java后台
1、xml配置
<action name="debtWxpay" class="hisense.crm.box.client.action.BoxOperateAction" method="wxpay"></action>
2、生成单据
public void wxpay() {
createBill();
}
public void createBill() {
try {
Date date = new Date();
double regtotal = 0;
double zstotal = 0;
List<Twxvipcard> lstcard = vipcardManage.getHibernateTemplate().find("from Twxvipcard where vipcardno ='" + vipcardno + "'");
if (lstcard.size() > 0) {
if ((lstcard.get(0).getCardfaceno() != null) && (lstcard.get(0).getCardfaceno().equals("") == false))
vipcardfaceno = lstcard.get(0).getCardfaceno();
}
if ((lstcard.get(0).getVipcardno() > 0)) {
vipcardno = String.valueOf(lstcard.get(0).getVipcardno());
openid = lstcard.get(0).getWxopenid();
}
boolean isBill = false;
String guid = "";
List<Twxdebtpay> debtlst = debtpayManage.getHibernateTemplate().find("from Twxdebtpay where refhsbillno='" + saleno + "'");
if (debtlst.size() > 0) {
guid = debtlst.get(0).getBillno();
isBill = true;
} else {
Twxdebtpay head = new Twxdebtpay();
guid=java.util.UUID.randomUUID().toString();
int i=guid.length()>40?40:guid.length();
guid= guid.substring(0, i);
head.setCardfaceno(vipcardfaceno);
head.setGendate(new SimpleDateFormat("yyyy-MM-dd").format(date));
head.setGentime(new SimpleDateFormat("HH:mm:ss").format(date));
head.setMsg("微信追讨");
head.setOpenid(openid);
head.setRefweixinbillno("");
head.setBillno(guid);
head.setVipcarno(vipcardno);
head.setRefhsbillno(saleno);
head.setStutas("0");
head.setXjtotal(Double.parseDouble(xjtotal));
isBill = debtpayManage.insert(head);
System.out.println("---------------isBill:" + isBill);
}
if (isBill) {
payResultRun(openid, guid, Double.parseDouble(xjtotal));
} else {
responseMsg(Utils.failMsg("生成单据失败"));
}
} catch (Exception e) {
e.printStackTrace();
log.error("payResultRun-------->生成充值单异常" + e);
responseMsg(Utils.failMsg("生成充值单异常"));
}
}
public void payResultRun(String openid, String billNo, Double totalfee) {
try {
IWebPayResult result = new WebPayResult();
String key = "";
String appID = "";
String body = "";
String mchID = "";
String payUrl = "";
String notityUrl = "";
List<Twxsysoption> list = sysoptionManage.getHibernateTemplate().find("from Twxsysoption where section ='WXPAY'");
for (int i = 0; i < list.size(); i++) {
if ("WXKEY".equals(list.get(i).getSysoption())) {
key = list.get(i).getOptionvalue();
}
if ("APPID".equals(list.get(i).getSysoption())) {
appID = list.get(i).getOptionvalue();
}
if ("BODY".equals(list.get(i).getSysoption())) {
body = list.get(i).getOptionvalue();
}
if ("MCHID".equals(list.get(i).getSysoption())) {
mchID = list.get(i).getOptionvalue();
}
if ("PAYURL".equals(list.get(i).getSysoption())) {
payUrl = list.get(i).getOptionvalue();
}
if ("WXNOTITYURL".equals(list.get(i).getSysoption())) {
notityUrl = list.get(i).getOptionvalue();
}
}
System.out.println("---------------notityUrl:" + notityUrl);
WxPay wxPay = new WxPay(key, appID, body, mchID, payUrl, notityUrl);
WebPayResData resData = wxPay.doWebPayBusiness(payData(openid, billNo, totalfee), result);
if (resData != null) {
String returnCode = resData.getReturn_code();
log.info("payResultRun-------->【统一接口调用成功返回结果returnCode】" + returnCode);
String resultCode = resData.getResult_code();
log.info("payResultRun-------->【统一接口调用成功返回结果resultCode】" + resultCode);
if (!"".equals(returnCode) && !"".equals(resultCode)) {
if (returnCode.equals("SUCCESS") && resultCode.equals("SUCCESS")) {
log.info("payResultRun-------->【统一接口调用成功返回结果】" + wxPay.jsSdkParameter(resData).toString());
JSONObject payData = wxPay.jsSdkParameter(resData);
payData.put("billNo", billNo);
responseMsg(Utils.successMsg("统一接口调用成功", payData));
} else {
errorMsg(resData);
}
} else {
log.error("payResultRun-------->【支付未知失败】");
responseMsg(Utils.failMsg("调起支付出现未知失败"));
}
} else {
responseMsg(Utils.failMsg("支付出现异常:接口无数据返回"));
}
} catch (Exception e) {
e.printStackTrace();
log.error("payResultRun-------->【支付出现异常】:" + e);
}
}
private WebPayReqData payData(String openid, String billNo, Double totalfee) {
HttpServletRequest res = ServletActionContext.getRequest();
int fee = (new Double(totalfee * 100)).intValue();
String ip = Util.getIp(res);
WebPayReqData webdata = new WebPayReqData("2", openid, billNo, fee, ip);
return webdata;
}