无关风月
22 小时以前 5dc40fcd64b0513150f1d8335ab849e6d8cdc28e
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package com.dsh.account.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> 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;
  }
}