@Controller
@RequestMapping("/api/pay/")
public class OrderPayController extends BaseController{
@Autowired
private OrderManager orderManager;
@Autowired
private ProductByIdCacheManager productByIdCacheManager;
@Autowired
OrderPaymentTypeRecordManager orderPaymentTypeRecordManager;
/**
* 订单支付支付
* @param request
* @return
*/
@LoginInterceptor(value=LoginInterceptor.LOGIN, contType=LoginInterceptor.CONT_JOSN_TYPE)
@RequestMapping(value="/orderPay")
public @ResponseBody Map<String, Object> orderPay(final HttpServletRequest request ,final HttpServletResponse response) {
Map<String, Object> resultMap = new HashMap<String, Object> ();
Long orderId = StringUtil.nullToLong(request.getParameter("orderId"));
String payType = StringUtil.null2Str(request.getParameter("payType"));
response.setHeader("Access-Control-Allow-Origin", "*");
try{
String body = "订单支付";
String outTradeNo = "";
String clientIp = RequestUtil.getClientIp(request);
// 是否微信请求
boolean isWebRequest = RequestUtil.isWeixin(request);
Integer paymentType = 0;
Long weChatConfigId = null;
if(StringUtil.compareObject(payType, "weixin")){
// 微信支付
int orderTotal = 0;
String notifyURL = RequestUtil.getRequestURL(request) + "/api/order/wxpayNotify.msp";
String openId = StringUtil.null2Str(request.getSession().getAttribute(PortalConstants.SESSION_CURRENT_OPEN_ID));
Map<String, String> orderPayInfoMap = new HashMap<String, String>();
WeChatAppConfig weChatAppConfig = null;
if(isWebRequest){
//微页面-微信公众号支付
weChatAppConfig = Constants.WECHAT_APP_CONFIG_MAP.get(StringUtil.null2Str("test"));
orderPayInfoMap = WeiXinPayUtil.getWeixinH5PayInfo(weChatAppConfig, outTradeNo, openId, notifyURL, orderTotal, body, clientIp);
if(orderPayInfoMap != null && !StringUtil.isNull(orderPayInfoMap.get("code_url"))){
//跨号支付
String codeUrl = StringUtil.null2Str(orderPayInfoMap.get("code_url"));
if(StringUtil.isNull(codeUrl)){
resultMap.put(PortalConstants.CODE, PortalConstants.CODE_ERROR);
resultMap.put(PortalConstants.MSG, "支付失败");
resultMap.put(PortalConstants.SYSTEMTIME, DateUtil.getCurrentTime());
return resultMap;
}
//生成二维码图片,并返回地址
String filePath = MatrixToImageWriterUtil.saveMatrixImage(codeUrl);
if(StringUtil.isNull(filePath)){
resultMap.put(PortalConstants.CODE, PortalConstants.CODE_ERROR);
resultMap.put(PortalConstants.MSG, "支付失败");
resultMap.put(PortalConstants.SYSTEMTIME, DateUtil.getCurrentTime());
return resultMap;
}
String requestURL = RequestUtil.getRequestURL(request);
filePath = requestURL + filePath;
Map<String, String> paramMap = new HashMap<String, String>();
paramMap.put("imagePath", filePath);
paramMap.put("isOther", "1"); //是否跨号支付
resultMap.put("orderPayInfo", paramMap);
resultMap.put(PortalConstants.CODE, PortalConstants.CODE_SUCCESS);
resultMap.put(PortalConstants.MSG, "拉取支付信息成功");
resultMap.put(PortalConstants.SYSTEMTIME, DateUtil.getCurrentTime());
return resultMap;
}else{
orderPayInfoMap.put("isOther", "0"); //是否跨号支付
}
}else{
weChatAppConfig = Constants.WECHAT_APP_CONFIG_MAP.get(StringUtil.null2Str("app_client"));
orderPayInfoMap = WeiXinPayUtil.getWeixinAppPayInfo(weChatAppConfig, outTradeNo, openId, notifyURL, orderTotal, body, clientIp);
}
resultMap.put("orderPayInfo", orderPayInfoMap);
paymentType = PaymentType.PAYMENT_TYPE_WECHAT;
weChatConfigId = weChatAppConfig.getConfigId();
}else{
String orderTotal = "";
String notifyURL = RequestUtil.getRequestURL(request) + "/api/order/alipayNotify.msp";
String returnURL = RequestUtil.getRequestURL(request) + "/api/order/alipayNotify.msp";
if(isWebRequest){
//浏览器支付宝支付
String orderPayURL = RequestUtil.getRequestURL(request) + "/wap/pay.html?orderNo=" + outTradeNo;
resultMap.put("orderPayInfo", orderPayURL);
}else{
//客户端支付宝支付
String orderPayInfo = AliPayUtil.getAliPayAppInfo(AliPayUtil.ALI_PAY_CLT_APP_TYPE, outTradeNo, orderTotal, body, notifyURL, returnURL);
resultMap.put("orderPayInfo", orderPayInfo);
}
paymentType = PaymentType.PAYMENT_TYPE_ALIPAY;
}
//记录客户端调用支付方式
OrderPaymentTypeRecord record = new OrderPaymentTypeRecord();
record.setOrderId(orderId);
record.setPaymentType(paymentType);
record.setWeChatConfigId(weChatConfigId);
record.setCreateTime(new Date());
record.setUpdateTime(new Date());
record.setSyncNumber(0);
orderPaymentTypeRecordManager.save(record);
resultMap.put(PortalConstants.CODE, PortalConstants.CODE_SUCCESS);
resultMap.put(PortalConstants.MSG, "拉取支付信息成功");
resultMap.put(PortalConstants.SYSTEMTIME, DateUtil.getCurrentTime());
return resultMap;
}catch(Exception e){
e.printStackTrace();
}
resultMap.put(PortalConstants.CODE, PortalConstants.CODE_ERROR);
resultMap.put(PortalConstants.MSG, this.getText("获取支付失败"));
resultMap.put(PortalConstants.SYSTEMTIME, DateUtil.getCurrentTime());
return resultMap;
}
}
@Controller
@RequestMapping("/api/order/")
public class OrderPayNotifyController extends BaseController{
// 定义锁对象
private static Lock lock = new ReentrantLock();
@Autowired
private OrderManager orderManager;
@Autowired
private OrderByIdCacheManager orderByIdCacheManager;
@Autowired
OrderPaymentTypeRecordManager orderPaymentTypeRecordManager;
/**
* 更新订单支付成功信息
* @param orderId
* @param outTradeNo
* @param transactionId
* @param typeCode
* @return
*/
public static void updateOrderPaymentSuccStatus(Long orderId, String outTradeNo, String transactionId, Integer paymentType, Long weChatConfigId){
}
/**
* 微信支付回调通知
* @param request
* @param response
*/
@RequestMapping(value="/wxpayNotify")
public void wxpayNotify(final HttpServletRequest request, HttpServletResponse response) {
try{
String resultXML = FileIO.inputStream2String(request.getInputStream());
log.info(String.format("weChatPayNotify[XML]==>[%s]", resultXML));
Map<String, Object> notifyObjectMap = XmlParseUtil.xmlCont2Map(resultXML);
if(notifyObjectMap != null
&& notifyObjectMap.size() > 0
&& notifyObjectMap.containsKey("return_code")
&& StringUtil.compareObject("SUCCESS", StringUtil.null2Str(notifyObjectMap.get("return_code")))){
String appid = StringUtil.null2Str(notifyObjectMap.get("appid"));
String transactionId = StringUtil.null2Str(notifyObjectMap.get("transaction_id"));
String outTradeNo = StringUtil.null2Str(notifyObjectMap.get("out_trade_no"));
String sign = StringUtil.null2Str(notifyObjectMap.get("sign"));
//标识微信支付 1 或者 微信公众号支付 5
WeChatAppConfig weChatAppConfig = Constants.WECHAT_APP_ID_MAP.get(appid);
if(weChatAppConfig == null || weChatAppConfig.getAppId() == null){
this.writeTextResponse(response, "<xml><return_code><![CDATA[FAIL]]></return_code></xml>");
return;
}
notifyObjectMap.remove("sign");
String notifySign = WeiXinPayUtil.getNotifySignString(notifyObjectMap, weChatAppConfig.getSecretKey());
// 验证签名是否正确
if(StringUtil.compareObject(notifySign, sign)){
Order order = this.orderManager.getOrderByOrderNo(outTradeNo);
if(order != null && order.getOrderId() != null){
//根据支付流水号获取支付信息
MsgModel<String> msModel = WeiXinPayUtil.getQueryPayInfo(weChatAppConfig, transactionId, null);
int orderTotal = WeiXinPayUtil.orderAmountToBranch(order.getOrderAmount());
if(StringUtil.nullToBoolean(msModel.getIsSucc()) && StringUtil.compareObject(msModel.getData(), orderTotal)){
OrderPayNotifyController.updateOrderPaymentSuccStatus(order.getOrderId(), outTradeNo, transactionId, PaymentType.PAYMENT_TYPE_WECHAT, weChatAppConfig.getConfigId());
//删除订单对应的支付方式记录
orderPaymentTypeRecordManager.deleteByOrderId(order.getOrderId());
this.writeTextResponse(response, "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>");
return;
}
}
}
}
}catch(Exception e){
e.printStackTrace();
}
this.writeTextResponse(response, "<xml><return_code><![CDATA[FAIL]]></return_code></xml>");
}
/**
* 支付宝支付回调通知
* @param request
* @param response
*/
@RequestMapping(value="/alipayNotify")
public void alipayNotify(final HttpServletRequest request, HttpServletResponse response) {
Map<String, String> params = new HashMap<String, String>(); //将异步通知中收到的待验证所有参数都存放到map中
Map<String, String[]> requestParams = request.getParameterMap();
StringBuffer paramBuffer = new StringBuffer();
if(requestParams != null && requestParams.size() > 0){
for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
String name = iter.next();
String[] values = (String[])requestParams.get(name);
String valueStr = "";
for (int i = 0; i < values.length; i++) {
valueStr = i == values.length - 1 ? new StringBuilder().append(valueStr).append(values[i]).toString() : new StringBuilder().append(valueStr).append(values[i]).append(",").toString();
}
params.put(name, valueStr);
paramBuffer.append(new StringBuilder().append(StringUtil.null2Str(name)).append("=").append(StringUtil.null2Str(valueStr)).append(",").toString());
}
}
String out_trade_no = StringUtil.null2Str(params.get("out_trade_no"));
String trade_no = StringUtil.null2Str(params.get("trade_no"));
String trade_status = StringUtil.null2Str(params.get("trade_status"));
log.info(String.format("quickPayment[record]==>[%s]", paramBuffer.toString()));
boolean verify = AliPayUtil.verify(params);
if(verify){
try{
if(trade_status.equals("TRADE_FINISHED") || trade_status.equals("TRADE_SUCCESS")){
Order order = this.orderManager.getOrderByOrderNo(out_trade_no);
if(order != null && order.getOrderId() != null){
//根据支付流水号获取支付信息
MsgModel<String> msModel = AliPayUtil.getQueryAliPayInfo(out_trade_no, trade_no);
if(StringUtil.nullToBoolean(msModel.getIsSucc()) && StringUtil.compareObject(msModel.getData(), StringUtil.formatDouble2Str(order.getOrderAmount()))){
OrderPayNotifyController.updateOrderPaymentSuccStatus(order.getOrderId(), out_trade_no, trade_no, PaymentType.PAYMENT_TYPE_ALIPAY, null);
//删除订单对应的支付方式记录
orderPaymentTypeRecordManager.deleteByOrderId(order.getOrderId());
this.writeTextResponse(response, "success");
return;
}
}
}
}catch(Exception ex){
ex.printStackTrace();
}
}
this.writeTextResponse(response, "fail");
}
}

