---
url: "https://pay.weixin.qq.com/doc/v3/partner/4015336976-java.md"
title: "查询会员卡模板列表-请求示例-java"
doc_id: 4015336976
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;

/**
 * 查询会员卡模板列表
 */
public class ListCard {
  private static String HOST = "https://api.mch.weixin.qq.com";
  private static String METHOD = "GET";
  private static String PATH = "/v3/brand/partner/card-member/cards";

  public static void main(String[] args) {
    // TODO: 请准备商户开发必要参数，参考：https://pay.weixin.qq.com/doc/v3/partner/4013080340
    ListCard client = new ListCard(
      "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"           // 微信支付公钥文件路径，本地文件路径
    );

    ListCardRequest request = new ListCardRequest();
    request.brandId = "1004";
    request.state = CardState.CARD_EFFECTIVE;
    request.offset = 0L;
    request.limit = 20L;
    try {
      ListCardResponse response = client.run(request);
        // TODO: 请求成功，继续业务逻辑
        System.out.println(response);
    } catch (WXPayUtility.ApiException e) {
        // TODO: 请求失败，根据状态码执行不同的逻辑
        e.printStackTrace();
    }
  }

  public ListCardResponse run(ListCardRequest request) {
    String uri = PATH;
    Map<String, Object> args = new HashMap<>();
    args.put("brand_id", request.brandId);
    args.put("state", request.state);
    args.put("offset", request.offset);
    args.put("limit", request.limit);
    String queryString = WXPayUtility.urlEncode(args);
    if (!queryString.isEmpty()) {
        uri = uri + "?" + queryString;
    }

    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, null));
    reqBuilder.method(METHOD, null);
    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, ListCardResponse.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 ListCard(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 ListCardRequest {
    @SerializedName("brand_id")
    @Expose(serialize = false)
    public String brandId;

    @SerializedName("offset")
    @Expose(serialize = false)
    public Long offset;

    @SerializedName("limit")
    @Expose(serialize = false)
    public Long limit;

    @SerializedName("state")
    @Expose(serialize = false)
    public CardState state;
  }

  public static class ListCardResponse {
    @SerializedName("data")
    public List<Card> data;

    @SerializedName("total_count")
    public Long totalCount;

    @SerializedName("offset")
    public Long offset;

    @SerializedName("limit")
    public Long limit;
  }

  public enum CardState {
    @SerializedName("CARD_EFFECTIVE")
    CARD_EFFECTIVE,
    @SerializedName("CARD_INVALID")
    CARD_INVALID
  }

  public static class Card {
    @SerializedName("out_request_no")
    public String outRequestNo;

    @SerializedName("card_id")
    public String cardId;

    @SerializedName("brand_id")
    public String brandId;

    @SerializedName("appid")
    public String appid;

    @SerializedName("card_type")
    public CardType cardType;

    @SerializedName("card_title")
    public String cardTitle;

    @SerializedName("card_color")
    public String cardColor;

    @SerializedName("card_picture_url")
    public String cardPictureUrl;

    @SerializedName("code_mode")
    public CodeMode codeMode;

    @SerializedName("code_type")
    public CodeType codeType;

    @SerializedName("code_jump_information")
    public CodeJumpInfo codeJumpInformation;

    @SerializedName("benefits")
    public String benefits;

    @SerializedName("notify_url")
    public String notifyUrl;

    @SerializedName("need_pinned")
    public Boolean needPinned;

    @SerializedName("need_display_level")
    public Boolean needDisplayLevel;

    @SerializedName("init_level")
    public String initLevel;

    @SerializedName("service_phone")
    public String servicePhone;

    @SerializedName("legal_agreement")
    public String legalAgreement;

    @SerializedName("valid_date_information")
    public DateInfo validDateInformation;

    @SerializedName("member_information")
    public MemberInfo memberInformation;

    @SerializedName("points_information")
    public PointsInfo pointsInformation;

    @SerializedName("balance_information")
    public BalanceInfo balanceInformation;

    @SerializedName("purchase_information")
    public PurchaseInfo purchaseInformation;

    @SerializedName("user_information")
    public UserInfoForm userInformation;

    @SerializedName("state")
    public CardState state;

    @SerializedName("create_time")
    public String createTime;

    @SerializedName("modify_time")
    public String modifyTime;
  }

  public enum CardType {
    @SerializedName("PURCHASE")
    PURCHASE,
    @SerializedName("NORMAL")
    NORMAL,
    @SerializedName("BALANCE")
    BALANCE
  }

  public enum CodeMode {
    @SerializedName("SYSTEM_ALLOCATE")
    SYSTEM_ALLOCATE,
    @SerializedName("MERCHANT_ALLOCATE")
    MERCHANT_ALLOCATE
  }

  public enum CodeType {
    @SerializedName("NONE_CODE")
    NONE_CODE,
    @SerializedName("BAR_CODE")
    BAR_CODE,
    @SerializedName("QR_CODE")
    QR_CODE,
    @SerializedName("BAR_CODE_AND_QR_CODE")
    BAR_CODE_AND_QR_CODE,
    @SerializedName("JUMP_MINI_PROGRAM")
    JUMP_MINI_PROGRAM
  }

  public static class CodeJumpInfo {
    @SerializedName("jump_appid")
    public String jumpAppid;

    @SerializedName("jump_path")
    public String jumpPath;
  }

  public static class DateInfo {
    @SerializedName("type")
    public DateType type;

    @SerializedName("available_begin_time")
    public String availableBeginTime;

    @SerializedName("available_end_time")
    public String availableEndTime;

    @SerializedName("available_day_after_receive")
    public Long availableDayAfterReceive;
  }

  public static class MemberInfo {
    @SerializedName("jump_appid")
    public String jumpAppid;

    @SerializedName("jump_path")
    public String jumpPath;
  }

  public static class PointsInfo {
    @SerializedName("jump_appid")
    public String jumpAppid;

    @SerializedName("jump_path")
    public String jumpPath;
  }

  public static class BalanceInfo {
    @SerializedName("jump_appid")
    public String jumpAppid;

    @SerializedName("jump_path")
    public String jumpPath;
  }

  public static class PurchaseInfo {
    @SerializedName("price")
    public Long price;

    @SerializedName("jump_appid")
    public String jumpAppid;

    @SerializedName("jump_path")
    public String jumpPath;
  }

  public static class UserInfoForm {
    @SerializedName("common_field_list")
    public List<CommonFieldFlag> commonFieldList;

    @SerializedName("custom_field_list")
    public List<UserCustomFieldForm> customFieldList;
  }

  public enum DateType {
    @SerializedName("FIX_TIME_RANGE")
    FIX_TIME_RANGE,
    @SerializedName("FIX_TERM")
    FIX_TERM,
    @SerializedName("PERMANENT")
    PERMANENT
  }

  public enum CommonFieldFlag {
    @SerializedName("USER_FORM_FLAG_SEX")
    USER_FORM_FLAG_SEX,
    @SerializedName("USER_FORM_FLAG_NAME")
    USER_FORM_FLAG_NAME,
    @SerializedName("USER_FORM_FLAG_BIRTHDAY")
    USER_FORM_FLAG_BIRTHDAY,
    @SerializedName("USER_FORM_FLAG_ADDRESS")
    USER_FORM_FLAG_ADDRESS,
    @SerializedName("USER_FORM_FLAG_EMAIL")
    USER_FORM_FLAG_EMAIL,
    @SerializedName("USER_FORM_FLAG_CITY")
    USER_FORM_FLAG_CITY
  }

  public static class UserCustomFieldForm {
    @SerializedName("type")
    public CustomFieldType type;

    @SerializedName("name")
    public String name;

    @SerializedName("values")
    public List<String> values;
  }

  public enum CustomFieldType {
    @SerializedName("CHECK_BOX")
    CHECK_BOX,
    @SerializedName("RADIO")
    RADIO
  }

}

```
