整理微信v3支付 ,接口规则, 请求通用方法

1.需要的依赖

com.github.wechatpay-apiv3
wechatpay-apache-httpclient
0.2.0

//创建连接时的请求认证数据
merchantId:商户id
serialNo : 商户证书序列号
privateKeyFilePath :商户私钥路径
wechatpayCertificatePath :平台证书路径

public static CloseableHttpClient getClient(String merchantId, String serialNo, String privateKeyFilePath, String wechatpayCertificatePath) throws Exception {
		WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
				.withMerchant(merchantId, serialNo, PemUtil.loadPrivateKey(new FileInputStream(privateKeyFilePath)));

		List<X509Certificate> certs = new ArrayList<>();
		certs.add(PemUtil.loadCertificate(new FileInputStream(wechatpayCertificatePath)));
		builder.withWechatpay(certs);
		return builder.build();
	}

//使用方法
//创建post请求
// url 微信请求路径
 //jsonData 参数 jsonString类型
 //  ResultModel  自己去定义,返回
public static ResultModel<HashMap> createPostHttp(String url,String jsonData) throws Exception {
		ResultModel<HashMap> result=new ResultModel<>();
		CloseableHttpResponse response=null;

			HttpPost httpPost = new HttpPost(url);
			StringEntity reqEntity = new StringEntity(
					jsonData, ContentType.create("application/json", "utf-8"));
			System.out.println(jsonData);
			httpPost.setEntity(reqEntity);
			httpPost.addHeader("Accept", "application/json");
			//merchantId,  serialNo, privateKeyFilePath, wechatpayCertificatePath 省略
			response = getClient(,,,,).execute(httpPost);

		assertTrue(response.getStatusLine().getStatusCode() != 401);
		try {
			int statusCode = response.getStatusLine().getStatusCode();
			HttpEntity entity = response.getEntity();
			String body = EntityUtils.toString(entity);
			result.setCode(statusCode);
			result.setData(JSON.parseObject(body, HashMap.class));
			EntityUtils.consume(entity);
		} finally {
			response.close();
		}
		return result;
	}

你可能感兴趣的:(java,微信v3支付,java,spring)