OmgPaymentProperties.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package com.ruoyi.app.omgpay;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. import org.springframework.stereotype.Component;
  4. import java.net.URI;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. @Component
  8. @ConfigurationProperties(prefix = "omgpay")
  9. public class OmgPaymentProperties {
  10. private static final String APP_RETURN_SCHEME = "com.twanmsdyh.app";
  11. private static final String APP_RETURN_HOST = "pages";
  12. private static final String APP_RETURN_PATH = "/OrderList/paySuccess/paySuccess";
  13. private String returnUrl;
  14. private String orderResultUrl;
  15. private String appReturnUrl;
  16. /** TEST is the safe default. Production credentials must explicitly enable PRODUCTION. */
  17. private String environment = "TEST";
  18. /** MerchantID -> CreditCheckCode, supplied through protected deployment configuration. */
  19. private Map<String, String> creditCheckCodes = new HashMap<>();
  20. public boolean isProduction() {
  21. return "PRODUCTION".equalsIgnoreCase(environment);
  22. }
  23. public String getEnvironment() { return environment; }
  24. public void setEnvironment(String environment) { this.environment = environment; }
  25. public Map<String, String> getCreditCheckCodes() { return creditCheckCodes; }
  26. public void setCreditCheckCodes(Map<String, String> creditCheckCodes) {
  27. this.creditCheckCodes = creditCheckCodes == null ? new HashMap<>() : creditCheckCodes;
  28. }
  29. public String getReturnUrl() {
  30. return returnUrl;
  31. }
  32. public void setReturnUrl(String returnUrl) {
  33. this.returnUrl = returnUrl;
  34. }
  35. public String getOrderResultUrl() {
  36. return orderResultUrl;
  37. }
  38. public void setOrderResultUrl(String orderResultUrl) {
  39. this.orderResultUrl = orderResultUrl;
  40. }
  41. public String getAppReturnUrl() {
  42. return appReturnUrl;
  43. }
  44. public void setAppReturnUrl(String appReturnUrl) {
  45. this.appReturnUrl = appReturnUrl;
  46. }
  47. public String requireSafeReturnUrl() {
  48. return requireSafeCallbackUrl(returnUrl, "/pay/omg/notify", "ReturnURL");
  49. }
  50. public String requireSafeOrderResultUrl() {
  51. return requireSafeCallbackUrl(orderResultUrl, "/pay/omg/result", "OrderResultURL");
  52. }
  53. public String safeAppReturnUrlOrNull() {
  54. if (appReturnUrl == null || appReturnUrl.isBlank()) {
  55. return null;
  56. }
  57. URI uri;
  58. try {
  59. uri = URI.create(appReturnUrl.trim());
  60. } catch (IllegalArgumentException error) {
  61. throw new IllegalArgumentException("App return URL is invalid", error);
  62. }
  63. if (!APP_RETURN_SCHEME.equalsIgnoreCase(uri.getScheme())
  64. || !APP_RETURN_HOST.equals(uri.getHost())
  65. || !APP_RETURN_PATH.equals(uri.getPath())
  66. || uri.getUserInfo() != null || uri.getQuery() != null || uri.getFragment() != null) {
  67. throw new IllegalArgumentException("App return URL is unsafe");
  68. }
  69. return uri.toASCIIString();
  70. }
  71. private static String requireSafeCallbackUrl(String value, String expectedPath, String name) {
  72. URI uri = requireSafeHttpsUrl(value, "OMG " + name, true);
  73. if (!expectedPath.equals(uri.getPath())) {
  74. throw new IllegalArgumentException("OMG " + name + " is unsafe");
  75. }
  76. return uri.toASCIIString();
  77. }
  78. private static URI requireSafeHttpsUrl(String value, String name, boolean rejectRootPath) {
  79. if (value == null || value.isBlank()) {
  80. throw new IllegalArgumentException(name + " is required");
  81. }
  82. URI uri;
  83. try {
  84. uri = URI.create(value.trim());
  85. } catch (IllegalArgumentException error) {
  86. throw new IllegalArgumentException(name + " is invalid", error);
  87. }
  88. if (value.trim().length() > 200) {
  89. throw new IllegalArgumentException(name + " exceeds 200 characters");
  90. }
  91. if (!uri.isAbsolute() || !"https".equalsIgnoreCase(uri.getScheme())
  92. || uri.getHost() == null || uri.getHost().isBlank()
  93. || uri.getUserInfo() != null || uri.getQuery() != null || uri.getFragment() != null
  94. || (rejectRootPath && (uri.getPath() == null || "/".equals(uri.getPath())))) {
  95. throw new IllegalArgumentException(name + " is unsafe");
  96. }
  97. return uri;
  98. }
  99. }