OmgPaymentRetryService.java 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package com.ruoyi.app.omgpay;
  2. import com.ruoyi.app.omgpay.dto.OmgQueryPaymentResponse;
  3. import org.springframework.stereotype.Service;
  4. import static com.ruoyi.app.omgpay.OmgPaymentErrorCode.ORDER_ALREADY_PAID;
  5. import static com.ruoyi.app.omgpay.OmgPaymentErrorCode.PAYMENT_METHOD_INVALID;
  6. import static com.ruoyi.app.omgpay.OmgPaymentErrorCode.PAYMENT_RETRY_NOT_AVAILABLE;
  7. /** Verifies the gateway state before replacing an abandoned payment attempt. */
  8. @Service
  9. public class OmgPaymentRetryService {
  10. private final OmgPaymentQueryService queryService;
  11. private final OmgPaymentCreateService createService;
  12. public OmgPaymentRetryService(OmgPaymentQueryService queryService,
  13. OmgPaymentCreateService createService) {
  14. this.queryService = queryService;
  15. this.createService = createService;
  16. }
  17. public OmgPaymentCreateOutcome retry(Long userId, String orderId, OmgPaymentMethod paymentMethod) {
  18. if (paymentMethod == null) {
  19. throw business(PAYMENT_METHOD_INVALID);
  20. }
  21. OmgQueryPaymentResponse query = queryService.query(userId, orderId);
  22. if (query == null || query.status() == null) {
  23. throw business(PAYMENT_RETRY_NOT_AVAILABLE);
  24. }
  25. if ("PAID".equals(query.status())) {
  26. throw business(ORDER_ALREADY_PAID);
  27. }
  28. if ("FAILED".equals(query.status())) {
  29. return createService.create(userId, orderId, paymentMethod);
  30. }
  31. if ("UNPAID".equals(query.status()) && isBlank(query.paymentType())) {
  32. return createService.replaceActiveForRetry(userId, orderId, query.merchantTradeNo(), paymentMethod);
  33. }
  34. throw business(PAYMENT_RETRY_NOT_AVAILABLE);
  35. }
  36. private static boolean isBlank(String value) {
  37. return value == null || value.isBlank();
  38. }
  39. private static OmgPaymentBusinessException business(OmgPaymentErrorCode code) {
  40. return new OmgPaymentBusinessException(code);
  41. }
  42. }