---
url: "https://pay.weixin.qq.com/doc/v3/partner/4016468440-go.md"
title: "提交商家名片配置申请-请求示例-go"
doc_id: 4016468440
example_lang: "go"
---

## Go


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

```
package main

import (
	"bytes"
	"demo/wxpay_utility" // 引用微信支付工具库，参考 https://pay.weixin.qq.com/doc/v3/partner/4015119446
	"encoding/json"
	"fmt"
	"net/http"
	"net/url"
	"time"
)

func main() {
	// TODO: 请准备商户开发必要参数，参考：https://pay.weixin.qq.com/doc/v3/partner/4013080340
	config, err := wxpay_utility.CreateMchConfig(
		"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",       // 微信支付公钥文件路径，本地文件路径
	)
	if err != nil {
		fmt.Println(err)
		return
	}

	request := &SubmitMchCardConfigRequest{
		BusinessCode: wxpay_utility.String("1900013511_10000"),
		BrandId:      wxpay_utility.String("1234567"),
		BrandMiniProgramInfo: &MiniProgramInfo{
			Appid:           wxpay_utility.String("wx1234567890abcdef"),
			DefaultJumpPath: wxpay_utility.String("pages/shop/index"),
			ButtonText:      wxpay_utility.String("前往小程序"),
		},
		BrandCustomerService: &MerchantCardCustomerService{
			CustomerServiceType:  CUSTOMERSERVICETYPE_MINI_PROGRAM.Ptr(),
			CustomerServicePhone: wxpay_utility.String("12345678900"),
			CustomerServicePath:  wxpay_utility.String("pages/service/index"),
			Appid:                wxpay_utility.String("wx1234567890abcdef"),
		},
		ServiceList: []MerchantCardService{MerchantCardService{
			ServiceClassifyName: wxpay_utility.String("会员服务"),
			ServiceName:         wxpay_utility.String("会员中心"),
			ServiceJumpType:     SERVICEJUMPTYPE_JUMP_MINI_PROGRAM.Ptr(),
			ServiceJumpPath:     wxpay_utility.String("pages/shop/index"),
			Appid:               wxpay_utility.String("wx1234567890abcdef"),
		}},
	}

	response, err := SubmitMchCardConfig(config, request)
	if err != nil {
		fmt.Printf("请求失败: %+v\n", err)
		// TODO: 请求失败，根据状态码执行不同的处理
		return
	}

	// TODO: 请求成功，继续业务逻辑
	fmt.Printf("请求成功: %+v\n", response)
}

func SubmitMchCardConfig(config *wxpay_utility.MchConfig, request *SubmitMchCardConfigRequest) (response *SubmitMchCardConfigResponse, err error) {
	const (
		host   = "https://api.mch.weixin.qq.com"
		method = "POST"
		path   = "/v3/brand/card/card-configs"
	)

	reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
	if err != nil {
		return nil, err
	}
	reqBody, err := json.Marshal(request)
	if err != nil {
		return nil, err
	}
	httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody))
	if err != nil {
		return nil, err
	}
	httpRequest.Header.Set("Accept", "application/json")
	httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
	httpRequest.Header.Set("Content-Type", "application/json")
	authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody)
	if err != nil {
		return nil, err
	}
	httpRequest.Header.Set("Authorization", authorization)

	client := &http.Client{}
	httpResponse, err := client.Do(httpRequest)
	if err != nil {
		return nil, err
	}
	respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
	if err != nil {
		return nil, err
	}
	if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
		// 2XX 成功，验证应答签名
		err = wxpay_utility.ValidateResponse(
			config.WechatPayPublicKeyId(),
			config.WechatPayPublicKey(),
			&httpResponse.Header,
			respBody,
		)
		if err != nil {
			return nil, err
		}
		response := &SubmitMchCardConfigResponse{}
		if err := json.Unmarshal(respBody, response); err != nil {
			return nil, err
		}

		return response, nil
	} else {
		return nil, wxpay_utility.NewApiException(
			httpResponse.StatusCode,
			httpResponse.Header,
			respBody,
		)
	}
}

type SubmitMchCardConfigRequest struct {
	BusinessCode         *string                      `json:"business_code,omitempty"`
	BrandId              *string                      `json:"brand_id,omitempty"`
	BrandMiniProgramInfo *MiniProgramInfo             `json:"brand_mini_program_info,omitempty"`
	BrandCustomerService *MerchantCardCustomerService `json:"brand_customer_service,omitempty"`
	ServiceList          []MerchantCardService        `json:"service_list,omitempty"`
}

type SubmitMchCardConfigResponse struct {
	BusinessCode   *string    `json:"business_code,omitempty"`
	ApplymentId    *string    `json:"applyment_id,omitempty"`
	BrandId        *string    `json:"brand_id,omitempty"`
	CardPreviewUrl *string    `json:"card_preview_url,omitempty"`
	UrlExpiredTime *time.Time `json:"url_expired_time,omitempty"`
}

type MiniProgramInfo struct {
	Appid           *string `json:"appid,omitempty"`
	DefaultJumpPath *string `json:"default_jump_path,omitempty"`
	ButtonText      *string `json:"button_text,omitempty"`
}

type MerchantCardCustomerService struct {
	CustomerServiceType  *CustomerServiceType `json:"customer_service_type,omitempty"`
	CustomerServicePhone *string              `json:"customer_service_phone,omitempty"`
	CustomerServicePath  *string              `json:"customer_service_path,omitempty"`
	Appid                *string              `json:"appid,omitempty"`
}

type MerchantCardService struct {
	ServiceClassifyName *string          `json:"service_classify_name,omitempty"`
	ServiceName         *string          `json:"service_name,omitempty"`
	ServiceJumpType     *ServiceJumpType `json:"service_jump_type,omitempty"`
	ServiceJumpPath     *string          `json:"service_jump_path,omitempty"`
	Appid               *string          `json:"appid,omitempty"`
}

type CustomerServiceType string

func (e CustomerServiceType) Ptr() *CustomerServiceType {
	return &e
}

const (
	CUSTOMERSERVICETYPE_MINI_PROGRAM  CustomerServiceType = "MINI_PROGRAM"
	CUSTOMERSERVICETYPE_WECOM         CustomerServiceType = "WECOM"
	CUSTOMERSERVICETYPE_CUSTOMIZE_WEB CustomerServiceType = "CUSTOMIZE_WEB"
	CUSTOMERSERVICETYPE_CUSTOMIZE_MP  CustomerServiceType = "CUSTOMIZE_MP"
	CUSTOMERSERVICETYPE_SERVICE_PHONE CustomerServiceType = "SERVICE_PHONE"
)

type ServiceJumpType string

func (e ServiceJumpType) Ptr() *ServiceJumpType {
	return &e
}

const (
	SERVICEJUMPTYPE_JUMP_MINI_PROGRAM ServiceJumpType = "JUMP_MINI_PROGRAM"
	SERVICEJUMPTYPE_JUMP_WEB_PAGE     ServiceJumpType = "JUMP_WEB_PAGE"
)

```
