微信支付v3版

微信支付官方地址
1
| https://pay.weixin.qq.com/index.php/core/home/login?return_url=%2F
|
1.依赖
1 2 3 4
| // https://mvnrepository.com/artifact/com.github.wechatpay-apiv3/wechatpay-apache-httpclient implementation group: 'com.github.wechatpay-apiv3', name: 'wechatpay-apache-httpclient', version: '0.2.2' // https://mvnrepository.com/artifact/com.github.wxpay/wxpay-sdk implementation group: 'com.github.wxpay', name: 'wxpay-sdk', version: '0.0.3'
|
2.需要准备的材料
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public class WxPayConstant {
private String wxMerchantId;
private String wxAppId;
private String wxPrivateKey;
private String wxApiKey;
private String wxCertSerialNo; }
|
3.代码示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| package cn.bodor.order.utils;
import cn.bodor.order.exception.BusinessException; import com.alibaba.fastjson.JSONObject; import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder; import com.wechat.pay.contrib.apache.httpclient.auth.*; import com.wechat.pay.contrib.apache.httpclient.util.PemUtil; import lombok.extern.slf4j.Slf4j; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.message.BasicHeader; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils;
import java.io.ByteArrayInputStream; import java.io.IOException; import java.security.PrivateKey;
@Slf4j public class WxpayUtils {
public static Verifier verifier; public static CloseableHttpClient httpClient;
public static JSONObject wxPayPost(String url, String data, String mchId, String serialNo, String privateKey, String apiv3) { try { PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey( new ByteArrayInputStream(privateKey.getBytes("utf-8"))); verifier = new AutoUpdateCertificatesVerifier( new WechatPay2Credentials(mchId, new PrivateKeySigner(serialNo, merchantPrivateKey)), apiv3.getBytes("utf-8"));
httpClient = WechatPayHttpClientBuilder.create() .withMerchant(mchId, serialNo, merchantPrivateKey) .withValidator(new WechatPay2Validator(verifier)) .build(); HttpPost httpPost = new HttpPost(url); httpPost.setHeader("Content-type", "application/json"); httpPost.setHeader("Accept", "application/json"); StringEntity entity = new StringEntity(data, "UTF-8"); entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); httpPost.setEntity(entity); CloseableHttpResponse response = httpClient.execute(httpPost); log.error("微信接口result状态码----code:{}",response.getStatusLine().getStatusCode()); if (response.getStatusLine().getStatusCode() != 200) { throw new BusinessException("微信接口调用失败!"); } HttpEntity entity2 = response.getEntity(); EntityUtils.consume(entity2); JSONObject jsonObject = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); log.info("微信接口调用成功----json:{}", jsonObject); return jsonObject; } catch (Exception e) { log.error("调起微信接口出现问题----message:{}" + e.getMessage()); throw new BusinessException("微信接口调用失败!"); } finally { try { httpClient.close(); } catch (IOException e) { log.error("微信接口关闭资源报错----message:{}" + e.getMessage()); throw new BusinessException("微信接口关闭资源报错!"); } } } }
|
如果返回400 Bad,用下面的postman工具测试参数
https://github.com/wechatpay-apiv3/wechatpay-postman-script