-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathapi_transactions_example_test.go
128 lines (114 loc) · 4.5 KB
/
api_transactions_example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2021 Tencent Inc. All rights reserved.
//
// 公共出行平台代扣服务对外API
//
// 公共出行平台代扣服务对外API
//
// API version: 1.0.0
// Code generated by WechatPay APIv3 Generator based on [OpenAPI Generator](https://openapi-generator.tech); DO NOT EDIT.
package weixinpayscanandride_test
import (
"context"
"log"
"github.com/wechatpay-apiv3/wechatpay-go/core"
"github.com/wechatpay-apiv3/wechatpay-go/core/option"
"github.com/wechatpay-apiv3/wechatpay-go/services/weixinpayscanandride"
"github.com/wechatpay-apiv3/wechatpay-go/utils"
)
func ExampleTransactionsApiService_CreateTransaction() {
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 := weixinpayscanandride.TransactionsApiService{Client: client}
resp, result, err := svc.CreateTransaction(ctx,
weixinpayscanandride.CreateTransactionRequest{
Appid: core.String("wxcbda96de0b165486"),
SubAppid: core.String("wxcbda96de0b165486"),
SubMchid: core.String("1900000109"),
Description: core.String("地铁扣费"),
Attach: core.String("深圳分店"),
OutTradeNo: core.String("20150806125346"),
TradeScene: weixinpayscanandride.TRADESCENE_BUS.Ptr(),
GoodsTag: core.String("WXG"),
ContractId: core.String("Wx15463511252015071056489715"),
NotifyUrl: core.String("https://yoursite.com/wxpay.html"),
Amount: &weixinpayscanandride.OrderAmount{
Total: core.Int64(600),
Currency: core.String("CNY"),
},
BusInfo: &weixinpayscanandride.BusSceneInfo{
StartTime: core.String("2017-08-26T10:43:39+08:00"),
LineName: core.String("1路公车"),
PlateNumber: core.String("粤B888888"),
},
MetroInfo: &weixinpayscanandride.MetroSceneInfo{
StartTime: core.String("2017-08-26T10:43:39+08:00"),
EndTime: core.String("2017-08-26T10:43:39+08:00"),
StartStation: core.String("西单"),
EndStation: core.String("天安门西"),
},
},
)
if err != nil {
// 处理错误
log.Printf("call CreateTransaction err:%s", err)
} else {
// 处理返回结果
log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
}
}
func ExampleTransactionsApiService_QueryTransaction() {
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 := weixinpayscanandride.TransactionsApiService{Client: client}
resp, result, err := svc.QueryTransaction(ctx,
weixinpayscanandride.QueryTransactionRequest{
OutTradeNo: core.String("20150806125346"),
SubMchid: core.String("1900000109"),
},
)
if err != nil {
// 处理错误
log.Printf("call QueryTransaction err:%s", err)
} else {
// 处理返回结果
log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
}
}