| | |
| | | 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; |
| | |
| | | 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; |
| | |
| | | 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; |
| | |
| | | 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; |
| | |
| | | */ |
| | | 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); |
| | | } |
| | |
| | | //完成签名并执行请求 |
| | | 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) { |
| | |
| | | * @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")); |
| | |
| | | } 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(); |
| | | } |
| | | } |
| | | |
| | | |
| | | } |