---
url: "https://pay.weixin.qq.com/doc/brand/4015782988-java.md"
title: "创建品牌门店-请求示例-java"
doc_id: 4015782988
example_lang: "java"
---

## Java


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

```
package com.java.demo;

import com.java.utils.WXPayBrandUtility; // 引用微信支付工具库，参考：https://pay.weixin.qq.com/doc/brand/4015826861

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;

/**
 * 创建品牌门店
 */
public class CreateBrandStore {
  private static String HOST = "https://api.mch.weixin.qq.com";
  private static String METHOD = "POST";
  private static String PATH = "/brand/store/brandstores";

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

    BrandStoresCreateRequest request = new BrandStoresCreateRequest();
    request.storeBasics = new StoreBase();
    request.storeBasics.storeReferenceId = "MDL001";
    request.storeBasics.branchName = "海岸城店";
    request.storeAddress = new StoreLocation();
    request.storeAddress.addressCode = "440305";
    request.storeAddress.addressDetail = "深南大道10000号腾讯大厦1楼";
    request.storeAddress.addressComplements = "地铁A口右侧100米";
    request.storeAddress.longitude = "112.63484";
    request.storeAddress.latitude = "37.75464";
    request.storeBusiness = new StoreBusiness();
    request.storeBusiness.servicePhone = "0755-86013388|0755-86013399";
    request.storeBusiness.businessHours = "周一至周五 09:00-20:00|周六至周日 10:00-次日22:00";
    try {
      BrandStoresEntity response = client.run(request);
        // TODO: 请求成功，继续业务逻辑
        System.out.println(response);
    } catch (WXPayBrandUtility.ApiException e) {
        // TODO: 请求失败，根据状态码执行不同的逻辑
        e.printStackTrace();
    }
  }

  public BrandStoresEntity run(BrandStoresCreateRequest request) {
    String uri = PATH;
    String reqBody = WXPayBrandUtility.toJson(request);

    Request.Builder reqBuilder = new Request.Builder().url(HOST + uri);
    reqBuilder.addHeader("Accept", "application/json");
    reqBuilder.addHeader("Wechatpay-Serial", wechatPayPublicKeyId);
    reqBuilder.addHeader("Authorization", WXPayBrandUtility.buildAuthorization(brand_id, 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 = WXPayBrandUtility.extractBody(httpResponse);
      if (httpResponse.code() >= 200 && httpResponse.code() < 300) {
        // 2XX 成功，验证应答签名
        WXPayBrandUtility.validateResponse(this.wechatPayPublicKeyId, this.wechatPayPublicKey,
            httpResponse.headers(), respBody);

        // 从HTTP应答报文构建返回数据
        return WXPayBrandUtility.fromJson(respBody, BrandStoresEntity.class);
      } else {
        throw new WXPayBrandUtility.ApiException(httpResponse.code(), respBody, httpResponse.headers());
      }
    } catch (IOException e) {
      throw new UncheckedIOException("Sending request to " + uri + " failed.", e);
    }
  }

  private final String brand_id;
  private final String certificateSerialNo;
  private final PrivateKey privateKey;
  private final String wechatPayPublicKeyId;
  private final PublicKey wechatPayPublicKey;

  public CreateBrandStore(String brand_id, String certificateSerialNo, String privateKeyFilePath, String wechatPayPublicKeyId, String wechatPayPublicKeyFilePath) {
    this.brand_id = brand_id;
    this.certificateSerialNo = certificateSerialNo;
    this.privateKey = WXPayBrandUtility.loadPrivateKeyFromPath(privateKeyFilePath);
    this.wechatPayPublicKeyId = wechatPayPublicKeyId;
    this.wechatPayPublicKey = WXPayBrandUtility.loadPublicKeyFromPath(wechatPayPublicKeyFilePath);
  }

  public static class BrandStoresCreateRequest {
    @SerializedName("store_basics")
    public StoreBase storeBasics;

    @SerializedName("store_address")
    public StoreLocation storeAddress;

    @SerializedName("store_business")
    public StoreBusiness storeBusiness;
  }

  public static class BrandStoresEntity {
    @SerializedName("store_id")
    public String storeId;

    @SerializedName("store_state")
    public StoreState storeState;

    @SerializedName("audit_state")
    public AuditState auditState;

    @SerializedName("review_reject_reason")
    public String reviewRejectReason;

    @SerializedName("store_basics")
    public StoreBase storeBasics;

    @SerializedName("store_address")
    public StoreLocation storeAddress;

    @SerializedName("store_business")
    public StoreBusiness storeBusiness;

    @SerializedName("store_recipient")
    public List<StoreRecipient> storeRecipient;
  }

  public static class StoreBase {
    @SerializedName("store_reference_id")
    public String storeReferenceId;

    @SerializedName("branch_name")
    public String branchName;
  }

  public static class StoreLocation {
    @SerializedName("address_code")
    public String addressCode;

    @SerializedName("address_detail")
    public String addressDetail;

    @SerializedName("address_complements")
    public String addressComplements;

    @SerializedName("longitude")
    public String longitude;

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

  public static class StoreBusiness {
    @SerializedName("service_phone")
    public String servicePhone;

    @SerializedName("business_hours")
    public String businessHours;
  }

  public enum StoreState {
    @SerializedName("OPEN")
    OPEN,
    @SerializedName("CREATING")
    CREATING,
    @SerializedName("CLOSED")
    CLOSED
  }

  public enum AuditState {
    @SerializedName("SUCCESS")
    SUCCESS,
    @SerializedName("PROCESSING")
    PROCESSING,
    @SerializedName("REJECTED")
    REJECTED
  }

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

    @SerializedName("company_name")
    public String companyName;

    @SerializedName("recipient_state")
    public RecipientState recipientState;
  }

  public enum RecipientState {
    @SerializedName("CONFIRMED")
    CONFIRMED,
    @SerializedName("ADMIN_REJECTED")
    ADMIN_REJECTED,
    @SerializedName("CONFIRMING")
    CONFIRMING,
    @SerializedName("TIMEOUT_REJECTED")
    TIMEOUT_REJECTED
  }

}

```
