| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- package com.ruoyi.app.omgpay;
- import org.apache.http.NameValuePair;
- import org.apache.http.client.config.RequestConfig;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- import org.apache.http.client.methods.CloseableHttpResponse;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.impl.client.CloseableHttpClient;
- import org.apache.http.impl.client.HttpClients;
- import org.apache.http.message.BasicNameValuePair;
- import org.apache.http.util.EntityUtils;
- import org.springframework.stereotype.Component;
- import java.io.IOException;
- import java.nio.charset.StandardCharsets;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- /** OMG 正式信用卡查询/退款 HTTP 边界;调用前由服务确认环境。 */
- @Component
- public class OmgRefundGateway {
- private static final String QUERY_URL = "https://payment.funpoint.com.tw/CreditDetail/QueryTrade/V2";
- private static final String ACTION_URL = "https://payment.funpoint.com.tw/CreditDetail/DoAction";
- private static final int MAX_RESPONSE_BYTES = 32 * 1024;
- private static final RequestConfig CONFIG = RequestConfig.custom()
- .setConnectTimeout(5_000).setConnectionRequestTimeout(5_000)
- .setSocketTimeout(10_000).build();
- public String queryCredit(Map<String, String> fields) {
- return post(QUERY_URL, fields);
- }
- public String doAction(Map<String, String> fields) {
- return post(ACTION_URL, fields);
- }
- private String post(String url, Map<String, String> fields) {
- List<NameValuePair> form = new ArrayList<>();
- fields.forEach((name, value) -> form.add(new BasicNameValuePair(name, value)));
- HttpPost post = new HttpPost(url);
- post.setEntity(new UrlEncodedFormEntity(form, StandardCharsets.UTF_8));
- try (CloseableHttpClient client = HttpClients.custom()
- .setDefaultRequestConfig(CONFIG).disableRedirectHandling().build();
- CloseableHttpResponse response = client.execute(post)) {
- int status = response.getStatusLine().getStatusCode();
- if (status < 200 || status >= 300 || response.getEntity() == null
- || response.getEntity().getContentLength() > MAX_RESPONSE_BYTES) {
- throw new IllegalStateException("OMG refund gateway returned invalid HTTP response");
- }
- byte[] bytes = EntityUtils.toByteArray(response.getEntity());
- if (bytes.length > MAX_RESPONSE_BYTES) {
- throw new IllegalStateException("OMG refund gateway response is oversized");
- }
- return new String(bytes, StandardCharsets.UTF_8);
- } catch (IOException error) {
- throw new IllegalStateException("OMG refund gateway unavailable", error);
- }
- }
- }
|