use of com.utils.payUtils.RequestHandler in project portal by ixinportal.
the class PayWebController method pay.
/**
* 测试的接口
* @param request
* @param response
*/
@RequestMapping(produces = "text/html")
public void pay(HttpServletRequest request, HttpServletResponse response) {
URL = request.getParameter("url");
// 应用appid
String appid = request.getParameter("appid");
// 应用秘钥
APP_SECRET_KEY = request.getParameter("appSecretKey");
String payid = request.getParameter("payid");
// 订单号
String orderNo = request.getParameter("orderNo");
// 订单号
String money = request.getParameter("money");
String spbill_create_ip = request.getParameter("spbill_create_ip");
String attach = request.getParameter("attach");
String requestURI = request.getRequestURI();
String requestURL = request.getRequestURL().toString();
int indexOfURI = requestURL.indexOf(requestURI);
StringBuffer notifySB = new StringBuffer();
StringBuffer returnSB = new StringBuffer();
// 回调url
String notifyStr = request.getContextPath() + "/web/pay/notifyUrl";
// 页面跳转url
String returnStr = request.getContextPath() + "/web/pay/returnUrl";
notifySB.append(requestURL.substring(0, indexOfURI)).append(notifyStr);
returnSB.append(requestURL.substring(0, indexOfURI)).append(returnStr);
// 异步回调url
String notify_url = notifySB.toString();
// 页面跳转url
String return_url = returnSB.toString();
// 交易类型 all 获取在平台开通的支付方式 , alipay:支付宝 ,wechat:微信,unionPay 银联,thirdPay 第三方付款
String pay_type = request.getParameter("pay_type");
// 描述 商品的标题/交易标题/订单标题/订单关键字等。该参数最长为128个汉字。
String describe = request.getParameter("body");
// 随机字符串
String nonce_str = PayUtil.getNonceStr();
SortedMap<String, String> packageParams = new TreeMap<String, String>();
packageParams.put("appid", appid);
packageParams.put("nonce_str", nonce_str);
packageParams.put("body", describe);
packageParams.put("out_trade_no", orderNo);
packageParams.put("total_fee", money.toString());
packageParams.put("spbill_create_ip", spbill_create_ip);
packageParams.put("notify_url", notify_url);
packageParams.put("return_url", return_url);
packageParams.put("pay_type", pay_type);
packageParams.put("attach", attach);
RequestHandler reqHandler = new RequestHandler(null, null);
reqHandler.init(null, null, APP_SECRET_KEY);
String sign = reqHandler.createSign(packageParams);
String xmlParam = "<xml><appid>" + appid + "</appid>" + "<nonce_str>" + nonce_str + "</nonce_str><sign>" + sign + "</sign>" + "<body><![CDATA[" + describe.trim() + "]]></body>" + "<out_trade_no>" + orderNo + "</out_trade_no><total_fee>" + money + "</total_fee>" + "<spbill_create_ip>" + spbill_create_ip + "</spbill_create_ip><notify_url>" + notify_url + "</notify_url><return_url>" + return_url + "</return_url><pay_type>" + pay_type + "</pay_type><attach>" + attach + "</attach></xml>";
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL);
// DefaultTransactionDefinition def = new DefaultTransactionDefinition();
// def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
// TransactionStatus s = transactionManager.getTransaction(def);
String jsonStr = null;
try {
httppost.setEntity(new StringEntity(xmlParam, "UTF-8"));
HttpResponse respon = httpclient.execute(httppost);
jsonStr = EntityUtils.toString(respon.getEntity(), "UTF-8");
if (jsonStr.indexOf("FAIL") != -1) {
PayUtil.showJsp(response, SytemUtil.getContextUrl(request) + "/web/pay/returnUrl1");
return;
} else if (jsonStr.indexOf("SUCCESS") != -1) {
Map m = PayUtil.parseXmlToList2(jsonStr);
String contextUrl = (String) m.get("redirect_url");
String sign01 = (String) m.get("sign");
// 必须进行验签,否则存在支付风险
boolean result01 = PayUtil.validate(sign01, m, APP_SECRET_KEY);
if (result01) {
SortedMap<String, String> pk = new TreeMap<String, String>();
pk.put("redirect_url", contextUrl);
String encryString = PayUtil.getEncryString(pk, APP_SECRET_KEY);
BillExample be = new BillExample();
BillExample.Criteria bc = be.createCriteria();
bc.andBillIdEqualTo(orderNo);
Bill bill = sqlSession.selectOne("com.itrus.portal.db.BillMapper.selectByExample", be);
OnPayInfo payInfo;
HttpSession session = request.getSession();
UserInfo userInfo = (UserInfo) session.getAttribute("webuserInfo");
if (bill.getOnPayInfo() == null) {
payInfo = new OnPayInfo();
payInfo.setOnlinePay(Long.parseLong(payid));
payInfo.setPaySum(Double.parseDouble(money));
payInfo.setPayStatus(0);
payInfo.setDyTime(new Date());
payInfo.setName(String.valueOf(userInfo.getId()));
sqlSession.insert("com.itrus.portal.db.OnPayInfoMapper.insert", payInfo);
sqlSession.flushStatements();
bill.setOnPayInfo(payInfo.getId());
sqlSession.update("com.itrus.portal.db.BillMapper.updateByPrimaryKeySelective", bill);
} else {
payInfo = sqlSession.selectOne("com.itrus.portal.db.OnPayInfoMapper.selectByPrimaryKey", bill.getOnPayInfo());
payInfo.setOnlinePay(Long.parseLong(payid));
payInfo.setPaySum(Double.parseDouble(money));
payInfo.setDyTime(new Date());
payInfo.setName(String.valueOf(userInfo.getId()));
sqlSession.update("com.itrus.portal.db.OnPayInfoMapper.updateByPrimaryKeySelective", payInfo);
}
// transactionManager.commit(s);
response.sendRedirect(contextUrl + "&sign=" + encryString);
}
return;
}
} catch (Exception e) {
LogUtil.syslog(sqlSession, "在线支付", orderNo + "在线支付错误:" + e.toString() + xmlParam + jsonStr);
e.printStackTrace();
}
// finally{
// if (!s.isCompleted()) {
// transactionManager.rollback(s);
// }
// }
}
Aggregations