package com.ruoyi.system.wxPay.utils; import com.google.gson.annotations.SerializedName; import okhttp3.*; import java.io.IOException; import java.io.UncheckedIOException; import java.security.PrivateKey; import java.security.PublicKey; /** * JSAPI下单 */ public class JsapiPrepay { private static String HOST = "https://api.mch.weixin.qq.com"; private static String METHOD = "POST"; private static String PATH = "/v3/pay/transactions/jsapi"; private final String mchId; private final String certificateSerialNo; private final PrivateKey privateKey; private final String wechatPayPublicKeyId; private final PublicKey wechatPayPublicKey; public JsapiPrepay(String mchId, String certificateSerialNo, String privateKeyFilePath, String wechatPayPublicKeyId, String wechatPayPublicKeyFilePath) { this.mchId = mchId; this.certificateSerialNo = certificateSerialNo; this.privateKey = WXPayUtility.loadPrivateKeyFromPath(privateKeyFilePath); this.wechatPayPublicKeyId = wechatPayPublicKeyId; this.wechatPayPublicKey = WXPayUtility.loadPublicKeyFromPath(wechatPayPublicKeyFilePath); } public DirectAPIv3JsapiPrepayResponse run(DirectAPIv3JsapiPrepayRequest request) { String uri = PATH; String reqBody = WXPayUtility.toJson(request); Request.Builder reqBuilder = new Request.Builder().url(HOST + uri); reqBuilder.addHeader("Accept", "application/json"); reqBuilder.addHeader("Wechatpay-Serial", wechatPayPublicKeyId); reqBuilder.addHeader("Authorization", WXPayUtility.buildAuthorization(mchId, certificateSerialNo,privateKey, METHOD, uri, reqBody)); reqBuilder.addHeader("Content-Type", "application/json"); RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), reqBody); reqBuilder.method(METHOD, requestBody); Request httpRequest = reqBuilder.build(); // 发送HTTP请求 OkHttpClient client = new OkHttpClient.Builder().build(); try (Response httpResponse = client.newCall(httpRequest).execute()) { String respBody = WXPayUtility.extractBody(httpResponse); if (httpResponse.code() >= 200 && httpResponse.code() < 300) { // 2XX 成功,验证应答签名 WXPayUtility.validateResponse(this.wechatPayPublicKeyId, this.wechatPayPublicKey, httpResponse.headers(), respBody); // 从HTTP应答报文构建返回数据 return WXPayUtility.fromJson(respBody, DirectAPIv3JsapiPrepayResponse.class); } else { throw new WXPayUtility.ApiException(httpResponse.code(), respBody, httpResponse.headers()); } } catch (IOException e) { throw new UncheckedIOException("Sending request to " + uri + " failed.", e); } } public static class DirectAPIv3JsapiPrepayRequest { @SerializedName("appid") public String appid; @SerializedName("mchid") public String mchid; @SerializedName("description") public String description; @SerializedName("out_trade_no") public String outTradeNo; @SerializedName("time_expire") public String timeExpire; @SerializedName("attach") public String attach; @SerializedName("notify_url") public String notifyUrl; @SerializedName("goods_tag") public String goodsTag; @SerializedName("support_fapiao") public Boolean supportFapiao; @SerializedName("amount") public CommonAmountInfo amount; @SerializedName("payer") public JsapiReqPayerInfo payer; } public static class DirectAPIv3JsapiPrepayResponse { @SerializedName("prepay_id") public String prepayId; } public static class CommonAmountInfo { @SerializedName("total") public Long total; @SerializedName("currency") public String currency; } public static class JsapiReqPayerInfo { @SerializedName("openid") public String openid; } }