OmgRefundGateway.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package com.ruoyi.app.omgpay;
  2. import org.apache.http.NameValuePair;
  3. import org.apache.http.client.config.RequestConfig;
  4. import org.apache.http.client.entity.UrlEncodedFormEntity;
  5. import org.apache.http.client.methods.CloseableHttpResponse;
  6. import org.apache.http.client.methods.HttpPost;
  7. import org.apache.http.impl.client.CloseableHttpClient;
  8. import org.apache.http.impl.client.HttpClients;
  9. import org.apache.http.message.BasicNameValuePair;
  10. import org.apache.http.util.EntityUtils;
  11. import org.springframework.stereotype.Component;
  12. import java.io.IOException;
  13. import java.nio.charset.StandardCharsets;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. import java.util.Map;
  17. /** OMG 正式信用卡查询/退款 HTTP 边界;调用前由服务确认环境。 */
  18. @Component
  19. public class OmgRefundGateway {
  20. private static final String QUERY_URL = "https://payment.funpoint.com.tw/CreditDetail/QueryTrade/V2";
  21. private static final String ACTION_URL = "https://payment.funpoint.com.tw/CreditDetail/DoAction";
  22. private static final int MAX_RESPONSE_BYTES = 32 * 1024;
  23. private static final RequestConfig CONFIG = RequestConfig.custom()
  24. .setConnectTimeout(5_000).setConnectionRequestTimeout(5_000)
  25. .setSocketTimeout(10_000).build();
  26. public String queryCredit(Map<String, String> fields) {
  27. return post(QUERY_URL, fields);
  28. }
  29. public String doAction(Map<String, String> fields) {
  30. return post(ACTION_URL, fields);
  31. }
  32. private String post(String url, Map<String, String> fields) {
  33. List<NameValuePair> form = new ArrayList<>();
  34. fields.forEach((name, value) -> form.add(new BasicNameValuePair(name, value)));
  35. HttpPost post = new HttpPost(url);
  36. post.setEntity(new UrlEncodedFormEntity(form, StandardCharsets.UTF_8));
  37. try (CloseableHttpClient client = HttpClients.custom()
  38. .setDefaultRequestConfig(CONFIG).disableRedirectHandling().build();
  39. CloseableHttpResponse response = client.execute(post)) {
  40. int status = response.getStatusLine().getStatusCode();
  41. if (status < 200 || status >= 300 || response.getEntity() == null
  42. || response.getEntity().getContentLength() > MAX_RESPONSE_BYTES) {
  43. throw new IllegalStateException("OMG refund gateway returned invalid HTTP response");
  44. }
  45. byte[] bytes = EntityUtils.toByteArray(response.getEntity());
  46. if (bytes.length > MAX_RESPONSE_BYTES) {
  47. throw new IllegalStateException("OMG refund gateway response is oversized");
  48. }
  49. return new String(bytes, StandardCharsets.UTF_8);
  50. } catch (IOException error) {
  51. throw new IllegalStateException("OMG refund gateway unavailable", error);
  52. }
  53. }
  54. }