---
url: "https://pay.weixin.qq.com/doc/v3/partner/4012758021-java.md"
title: "APP合单下单-请求示例-java"
doc_id: 4012758021
example_lang: "java"
---

## Java


需配合微信支付工具库 WXPayUtility 使用，请参考 [Java](https://pay.weixin.qq.com/doc/v3/partner/4014985777.md)

```
package com.java.demo;

import com.java.utils.WXPayUtility; // 引用微信支付工具库，参考：https://pay.weixin.qq.com/doc/v3/partner/4014985777

import com.google.gson.annotations.SerializedName;
import com.google.gson.annotations.Expose;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 合单下单-APP
 */
public class UnionAppPrepay {
  private static String HOST = "https://api.mch.weixin.qq.com";
  private static String METHOD = "POST";
  private static String PATH = "/v3/combine-transactions/app";

  public static void main(String[] args) {
    // TODO: 请准备商户开发必要参数，参考：https://pay.weixin.qq.com/doc/v3/partner/4013080340
    UnionAppPrepay client = new UnionAppPrepay(
      "19xxxxxxxx",                    // 商户号，是由微信支付系统生成并分配给每个商户的唯一标识符，商户号获取方式参考 https://pay.weixin.qq.com/doc/v3/partner/4013080340
      "1DDE55AD98Exxxxxxxxxx",         // 商户API证书序列号，如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013058924
      "/path/to/apiclient_key.pem",     // 商户API证书私钥文件路径，本地文件路径
      "PUB_KEY_ID_xxxxxxxxxxxxx",      // 微信支付公钥ID，如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013038589
      "/path/to/wxp_pub.pem"           // 微信支付公钥文件路径，本地文件路径
    );

    UnionAPIv3AppPrepayRequest request = new UnionAPIv3AppPrepayRequest();
    request.combineAppid = "wxd678efh567hg6787";
    request.combineOutTradeNo = "20150806125346";
    request.combineMchid = "1900000109";
    request.sceneInfo = new UnionSceneInfo();
    request.sceneInfo.deviceId = "POS1:1";
    request.sceneInfo.payerClientIp = "14.17.22.32";
    request.subOrders = new ArrayList<>();
    {
      UnionSubOrder subOrdersItem0 = new UnionSubOrder();
      subOrdersItem0.mchid = "1230000109";
      subOrdersItem0.subMchid = "1900000109";
      subOrdersItem0.subAppid = "wxd678efh567hg6999";
      subOrdersItem0.outTradeNo = "20150806125346";
      subOrdersItem0.amount = new UnionAmountInfo();
      subOrdersItem0.amount.totalAmount = 10L;
      subOrdersItem0.amount.currency = "CNY";
      subOrdersItem0.attach = "深圳分店";
      subOrdersItem0.description = "腾讯充值中心-QQ会员充值";
      subOrdersItem0.detail = "买单费用";
      subOrdersItem0.goodsTag = "WXG";
      subOrdersItem0.settleInfo = new UnionSettleInfo();
      subOrdersItem0.settleInfo.profitSharing = false;
      request.subOrders.add(subOrdersItem0);
    };
    request.combinePayerInfo = new UnionAppPayerInfo();
    request.combinePayerInfo.openid = "oUpF8uMuAJO_M2pxb1Q9zNjWeS6o";
    request.timeExpire = "2000-01-01T00:00:00+08:00";
    request.notifyUrl = "https://yourapp.com/notify";
    try {
      UnionAPIv3AppPrepayResponse response = client.run(request);
        // TODO: 请求成功，继续业务逻辑
        System.out.println(response);
    } catch (WXPayUtility.ApiException e) {
        // TODO: 请求失败，根据状态码执行不同的逻辑
        e.printStackTrace();
    }
  }

  public UnionAPIv3AppPrepayResponse run(UnionAPIv3AppPrepayRequest 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, UnionAPIv3AppPrepayResponse.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 UnionAppPrepay(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 UnionAPIv3AppPrepayRequest {
    @SerializedName("combine_appid")
    public String combineAppid;

    @SerializedName("combine_out_trade_no")
    public String combineOutTradeNo;

    @SerializedName("combine_mchid")
    public String combineMchid;

    @SerializedName("scene_info")
    public UnionSceneInfo sceneInfo;

    @SerializedName("sub_orders")
    public List<UnionSubOrder> subOrders = new ArrayList<UnionSubOrder>();

    @SerializedName("combine_payer_info")
    public UnionAppPayerInfo combinePayerInfo;

    @SerializedName("time_expire")
    public String timeExpire;

    @SerializedName("notify_url")
    public String notifyUrl;

    @SerializedName("trade_scenario")
    public String tradeScenario;
  }

  public static class UnionAPIv3AppPrepayResponse {
    @SerializedName("prepay_id")
    public String prepayId;
  }

  public static class UnionSceneInfo {
    @SerializedName("device_id")
    public String deviceId;

    @SerializedName("payer_client_ip")
    public String payerClientIp;
  }

  public static class UnionSubOrder {
    @SerializedName("mchid")
    public String mchid;

    @SerializedName("sub_mchid")
    public String subMchid;

    @SerializedName("sub_appid")
    public String subAppid;

    @SerializedName("out_trade_no")
    public String outTradeNo;

    @SerializedName("amount")
    public UnionAmountInfo amount;

    @SerializedName("attach")
    public String attach;

    @SerializedName("description")
    public String description;

    @SerializedName("detail")
    public String detail;

    @SerializedName("goods_tag")
    public String goodsTag;

    @SerializedName("settle_info")
    public UnionSettleInfo settleInfo;
  }

  public static class UnionAppPayerInfo {
    @SerializedName("openid")
    public String openid;
  }

  public static class UnionAmountInfo {
    @SerializedName("total_amount")
    public Long totalAmount;

    @SerializedName("currency")
    public String currency;
  }

  public static class UnionSettleInfo {
    @SerializedName("profit_sharing")
    public Boolean profitSharing;
  }

}

```
