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);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
weChatpay.zip (63个子文件)
weChatpay
.project 2KB
.settings
.jsdtscope 658B
org.eclipse.core.resources.prefs 112B
org.eclipse.wst.common.project.facet.core.xml 163B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.name 6B
.mymetadata 300B
WebRoot
pay.jsp 2KB
WEB-INF
web.xml 1KB
lib
httpcore-4.2.4.jar 222KB
json-lib-2.4-jdk15.jar 155KB
commons-lang-2.6.jar 278KB
commons-pool-1.6.jar 109KB
commons-dbcp-20030825.184428.jar 92KB
ezmorph-1.0.6.jar 84KB
httpclient-4.2.5.jar 423KB
commons-codec-1.7.jar 254KB
commons-cli-1.0.jar 29KB
jdom-1.0.jar 150KB
dom4j-1.6.1.jar 307KB
commons-logging-1.1.3.jar 61KB
commons-httpclient-3.1.jar 298KB
commons-collections-3.2.1.jar 562KB
commons-beanutils-1.8.0.jar 226KB
classes
com
utils
MD5Util.class 2KB
TenpayUtil.class 3KB
CommonUtil.class 4KB
http
HttpResponse.class 1KB
HttpResultType.class 1KB
MySSLSocketFactory.class 2KB
HttpClientConnectionManager.class 3KB
HttpRequest.class 3KB
TrustAnyTrustManager.class 889B
GetWxOrderno.class 5KB
RequestHandler.class 6KB
MyX509TrustManager.class 873B
WeixinOauth2Token.class 1KB
Sha1Util.class 3KB
servlet
MainServlet.class 2KB
TopayServlet.class 6KB
NotifyServlet.class 1KB
index.jsp 840B
META-INF
MANIFEST.MF 36B
error.jsp 623B
aa.html 0B
src
com
utils
WeixinOauth2Token.java 1KB
TenpayUtil.java 4KB
MyX509TrustManager.java 688B
http
HttpClientConnectionManager.java 2KB
HttpResponse.java 2KB
MySSLSocketFactory.java 1KB
HttpResultType.java 703B
HttpRequest.java 3KB
TrustAnyTrustManager.java 592B
Sha1Util.java 2KB
RequestHandler.java 6KB
CommonUtil.java 4KB
GetWxOrderno.java 4KB
MD5Util.java 1KB
servlet
TopayServlet.java 7KB
MainServlet.java 2KB
NotifyServlet.java 1KB
.classpath 2KB
共 63 条
- 1
资源评论
淞元
- 粉丝: 88
- 资源: 28
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功