All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.mingsoft.pay.action.web.PayAction Maven / Gradle / Ivy

/**
 * Copyright (c) 2012-present 铭软科技(mingsoft.net)
 * 本软件及相关文档文件(以下简称“软件”)的版权归 铭软科技 所有
 * 遵循 铭软科技《服务协议》中的《保密条款》
 */

package net.mingsoft.pay.action.web;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.mingsoft.base.entity.ResultData;
import net.mingsoft.basic.util.BasicUtil;
import net.mingsoft.basic.util.StringUtil;
import net.mingsoft.pay.action.BaseAction;
import net.mingsoft.pay.bean.PayBean;
import net.mingsoft.pay.biz.IPayLogBiz;
import net.mingsoft.pay.constant.Const;
import net.mingsoft.pay.entity.PayLogEntity;
import net.mingsoft.pay.entity.PayLogEntity.LogStatusEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.Map;

/**
 * 支付转发接口
 *
 * @author 铭飞开发团队
 * @version 版本号:100-000-000
* 创建日期:2017年11月24日
* 历史修订:
*/ @Api(tags = {"前端-支付模块接口"}) @Controller("webPayAction") @RequestMapping("/mpay/pay") public class PayAction extends BaseAction { /** * 交易记录 */ @Autowired private IPayLogBiz payLogBiz; @ApiOperation(value = "支付接口") @ApiImplicitParams({ @ApiImplicitParam(name = "orderName", value = "订单标题", required = true, paramType = "query"), @ApiImplicitParam(name = "type", value = "支付类型 weixin:微信 weixin_h5:微信h5 weixin_app: 微信app alipay:支付宝 alipay_app: app支付宝", required = true, paramType = "query"), @ApiImplicitParam(name = "orderPrice", value = "订单价格,单位元", required = true, paramType = "query"), @ApiImplicitParam(name = "orderDesc", value = "订单描述", required = true, paramType = "query"), @ApiImplicitParam(name = "notifyUrl", value = "接口异步请求地址绝对地址, 适用于:(支付宝支付、微信公众支付、微信扫码支付) 支付宝默认回调地址:mpay/alipay/notify.do, 微信默认回调:mapy/weixin/notify.do 开发者根据实际情况可以自定义回调地址", required = false, paramType = "query"), @ApiImplicitParam(name = "orderNo", value = "订单编号,不传时会自动生成", required = false, paramType = "query"), @ApiImplicitParam(name = "returnUrl", value = "返回地址绝对地址(支付宝必填)", required = false, paramType = "query"), @ApiImplicitParam(name = "attach", value = "公用回传参数也就是自定义扩展参数,例如:店名、多个订单编号集合等等(本参数必须进行UrlEncode之后才可以发送给支付宝)", required = false, paramType = "query"), @ApiImplicitParam(name = "authCode", value = "授权码(微信扫描支付必填)", required = false, paramType = "query"), }) @RequestMapping("/gateway") public String gateway(@ApiIgnore PayBean pay, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException { if (ObjectUtil.isNull(pay)) { return null; } String requestUrlParams = BasicUtil.assemblyRequestUrlParams(); LOG.debug("接收的参数:{}", JSONUtil.toJsonStr(pay)); //保存交易日志 PayLogEntity payLog = new PayLogEntity(); if (StringUtils.isEmpty(pay.getOrderNo())) { payLog.setOrderNo(StringUtil.getDateSimpleStr() + RandomUtil.randomNumbers(4)); pay.setOrderNo(payLog.getOrderNo()); } else { payLog.setOrderNo(pay.getOrderNo()); } PayLogEntity payLogEntity = (PayLogEntity) payLogBiz.getEntity(payLog); //交易记录存在就不需要添加记录 if (ObjectUtil.isNull(payLogEntity)) { payLog.setLogMoney(Double.valueOf(pay.getOrderPrice())); payLog.setLogStatus(LogStatusEnum.UN_PAY.toString()); payLog.setLogTitle(pay.getOrderName()); LOG.debug("pay.getAttach():{}", pay.getAttach()); if (!StringUtils.isBlank(pay.getAttach())) { Map map = JSONUtil.toBean(pay.getAttach(), Map.class); payLog.setPeopleId(String.valueOf(map.get(Const.Attach.USER_ID))); LOG.debug("USER_ID:{}", map.get(Const.Attach.USER_ID)); if (!ObjectUtil.isNotNull(map.get(Const.Attach.USER_ID))) { payLog.setPeopleId(map.get(Const.Attach.USER_ID)); } } payLog.setCreateDate(new Date()); payLogBiz.saveEntity(payLog); } // 这里重新组织参数为了获取前端不传orderNo,当前类中自动生成 // 使用forward转发是获取不到当前类中拼接的orderNo String parms = URLEncoder.encode(requestUrlParams, "UTF-8"); parms = parms.replace("%3D", "="); parms = parms.replace("%26", "&"); if (pay.getType().equals(PayBean.Type.WEIXIN) || pay.getType().equals(PayBean.Type.WEIXIN_H5) || pay.getType().equals(PayBean.Type.WEIXIN_APP)) { // 微信支付 if(StringUtils.isBlank(pay.getNotifyUrl())) { pay.setNotifyUrl(BasicUtil.getUrl().concat(Const.WEIXIN_NOTIFY_URL)); } request.setAttribute("pay", pay); return "forward:" + Const.WEIXIN_PAY_URL + "?" + parms; } else if (pay.getType().equals(PayBean.Type.ALIPAY)) { // 支付宝支付 if(StringUtils.isBlank(pay.getNotifyUrl())) { pay.setNotifyUrl(BasicUtil.getUrl().concat(Const.ALIPAY_NOTIFY_URL)); } request.setAttribute("pay", pay); return "forward:" + Const.ALIPAY_PAY_URL + "?" + parms; } else if (pay.getType().equals(PayBean.Type.ALIPAY_APP)) { // 支付宝app支付 if(StringUtils.isBlank(pay.getNotifyUrl())) { pay.setNotifyUrl(BasicUtil.getUrl().concat(Const.ALIPAY_NOTIFY_URL)); } request.setAttribute("pay", pay); return "forward:" + Const.ALIPAY_APP_PAY_URL + "?" + parms; } LOG.debug("无匹配类型:type{}", pay.getType()); return null; } @ApiOperation(value = "订单状态查询接口") @ApiImplicitParams({ @ApiImplicitParam(name = "orderNo", value = "订单编号", required = false, paramType = "query") }) @ResponseBody @GetMapping("/status") public ResultData status(String orderNo, HttpServletRequest request, HttpServletResponse response){ if (StringUtils.isBlank(orderNo)){ return ResultData.build().error(getResString("err.empty",this.getResString("order.no"))); } LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(PayLogEntity::getOrderNo,orderNo); PayLogEntity payLog = payLogBiz.getOne(wrapper, true); if (payLog==null){ return ResultData.build().error(getResString("err.empty",this.getResString("order.no"))); } return ResultData.build().success(payLog.getLogStatus()); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy