---
url: "https://pay.weixin.qq.com/doc/brand/4015582722-java.md"
title: "修改会员卡模板信息-请求示例-java"
doc_id: 4015582722
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 UpdateCard {
  private static String HOST = "https://api.mch.weixin.qq.com";
  private static String METHOD = "PATCH";
  private static String PATH = "/brand/card-member/cards/{card_id}";

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

    UpdateCardRequest request = new UpdateCardRequest();
    request.cardId = "pbLatjvWOibDc5-TBnbUk1pD1";
    request.cardTitle = "测试卡";
    request.cardColor = "#FFFF00";
    request.cardPictureUrl = "https://wxpaylogo.qpic.cn/wxpaylogo/xxxxx/0";
    request.codeJumpInformation = new CodeJumpInfo();
    request.codeJumpInformation.jumpAppid = "wxea9c30a90fs8d3fe";
    request.codeJumpInformation.jumpPath = "/pages/code/code";
    request.benefits = "会员折扣、专属价";
    request.notifyUrl = "https://www.weixin.qq.com/wxpay/notify.php";
    request.needPinned = true;
    request.needDisplayLevel = true;
    request.servicePhone = "010-8877xxxx";
    request.validDateInformation = new DateInfo();
    request.validDateInformation.type = DateType.PERMANENT;
    request.validDateInformation.availableBeginTime = "2020-05-20T13:29:35.120+08:00";
    request.validDateInformation.availableEndTime = "2020-05-20T13:29:35.120+08:00";
    request.validDateInformation.availableDayAfterReceive = 30L;
    request.memberInformation = new MemberInfo();
    request.memberInformation.jumpAppid = "wxea9c30a90fs8d3fe";
    request.memberInformation.jumpPath = "/pages/points/points";
    request.pointsInformation = new PointsInfo();
    request.pointsInformation.jumpAppid = "wxea9c30a90fs8d3fe";
    request.pointsInformation.jumpPath = "/pages/points/points";
    request.balanceInformation = new BalanceInfo();
    request.balanceInformation.jumpAppid = "wxea9c30a90fs8d3fe";
    request.balanceInformation.jumpPath = "/pages/balance/balance";
    request.purchaseInformation = new PurchaseInfo();
    request.purchaseInformation.price = 100L;
    request.purchaseInformation.jumpAppid = "wxea9c30a90fs8d3fe";
    request.purchaseInformation.jumpPath = "/pages/purchase/purchase";
    request.userInformation = new UserInfoForm();
    request.userInformation.commonFieldList = new ArrayList<>();
    {
      request.userInformation.commonFieldList.add(CommonFieldFlag.USER_FORM_FLAG_SEX);
    };
    request.userInformation.customFieldList = new ArrayList<>();
    {
      UserCustomFieldForm customFieldListItem = new UserCustomFieldForm();
      customFieldListItem.type = CustomFieldType.CHECK_BOX;
      customFieldListItem.name = "喜欢的运动";
      customFieldListItem.values = new ArrayList<>();
      {
        customFieldListItem.values.add("篮球");
      };
      request.userInformation.customFieldList.add(customFieldListItem);
    };
    try {
      Card response = client.run(request);
        // TODO: 请求成功，继续业务逻辑
        System.out.println(response);
    } catch (WXPayBrandUtility.ApiException e) {
        // TODO: 请求失败，根据状态码执行不同的逻辑
        e.printStackTrace();
    }
  }

  public Card run(UpdateCardRequest request) {
    String uri = PATH;
    uri = uri.replace("{card_id}", WXPayBrandUtility.urlEncode(request.cardId));
    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, Card.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 UpdateCard(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 UpdateCardRequest {
    @SerializedName("card_id")
    @Expose(serialize = false)
    public String cardId;

    @SerializedName("card_title")
    public String cardTitle;

    @SerializedName("card_color")
    public String cardColor;

    @SerializedName("card_picture_url")
    public String cardPictureUrl;

    @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("service_phone")
    public String servicePhone;

    @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;
  }

  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 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 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 enum CardState {
    @SerializedName("CARD_EFFECTIVE")
    CARD_EFFECTIVE,
    @SerializedName("CARD_INVALID")
    CARD_INVALID
  }

  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
  }

}

```
