LinePayOrderNotificationService.java 4.3 KB

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