package com.dsh.course.util.wx; import com.google.gson.annotations.SerializedName; import okhttp3.*; import java.io.IOException; import java.io.UncheckedIOException; import java.security.PrivateKey; import java.security.PublicKey; import java.util.List; /** * App下单 */ public class PartnerAppPrepay { private static String HOST = "https://api.mch.weixin.qq.com"; private static String METHOD = "POST"; private static String PATH = "/v3/pay/partner/transactions/app"; // public static void main(String[] args) { // // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/partner/4013080340 // PartnerAppPrepay client = new PartnerAppPrepay( // "1681873607", // 商户号,是由微信支付系统生成并分配给每个商户的唯一标识符,商户号获取方式参考https://pay.weixin.qq.com/doc/v3/partner/4013080340 // "55714944F7A7E52526F708280B176DCC838F371A", // 商户API证书序列号,如何获取请参考https://pay.weixin.qq.com/doc/v3/partner/4013058924 // "E:\\wanpai\\1681873607_20250424_cert\\apiclient_key.pem", // 商户API证书私钥文件路径,本地文件路径 // "PUB_KEY_ID_0116818736072025042400351694002605", // 微信支付公钥ID,如何获取请参考https://pay.weixin.qq.com/doc/v3/partner/4013038589 // "E:\\wanpai\\pub_key.pem" // 微信支付公钥文件路径,本地文件路径 // ); // // PartnerAPIv3CommonPrepayRequest request = new PartnerAPIv3CommonPrepayRequest(); // request.spAppid = "wx41d32f362ba0f911"; // request.spMchid = WxV3PayConfig.Mch_ID; // request.subMchid = "1720719391"; // request.description = "Image形象店-深圳腾大-QQ公仔"; // request.outTradeNo = "12177525012014070332333680182"; // request.notifyUrl = "https://www.weixin.qq.com/wxpay/pay.php"; //// request.goodsTag = "WXG"; //// request.settleInfo = new PartnerSettleInfo(); //// request.settleInfo.profitSharing = false; // request.amount = new CommonAmountInfo(); // request.amount.total = 100L; // request.amount.currency = "CNY"; //// request.detail = new CouponInfo(); //// request.detail.costPrice = 1L; //// request.detail.invoiceId = "wx123"; //// request.detail.goodsDetail = new ArrayList<>(); //// { //// GoodsDetail item0 = new GoodsDetail(); //// item0.merchantGoodsId = "1246464644"; //// item0.wechatpayGoodsId = "1001"; //// item0.goodsName = "iPhone6s 16G"; //// item0.quantity = 1L; //// item0.unitPrice = 528800L; //// request.detail.goodsDetail.add(item0); //// }; //// request.sceneInfo = new CommonSceneInfo(); //// request.sceneInfo.payerClientIp = "14.23.150.211"; //// request.sceneInfo.deviceId = "013467007045764"; //// request.sceneInfo.storeInfo = new StoreInfo(); //// request.sceneInfo.storeInfo.id = "0001"; //// request.sceneInfo.storeInfo.name = "腾讯大厦分店"; //// request.sceneInfo.storeInfo.areaCode = "440305"; //// request.sceneInfo.storeInfo.address = "广东省深圳市南山区科技中一道10000号"; // try { // PartnerAPIv3AppPrepayResponse response = client.run(request); // // // TODO: 请求成功,继续业务逻辑 // System.err.println("微信申请成功,预支付ID: " + response.prepayId); // } catch (WXPayUtility.ApiException e) { // // TODO: 请求失败,根据状态码执行不同的逻辑 // e.printStackTrace(); // } // } public PartnerAPIv3AppPrepayResponse run(PartnerAPIv3CommonPrepayRequest 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, PartnerAPIv3AppPrepayResponse.class); } else { throw new WXPayUtility.ApiException(httpResponse.code(), respBody, httpResponse.headers()); } } catch (IOException e) { throw new UncheckedIOException("Sending request to " + uri + " failed.", e); } } private final String mchid; private final String certificateSerialNo; private final PrivateKey privateKey; private final String wechatPayPublicKeyId; private final PublicKey wechatPayPublicKey; public PartnerAppPrepay(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 static class PartnerSettleInfo { @SerializedName("profit_sharing") public Boolean profitSharing; } public static class CommonAmountInfo { @SerializedName("total") public Long total; @SerializedName("currency") public String currency; } public static class CommonSceneInfo { @SerializedName("payer_client_ip") public String payerClientIp; @SerializedName("device_id") public String deviceId; @SerializedName("store_info") public StoreInfo storeInfo; } public static class GoodsDetail { @SerializedName("merchant_goods_id") public String merchantGoodsId; @SerializedName("wechatpay_goods_id") public String wechatpayGoodsId; @SerializedName("goods_name") public String goodsName; @SerializedName("quantity") public Long quantity; @SerializedName("unit_price") public Long unitPrice; } public static class PartnerAPIv3AppPrepayResponse { @SerializedName("prepay_id") public String prepayId; } public static class PartnerAPIv3CommonPrepayRequest { @SerializedName("sp_appid") public String spAppid; @SerializedName("sp_mchid") public String spMchid; @SerializedName("sub_appid") public String subAppid; @SerializedName("sub_mchid") public String subMchid; @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("settle_info") public PartnerSettleInfo settleInfo; @SerializedName("support_fapiao") public Boolean supportFapiao; @SerializedName("amount") public CommonAmountInfo amount; @SerializedName("detail") public CouponInfo detail; @SerializedName("scene_info") public CommonSceneInfo sceneInfo; } public static class CouponInfo { @SerializedName("cost_price") public Long costPrice; @SerializedName("invoice_id") public String invoiceId; @SerializedName("goods_detail") public List goodsDetail; } public static class StoreInfo { @SerializedName("id") public String id; @SerializedName("name") public String name; @SerializedName("area_code") public String areaCode; @SerializedName("address") public String address; } }