LinePayOrderNotificationService.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package com.ruoyi.app.pay;
  2. import com.ruoyi.app.order.dto.OrderPushBodyDto;
  3. import com.ruoyi.app.order.DeliveryOrderNotificationService;
  4. import com.ruoyi.app.order.MerchantNotificationRouter;
  5. import com.ruoyi.app.push.BusinessPushService;
  6. import com.ruoyi.app.push.RecipientApp;
  7. import com.ruoyi.app.push.UserNotificationScene;
  8. import com.ruoyi.app.utils.event.PushEventService;
  9. import com.ruoyi.common.utils.LocaleUtils;
  10. import com.ruoyi.common.utils.MessageUtils;
  11. import com.ruoyi.system.domain.InfoUser;
  12. import com.ruoyi.system.domain.PosOrder;
  13. import com.ruoyi.system.service.IInfoUserService;
  14. import com.ruoyi.system.utils.OrderLogHelper;
  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17. import org.springframework.stereotype.Service;
  18. import org.springframework.transaction.support.TransactionSynchronization;
  19. import java.util.Locale;
  20. import org.springframework.transaction.support.TransactionSynchronizationManager;
  21. /** Executes the existing one-time order log and push side effects after LINE payment capture. */
  22. @Service
  23. public class LinePayOrderNotificationService {
  24. private static final Logger log = LoggerFactory.getLogger(LinePayOrderNotificationService.class);
  25. private final IInfoUserService infoUserService;
  26. private final PushEventService pushEventService;
  27. private final OrderLogHelper orderLogHelper;
  28. private final DeliveryOrderNotificationService deliveryOrderNotificationService;
  29. private final MerchantNotificationRouter merchantNotificationRouter;
  30. private final BusinessPushService businessPushService;
  31. public LinePayOrderNotificationService(IInfoUserService infoUserService,
  32. PushEventService pushEventService,
  33. OrderLogHelper orderLogHelper,
  34. DeliveryOrderNotificationService deliveryOrderNotificationService,
  35. MerchantNotificationRouter merchantNotificationRouter,
  36. BusinessPushService businessPushService) {
  37. this.infoUserService = infoUserService;
  38. this.pushEventService = pushEventService;
  39. this.orderLogHelper = orderLogHelper;
  40. this.deliveryOrderNotificationService = deliveryOrderNotificationService;
  41. this.merchantNotificationRouter = merchantNotificationRouter;
  42. this.businessPushService = businessPushService;
  43. }
  44. public void paymentCaptured(PosOrder order) {
  45. orderLogHelper.logSync(String.valueOf(order.getDdId()), 0, null, "system",
  46. "LINE Pay payment captured");
  47. runAfterCommit(() -> pushPaymentSuccess(order));
  48. }
  49. private void pushPaymentSuccess(PosOrder order) {
  50. try {
  51. String ddId = String.valueOf(order.getDdId());
  52. String body = OrderPushBodyDto.getJson(ddId, String.valueOf(order.getState()), 0);
  53. InfoUser user = order.getUserId() == null ? null : infoUserService.getById(order.getUserId());
  54. if (user != null) {
  55. // 推送常在异步线程执行(confirm 回调投递 linePayTaskExecutor),无请求级 locale,
  56. // 必须按收件人用户语言解析,否则回落 JVM 默认语言(线上为英文)
  57. Locale userLocale = LocaleUtils.getUserLocale(user.getUserId());
  58. String title = MessageUtils.message("no.message.push.message", userLocale);
  59. String content = MessageUtils.message("no.message.push.payment.success", userLocale);
  60. // 用户场景不写入商家新单 body;入库和外发使用同一完整 JSON。
  61. String userBody = OrderPushBodyDto.getUserJson(ddId, String.valueOf(order.getState()),
  62. 0, 1, UserNotificationScene.PAYMENT_SUCCEEDED);
  63. pushEventService.PublisherEvent(user.getUserId(), title, content, userBody);
  64. businessPushService.send(RecipientApp.USER, user.getUserId(), user.getCid(), title, content, userBody);
  65. }
  66. if (Long.valueOf(0L).equals(order.getType())) {
  67. deliveryOrderNotificationService.notifyOrderAvailable(order);
  68. } else {
  69. merchantNotificationRouter.sendStoreNotification(order.getMdId(), order.getShId(),
  70. "no.message.push.message", "no.message.push.new.order", body, ddId);
  71. }
  72. } catch (Exception exception) {
  73. log.error("LINE Pay payment success push failed, ddId={}", order.getDdId(), exception);
  74. }
  75. }
  76. private static void runAfterCommit(Runnable action) {
  77. if (!TransactionSynchronizationManager.isSynchronizationActive()) {
  78. action.run();
  79. return;
  80. }
  81. TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
  82. @Override
  83. public void afterCommit() {
  84. action.run();
  85. }
  86. });
  87. }
  88. }