添加文件: xy_sh/example_test.go
This commit is contained in:
parent
a3b253000f
commit
f53e1cfc4d
|
|
@ -0,0 +1,117 @@
|
||||||
|
package xy_sh
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ExampleClient_Order() {
|
||||||
|
// 初始化客户端
|
||||||
|
sm4Key := []byte("1234567890abcdef") // 16字节SM4密钥
|
||||||
|
sm3Salt := []byte("my_sm3_salt") // SM3盐值
|
||||||
|
orderURL := "https://api.example.com/order"
|
||||||
|
queryURL := "https://api.example.com/query"
|
||||||
|
rechargeURL := "https://api.example.com/recharge"
|
||||||
|
|
||||||
|
client, err := NewClient(sm4Key, sm3Salt, orderURL, queryURL, rechargeURL)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 下单
|
||||||
|
req := &OrderRequest{
|
||||||
|
ActCode: "ACT001",
|
||||||
|
GoodsCode: "123456",
|
||||||
|
ActOrderNum: "00001",
|
||||||
|
Account: "19912345678",
|
||||||
|
CallbackUrl: "https://xxx/notice",
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := client.Order(context.Background(), req)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("code: %d, msg: %s\n", resp.Code, resp.Msg)
|
||||||
|
if resp.Data != nil {
|
||||||
|
fmt.Printf("orderNo: %s, status: %d\n", resp.Data.OrderNo, resp.Data.Status)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleClient_QueryOrder() {
|
||||||
|
sm4Key := []byte("1234567890abcdef")
|
||||||
|
sm3Salt := []byte("my_sm3_salt")
|
||||||
|
|
||||||
|
client, err := NewClient(sm4Key, sm3Salt, "https://api.example.com/order", "https://api.example.com/query", "https://api.example.com/recharge")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
req := &QueryRequest{
|
||||||
|
ActCode: "FBG6vdYqGE4mGX7EH/woEg==",
|
||||||
|
OrderNo: "HM1757046717684000102707339840b23222",
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := client.QueryOrder(context.Background(), req)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("code: %d, msg: %s\n", resp.Code, resp.Msg)
|
||||||
|
if resp.Data != nil {
|
||||||
|
fmt.Printf("orderNo: %s, status: %d\n", resp.Data.OrderNo, resp.Data.Status)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleClient_WechatRecharge() {
|
||||||
|
sm4Key := []byte("1234567890abcdef")
|
||||||
|
sm3Salt := []byte("my_sm3_salt")
|
||||||
|
|
||||||
|
client, err := NewClient(sm4Key, sm3Salt, "https://api.example.com/order", "https://api.example.com/query", "https://api.example.com/recharge")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
req := &WechatRechargeRequest{
|
||||||
|
ActCode: "FBG6vdYqGE4mGX7EH/woEg==",
|
||||||
|
GoodsCode: "0001",
|
||||||
|
ActOrderNum: "0001",
|
||||||
|
OpenId: "0001",
|
||||||
|
AppId: "00001",
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := client.WechatRecharge(context.Background(), req)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("code: %d, msg: %s\n", resp.Code, resp.Msg)
|
||||||
|
if resp.Data != nil {
|
||||||
|
fmt.Printf("orderNo: %s, status: %d, couponId: %s\n", resp.Data.OrderNo, resp.Data.Status, resp.Data.CouponId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleClient_SendCallback() {
|
||||||
|
sm4Key := []byte("1234567890abcdef")
|
||||||
|
sm3Salt := []byte("my_sm3_salt")
|
||||||
|
|
||||||
|
client, err := NewClient(sm4Key, sm3Salt, "https://api.example.com/order", "https://api.example.com/query", "https://api.example.com/recharge")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
req := &CallbackRequest{
|
||||||
|
OrderNo: "HM202509291010001",
|
||||||
|
ActOrderNum: "XY2025092910100001",
|
||||||
|
Status: 3,
|
||||||
|
Account: "19912345678",
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := client.SendCallback(context.Background(), "https://bank.example.com/callback", req)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("statusCode: %d, body: %s\n", resp.StatusCode, resp.Body)
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue