亲测全套支付可用(微信支付宝)复制粘贴即可跑

亲测全套支付可用(微信支付宝包括退款)复制粘贴即可跑



/**
 * @Auther: tjr
 * @Date: 2020/9/30 15:11
 * @Description: 支付宝后台接口
 */
@Slf4j
@RestController
@RequestMapping(value = "/pay")
@Api(value="支付", tags="支付模块")
public class PayController {
    @Autowired
    AliPayService aliPayService;
    @Autowired
    WxPayService wxPayService;
    @Autowired
    CouOrderService couOrderService;
    @Autowired
    CouOrderMapper couOrderMapper;
    @Autowired
    CouUserMapper couUserMapper;


    @ApiOperation(value = "统一微信公众号支付接口")
    @PostMapping("/jspaPay")
    public CommonResult jspaPay(@RequestBody Request<AliWxPayEntity> request, HttpServletRequest httpServletRequest) {
        if (request == null) {
            return CommonResult.validateFailed();
        }
        AliWxPayEntity entity = JSON.parseObject(JSON.toJSONString(request.getData()), AliWxPayEntity.class);
        if (entity.getMoney() == null|| entity.getPayStatus() == null ) {

        }
        if (entity.getMoney() == null || entity.getPayStatus() == null) {
            return CommonResult.validateFailed();
        }
        if (entity.getPayStatus() == 1) {
            WxPayEntity wxPayEntity = new WxPayEntity();
            String ip = HttpUtils.getRemoteHost(httpServletRequest);
            wxPayEntity.setOrderNo(entity.getOdd());
            wxPayEntity.setTotalMoney(entity.getMoney());
            wxPayEntity.setIp(ip);
            CouUser couUser = couUserMapper.selectById(entity.getUserId());
            if (couUser==null){
                return CommonResult.failed("找不到该用户");
            }
            if (StringUtil.isBlank(couUser.getOpenId())){
                return CommonResult.failed("该用户暂未绑定微信,没有openid");
            }
            wxPayEntity.setOpenid(couUser.getOpenId());
            //微信支付
            ResultMap resultMap = wxPayService.JSAPIPay(wxPayEntity);
            PayEntity payEntity = new PayEntity();
            payEntity.setNum(entity.getOdd());
            payEntity.setResultMap(resultMap);
            return CommonResult.success(payEntity);
        }

        return CommonResult.failed("下单失败");
    }

        /**
         * app支付接口-统一下单支付接口
         *
         * @return
         * @throws AlipayApiException
         */
    @ApiOperation(value = "统一app支付接口")
    @PostMapping("/appPay")
    public CommonResult appPay(@RequestBody Request<AliWxPayEntity> request, HttpServletRequest httpServletRequest) {
        if (request == null) {
            return CommonResult.validateFailed();
        }
        AliWxPayEntity entity = JSON.parseObject(JSON.toJSONString(request.getData()), AliWxPayEntity.class);
        if (entity.getMoney() == null|| entity.getPayStatus() == null ) {

        }
        if (entity.getMoney() == null || entity.getPayStatus() == null) {
            return CommonResult.validateFailed();
        }

            if (entity.getPayStatus() == 2) {
                //发起支付,支付宝
                PayOrderNo payOrderNo = new PayOrderNo();
                payOrderNo.setTotalFee(String.valueOf((entity.getMoney().multiply(new BigDecimal(100)).intValue())));//分
                //设置订单号
                payOrderNo.setOrderNo(entity.getOdd());
                //获取支付
                AlipayTradeAppPayResponse response = aliPayService.tradeAppPay(payOrderNo);
                if (response.isSuccess()) {
                    log.debug("调用成功" + response.getBody());
                    PayEntity payEntity = new PayEntity();
                    payEntity.setNum(entity.getOdd());
                    payEntity.setPay(response.getBody());
                    return CommonResult.success(payEntity);
                } else {
                    log.error("调用失败" + response.getBody());
                    return CommonResult.failed(response.getBody());
                }
            }
            if (entity.getPayStatus() == 1) {
                WxPayEntity wxPayEntity = new WxPayEntity();
                String ip = HttpUtils.getRemoteHost(httpServletRequest);
                wxPayEntity.setOrderNo(entity.getOdd());
                wxPayEntity.setTotalMoney(entity.getMoney());
                wxPayEntity.setIp(ip);
                //微信支付
                ResultMap resultMap = wxPayService.unifiedOrder(wxPayEntity);
                PayEntity payEntity = new PayEntity();
                payEntity.setNum(entity.getOdd());
                payEntity.setResultMap(resultMap);
                return CommonResult.success(payEntity);
            }

        return CommonResult.failed("下单失败");
    }
        /**
         *   支付网站扫码支付接口-统一下单支付接口方法
         * @author tujr
         * @createTime 2020/8/27 0027 10:09
         **/

        @ApiOperation(value="统一网站支付接口")
        @PostMapping("/nativePay")
        public CommonResult nativePay(@RequestBody Request<AliWxPayEntity> request, HttpServletRequest httpServletRequest, HttpServletResponse response) throws Exception {
            if (request==null){
                return CommonResult.validateFailed();
            }
            AliWxPayEntity entity = JSON.parseObject(JSON.toJSONString(request.getData()), AliWxPayEntity.class);
            if (entity.getMoney()==null ||entity.getPayStatus()==null||entity.getPayStatus()<1||entity.getPayStatus()>2){
                return CommonResult.validateFailed();
            }

                if (entity.getPayStatus()==2) {
                    AliPayEntity aliPayEntity = new AliPayEntity();
                    aliPayEntity.setMoney(entity.getMoney());
                    aliPayEntity.setOrderNo(entity.getOdd());
                    aliPayService.alipayPay(aliPayEntity,response);
                }
                if (entity.getPayStatus()==1){
                    WxPayEntity wxPayEntity = new WxPayEntity();
                    String ip = HttpUtils.getRemoteHost(httpServletRequest);
                    wxPayEntity.setOrderNo(entity.getOdd());
                    wxPayEntity.setTotalMoney(entity.getMoney());
                    wxPayEntity.setIp(ip);
                    //微信支付
                    wxPayService.nativePay(wxPayEntity,httpServletRequest,response);
                }
            return CommonResult.failed("下单失败");
        }
        @ApiOperation(value="轮询查状态接口")
        @PostMapping("/selectPayStatus")
        public CommonResult selectPayStatus(@RequestBody Request<AliPayStatusEntity> request) {
            return aliPayService.selectPayStatus(request);
        }
    }

代码地址

https://download.csdn.net/download/weixin_43285931/12849640

你可能感兴趣的:(支付,java)