实例介绍
【实例简介】
java版和php版都是经本人测试成功后上传,功能都没有问题。代码逻辑有困惑时可以参考开发文档,上面很清楚。
java版 说明
1. 在开通微信支付的公众号下访问 htpp://服务器地址/mainServlet
2. 修改MainServlet.java和TopayServlet.java 里面的商户参数,授权返回地址和notify_url部分即可。
3. 代码是别人的劳动成果,经自己稍微修改后测试成功,demo并没有十分的简化,大家可以做一个参考吧。如果出现签名失败,仔细检查下自己填写的appid和partnerkey是否正确。
4. 请注意:微信支付成功后的通知的url为notifyServlet,sb是返回的xml
5.最后祝大家都通过微信支付的测试。
【实例截图】
【核心代码】
package com.servlet; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.SortedMap; import java.util.TreeMap; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONObject; import com.utils.CommonUtil; import com.utils.GetWxOrderno; import com.utils.RequestHandler; import com.utils.Sha1Util; import com.utils.TenpayUtil; import com.utils.WeixinOauth2Token; import com.utils.http.HttpResponse; public class TopayServlet extends HttpServlet { /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //网页授权后获取传递的参数 String userId = request.getParameter("userId"); String orderNo = request.getParameter("orderNo"); String money = request.getParameter("money"); String code = request.getParameter("code"); //金额转化为分为单位 float sessionmoney = Float.parseFloat(money); String finalmoney = String.format("%.2f", sessionmoney); finalmoney = finalmoney.replace(".", ""); //商户相关资料 String appid = ""; String appsecret = ""; String partner = ""; String partnerkey = ""; String openId =""; String URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" appid "&secret=" appsecret "&code=" code "&grant_type=authorization_code"; JSONObject jsonObject = CommonUtil.httpsRequest(URL, "GET", null); if (null != jsonObject) { openId = jsonObject.getString("openid"); } //获取openId后调用统一支付接口https://api.mch.weixin.qq.com/pay/unifiedorder String currTime = TenpayUtil.getCurrTime(); //8位日期 String strTime = currTime.substring(8, currTime.length()); //四位随机数 String strRandom = TenpayUtil.buildRandom(4) ""; //10位序列号,可以自行调整。 String strReq = strTime strRandom; //商户号 String mch_id = partner; //子商户号 非必输 //String sub_mch_id=""; //设备号 非必输 String device_info=""; //随机数 String nonce_str = strReq; //商品描述 //String body = describe; //商品描述根据情况修改 String body = "美食"; //附加数据 String attach = userId; //商户订单号 String out_trade_no = orderNo; int intMoney = Integer.parseInt(finalmoney); //总金额以分为单位,不带小数点 int total_fee = intMoney; //订单生成的机器 IP String spbill_create_ip = request.getRemoteAddr(); //订 单 生 成 时 间 非必输 // String time_start =""; //订单失效时间 非必输 // String time_expire = ""; //商品标记 非必输 // String goods_tag = ""; //这里notify_url是 支付完成后微信发给该链接信息,可以判断会员是否支付成功,改变订单状态等。 String notify_url ="http://***/notifyServlet"; String trade_type = "JSAPI"; String openid = openId; //非必输 // String product_id = ""; SortedMap<String, String> packageParams = new TreeMap<String, String>(); packageParams.put("appid", appid); packageParams.put("mch_id", mch_id); packageParams.put("nonce_str", nonce_str); packageParams.put("body", body); packageParams.put("attach", attach); packageParams.put("out_trade_no", out_trade_no); //这里写的金额为1 分到时修改 packageParams.put("total_fee", "1"); // packageParams.put("total_fee", "finalmoney"); packageParams.put("spbill_create_ip", spbill_create_ip); packageParams.put("notify_url", notify_url); packageParams.put("trade_type", trade_type); packageParams.put("openid", openid); RequestHandler reqHandler = new RequestHandler(request, response); reqHandler.init(appid, appsecret, partnerkey); String sign = reqHandler.createSign(packageParams); String xml="<xml>" "<appid>" appid "</appid>" "<mch_id>" mch_id "</mch_id>" "<nonce_str>" nonce_str "</nonce_str>" "<sign>" sign "</sign>" "<body><![CDATA[" body "]]></body>" "<attach>" attach "</attach>" "<out_trade_no>" out_trade_no "</out_trade_no>" //金额,这里写的1 分到时修改 "<total_fee>" 1 "</total_fee>" //"<total_fee>" finalmoney "</total_fee>" "<spbill_create_ip>" spbill_create_ip "</spbill_create_ip>" "<notify_url>" notify_url "</notify_url>" "<trade_type>" trade_type "</trade_type>" "<openid>" openid "</openid>" "</xml>"; System.out.println(xml); String allParameters = ""; try { allParameters = reqHandler.genPackage(packageParams); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } String createOrderURL = "https://api.mch.weixin.qq.com/pay/unifiedorder"; String prepay_id=""; try { prepay_id = new GetWxOrderno().getPayNo(createOrderURL, xml); if(prepay_id.equals("")){ request.setAttribute("ErrorMsg", "统一支付接口获取预支付订单出错"); response.sendRedirect("error.jsp"); } } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } SortedMap<String, String> finalpackage = new TreeMap<String, String>(); String appid2 = appid; String timestamp = Sha1Util.getTimeStamp(); String nonceStr2 = nonce_str; String prepay_id2 = "prepay_id=" prepay_id; String packages = prepay_id2; finalpackage.put("appId", appid2); finalpackage.put("timeStamp", timestamp); finalpackage.put("nonceStr", nonceStr2); finalpackage.put("package", packages); finalpackage.put("signType", "MD5"); String finalsign = reqHandler.createSign(finalpackage); System.out.println("pay.jsp?appid=" appid2 "&timeStamp=" timestamp "&nonceStr=" nonceStr2 "&package=" packages "&sign=" finalsign); response.sendRedirect("pay.jsp?appid=" appid2 "&timeStamp=" timestamp "&nonceStr=" nonceStr2 "&package=" packages "&sign=" finalsign); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论