CouponApi.md 9.0 KB

cashcoupons/CouponApi

所有URI均基于微信支付 API 地址: https://api.mch.weixin.qq.com

方法名 HTTP 请求 描述
ListCouponsByFilter Get /v3/marketing/favor/users/{openid}/coupons 根据过滤条件查询用户的券
QueryCoupon Get /v3/marketing/favor/users/{openid}/coupons/{coupon_id} 查询代金券详情
SendCoupon Post /v3/marketing/favor/users/{openid}/coupons 发放指定批次的代金券

ListCouponsByFilter

CouponCollection ListCouponsByFilter(ListCouponsByFilterRequest)

根据过滤条件查询用户的券

调用示例

package main

import (
	"context"
	"log"

	"github.com/wechatpay-apiv3/wechatpay-go/core"
	"github.com/wechatpay-apiv3/wechatpay-go/services/cashcoupons"
	"github.com/wechatpay-apiv3/wechatpay-go/utils"
)

func main() {
	var (
		mchID                      string = "190000****"                               // 商户号
		mchCertificateSerialNumber string = "3775************************************" // 商户证书序列号
		mchAPIv3Key                string = "2ab9****************************"         // 商户APIv3密钥
	)

	// 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
	mchPrivateKey, err := utils.LoadPrivateKeyWithPath("/path/to/merchant/apiclient_key.pem")
	if err != nil {
		log.Printf("load merchant private key error:%s", err)
		return
	}

	ctx := context.Background()
	// 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
	opts := []core.ClientOption{
		option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
	}
	client, err := core.NewClient(ctx, opts...)
	if err != nil {
		log.Printf("new wechat pay client err:%s", err)
		return
	}

	svc := cashcoupons.CouponApiService{Client: client}
	resp, result, err := svc.ListCouponsByFilter(ctx,
		cashcoupons.ListCouponsByFilterRequest{
			Openid:         core.String("Openid_example"),
			Appid:          core.String("Appid_example"),
			StockId:        core.String("9865000"),
			Status:         core.String("USED"),
			CreatorMchid:   core.String("9865002"),
			SenderMchid:    core.String("9865001"),
			AvailableMchid: core.String("9865000"),
			Offset:         core.Int64(0),
			Limit:          core.Int64(20),
		},
	)

	if err != nil {
		// 处理错误
		log.Printf("call ListCouponsByFilter err:%s", err)
	} else {
		// 处理返回结果
		log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
	}
}

参数列表

参数名 参数类型 参数描述
ctx context.Context Golang 上下文,可用于日志、请求取消、请求跟踪等功能
req ListCouponsByFilterRequest API cashcoupons 所定义的本接口需要的所有参数,包括Path/Query/Body 3类参数

返回结果

Name Type Description
resp *CouponCollection 结构化的接口返回结果
result *core.APIResult 本次 API 访问的请求与应答信息
err error 本次 API 访问中发生的错误,当且仅当 API 失败时存在

[返回顶部] [返回接口列表] [返回类型列表] [返回服务README]

QueryCoupon

Coupon QueryCoupon(QueryCouponRequest)

查询代金券详情

调用示例

package main

import (
	"context"
	"log"

	"github.com/wechatpay-apiv3/wechatpay-go/core"
	"github.com/wechatpay-apiv3/wechatpay-go/services/cashcoupons"
	"github.com/wechatpay-apiv3/wechatpay-go/utils"
)

func main() {
	var (
		mchID                      string = "190000****"                               // 商户号
		mchCertificateSerialNumber string = "3775************************************" // 商户证书序列号
		mchAPIv3Key                string = "2ab9****************************"         // 商户APIv3密钥
	)

	// 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
	mchPrivateKey, err := utils.LoadPrivateKeyWithPath("/path/to/merchant/apiclient_key.pem")
	if err != nil {
		log.Printf("load merchant private key error:%s", err)
		return
	}

	ctx := context.Background()
	// 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
	opts := []core.ClientOption{
		option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
	}
	client, err := core.NewClient(ctx, opts...)
	if err != nil {
		log.Printf("new wechat pay client err:%s", err)
		return
	}

	svc := cashcoupons.CouponApiService{Client: client}
	resp, result, err := svc.QueryCoupon(ctx,
		cashcoupons.QueryCouponRequest{
			CouponId: core.String("9856888"),
			Appid:    core.String("Appid_example"),
			Openid:   core.String("Openid_example"),
		},
	)

	if err != nil {
		// 处理错误
		log.Printf("call QueryCoupon err:%s", err)
	} else {
		// 处理返回结果
		log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
	}
}

参数列表

参数名 参数类型 参数描述
ctx context.Context Golang 上下文,可用于日志、请求取消、请求跟踪等功能
req QueryCouponRequest API cashcoupons 所定义的本接口需要的所有参数,包括Path/Query/Body 3类参数

返回结果

Name Type Description
resp *Coupon 结构化的接口返回结果
result *core.APIResult 本次 API 访问的请求与应答信息
err error 本次 API 访问中发生的错误,当且仅当 API 失败时存在

[返回顶部] [返回接口列表] [返回类型列表] [返回服务README]

SendCoupon

SendCouponResponse SendCoupon(SendCouponRequest)

发放指定批次的代金券

调用示例

package main

import (
	"context"
	"log"

	"github.com/wechatpay-apiv3/wechatpay-go/core"
	"github.com/wechatpay-apiv3/wechatpay-go/services/cashcoupons"
	"github.com/wechatpay-apiv3/wechatpay-go/utils"
)

func main() {
	var (
		mchID                      string = "190000****"                               // 商户号
		mchCertificateSerialNumber string = "3775************************************" // 商户证书序列号
		mchAPIv3Key                string = "2ab9****************************"         // 商户APIv3密钥
	)

	// 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
	mchPrivateKey, err := utils.LoadPrivateKeyWithPath("/path/to/merchant/apiclient_key.pem")
	if err != nil {
		log.Printf("load merchant private key error:%s", err)
		return
	}

	ctx := context.Background()
	// 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
	opts := []core.ClientOption{
		option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
	}
	client, err := core.NewClient(ctx, opts...)
	if err != nil {
		log.Printf("new wechat pay client err:%s", err)
		return
	}

	svc := cashcoupons.CouponApiService{Client: client}
	resp, result, err := svc.SendCoupon(ctx,
		cashcoupons.SendCouponRequest{
			Openid:            core.String("Openid_example"),
			StockId:           core.String("example_stock_id"),
			OutRequestNo:      core.String("example_out_request_no"),
			Appid:             core.String("example_appid"),
			StockCreatorMchid: core.String("example_stock_creator_mchid"),
			CouponValue:       core.Int64(1),
			CouponMinimum:     core.Int64(1),
		},
	)

	if err != nil {
		// 处理错误
		log.Printf("call SendCoupon err:%s", err)
	} else {
		// 处理返回结果
		log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
	}
}

参数列表

参数名 参数类型 参数描述
ctx context.Context Golang 上下文,可用于日志、请求取消、请求跟踪等功能
req SendCouponRequest API cashcoupons 所定义的本接口需要的所有参数,包括Path/Query/Body 3类参数

返回结果

Name Type Description
resp *SendCouponResponse 结构化的接口返回结果
result *core.APIResult 本次 API 访问的请求与应答信息
err error 本次 API 访问中发生的错误,当且仅当 API 失败时存在

[返回顶部] [返回接口列表] [返回类型列表] [返回服务README]