mark Pay http://git.oschina.net/littleCrazy/dianshangpingtai-zhifu/blob/master/OrderPayController的更多相关文章

  1. 在Ubuntu 16.10 安装 git 并上传代码至 git.oschina.net

    1. 注册一个账号和创建项目 先在git.oschina.net上注册一个账号和新建一个project ,如project name 是"myTest". 2.安装git sudo ...

  2. Git(远程仓库:git@oschina)-V2.0

    1.注册git@osc(也就是“码云”) 这里会提示注册密码==push密码,反正一定要记住的东西.   2.安装git 这里要设置个人信息 git config --list //查看git信息 g ...

  3. 使用GIT@OSChina 实现协同工作的方法。

    由于我新建了一个团队,团队里的人对于GIT都不太熟悉,所以才有了这篇文章.我用的是git-1.9.4的版本,所以我建议团队里面的成员也使用这个版本.首先是下载git,这个自己去网上找吧,一大堆,记得是 ...

  4. 4.代码同时托管到github和git.oschina.net

    我的开源项目托管在Github,同时在Git@OSC也有备份,有两个地方,是不是很麻烦呢?非也非也,下面介绍一下我是怎么做的. 1.先在Github新建一个项目,点击Github主页右上角的加号 -& ...

  5. Windows 下使用git 将代码托管到开源中国-(http://git.oschina.net/)

    一.准备工作 当然是准备在windows 下使用需要的环境,和工具. msysgit  下载地址:http://msysgit.github.io/ TortoiseGit 下载地址:https:// ...

  6. vs2015使用GIt连接git.oschina.net/

    本文转自:http://www.bubuko.com/infodetail-1066588.html.谢谢作者 先安装Git命令行,下载地址:https://github.com/git-for-wi ...

  7. git@oschina.net源代码管理使用日记

    git的优势: 1 可以创建分支: 2 版本控制是基于每一次提交的,而不需要考虑每次提交了多少个文件. 下载: 下载网址为:http://git-scm.com/download,根据您的操作系统选择 ...

  8. git 教程 git.oschina.net

    官方说明:http://git.oschina.net/oschina/git-osc/wikis/%E5%B8%AE%E5%8A%A9#ssh-keys 安装完成后,在开始菜单里找到"Gi ...

  9. 第一次在gitHub上传项目到git.oschina的方法

    首先在Git@osChina创建一个项目仓库 1.创建sshKey公钥 ssh-keygen -t rsa -C "ty635725964@qq.com" 之后连续三个空格,默认无 ...

随机推荐

  1. mobile移动网页开发常用代码模板

    index.html <!DOCTYPE HTML> <html> <head> <!--申明当前页面的编码集--> <meta http-equ ...

  2. mysql存储引擎简析

    一.常见存储引擎特性 Innodb 具有提交.回滚和崩溃恢复能力的事务安全.支持外键.使用mvcc以及行锁来提供事务支持,因此支持高并发.适用于写频繁,并发率高的应用. Myisam 不支持事务和灾难 ...

  3. 优秀web资源

    http://www.filewatcher.com 一步一步asp.net_页面静态化管理 http://www.cnblogs.com/ylwn817/articles/2006923.html ...

  4. 页面回到顶部的三种实现(锚标记,js)

    一.使用锚标记返回页面顶部 使用HTML锚标记最简单,就是看起来有点不好看,点击后会在地址栏显示这个锚标记,其它的倒没什么. 页面顶部放置: <a name="top" id ...

  5. Java动态加载属性文件.properties

    当我们使用如下语句加载.properties时: ClassLoader classLoader = this.getClass().getClassLoader(); Properties prop ...

  6. ionic 项目中 使用 sass

    注: 1.先安装node-sass  -->> npm install --save node-sass --registry=https://registry.npm.taobao.or ...

  7. SettingsTortoiseSVN

      迁移时间:2017年5月20日11:16:05CreateTime--2016年9月18日18:20:15Author:Marydon在windows下安装SVN软件 说明:64位的系统只能安装6 ...

  8. SettingsNotePad++

      迁移时间:2017年5月20日11:04:46Author:Marydon官网下载 http://notepad-plus.en.softonic.com/1.修改字体大小及背景色 设置--> ...

  9. API密钥

    什么是API密钥? 答:在api调用时,用来按照指定规则对您的请求参数进行签名,服务器收到你的请求时会进行签名验证,即可以界定你的身份也可以防止其他人通过某种手段恶意篡改你的请求数据. 密钥的使用? ...

  10. Redis学习(1)--环境配置,安装JDK,MySQL,tomcat

    Linux上安装jdk,mysql,tomcat安装 rpm命令: 相当于Windows的安装/卸载程序.可以进行程序的安装,更新,卸载,查看. 本地程序安装:rpm -ivh 程序名 本地程序查看: ...