123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- package cashcoupons_test
- import (
- "context"
- "log"
- "git.nanodreamtech.com/sg/wechatpay-go/core"
- "git.nanodreamtech.com/sg/wechatpay-go/core/option"
- "git.nanodreamtech.com/sg/wechatpay-go/services/cashcoupons"
- "git.nanodreamtech.com/sg/wechatpay-go/utils"
- )
- func ExampleCouponApiService_ListCouponsByFilter() {
- var (
- mchID string = "190000****"
- mchCertificateSerialNumber string = "3775************************************"
- mchAPIv3Key string = "2ab9****************************"
- )
-
- 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()
-
- 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)
- }
- }
- func ExampleCouponApiService_QueryCoupon() {
- var (
- mchID string = "190000****"
- mchCertificateSerialNumber string = "3775************************************"
- mchAPIv3Key string = "2ab9****************************"
- )
-
- 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()
-
- 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)
- }
- }
- func ExampleCouponApiService_SendCoupon() {
- var (
- mchID string = "190000****"
- mchCertificateSerialNumber string = "3775************************************"
- mchAPIv3Key string = "2ab9****************************"
- )
-
- 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()
-
- 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)
- }
- }
|