网站建设预览,wordpress 网址站,wordpress id连续插件,个人建网站做站长账单信息
司机结束代驾之后#xff0c;生成账单#xff08;包含账单信息和分账信息#xff09;司机发送账单给乘客乘客获取账单之后#xff0c;进行支付 获取账单信息
order_bill表记录的账单信息#xff0c;我们直接获取即可
Operation(summary 根据订单id获取…账单信息
司机结束代驾之后生成账单包含账单信息和分账信息司机发送账单给乘客乘客获取账单之后进行支付 获取账单信息
order_bill表记录的账单信息我们直接获取即可
Operation(summary 根据订单id获取实际账单信息)
GetMapping(/getOrderBillInfo/{orderId})
public ResultOrderBillVo getOrderBillInfo(PathVariable Long orderId) {return Result.ok(orderInfoService.getOrderBillInfo(orderId));
}Override
public OrderBillVo getOrderBillInfo(Long orderId) {LambdaQueryWrapperOrderBill wrapper new LambdaQueryWrapper();wrapper.eq(OrderBill::getOrderId,orderId);OrderBill orderBill orderBillMapper.selectOne(wrapper);OrderBillVo orderBillVo new OrderBillVo();BeanUtils.copyProperties(orderBill,orderBillVo);return orderBillVo;
}/*** 根据订单id获取实际账单信息* param orderId* return*/
GetMapping(/order/info/getOrderBillInfo/{orderId})
ResultOrderBillVo getOrderBillInfo(PathVariable(orderId) Long orderId);获取分账信息
Operation(summary 根据订单id获取实际分账信息)
GetMapping(/getOrderProfitsharing/{orderId})
public ResultOrderProfitsharingVo getOrderProfitsharing(PathVariable Long orderId) {return Result.ok(orderInfoService.getOrderProfitsharing(orderId));
}Override
public OrderProfitsharingVo getOrderProfitsharing(Long orderId) {LambdaQueryWrapperOrderProfitsharing wrapper new LambdaQueryWrapper();wrapper.eq(OrderProfitsharing::getOrderId,orderId);OrderProfitsharing orderProfitsharing orderProfitsharingMapper.selectOne(wrapper);OrderProfitsharingVo orderProfitsharingVo new OrderProfitsharingVo();BeanUtils.copyProperties(orderProfitsharing,orderProfitsharingVo);return orderProfitsharingVo;
}/*** 根据订单id获取实际分账信息* param orderId* return*/
GetMapping(/order/info/getOrderProfitsharing/{orderId})
ResultOrderProfitsharingVo getOrderProfitsharing(PathVariable(orderId) Long orderId);司机端获取账单信息
Operation(summary 获取订单账单详细信息)
GuiguLogin
GetMapping(/getOrderInfo/{orderId})
public ResultOrderInfoVo getOrderInfo(PathVariable Long orderId) {Long driverId AuthContextHolder.getUserId();return Result.ok(orderService.getOrderInfo(orderId, driverId));
}Override
public OrderInfoVo getOrderInfo(Long orderId, Long driverId) {OrderInfo orderInfo orderInfoFeignClient.getOrderInfo(orderId).getData();if(orderInfo.getDriverId() ! driverId) {throw new GuiguException(ResultCodeEnum.ILLEGAL_REQUEST);}//获取账单和分账数据封装到vo里面OrderBillVo orderBillVo null;OrderProfitsharingVo orderProfitsharingVo null;//判断是否结束代驾if(orderInfo.getStatus() OrderStatus.END_SERVICE.getStatus()) {//账单信息orderBillVo orderInfoFeignClient.getOrderBillInfo(orderId).getData();//分账信息orderProfitsharingVo orderInfoFeignClient.getOrderProfitsharing(orderId).getData();}OrderInfoVo orderInfoVo new OrderInfoVo();orderInfoVo.setOrderId(orderId);BeanUtils.copyProperties(orderInfo,orderInfoVo);orderInfoVo.setOrderBillVo(orderBillVo);orderInfoVo.setOrderProfitsharingVo(orderProfitsharingVo);return orderInfoVo;
}司机发送账单
发送账单就是更新订单状态未支付
Operation(summary 发送账单信息)
GetMapping(/sendOrderBillInfo/{orderId}/{driverId})
ResultBoolean sendOrderBillInfo(PathVariable Long orderId, PathVariable Long driverId) {return Result.ok(orderInfoService.sendOrderBillInfo(orderId, driverId));
}Override
public Boolean sendOrderBillInfo(Long orderId, Long driverId) {//更新订单信息LambdaQueryWrapperOrderInfo queryWrapper new LambdaQueryWrapper();queryWrapper.eq(OrderInfo::getId, orderId);queryWrapper.eq(OrderInfo::getDriverId, driverId);//更新字段OrderInfo updateOrderInfo new OrderInfo();updateOrderInfo.setStatus(OrderStatus.UNPAID.getStatus());//只能更新自己的订单int row orderInfoMapper.update(updateOrderInfo, queryWrapper);if(row 1) {return true;} else {throw new GuiguException(ResultCodeEnum.UPDATE_ERROR);}
}/*** 司机发送账单信息* param orderId* param driverId* return*/
GetMapping(/order/info/sendOrderBillInfo/{orderId}/{driverId})
ResultBoolean sendOrderBillInfo(PathVariable(orderId) Long orderId, PathVariable(driverId) Long driverId);Operation(summary 司机发送账单信息)
GuiguLogin
GetMapping(/sendOrderBillInfo/{orderId})
public ResultBoolean sendOrderBillInfo(PathVariable Long orderId) {Long driverId AuthContextHolder.getUserId();return Result.ok(orderService.sendOrderBillInfo(orderId, driverId));
}Override
public Boolean sendOrderBillInfo(Long orderId, Long driverId) {return orderInfoFeignClient.sendOrderBillInfo(orderId, driverId).getData();
}乘客获取账单
Override
public OrderInfoVo getOrderInfo(Long orderId, Long customerId) {OrderInfo orderInfo orderInfoFeignClient.getOrderInfo(orderId).getData();//判断if(orderInfo.getCustomerId() ! customerId) {throw new GuiguException(ResultCodeEnum.ILLEGAL_REQUEST);}//获取司机信息DriverInfoVo driverInfoVo null;Long driverId orderInfo.getDriverId();if(driverId ! null) {driverInfoVo driverInfoFeignClient.getDriverInfo(driverId).getData();}//获取账单信息OrderBillVo orderBillVo null;if(orderInfo.getStatus() OrderStatus.UNPAID.getStatus()) {orderBillVo orderInfoFeignClient.getOrderBillInfo(orderId).getData();}OrderInfoVo orderInfoVo new OrderInfoVo();orderInfoVo.setOrderId(orderId);BeanUtils.copyProperties(orderInfo,orderInfoVo);orderInfoVo.setOrderBillVo(orderBillVo);orderInfoVo.setDriverInfoVo(driverInfoVo);return orderInfoVo;
}微信支付
获取乘客openid
Operation(summary 获取客户OpenId)
GetMapping(/getCustomerOpenId/{customerId})
public ResultString getCustomerOpenId(PathVariable Long customerId) {return Result.ok(customerInfoService.getCustomerOpenId(customerId));
}Override
public String getCustomerOpenId(Long customerId) {LambdaQueryWrapperCustomerInfo wrapper new LambdaQueryWrapper();wrapper.eq(CustomerInfo::getId,customerId);CustomerInfo customerInfo customerInfoMapper.selectOne(wrapper);return customerInfo.getWxOpenId();
}/*** 获取客户OpenId* param customerId* return*/
GetMapping(/customer/info/getCustomerOpenId/{customerId})
ResultString getCustomerOpenId(PathVariable(customerId) Long customerId);获取司机openid
与获取乘客openid基本一致
获取订单支付信息
Operation(summary 获取订单支付信息)
GetMapping(/getOrderPayVo/{orderNo}/{customerId})
public ResultOrderPayVo getOrderPayVo(PathVariable String orderNo, PathVariable Long customerId) {return Result.ok(orderInfoService.getOrderPayVo(orderNo, customerId));
}Override
public OrderPayVo getOrderPayVo(String orderNo, Long customerId) {OrderPayVo orderPayVo orderInfoMapper.selectOrderPayVo(orderNo,customerId);if(orderPayVo ! null) {String content orderPayVo.getStartLocation() 到 orderPayVo.getEndLocation();orderPayVo.setContent(content);}return orderPayVo;
}select idselectOrderPayVo resultTypecom.atguigu.daijia.model.vo.order.OrderPayVoselectinfo.id as order_id,info.customer_id,info.driver_id,info.order_no,info.start_location,info.end_location,info.status,bill.pay_amount,bill.coupon_amountfrom order_info info inner join order_bill bill on info.id bill.order_idwhere info.customer_id #{customerId}and info.order_no #{orderNo}
/select/*** 获取订单支付信息* param orderNo* param customerId* return*/
GetMapping(/order/info/getOrderPayVo/{orderNo}/{customerId})
ResultOrderPayVo getOrderPayVo(PathVariable(orderNo) String orderNo, PathVariable(customerId) Long customerId);申请并绑定微信支付 微信支付商户平台https://pay.weixin.qq.com/index.php/core/home/login
对于商家来说想要开通微信支付必须要去“微信支付商户平台”注册然后把需要的资料提交上去经过审核通过你就开通了微信支付功能。
企业申请资料
1、营业执照彩色扫描件或数码照片
2、组织机构代码证彩色扫描件或数码照片若已三证合一则无需提供
3、对公银行账户包含开户行省市信息开户账号
4、法人身份证彩色扫描件或数码照片
如果想要在网站或者小程序上面使用微信支付还要在微信公众平台上面关联你自己的微信商户账号。前提是你的微信开发者账号必须是企业身份个人身份的开发者账号是无法调用微信支付API的。
支付密钥和数字证书 因为调用微信支付平台的API接口必须要用到支付密钥和数字证书这些参数在微信支付商户平台都可以获取。
dependenciesdependencygroupIdcom.github.wechatpay-apiv3/groupIdartifactIdwechatpay-java/artifactId/dependency
/dependencieswx:v3pay:#小程序微信公众平台appIdappid: wxcc651fcbab275e33#商户号merchantId: 163*******#商户API私钥路径privateKeyPath: /root/daijia/apiclient_key.pem#商户证书序列号merchantSerialNumber: 4AE80**********#商户APIV3密钥apiV3key: 84***************#异步回调地址notifyUrl: http://139.198.127.41:8600/payment/wxPay/notifypackage com.atguigu.daijia.payment.config;import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;Configuration
ConfigurationProperties(prefixwx.v3pay) //读取节点
Data
public class WxPayV3Properties {private String appid;/** 商户号 */public String merchantId;/** 商户API私钥路径 */public String privateKeyPath;/** 商户证书序列号 */public String merchantSerialNumber;/** 商户APIV3密钥 */public String apiV3key;/** 回调地址 */private String notifyUrl;Beanpublic RSAAutoCertificateConfig getConfig(){return new RSAAutoCertificateConfig.Builder().merchantId(this.getMerchantId()).privateKeyFromPath(this.getPrivateKeyPath()).merchantSerialNumber(this.getMerchantSerialNumber()).apiV3Key(this.getApiV3key()).build();}
}微信支付接口
Tag(name 微信支付接口)
RestController
RequestMapping(payment/wxPay)
Slf4j
public class WxPayController {Operation(summary 创建微信支付)PostMapping(/createJsapi)public ResultWxPrepayVo createWxPayment(RequestBody PaymentInfoForm paymentInfoForm) {return Result.ok(wxPayService.createWxPayment(paymentInfoForm));}
}Override
public WxPrepayVo createWxPayment(PaymentInfoForm paymentInfoForm) {try {//1 添加支付记录到支付表里面//判断如果表存在订单支付记录不需要添加LambdaQueryWrapperPaymentInfo wrapper new LambdaQueryWrapper();wrapper.eq(PaymentInfo::getOrderNo,paymentInfoForm.getOrderNo());PaymentInfo paymentInfo paymentInfoMapper.selectOne(wrapper);if(paymentInfo null) {paymentInfo new PaymentInfo();BeanUtils.copyProperties(paymentInfoForm,paymentInfo);paymentInfo.setPaymentStatus(0);paymentInfoMapper.insert(paymentInfo);}//2 创建微信支付使用对象JsapiServiceExtension service new JsapiServiceExtension.Builder().config(rsaAutoCertificateConfig).build();//3 创建request对象封装微信支付需要参数PrepayRequest request new PrepayRequest();Amount amount new Amount();amount.setTotal(paymentInfoForm.getAmount().multiply(new BigDecimal(100)).intValue());request.setAmount(amount);request.setAppid(wxPayV3Properties.getAppid());request.setMchid(wxPayV3Properties.getMerchantId());//string[1,127]String description paymentInfo.getContent();if(description.length() 127) {description description.substring(0, 127);}request.setDescription(description);request.setNotifyUrl(wxPayV3Properties.getNotifyUrl());request.setOutTradeNo(paymentInfo.getOrderNo());//获取用户信息Payer payer new Payer();payer.setOpenid(paymentInfoForm.getCustomerOpenId());request.setPayer(payer);//是否指定分账不指定不能分账SettleInfo settleInfo new SettleInfo();settleInfo.setProfitSharing(true);request.setSettleInfo(settleInfo);//4 调用微信支付使用对象里面方法实现微信支付调用PrepayWithRequestPaymentResponse response service.prepayWithRequestPayment(request);//5 根据返回结果封装到WxPrepayVo里面WxPrepayVo wxPrepayVo new WxPrepayVo();BeanUtils.copyProperties(response,wxPrepayVo);wxPrepayVo.setTimeStamp(response.getTimeStamp());return wxPrepayVo;}catch (Exception e) {throw new GuiguException(ResultCodeEnum.DATA_ERROR);}
}FeignClient(value service-payment)
public interface WxPayFeignClient {/*** 创建微信支付* param paymentInfoForm* return*/PostMapping(/payment/wxPay/createWxPayment)ResultWxPrepayVo createWxPayment(RequestBody PaymentInfoForm paymentInfoForm);
}Operation(summary 创建微信支付)
GuiguLogin
PostMapping(/createWxPayment)
public ResultWxPrepayVo createWxPayment(RequestBody CreateWxPaymentForm createWxPaymentForm) {Long customerId AuthContextHolder.getUserId();createWxPaymentForm.setCustomerId(customerId);return Result.ok(orderService.createWxPayment(createWxPaymentForm));
}Autowired
private WxPayFeignClient wxPayFeignClient;Override
public WxPrepayVo createWxPayment(CreateWxPaymentForm createWxPaymentForm) {//获取订单支付信息OrderPayVo orderPayVo orderInfoFeignClient.getOrderPayVo(createWxPaymentForm.getOrderNo(),createWxPaymentForm.getCustomerId()).getData();//判断if(orderPayVo.getStatus() ! OrderStatus.UNPAID.getStatus()) {throw new GuiguException(ResultCodeEnum.ILLEGAL_REQUEST);}//获取乘客和司机openidString customerOpenId customerInfoFeignClient.getCustomerOpenId(orderPayVo.getCustomerId()).getData();String driverOpenId driverInfoFeignClient.getDriverOpenId(orderPayVo.getDriverId()).getData();//封装需要数据到实体类远程调用发起微信支付PaymentInfoForm paymentInfoForm new PaymentInfoForm();paymentInfoForm.setCustomerOpenId(customerOpenId);paymentInfoForm.setDriverOpenId(driverOpenId);paymentInfoForm.setOrderNo(orderPayVo.getOrderNo());paymentInfoForm.setAmount(orderPayVo.getPayAmount());paymentInfoForm.setContent(orderPayVo.getContent());paymentInfoForm.setPayWay(1);WxPrepayVo wxPrepayVo wxPayFeignClient.createWxPayment(paymentInfoForm).getData();return wxPrepayVo;
}查询支付状态 判断微信支付是否成功有两种方式就是以上两种红字微信支付成功/失败就会回调我们的接作收到调用之后我们就可以继续接下来的操作用消息队列保证数据的最终一致性在支付成功后我们后续还有很多的操作如果把所有的操作全部成功再向用户进行返回那就太慢了如果支付成功我们就先向用户返回结果并向消息队列发送关键信息用于后续的操作
根据订单编号查询支付状态
Operation(summary 支付状态查询)
GetMapping(/queryPayStatus/{orderNo})
public Result queryPayStatus(PathVariable String orderNo) {return Result.ok(wxPayService.queryPayStatus(orderNo));
}//查询支付状态
Override
public Boolean queryPayStatus(String orderNo) {//1 创建微信操作对象JsapiServiceExtension service new JsapiServiceExtension.Builder().config(rsaAutoCertificateConfig).build();//2 封装查询支付状态需要参数QueryOrderByOutTradeNoRequest queryRequest new QueryOrderByOutTradeNoRequest();queryRequest.setMchid(wxPayV3Properties.getMerchantId());queryRequest.setOutTradeNo(orderNo);//3 调用微信操作对象里面方法实现查询操作Transaction transaction service.queryOrderByOutTradeNo(queryRequest);//4 查询返回结果根据结果判断if(transaction ! null transaction.getTradeState() Transaction.TradeStateEnum.SUCCESS) {//5 如果支付成功调用其他方法实现支付后处理逻辑this.handlePayment(transaction);return true;}return false;
}/*** 支付状态查询* param orderNo* return*/
GetMapping(/payment/wxPay/queryPayStatus/{orderNo})
ResultBoolean queryPayStatus(PathVariable(orderNo) String orderNo);Operation(summary 支付状态查询)
GuiguLogin
GetMapping(/queryPayStatus/{orderNo})
public ResultBoolean queryPayStatus(PathVariable String orderNo) {return Result.ok(orderService.queryPayStatus(orderNo));
}Override
public Boolean queryPayStatus(String orderNo) {return wxPayFeignClient.queryPayStatus(orderNo).getData();
}微信支付成功回调接口
Operation(summary 微信支付异步通知接口)
PostMapping(/notify)
public MapString,Object notify(HttpServletRequest request) {try {wxPayService.wxnotify(request);//返回成功MapString,Object result new HashMap();result.put(code, SUCCESS);result.put(message, 成功);return result;} catch (Exception e) {e.printStackTrace();}//返回失败MapString,Object result new HashMap();result.put(code, FAIL);result.put(message, 失败);return result;
}//微信支付成功后进行的回调Overridepublic void wxnotify(HttpServletRequest request) {
//1.回调通知的验签与解密//从request头信息获取参数//HTTP 头 Wechatpay-Signature// HTTP 头 Wechatpay-Nonce//HTTP 头 Wechatpay-Timestamp//HTTP 头 Wechatpay-Serial//HTTP 头 Wechatpay-Signature-Type//HTTP 请求体 body。切记使用原始报文不要用 JSON 对象序列化后的字符串避免验签的 body 和原文不一致。String wechatPaySerial request.getHeader(Wechatpay-Serial);String nonce request.getHeader(Wechatpay-Nonce);String timestamp request.getHeader(Wechatpay-Timestamp);String signature request.getHeader(Wechatpay-Signature);String requestBody RequestUtils.readData(request);//2.构造 RequestParamRequestParam requestParam new RequestParam.Builder().serialNumber(wechatPaySerial).nonce(nonce).signature(signature).timestamp(timestamp).body(requestBody).build();//3.初始化 NotificationParserNotificationParser parser new NotificationParser(rsaAutoCertificateConfig);//4.以支付通知回调为例验签、解密并转换成 TransactionTransaction transaction parser.parse(requestParam, Transaction.class);if(null ! transaction transaction.getTradeState() Transaction.TradeStateEnum.SUCCESS) {//5.处理支付业务this.handlePayment(transaction);}}内网穿透配置 还可以把项目部署到腾讯云、华为云等来解决这个问题测试的时候我们没有域名就可以使用内网穿透的工具
这里使用的工具是ngrok.cc
支付成功后续处理
封装RabbitMQ
Service
public class RabbitService {Autowiredprivate RabbitTemplate rabbitTemplate;//发送消息public boolean sendMessage(String exchange,String routingkey,Object message) {rabbitTemplate.convertAndSend(exchange,routingkey,message);return true;}
}修改nacos配置
发送端
//如果支付成功调用其他方法实现支付后处理逻辑
public void handlePayment(Transaction transaction) {//1 更新支付记录状态修改为 已经支付//订单编号String orderNo transaction.getOutTradeNo();//根据订单编号查询支付记录LambdaQueryWrapperPaymentInfo wrapper new LambdaQueryWrapper();wrapper.eq(PaymentInfo::getOrderNo,orderNo);PaymentInfo paymentInfo paymentInfoMapper.selectOne(wrapper);//如果已经支付不需要更新if(paymentInfo.getPaymentStatus() 1) {return;}paymentInfo.setPaymentStatus(1);paymentInfo.setOrderNo(transaction.getOutTradeNo());paymentInfo.setTransactionId(transaction.getTransactionId());paymentInfo.setCallbackTime(new Date());paymentInfo.setCallbackContent(JSON.toJSONString(transaction));paymentInfoMapper.updateById(paymentInfo);//2 发送端发送mq消息传递 订单编号// 接收端获取订单编号完成后续处理rabbitService.sendMessage(MqConst.EXCHANGE_ORDER,MqConst.ROUTING_PAY_SUCCESS,orderNo);
}接收端
Component
public class PaymentReceiver {Autowiredprivate WxPayService wxPayService;RabbitListener(bindings QueueBinding(value Queue(value MqConst.QUEUE_PAY_SUCCESS,durable true),exchange Exchange(value MqConst.EXCHANGE_ORDER),key {MqConst.ROUTING_PAY_SUCCESS}))public void paySuccess(String orderNo, Message message, Channel channel) {wxPayService.handleOrder(orderNo);}
}//支付成功后续处理
Override
public void handleOrder(String orderNo) {//1 远程调用更新订单状态已经支付orderInfoFeignClient.updateOrderPayStatus(orderNo);//2 远程调用获取系统奖励打入到司机账户OrderRewardVo orderRewardVo orderInfoFeignClient.getOrderRewardFee(orderNo).getData();if(orderRewardVo ! null orderRewardVo.getRewardFee().doubleValue()0) {TransferForm transferForm new TransferForm();transferForm.setTradeNo(orderNo);transferForm.setTradeType(TradeType.REWARD.getType());transferForm.setContent(TradeType.REWARD.getContent());transferForm.setAmount(orderRewardVo.getRewardFee());transferForm.setDriverId(orderRewardVo.getDriverId());driverAccountFeignClient.transfer(transferForm);}//3 TODO 其他}订单状态更新接口
Operation(summary 更改订单支付状态)
GetMapping(/updateOrderPayStatus/{orderNo})
public ResultBoolean updateOrderPayStatus(PathVariable String orderNo) {return Result.ok(orderInfoService.updateOrderPayStatus(orderNo));
}Override
public Boolean updateOrderPayStatus(String orderNo) {//1 根据订单编号查询判断订单状态LambdaQueryWrapperOrderInfo wrapper new LambdaQueryWrapper();wrapper.eq(OrderInfo::getOrderNo,orderNo);OrderInfo orderInfo orderInfoMapper.selectOne(wrapper);if(orderInfo null || orderInfo.getStatus() OrderStatus.PAID.getStatus()) {return true;}//2 更新状态LambdaQueryWrapperOrderInfo updateWrapper new LambdaQueryWrapper();updateWrapper.eq(OrderInfo::getOrderNo,orderNo);OrderInfo updateOrderInfo new OrderInfo();updateOrderInfo.setStatus(OrderStatus.PAID.getStatus());updateOrderInfo.setPayTime(new Date());int rows orderInfoMapper.update(updateOrderInfo, updateWrapper);if(rows 1) {return true;} else {throw new GuiguException(ResultCodeEnum.UPDATE_ERROR);}
}/*** 更改订单支付状态* param orderNo* return*/
GetMapping(/order/info//updateOrderPayStatus/{orderNo})
ResultBoolean updateOrderPayStatus(PathVariable(orderNo) String orderNo);获取订单系统奖励
Operation(summary 获取订单的系统奖励)
GetMapping(/getOrderRewardFee/{orderNo})
public ResultOrderRewardVo getOrderRewardFee(PathVariable String orderNo) {return Result.ok(orderInfoService.getOrderRewardFee(orderNo));
}Override
public OrderRewardVo getOrderRewardFee(String orderNo) {//根据订单编号查询订单表OrderInfo orderInfo orderInfoMapper.selectOne(new LambdaQueryWrapperOrderInfo().eq(OrderInfo::getOrderNo, orderNo).select(OrderInfo::getId,OrderInfo::getDriverId));//根据订单id查询系统奖励表OrderBill orderBill orderBillMapper.selectOne(new LambdaQueryWrapperOrderBill().eq(OrderBill::getOrderId, orderInfo.getId()).select(OrderBill::getRewardFee));//封装到vo里面OrderRewardVo orderRewardVo new OrderRewardVo();orderRewardVo.setOrderId(orderInfo.getId());orderRewardVo.setDriverId(orderInfo.getDriverId());orderRewardVo.setRewardFee(orderBill.getRewardFee());return orderRewardVo;
}/*** 获取订单的系统奖励* param orderNo* return*/
GetMapping(/order/info//getOrderRewardFee/{orderNo})
ResultOrderRewardVo getOrderRewardFee(PathVariable(orderNo) String orderNo);系统奖励打入司机账户
Tag(name 司机账户API接口管理)
RestController
RequestMapping(value/driver/account)
SuppressWarnings({unchecked, rawtypes})
public class DriverAccountController {Autowiredprivate DriverAccountService driverAccountService;Operation(summary 转账)PostMapping(/transfer)public ResultBoolean transfer(RequestBody TransferForm transferForm) {return Result.ok(driverAccountService.transfer(transferForm));}
}Override
public Boolean transfer(TransferForm transferForm) {//1 去重LambdaQueryWrapperDriverAccountDetail wrapper new LambdaQueryWrapper();wrapper.eq(DriverAccountDetail::getTradeNo,transferForm.getTradeNo());Long count driverAccountDetailMapper.selectCount(wrapper);if(count 0) {return true;}//2 添加奖励到司机账户表driverAccountMapper.add(transferForm.getDriverId(),transferForm.getAmount());//3 添加交易记录 DriverAccountDetail driverAccountDetail new DriverAccountDetail();BeanUtils.copyProperties(transferForm,driverAccountDetail);driverAccountDetailMapper.insert(driverAccountDetail);return true;
}update idaddupdate driver_accountset total_amount total_amount #{amount}, available_amount available_amount #{amount}, total_income_amount total_income_amount #{amount}where driver_id #{driverId}
/updateFeignClient(value service-driver)
public interface DriverAccountFeignClient {/*** 转账* param transferForm* return*/PostMapping(/driver/account/transfer)ResultBoolean transfer(RequestBody TransferForm transferForm);
}seata实现分布式事务
订单支付成功后订单状态更新、获取订单系统奖励、系统奖励打入司机账户都是通过远程调用来实现的我们就在这儿使用seata分布式事务
dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-seata/artifactId
/dependency
dependencygroupIdio.seata/groupIdartifactIdseata-spring-boot-starter/artifactIdversion1.7.1/version
/dependencyseata.tx-service-grouptingshu-tx-group
seata.service.vgroup-mapping.tingshu-tx-groupdefault
seata.service.grouplist.default127.0.0.1:8091// 在事务的入口添加GlobalTransactional
// 其他的小事务依然使用Transactional乘客下单功能
乘客登录之后选择代驾开始和结束地址之后乘客呼叫代驾乘客呼叫代驾之后等待司机接单但是如果在乘客呼叫代驾之后15分钟之后如果还是没有司机接单订单自动取消如果15分钟以内有司机接单完成代驾过程总结15分钟没有司机接单自动取消订单总体思路使用延迟队列消息实现订单到时间自动取消功能有很多种方式 第一种 使用RabbitMQ里面TTL和死信队列实现
第二种 在RabbitMQ安装延迟队列实现 就是装个插件在这个队列中的消息不能被消费只有延时时间到了才能被消费第三种 使用Redisson实现 使用RabbitMQ里面TTL和死信队列实现
TTLTime To Live消息存活时间 发送端发送消息设置存活时间10s
接收端发送完成之后10s以内如果从队列获取发送过来消息操作结束
如果超过10s消息没有被消费消息超时了无法被消费成为死信 死信队列无法被消费的消息 – 超过存活时间没有被消费的消息
– 消息端拒绝接收不能放回队列里面
– 队列最大长度 使用Redisson实现
利用redissonClient 发送延迟消息
redissonClient.getBlockingDeque(): 创建一个阻塞队列redissonClient.getDelayedQueue(): 获取延迟队列delayedQueue.offer(): 向队列中存储数据blockingDeque.take(): 从队列中获取数据//乘客下单
Override
public Long saveOrderInfo(OrderInfoForm orderInfoForm) {//order_info添加订单数据OrderInfo orderInfo new OrderInfo();BeanUtils.copyProperties(orderInfoForm,orderInfo);//订单号String orderNo UUID.randomUUID().toString().replaceAll(-,);orderInfo.setOrderNo(orderNo);//订单状态orderInfo.setStatus(OrderStatus.WAITING_ACCEPT.getStatus());orderInfoMapper.insert(orderInfo);//生成订单之后发送延迟消息this.sendDelayMessage(orderInfo.getId());//记录日志this.log(orderInfo.getId(),orderInfo.getStatus());//向redis添加标识//接单标识标识不存在了说明不在等待接单状态了redisTemplate.opsForValue().set(RedisConstant.ORDER_ACCEPT_MARK,0, RedisConstant.ORDER_ACCEPT_MARK_EXPIRES_TIME, TimeUnit.MINUTES);return orderInfo.getId();
}编写发送延迟消息的方法
//生成订单之后发送延迟消息
private void sendDelayMessage(Long orderId) {try{//1 创建队列RBlockingQueueObject blockingDueue redissonClient.getBlockingQueue(queue_cancel);//2 把创建队列放到延迟队列里面RDelayedQueueObject delayedQueue redissonClient.getDelayedQueue(blockingDueue);//3 发送消息到延迟队列里面//设置过期时间delayedQueue.offer(orderId.toString(),15,TimeUnit.MINUTES);}catch (Exception e) {e.printStackTrace();throw new GuiguException(ResultCodeEnum.DATA_ERROR);}
}监听延迟队列消息
//监听延迟队列
Component
public class RedisDelayHandle {Autowiredprivate RedissonClient redissonClient;Autowiredprivate OrderInfoService orderInfoService;PostConstructpublic void listener() {new Thread(()-{// while true 实现一直监听while(true) {//获取延迟队列里面阻塞队列RBlockingQueueString blockingQueue redissonClient.getBlockingQueue(queue_cancel);//从队列获取消息try {String orderId blockingQueue.take();//取消订单if(StringUtils.hasText(orderId)) {//调用方法取消订单orderInfoService.orderCancel(Long.parseLong(orderId));}} catch (InterruptedException e) {throw new RuntimeException(e);}}}).start();}
}//调用方法取消订单
Override
public void orderCancel(long orderId) {//orderId查询订单信息OrderInfo orderInfo orderInfoMapper.selectById(orderId);//判断if(orderInfo.getStatus()OrderStatus.WAITING_ACCEPT.getStatus()) {//修改订单状态取消状态orderInfo.setStatus(OrderStatus.CANCEL_ORDER.getStatus());int rows orderInfoMapper.updateById(orderInfo);if(rows 1) {//删除接单标识redisTemplate.delete(RedisConstant.ORDER_ACCEPT_MARK);}}
}