|
|
|
@ -6,6 +6,7 @@ import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder; |
|
|
|
import com.wechat.pay.contrib.apache.httpclient.auth.*; |
|
|
|
import com.wechat.pay.contrib.apache.httpclient.auth.*; |
|
|
|
import com.wechat.pay.contrib.apache.httpclient.util.PemUtil; |
|
|
|
import com.wechat.pay.contrib.apache.httpclient.util.PemUtil; |
|
|
|
import org.apache.http.client.methods.CloseableHttpResponse; |
|
|
|
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.client.methods.HttpPost; |
|
|
|
import org.apache.http.entity.StringEntity; |
|
|
|
import org.apache.http.entity.StringEntity; |
|
|
|
import org.apache.http.impl.client.CloseableHttpClient; |
|
|
|
import org.apache.http.impl.client.CloseableHttpClient; |
|
|
|
@ -44,6 +45,7 @@ public class WxPayUtils { |
|
|
|
|
|
|
|
|
|
|
|
// JSApi下单url地址
|
|
|
|
// JSApi下单url地址
|
|
|
|
public static String JSApiPayUrl = "https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi"; |
|
|
|
public static String JSApiPayUrl = "https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi"; |
|
|
|
|
|
|
|
public static String ClocePayUrl = "https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/{out_trade_no}/close"; |
|
|
|
|
|
|
|
|
|
|
|
// 商户私钥 获取对应的签名需要 在微信支付商户平台-账户中心-API安全里设置 养宠物那边复制的
|
|
|
|
// 商户私钥 获取对应的签名需要 在微信支付商户平台-账户中心-API安全里设置 养宠物那边复制的
|
|
|
|
// 对应文档 https://pay.weixin.qq.com/wiki/doc/apiv3_partner/open/pay/chapter2_1.shtml
|
|
|
|
// 对应文档 https://pay.weixin.qq.com/wiki/doc/apiv3_partner/open/pay/chapter2_1.shtml
|
|
|
|
@ -159,6 +161,89 @@ public class WxPayUtils { |
|
|
|
return ""; |
|
|
|
return ""; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 关闭订单 |
|
|
|
|
|
|
|
* @param outTradeNo |
|
|
|
|
|
|
|
* @return |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static String cloceOrderJSApiV3(String outTradeNo) { |
|
|
|
|
|
|
|
payLoading(); |
|
|
|
|
|
|
|
//请求URL
|
|
|
|
|
|
|
|
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/" + outTradeNo + "/close"); |
|
|
|
|
|
|
|
// 请求body参数
|
|
|
|
|
|
|
|
JSONObject amountJson = new JSONObject(); |
|
|
|
|
|
|
|
amountJson.put("mchid", mchId); |
|
|
|
|
|
|
|
StringEntity entity = new StringEntity(JSONObject.toJSONString(amountJson), "utf-8"); |
|
|
|
|
|
|
|
entity.setContentType("application/json"); |
|
|
|
|
|
|
|
httpPost.setEntity(entity); |
|
|
|
|
|
|
|
httpPost.setHeader("Accept", "application/json"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//完成签名并执行请求
|
|
|
|
|
|
|
|
CloseableHttpResponse response = null; |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
response = httpClient.execute(httpPost); |
|
|
|
|
|
|
|
int statusCode = response.getStatusLine().getStatusCode(); |
|
|
|
|
|
|
|
if (statusCode == 200) { |
|
|
|
|
|
|
|
return EntityUtils.toString(response.getEntity()); |
|
|
|
|
|
|
|
} else if (statusCode == 204) { |
|
|
|
|
|
|
|
return ""; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
System.out.println("failed,resp code = " + statusCode + ",return body = " + EntityUtils.toString(response.getEntity())); |
|
|
|
|
|
|
|
throw new IOException("request failed"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
assert response != null; |
|
|
|
|
|
|
|
response.close(); |
|
|
|
|
|
|
|
httpClient.close(); |
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return ""; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 查询订单 |
|
|
|
|
|
|
|
* @param outTradeNo |
|
|
|
|
|
|
|
* @return |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static String getOrderJSApiV3(String outTradeNo) { |
|
|
|
|
|
|
|
payLoading(); |
|
|
|
|
|
|
|
//请求URL
|
|
|
|
|
|
|
|
HttpGet httpGet = new HttpGet("https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/" + outTradeNo + "?mchid=" + mchId); |
|
|
|
|
|
|
|
// 请求body参数
|
|
|
|
|
|
|
|
httpGet.setHeader("Accept", "application/json"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//完成签名并执行请求
|
|
|
|
|
|
|
|
CloseableHttpResponse response = null; |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
response = httpClient.execute(httpGet); |
|
|
|
|
|
|
|
int statusCode = response.getStatusLine().getStatusCode(); |
|
|
|
|
|
|
|
if (statusCode == 200) { |
|
|
|
|
|
|
|
return EntityUtils.toString(response.getEntity()); |
|
|
|
|
|
|
|
} else if (statusCode == 204) { |
|
|
|
|
|
|
|
return ""; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
System.out.println("failed,resp code = " + statusCode + ",return body = " + EntityUtils.toString(response.getEntity())); |
|
|
|
|
|
|
|
throw new IOException("request failed"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
assert response != null; |
|
|
|
|
|
|
|
response.close(); |
|
|
|
|
|
|
|
httpClient.close(); |
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return ""; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 第四步:生成支付参数 |
|
|
|
* 第四步:生成支付参数 |
|
|
|
@ -202,7 +287,6 @@ public class WxPayUtils { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 构造下单的json(第三步需要) |
|
|
|
* 构造下单的json(第三步需要) |
|
|
|
* |
|
|
|
* |
|
|
|
|