@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. WPF DataTrigger的两个用法

    1.用在textbox等输入控件上,验证输入是否合法.首先定义一个Converter, 2.Xaml里面这样子写,意思是输入的数字,如果小于100则显示为红色. 3.combox等列表控件里面,也可以 ...

  2. Nuget添加新项目的问题

    为已有的几个项目添加了一个nuget package 后,在解决方法中添加了一个新项目,然后想把这个nuget package添加到这个新建的项目中去,可以此时无法添加.     怎么办那? [解决方 ...

  3. MyEclipse中使用Junit插件进行单元测试

    http://wenku.baidu.com/view/ec904a116bd97f192279e9a6.html

  4. android studio中使用adb wifi插件无线调试程序

    使用android studio中使用adb wifi插件无线调试程序的前提条件电脑和手机在同一个无线网 1.下载adb wifi插件 File->Settings->Plugins Br ...

  5. ZH奶酪:JavaScript中的JSON.stringify() and JSON.parse()

    JSON.stringify() JSON.stringify()可以将任意的JavaScript值序列化成JSON字符串. 语法 JSON.stringify(value[, replacer [, ...

  6. c的链接详解

    多目标文件的链接 stack.c #include <stdio.h> #define STACKSIZE 1000 typedef struct stack { int data[STA ...

  7. $nextTick 宏任务 微任务 macrotasks microtasks

    1.nextTick调用方法 首先看nextTick的调用方法: https://cn.vuejs.org/v2/api/#Vue-nextTick // 修改数据 vm.msg = 'Hello' ...

  8. jquery 保留两个小数的方法

    $()); 直接使用:toFixed(2)

  9. html中文乱码问题的解决

    当我试着用html写代码的时候,发现直接保存的文件用浏览器打开时中文显示是乱码的,所以我找了一些解决方法,可是原因不太明白,所以我也就不解释了,能够自己找找原因,以下提供解决方法: 在写的html的第 ...

  10. ValueError: Expecting property name: line 1 column 1 (char 1)

    # -*- coding: cp936 -*- #xiaodeng #python 2.7.10 import weibo s='{"name":"xiaodeng&qu ...