From 81da61b828e29b7745e1382dfbbaeb685dc083ef Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期二, 23 一月 2024 17:17:55 +0800 Subject: [PATCH] 抖音转链修改 --- utils/src/main/java/org/yeshi/utils/wx/WXPayV3Util.java | 87 ++++++++++++++++++++++++++++++++++++++++--- 1 files changed, 81 insertions(+), 6 deletions(-) diff --git a/utils/src/main/java/org/yeshi/utils/wx/WXPayV3Util.java b/utils/src/main/java/org/yeshi/utils/wx/WXPayV3Util.java index 457354d..d9b5448 100644 --- a/utils/src/main/java/org/yeshi/utils/wx/WXPayV3Util.java +++ b/utils/src/main/java/org/yeshi/utils/wx/WXPayV3Util.java @@ -1,5 +1,6 @@ package org.yeshi.utils.wx; +import com.google.gson.Gson; import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder; import com.wechat.pay.contrib.apache.httpclient.auth.AutoUpdateCertificatesVerifier; import com.wechat.pay.contrib.apache.httpclient.auth.PrivateKeySigner; @@ -10,6 +11,7 @@ import org.apache.commons.io.Charsets; import org.apache.commons.io.IOUtils; import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; @@ -17,6 +19,7 @@ import org.apache.http.util.EntityUtils; import org.yeshi.utils.StringUtil; import org.yeshi.utils.entity.wx.WXAPPInfo; +import org.yeshi.utils.entity.wx.WXPayOrderInfoV3; import org.yeshi.utils.entity.wx.WXPlaceOrderParams; import org.yeshi.utils.exception.WXOrderException; import org.yeshi.utils.exception.WXPlaceOrderParamsException; @@ -25,6 +28,7 @@ import java.io.FileInputStream; import java.math.BigDecimal; import java.net.URLEncoder; +import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Paths; import java.security.PrivateKey; @@ -62,9 +66,14 @@ */ private static JSONObject request(String url, String requestData, WXAPPInfo app) throws Exception { HttpPost httpPost = new HttpPost(url); + HttpGet httpGet=new HttpGet(url); + + if (StringUtil.isNullOrEmpty(requestData)) { + requestData = "{}"; + } if (!StringUtil.isNullOrEmpty(requestData)) { - StringEntity entity = new StringEntity(requestData, ContentType.APPLICATION_JSON.withCharset(Charsets.UTF_8)); + StringEntity entity = new StringEntity(requestData, ContentType.APPLICATION_JSON.withCharset(Charset.forName("UTF-8"))); entity.setContentType("application/json;charset=utf-8"); httpPost.setEntity(entity); } @@ -73,6 +82,43 @@ //瀹屾垚绛惧悕骞舵墽琛岃姹� CloseableHttpClient httpClient = getHttpClient(app); CloseableHttpResponse response = httpClient.execute(httpPost); + try { + int statusCode = response.getStatusLine().getStatusCode(); + if (statusCode == 200) { + System.out.println("success,return body = " + EntityUtils.toString(response.getEntity())); + String result = EntityUtils.toString(response.getEntity()); + JSONObject resultJson = JSONObject.fromObject(result); + return resultJson; + } else if (statusCode == 204) { + System.out.println("success"); + } else { + System.out.println("failed,resp code = " + statusCode + ",return body = " + EntityUtils.toString(response.getEntity())); + throw new Exception("request failed"); + } + } finally { + response.close(); + } + return null; + } + + + private static JSONObject requestGet(String url, String requestData, WXAPPInfo app) throws Exception { + HttpGet httpGet=new HttpGet(url); + + if (StringUtil.isNullOrEmpty(requestData)) { + requestData = "{}"; + } + +// if (!StringUtil.isNullOrEmpty(requestData)) { +// StringEntity entity = new StringEntity(requestData, ContentType.APPLICATION_JSON.withCharset(Charset.forName("UTF-8"))); +// entity.setContentType("application/json;charset=utf-8"); +// httpPost.setEntity(entity); +// } + httpGet.setHeader("Accept", "application/json;charset=utf-8"); + + //瀹屾垚绛惧悕骞舵墽琛岃姹� + CloseableHttpClient httpClient = getHttpClient(app); + CloseableHttpResponse response = httpClient.execute(httpGet); try { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == 200) { @@ -163,15 +209,33 @@ * @throws WXOrderException */ public static boolean isPaySuccess(String orderNo, WXAPPInfo app) throws Exception { - String url = String.format("https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/%s?mchid=%s", URLEncoder.encode(orderNo), app.getMchId()); - JSONObject result = request(url, null, app); - if (result == null) + WXPayOrderInfoV3 info = getPayOrderInfo(orderNo, app); + if (info == null) return false; - return "SUCCESS".equalsIgnoreCase(result.optString("trade_state")); + return "SUCCESS".equalsIgnoreCase(info.getTrade_state()); + } + + + /** + * 鑾峰彇璁㈠崟淇℃伅 + * + * @param orderNo + * @param app + * @return + * @throws Exception + */ + public static WXPayOrderInfoV3 getPayOrderInfo(String orderNo, WXAPPInfo app) throws Exception { + String url = String.format("https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/%s?mchid=%s", URLEncoder.encode(orderNo), app.getMchId()); + JSONObject result = requestGet(url, "", app); + if (result == null) + return null; + return new Gson().fromJson(result.toString(), WXPayOrderInfoV3.class); } public static void main(String[] args) { + + String privateKey = ""; try { String content = IOUtils.toString(new FileInputStream("D:\\椤圭洰\\杩斿埄鍒竆\鍟嗘埛骞冲彴\\1520950211_20210125_cert\\apiclient_key.pem")); @@ -181,19 +245,30 @@ } catch (Exception e) { } + WXAPPInfo app = new WXAPPInfo("wxa99686bb65a9f466", "1520950211", "454328C324C6CC21355D064B44D6524CD7506DD0", privateKey, "XYJkJ2018FAfaodCCx899mLl138rfGVd"); + + WXPlaceOrderParams params = new WXPlaceOrderParams(); params.setBody("褰辫澶у叏VIP-鍖呮湀"); params.setFee(new BigDecimal("0.1")); params.setNotifyUrl("http://api.ysdq.yeshitv.com:8089/BuWan/wx/pay/vip"); params.setOrderNo("buwan-vip-8"); params.setIp("113.249.192.231"); - params.setApp(new WXAPPInfo("wxa99686bb65a9f466", "1520950211", "454328C324C6CC21355D064B44D6524CD7506DD0", privateKey, "XYJkJ2018FAfaodCCx899mLl138rfGVd")); + params.setApp(app); try { String payUrl = WXPayV3Util.createH5Order(params, "http://vip.ysdq.yeshitv.com/wx_result.html"); System.out.println(payUrl); } catch (Exception e) { e.printStackTrace(); } + + try { + WXPayOrderInfoV3 info = WXPayV3Util.getPayOrderInfo("buwan-vip-8", app); + System.out.println(info); + } catch (Exception e) { + e.printStackTrace(); + } } + } -- Gitblit v1.8.0