package com.stylefeng.guns.modular.account.controller; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.stylefeng.guns.core.util.GetOpenBodySig; import com.stylefeng.guns.core.util.NotifyDemo; import com.stylefeng.guns.modular.CharteredCar.server.IOrderCharteredCarService; import com.stylefeng.guns.modular.account.model.TEnterpriseWithdrawal; import com.stylefeng.guns.modular.account.server.ITEnterpriseWithdrawalService; import com.stylefeng.guns.modular.account.server.UserWithdrawalService; import com.stylefeng.guns.modular.account.util.QrCodeConfiguration; import com.stylefeng.guns.modular.account.util.Util; import com.stylefeng.guns.modular.crossCity.server.IOrderCrossCityService; import com.stylefeng.guns.modular.smallLogistics.server.IOrderLogisticsService; import com.stylefeng.guns.modular.specialTrain.server.IOrderPrivateCarService; import com.stylefeng.guns.modular.system.model.Company; import com.stylefeng.guns.modular.system.model.Driver; import com.stylefeng.guns.modular.system.model.UserInfo; import com.stylefeng.guns.modular.system.model.UserWithdrawal; import com.stylefeng.guns.modular.system.service.ICompanyService; import com.stylefeng.guns.modular.system.service.IDriverService; import com.stylefeng.guns.modular.system.service.ITaxiCardService; import com.stylefeng.guns.modular.system.service.IUserInfoService; import com.stylefeng.guns.modular.system.util.ResultUtil; import com.stylefeng.guns.modular.taxi.service.IOrderTaxiService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.time.DateFormatUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.math.BigDecimal; import java.util.Date; import java.util.List; import java.util.Map; /** * @program: openPlatform * @description: 下单 */ @Api(tags = "下单") @Slf4j @RestController @RequestMapping("/api/placeOrder") public class AppOrderController { @Autowired private IOrderCharteredCarService orderCharteredCarService; @Autowired private IOrderCrossCityService orderCrossCityService; @Autowired private IOrderLogisticsService orderLogisticsService; @Autowired private IOrderPrivateCarService orderPrivateCarService; @Autowired private IOrderTaxiService orderTaxiService; @Autowired private IUserInfoService userInfoService; @Autowired private ITaxiCardService taxiCardService; @Autowired private ITEnterpriseWithdrawalService enterpriseWithdrawalService; private final ICompanyService companyService; private final QrCodeConfiguration qrCodeConfiguration; private final UserWithdrawalService userWithdrawalService; public AppOrderController(ICompanyService companyService, QrCodeConfiguration qrCodeConfiguration, UserWithdrawalService userWithdrawalService) { this.companyService = companyService; this.qrCodeConfiguration = qrCodeConfiguration; this.userWithdrawalService = userWithdrawalService; } /** * app下单 * * @throws Exception */ @ResponseBody @RequestMapping(value = "/appPay", method = RequestMethod.POST) @ApiOperation(value = "用户下单", tags = {"用户端-用户下单"}, notes = "") @ApiImplicitParams({ @ApiImplicitParam(name = "Authorization", value = "Bearer +token", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."), @ApiImplicitParam(value = "支付金额", name = "amount", required = true, dataType = "BigDecimal"), @ApiImplicitParam(value = "支付方式 1=微信,2=支付宝,4=云闪付", name = "type", required = true, dataType = "Integer"), @ApiImplicitParam(value = "订单id", name = "orderId", required = true, dataType = "Integer"), @ApiImplicitParam(value = "0感谢费 不用管 1专车OrderPrivateCar取消 2跨城感谢费 3跨成订单OrderCrossCity 4跨城取消订单 orderCrossCity 5小件物流OrderLogistics 6专车OrderPrivateCar 7用户余额充值 8出租车取消 OrderTaxi 9出租车订单OrderTaxi" + "10 小件物流感谢费 11专车感谢费 12出租车感谢费" + "", name = "types", required = true, dataType = "Integer"), }) public ResultUtil placeAnOrder(BigDecimal amount, Integer type,Integer orderId,Integer types,Integer paymentId) throws Exception { JSONObject json = new JSONObject(); json.put("requestTimestamp", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss")); // 报文请求时间 json.put("merOrderId", Util.getMerOrderId(qrCodeConfiguration.getMsgSrcId())); // 商户订单号 json.put("mid", "898150841210108"); // 商户号 json.put("tid", qrCodeConfiguration.getTid()); // 终端号 json.put("instMid", qrCodeConfiguration.getInstMid()); // 业务类型 BigDecimal multiply = amount.multiply(new BigDecimal(100)); int money = multiply.intValue(); json.put("totalAmount",money); // 支付总金额 json.put("orderDesc", ""); // 订单描述 json.put("subAppId", "wxasdasdasdasdads"); json.put("notifyUrl", "https://zhentonggongsi.com:443/user/api/placeOrder/notifyUrl"); json.put("tradeType", "APP"); // 交易类型,微信必传 json.put("srcReserve", orderId+"_"+types+"_"+paymentId); // 交易类型,微信必传 System.err.println("请求报文json:\n" + json); String url; if(1 == type){ // url = "https://test-api-open.chinaums.com/v1/netpay/wx/app-pre-order";// 测试 url = "https://api-mop.chinaums.com/v1/netpay/wx/app-pre-order"; }else if (2 == type){ // url = "https://test-api-open.chinaums.com/v1/netpay/trade/precreate";// 测试 url = "https://api-mop.chinaums.com/v1/netpay/trade/precreate"; }else { // url = "https://test-api-open.chinaums.com/v1/netpay/uac/app-order";// 测试 url = "https://api-mop.chinaums.com/v1/netpay/uac/app-order"; } //OPEN-BODY-SIG 方式 String result = GetOpenBodySig.sendOpenBodySig(url, json.toString()); System.err.println("响应报文json:\n" + result); JSONObject jsonObject = JSONObject.parseObject(result); Object appPayRequest = jsonObject.get("appPayRequest"); System.out.println(appPayRequest); return ResultUtil.success(appPayRequest); } public ResultUtil cancleMoney(BigDecimal amount) throws Exception { JSONObject json = new JSONObject(); json.put("requestTimestamp", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss")); // 报文请求时间 json.put("merOrderId", Util.getMerOrderId(qrCodeConfiguration.getMsgSrcId())); // 商户订单号 json.put("mid", "898150841210108"); // 商户号 json.put("tid", qrCodeConfiguration.getTid()); // 终端号 json.put("instMid", qrCodeConfiguration.getInstMid()); // 业务类型 BigDecimal multiply = amount.multiply(new BigDecimal(100)); int money = multiply.intValue(); json.put("refundAmount",money); // 支付总金额 System.err.println("请求报文json:\n" + json); String url = "https://test-api-open.chinaums.com/v1/netpay/refund"; // String url = "https://api-mop.chinaums.com/v1/netpay/refund"; //OPEN-BODY-SIG 方式 String result = GetOpenBodySig.sendOpenBodySig(url, json.toString()); System.err.println("响应报文json:\n" + result); JSONObject jsonObject = JSONObject.parseObject(result); Object appPayRequest = jsonObject.get("appPayRequest"); System.out.println(appPayRequest); return ResultUtil.success(appPayRequest); } public static void main(String[] args) throws Exception { JSONObject json = new JSONObject(); json.put("requestTimestamp", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss")); // 报文请求时间 json.put("merOrderId", "34U02023062418175413584261700"); // 商户订单号 json.put("mid", "898150841210108"); // 商户号 json.put("tid", "W4U185RJ"); // 终端号 json.put("instMid", "QRPAYDEFAULT"); // 业务类型 BigDecimal multiply = new BigDecimal(1); int money = multiply.intValue(); json.put("refundAmount",money); // 支付总金额 System.err.println("请求报文json:\n" + json); // String url = "https://test-api-open.chinaums.com/v1/netpay/refund"; String url = "https://api-mop.chinaums.com/v1/netpay/refund"; //OPEN-BODY-SIG 方式 String result = GetOpenBodySig.sendOpenBodySig(url, json.toString()); System.err.println("响应报文json:\n" + result); JSONObject jsonObject = JSONObject.parseObject(result); Object appPayRequest = jsonObject.get("appPayRequest"); System.out.println(appPayRequest); } @PostMapping(value = "/notifyUrl") @Transactional(rollbackFor = Exception.class,propagation = Propagation.SUPPORTS) public void notifyUrl(HttpServletRequest request,HttpServletResponse response) throws Exception { System.out.println("------用户端充值回调----------"); // 接收参数 Map params = NotifyDemo.getRequestParams(request); System.err.println("params:" + params); String sign = params.get("sign");// 签名 System.err.println("============================"+sign); String billStatus = params.get("status");// 状态 String targetOrderId = params.get("targetOrderId"); // 流水号 Integer totalAmount = Integer.valueOf(params.get("totalAmount")); // 总金额 Integer buyerPayAmount = Integer.valueOf(params.get("buyerPayAmount")); // 支付金额 Integer couponAmount = Integer.valueOf(params.get("couponAmount"));// 优惠金额 String srcReserve = params.get("srcReverse");// 预留字段 // 验签 //对通知内容生成sign String strSign = NotifyDemo.makeSign(qrCodeConfiguration.getMd5key(),params); System.out.println(billStatus); //判断签名是否相等 if (sign.equals(strSign)) { if("TRADE_SUCCESS".equals(billStatus)){ String[] split = srcReserve.split("_"); /* 1专车取消订单 2组老模式OrderCall 3跨成订单OrderCrossCity 4跨城取消订单 orderCrossCity 5小件物流OrderLogistics 6专车OrderPrivateCar 7用户余额充值 8出租车取消 OrderTaxi 9出租车订单OrderTaxi */ Integer integer = Integer.valueOf(split[1]); Integer orderId = Integer.valueOf(split[0]); Integer paymentId = Integer.valueOf(split[2]); System.out.println("订单类型:"+integer); System.out.println("预留:"+srcReserve); if(integer==3){ orderCrossCityService.payOrderCrossCityCallback(orderId,"",4,targetOrderId); } if(integer==4){ orderCrossCityService.payCancelOrderCrossCity(orderId,"",4); } if(integer==5){ orderLogisticsService.payOrderLogisticsCallback(orderId,",",4); } if(integer==6){ orderPrivateCarService.payOrderPrivateCarCallback(orderId,",",4); } if(integer==9){ orderTaxiService.payOrderTaxiCallback(orderId,"",4,paymentId); } if(integer==1){ orderPrivateCarService.payCancelOrderPrivateCar(orderId,",",4); } if(integer==8){ orderTaxiService.payCancelOrderTaxi(orderId,"",4); } if(integer==2){ orderCrossCityService.payThankYouFeeCallback(orderId,targetOrderId,4); } if(integer==10){ orderLogisticsService.payThankYouFeeCallback(orderId,targetOrderId,4); } if(integer==11){ orderPrivateCarService.payThankYouFeeCallback(orderId,targetOrderId,4); } if(integer==12){ orderTaxiService.payThankYouFeeCallback(orderId,targetOrderId,4); } if(integer==13){ //补差价 orderLogisticsService.payOrderLogisticsSpreadCallback(orderId,targetOrderId,4); } if(integer==14){ taxiCardService.payTaxiCardPaymentSpread(orderId,targetOrderId); } // 调用支付充值接口,将金额充值到平台账户 // 查询平台账户 Company company = companyService.selectOne(new EntityWrapper() .eq("type", 1) .last("LIMIT 1")); // 查询平台开户信息 TEnterpriseWithdrawal enterpriseWithdrawal = enterpriseWithdrawalService.selectOne(new EntityWrapper() .eq("companyId", company.getId())); // TODO 替换为公司id // DepositReq depositReq = new DepositReq(); if(integer==7){ // 充值回调 userInfoService.payCancelUserBalance(orderId,"",paymentId,4); UserInfo userInfo = userInfoService.selectById(orderId); UserWithdrawal driverWith = userWithdrawalService.selectOne(new EntityWrapper() .eq("phone", userInfo.getPhone()) .last("LIMIT 1")); createAllocationReqUser(1,orderId,1,new BigDecimal(totalAmount).divide(new BigDecimal(100)).doubleValue()); // depositReq.setOutOrderNo(ToolUtil.getRandomString(32)); // depositReq.setTotalAmount(totalAmount); // 总金额 // depositReq.setAmount(buyerPayAmount); // 支付金额 // depositReq.setDiscountAmount(couponAmount); // 优惠金额 // depositReq.setBalanceAcctId(driverWith.getBalanceAcctId()); // depositReq.setDepositType("1"); // depositReq.setPaymentType(PaymentTypeEnum.CUP_APP.getCode()); // depositReq.setPaymentTradeNo(targetOrderId); // 系统交易流水号 // depositReq.setPaymentSucceededAt(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").format(new Date())); // depositReq.setOrderNo(targetOrderId); // depositReq.setOrderAmount(Long.valueOf(totalAmount)); // depositReq.setProductName("用户支付充值"); // depositReq.setProductCount(1); } // depositReq.setOutOrderNo(ToolUtil.getRandomString(32)); // depositReq.setTotalAmount(totalAmount); // 总金额 // depositReq.setAmount(buyerPayAmount); // 支付金额 // depositReq.setDiscountAmount(couponAmount); // 优惠金额 // depositReq.setBalanceAcctId(enterpriseWithdrawal.getBalanceAcctId()); // depositReq.setDepositType("1"); // depositReq.setPaymentType(PaymentTypeEnum.CUP_APP.getCode()); // depositReq.setPaymentTradeNo(targetOrderId); // 系统交易流水号 // depositReq.setPaymentSucceededAt(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").format(new Date())); // depositReq.setOrderNo(targetOrderId); // depositReq.setOrderAmount(Long.valueOf(totalAmount)); // depositReq.setProductName("用户支付充值"); // depositReq.setProductCount(1); try { // DepositResp depositResp = DepositExample.create(depositReq); // System.err.println(depositResp); // log.info("支付充值成功:{}",depositResp); } catch (Exception e) { e.printStackTrace(); log.error("支付充值失败:{}", e.getMessage()); PrintWriter writer = response.getWriter(); response.setStatus(200); writer.write("FAILED"); } } // 收到通知后记得返回SUCCESS System.out.println("验签通过"); PrintWriter writer = response.getWriter(); response.setStatus(200); writer.write("SUCCESS"); } else { log.error("验签未通过!"); System.out.println("验签未通过!"); PrintWriter writer = response.getWriter(); response.setStatus(200); writer.write("FAILED"); } //跳转支付完成页面 // return "/WEB-INF/views/jsp/payResult.jsp"; } @RequestMapping(value = "/notifyUrlOne", method = RequestMethod.POST) public void notifyUrl(String event_id,String event_type, Map event_data, String created_at,HttpServletResponse response){ System.out.println("支付充值回调+++++++++"); System.out.println(event_id); System.out.println(event_type); System.out.println(event_data); System.out.println(created_at); try { PrintWriter writer = response.getWriter(); writer.print(200); } catch (IOException e) { e.printStackTrace(); } } public String createAllocationReqUser(Integer companyId,Integer userId,int type,double money) { List tEnterpriseWithdrawals = enterpriseWithdrawalService.selectList(new EntityWrapper().eq("companyId", companyId)); UserInfo tUser = userInfoService.selectById(userId); List userWithdrawals = userWithdrawalService.selectList(new EntityWrapper().eq("phone", tUser.getPhone())); if (userWithdrawals.size() == 0) { return "error"; } // AllocationReq allocationReq= new AllocationReq(); // allocationReq.setOutOrderNo(ToolUtil.getRandomString(32)); // if(type==1){ // allocationReq.setPayBalanceAcctId(tEnterpriseWithdrawals.get(0).getBalanceAcctId()); // allocationReq.setRecvBalanceAcctId(userWithdrawals.get(0).getBalanceAcctId()); // // allocationReq.setPassword(Base64Util.decode(tEnterpriseWithdrawals.get(0).getTransactionAuthorizationCode())); // }else { // allocationReq.setRecvBalanceAcctId(tEnterpriseWithdrawals.get(0).getBalanceAcctId()); // allocationReq.setPayBalanceAcctId(userWithdrawals.get(0).getBalanceAcctId()); // allocationReq.setPassword(Base64Util.decode(userWithdrawals.get(0).getTransactionAuthorizationCode())); // } // money = money * 100; // allocationReq.setAmount((int) money); // allocationReq.setProductName("分账"); // allocationReq.setProductCount(1); // allocationReq.setOrderNo(ToolUtil.getRandomString(32)); // allocationReq.setOrderAmount(1l); // try { // AllocationResp allocationResp = AllocationExample.create(allocationReq); // System.out.println(allocationResp); // return "ok"; // } catch (UpyztException e) { // e.printStackTrace(); // return e.getMessage(); // } return "ok"; } public ResultUtil moneyPay(Integer id,Integer userId,Double money){ Company company = companyService.selectOne(new EntityWrapper() .eq("type", 1) .last("LIMIT 1")); // 查询平台开户信息 TEnterpriseWithdrawal enterpriseWithdrawal = enterpriseWithdrawalService.selectOne(new EntityWrapper() .eq("companyId", company.getId())); UserInfo userInfo = userInfoService.selectById(userId); System.out.println("用户信息:" + userInfo); UserWithdrawal driverWith = userWithdrawalService.selectOne(new EntityWrapper() .eq("phone", userInfo.getPhone()) .last("LIMIT 1")); System.out.println("用户进件信息:" + driverWith); if (driverWith == null) { return ResultUtil.error("领取失败,请先设置进件信息"); } // AllocationReq allocationReq = new AllocationReq(); // allocationReq.setOutOrderNo(ToolUtil.getRandomString(32)); // allocationReq.setProductCount(1); // allocationReq.setPayBalanceAcctId(driverWith.getBalanceAcctId()); //// allocationReq.setPayBalanceAcctId("2008322294132024868"); // allocationReq.setOrderAmount((long) (money * 100)); // allocationReq.setAmount((int) (money * 100)); // allocationReq.setProductName("余额支付"); // allocationReq.setRecvBalanceAcctId(enterpriseWithdrawal.getBalanceAcctId()); //// allocationReq.setRecvBalanceAcctId("2007982285722883157"); // allocationReq.setOrderNo(id.toString()); // allocationReq.setPassword(Base64Util.decode(driverWith.getTransactionAuthorizationCode())); // allocationReq.setPassword("MTIzNDU2"); // System.out.println("余额支付请求信息:" + allocationReq); // try { // // AllocationResp allocationResp = AllocationExample.create(allocationReq); // System.out.println("余额支付操作:"+allocationResp); // return ResultUtil.success(); // } catch (UpyztException e) { // e.printStackTrace(); // System.out.println("余额支付操作error:"+e); // System.out.println(e.getIssue()); // return ResultUtil.error(e.getMessage()); // } return ResultUtil.success(); } @Autowired private IDriverService driverService; public String createAllocationReqDriver(Integer companyId,Integer userId,int type,double money) { List tEnterpriseWithdrawals = enterpriseWithdrawalService.selectList(new EntityWrapper().eq("companyId", companyId)); Driver tUser = driverService.selectById(userId); List userWithdrawals = userWithdrawalService.selectList(new EntityWrapper().eq("phone", tUser.getPhone())); if (userWithdrawals.size() == 0) { return "error"; } // AllocationReq allocationReq= new AllocationReq(); // allocationReq.setOutOrderNo(ToolUtil.getRandomString(32)); // if(type==1){ // allocationReq.setPayBalanceAcctId(tEnterpriseWithdrawals.get(0).getBalanceAcctId()); // allocationReq.setRecvBalanceAcctId(userWithdrawals.get(0).getBalanceAcctId()); // // allocationReq.setPassword(Base64Util.decode(tEnterpriseWithdrawals.get(0).getTransactionAuthorizationCode())); // }else { // allocationReq.setRecvBalanceAcctId(tEnterpriseWithdrawals.get(0).getBalanceAcctId()); // allocationReq.setPayBalanceAcctId(userWithdrawals.get(0).getBalanceAcctId()); // allocationReq.setPassword(Base64Util.decode(userWithdrawals.get(0).getTransactionAuthorizationCode())); // } // money = money * 100; // allocationReq.setAmount((int) money); // allocationReq.setProductName("分账"); // allocationReq.setProductCount(1); // allocationReq.setOrderNo(ToolUtil.getRandomString(32)); // allocationReq.setOrderAmount(1l); // try { // AllocationResp allocationResp = AllocationExample.create(allocationReq); // System.out.println(allocationResp); // return "ok"; // } catch (UpyztException e) { // e.printStackTrace(); // return e.getMessage(); // } return "ok"; } }