mark Pay http://git.oschina.net/littleCrazy/dianshangpingtai-zhifu/blob/master/OrderPayController

@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");

}

}
时间: 2024-08-11 01:20:21

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

使用Git上传本地项目到http://git.oschina.net

本文前言,因倡导开源精神,我也把代码传上了开源社区,可是,当初使用http://git.oschina.net 网站上传代码的时候不知道使用工具.我竟然一个文件一个文件复制粘贴,可费了我好大一个劲儿,那时在想,如果一但写的项目多,文件数量多的话,那不是要COPY死去?觉得这样完全不合理,就搜了一下“如何快速上传代码到开源社区”,哇!好LOW的关键词了,但还好用.果然让我搜到了,原来可以使用git工具来上传,这下可解放了我复制粘贴的动作了,越来越喜欢上用git了. 下面是我搜的比较好的一篇文章,照

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

先安装Git命令行,下载地址:https://github.com/git-for-windows/git/releases/tag/v2.5.0.windows.1 看自己的系统是23位还是64位下载对应的安装程序. 安装Git Extensions,下载地址:http://sourceforge.net/projects/gitextensions/ ,安装过程中会有两个勾选项msysGit 和 KDiff3,如果已经安装了Git命令行可以不勾选msysGit,KDiff3 是一个文件对比工

Markdown语法学习(Github/git.oschina.net上README.md书写规范)(转)

晚上在更新git.oschina.net项目时,突然想知道README.md后缀的来源,于是搜了下,发现README.md使用了一种小标记语言Markdown的语法,于是简单的看了一个,特转载如下,为了下次参考方便(http://blog.csdn.net/kaitiren/article/details/38513715也不错).中文参考手册可以参考http://wowubuntu.com/markdown/index.html Markdown 的目标是实现「易读易写」,兼容HTML. 但是

git.oschina.net账户中快速添加SSH公钥【Cygwin环境】

>>ssh-keygen         生成ssh公钥 >>Enter3次                 确定 >>cat ~/.ssh/id_rsa.pub 查看公钥 >>复制公钥文本到git.oschina.net的账户SSH公钥文本框中 >>保存即可

git.oschina上 git 初体验

git 初体验 第一步:进入开源中国社区 oschinagit http://git.oschina.net/,用您的邮箱注册一个账号. 第二步,在计算机上安装一个gitbush 安装好以后,点击进入,它是长这个样子的 第三步:在您的计算机上获取SSH(为了方便以后使用git时不用每次都输入用户名和密码) 输入命令行: ssh-keygen -t rsa -C "[email protected]" 上述"[email protected]"是注册oschinagi

快速提交代码到git.oschina.net之设置.gitignore

大家都知道 新建一个 Git 项目的时候 会有一个配置文件叫 .gitignore ,如图所示 这个文件是干嘛的呢? 这就是今天的主角,它的作用是提交你的项目.工程文件时,设置要忽略的一部分文件或文件夹的规则,从而 避免 提交一些不必要的文件比如 .classpath,settings目录等的东西到代码仓库,给其他开发人员 同步代码的时候 由于这些 .classpath,settings 等和之前设置不一样 带来的麻烦. 这里以 http://git.oschina.net/Gelopa/Mic

(Git)使用Git在git.oschina上面管理代码

代码托管地址为:https://git.oschina.net 想要在git.oschina上面托管代码,首先需要https://git.oschina.net上面注册一个帐号,然后就可以创建项目或者参与别人的协同项目开发. 1.在https://git.oschina.net注册一个帐号. 2.添加ssh公钥,使用SSH公钥可以让你在你的电脑和[email protected]通讯的时候使用安全连接(git的remote要使用SSH地址). 3.新建一个项目,具体步骤如下图所示: 4.添加SS

yispider 开源小说采集器 (来源http://git.oschina.net/yispider/yispider 我的修改版因为他的无法真正跑差很多东西)

我的git地址  http://git.oschina.net/yangdc/yispider 小说采集器 版权声明:本文为博主原创文章,未经博主允许不得转载.

jenkins使用Git为源码管理(windows master && linux slave)

作为一个不太经常总结的人,工作以来碰到过太多问题!往往解决之后没有有效记录,导致再次碰到需要重新查资料解决.现在改变下习惯,努力搞的了技术. 公司最近提倡开源(以前啥都机密,即使开源也没改变多少),代码从SVN迁移至Git管理,这样导致原来记录项目日志的wiki持续集成job需要重新配置.之前的设置为:每分钟检查SVN变化,有变化就执行编译发布.改为Git后,碰到坑坑洼洼,一并列在下面. jenkins的Git插件安装 git plugin,git client plugin,github pl