first commitrefactor(lklsdk):重构SDK配置和初始化方式
- 将Config结构体字段重新排序并添加dc标签 - 移除部分不再使用的配置字段 - 修改NewClient和NewSDK函数签名,使用JSON字符串初始化 - 更新README文档中的使用示例 - 添加退款功能示例到README - 修复签名和授权头构建时的字段引用错误menu v1.0.1
parent
ccd273840a
commit
bcb4a9536a
74
README.md
74
README.md
|
|
@ -19,7 +19,7 @@ lklsdk/
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# 将SDK引入到您的项目中
|
# 将SDK引入到您的项目中
|
||||||
go get -u github.com/black1552/lkl_sdk/lklsdk
|
go get -u github.com/black1552/lkl_sdk
|
||||||
```
|
```
|
||||||
|
|
||||||
## 快速开始
|
## 快速开始
|
||||||
|
|
@ -33,22 +33,21 @@ import (
|
||||||
"github.com/gogf/gf/v2/os/gctx"
|
"github.com/gogf/gf/v2/os/gctx"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 创建配置
|
// 创建配置JSON字符串
|
||||||
config := &lklsdk.Config{
|
cfgJson := `{
|
||||||
AppID: "your_app_id", // 拉卡拉分配的AppID
|
"public_key": "your_public_key", // 公钥字符串
|
||||||
TermNo: "your_term_no", // 终端号
|
"private_key": "your_private_key", // 私钥字符串
|
||||||
MerchantNo: "your_merchant_no", // 商户号
|
"app_id": "your_app_id", // lakala应用ID
|
||||||
SettleMerNo: "your_settle_mer_no", // 结算商户号
|
"serial_no": "your_serial_no", // 序列号
|
||||||
SettleTermNo: "your_settle_term_no", // 结算终端号
|
"sub_app_id": "your_sub_app_id", // 子应用ID 微信AppId
|
||||||
AccountType: "WECHAT", // 账户类型,如WECHAT、ALIPAY等
|
"version": "3.0", // lakala版本号
|
||||||
TransType: "71", // 交易类型
|
"account_type": "WECHAT", // 账户类型
|
||||||
Version: "3.0", // API版本
|
"trans_type": "71", // 交易类型
|
||||||
NotifyURL: "your_notify_url", // 回调URL
|
"notify_url": "your_notify_url" // 回调地址
|
||||||
SerialNo: "your_mch_api_key", // 商户API密钥
|
}`
|
||||||
}
|
|
||||||
|
|
||||||
// 初始化SDK(使用泛型指定响应类型)
|
// 初始化SDK(使用泛型指定响应类型)
|
||||||
sdk := lklsdk.NewSDK[model.ResponseType](gctx.New(), config)
|
sdk := lklsdk.NewSDK[model.ResponseType](gctx.New(), cfgJson)
|
||||||
```
|
```
|
||||||
|
|
||||||
## 功能模块
|
## 功能模块
|
||||||
|
|
@ -57,7 +56,7 @@ sdk := lklsdk.NewSDK[model.ResponseType](gctx.New(), config)
|
||||||
|
|
||||||
```go
|
```go
|
||||||
// 初始化特定响应类型的SDK
|
// 初始化特定响应类型的SDK
|
||||||
sdk := lklsdk.NewSDK[model.MergePreorderResponse](gctx.New(), config)
|
sdk := lklsdk.NewSDK[model.MergePreorderResponse](gctx.New(), cfgJson)
|
||||||
|
|
||||||
// 准备拆单信息
|
// 准备拆单信息
|
||||||
outSplitInfo := []*model.OutSplitInfo{
|
outSplitInfo := []*model.OutSplitInfo{
|
||||||
|
|
@ -110,7 +109,7 @@ if !mergePreorderResp.SuccessOrFail() {
|
||||||
|
|
||||||
```go
|
```go
|
||||||
// 初始化特定响应类型的SDK
|
// 初始化特定响应类型的SDK
|
||||||
sdk := lklsdk.NewSDK[model.ApplyLedgerMerResponse](gctx.New(), config)
|
sdk := lklsdk.NewSDK[model.ApplyLedgerMerResponse](gctx.New(), cfgJson)
|
||||||
|
|
||||||
// 构建请求参数
|
// 构建请求参数
|
||||||
applyLedgerReq := &model.ApplyLedgerMerReqData{
|
applyLedgerReq := &model.ApplyLedgerMerReqData{
|
||||||
|
|
@ -143,7 +142,7 @@ if !expectResp.SuccessOrFail() {
|
||||||
|
|
||||||
```go
|
```go
|
||||||
// 初始化特定响应类型的SDK
|
// 初始化特定响应类型的SDK
|
||||||
sdk := lklsdk.NewSDK[model.TradeQueryResponse](gctx.New(), config)
|
sdk := lklsdk.NewSDK[model.TradeQueryResponse](gctx.New(), cfgJson)
|
||||||
|
|
||||||
// 构建请求参数
|
// 构建请求参数
|
||||||
tradeQueryReq := &model.TradeQueryReqData{
|
tradeQueryReq := &model.TradeQueryReqData{
|
||||||
|
|
@ -168,7 +167,7 @@ if !tradeQueryResp.SuccessOrFail() {
|
||||||
|
|
||||||
```go
|
```go
|
||||||
// 初始化特定响应类型的SDK
|
// 初始化特定响应类型的SDK
|
||||||
sdk := lklsdk.NewSDK[model.OrderSplitLedgerResponse](gctx.New(), config)
|
sdk := lklsdk.NewSDK[model.OrderSplitLedgerResponse](gctx.New(), cfgJson)
|
||||||
|
|
||||||
// 准备分账接收方数据
|
// 准备分账接收方数据
|
||||||
var recvDatas []*model.OrderSplitLedgerRecvDatas
|
var recvDatas []*model.OrderSplitLedgerRecvDatas
|
||||||
|
|
@ -199,11 +198,44 @@ if !splitLedgerResp.SuccessOrFail() {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### 5. 账户余额查询
|
### 5. 退款
|
||||||
|
|
||||||
```go
|
```go
|
||||||
// 初始化特定响应类型的SDK
|
// 初始化特定响应类型的SDK
|
||||||
sdk := lklsdk.NewSDK[model.BalanceQueryResponse](gctx.New(), config)
|
sdk := lklsdk.NewSDK[model.RefundResponse](gctx.New(), cfgJson)
|
||||||
|
|
||||||
|
// 构建请求参数
|
||||||
|
refundReq := &model.RefundReqData{
|
||||||
|
MerchantNo: "your_merchant_no", // 商户号
|
||||||
|
TermNo: "your_term_no", // 终端号
|
||||||
|
OutTradeNo: "original_out_trade_no", // 原商户交易流水号
|
||||||
|
OutRefundNo: "your_refund_out_trade_no", // 退款商户流水号
|
||||||
|
RefundAmount: "100", // 退款金额,单位为分
|
||||||
|
TotalAmount: "100", // 原交易总金额,单位为分
|
||||||
|
RefundReason: "退款原因", // 退款原因
|
||||||
|
NotifyUrl: "https://your-notify-url.com", // 回调地址
|
||||||
|
}
|
||||||
|
|
||||||
|
// 调用接口
|
||||||
|
refundResp, err := sdk.Refound(refundReq)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("退款失败: %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用SuccessOrFail方法判断请求是否成功
|
||||||
|
if !refundResp.SuccessOrFail() {
|
||||||
|
log.Printf("退款失败: %s\n", refundResp.Msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理成功响应
|
||||||
|
// 可以从refundResp.RespData中获取退款结果数据
|
||||||
|
```
|
||||||
|
|
||||||
|
### 6. 账户余额查询
|
||||||
|
|
||||||
|
```go
|
||||||
|
// 初始化特定响应类型的SDK
|
||||||
|
sdk := lklsdk.NewSDK[model.BalanceQueryResponse](gctx.New(), cfgJson)
|
||||||
|
|
||||||
// 构建请求参数
|
// 构建请求参数
|
||||||
balanceQueryReq := &model.BalanceQueryReqData{
|
balanceQueryReq := &model.BalanceQueryReqData{
|
||||||
|
|
|
||||||
|
|
@ -16,22 +16,20 @@ import (
|
||||||
|
|
||||||
"github.com/black1552/base-common/utils"
|
"github.com/black1552/base-common/utils"
|
||||||
"github.com/gogf/gf/v2/os/gfile"
|
"github.com/gogf/gf/v2/os/gfile"
|
||||||
|
"github.com/gogf/gf/v2/util/gconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config 拉卡拉SDK配置
|
// Config 拉卡拉SDK配置
|
||||||
type Config struct {
|
type Config struct {
|
||||||
AppID string `json:"app_id"`
|
PublicKey string `json:"public_key" dc:"公钥字符串"`
|
||||||
TermNo string `json:"term_no"`
|
PrivateKey string `json:"private_key" dc:"私钥字符串"`
|
||||||
MerchantNo string `json:"merchant_no"`
|
AppId string `json:"app_id" dc:"lakala应用ID"`
|
||||||
SettleMerNo string `json:"settle_merchant_no"`
|
SerialNo string `json:"serial_no" dc:"序列号"`
|
||||||
SettleTermNo string `json:"settle_term_no"`
|
SubAppId string `json:"sub_app_id" dc:"子应用ID 微信AppId"`
|
||||||
AccountType string `json:"account_type"`
|
Version string `json:"version" dc:"lakala版本号"`
|
||||||
TransType string `json:"trans_type"`
|
AccountType string `json:"account_type" dc:"账户类型"`
|
||||||
Version string `json:"version"`
|
TransType string `json:"trans_type" dc:"交易类型"`
|
||||||
NotifyURL string `json:"notify_url"`
|
NotifyUrl string `json:"notify_url" dc:"回调地址"`
|
||||||
PublicKey string `json:"public_key"`
|
|
||||||
PrivateKey string `json:"private_key"`
|
|
||||||
SerialNo string `json:"serial_no"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Client 拉卡拉SDK客户端
|
// Client 拉卡拉SDK客户端
|
||||||
|
|
@ -42,7 +40,12 @@ type Client[T any] struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewClient 创建拉卡拉SDK客户端
|
// NewClient 创建拉卡拉SDK客户端
|
||||||
func NewClient[T any](ctx context.Context, config *Config) *Client[T] {
|
func NewClient[T any](ctx context.Context, cfgJson string) *Client[T] {
|
||||||
|
var config *Config
|
||||||
|
err := gconv.Struct(cfgJson, &config)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
return &Client[T]{
|
return &Client[T]{
|
||||||
config: config,
|
config: config,
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
|
|
@ -65,7 +68,7 @@ func (c *Client[T]) generateSign(request []byte) (string, error) {
|
||||||
timestamp := fmt.Sprintf("%d", time.Now().Unix())
|
timestamp := fmt.Sprintf("%d", time.Now().Unix())
|
||||||
|
|
||||||
// 构建待签名报文
|
// 构建待签名报文
|
||||||
signData := fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n", c.config.AppID, c.config.SerialNo, timestamp, nonceStr, string(request))
|
signData := fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n", c.config.AppId, c.config.SerialNo, timestamp, nonceStr, string(request))
|
||||||
|
|
||||||
// 计算签名
|
// 计算签名
|
||||||
hashed := sha256.Sum256([]byte(signData))
|
hashed := sha256.Sum256([]byte(signData))
|
||||||
|
|
@ -82,7 +85,7 @@ func (c *Client[T]) generateSign(request []byte) (string, error) {
|
||||||
|
|
||||||
// 构建Authorization头
|
// 构建Authorization头
|
||||||
authorization := fmt.Sprintf("LKLAPI-SHA256withRSA appid=\"%s\",serial_no=\"%s\",timestamp=\"%s\",nonce_str=\"%s\",signature=\"%s\"",
|
authorization := fmt.Sprintf("LKLAPI-SHA256withRSA appid=\"%s\",serial_no=\"%s\",timestamp=\"%s\",nonce_str=\"%s\",signature=\"%s\"",
|
||||||
c.config.AppID, c.config.SerialNo, timestamp, nonceStr, signatureBase64)
|
c.config.AppId, c.config.SerialNo, timestamp, nonceStr, signatureBase64)
|
||||||
return authorization, nil
|
return authorization, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,8 +17,8 @@ type SDK[T any] struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewSDK 创建拉卡拉SDK实例
|
// NewSDK 创建拉卡拉SDK实例
|
||||||
func NewSDK[T any](ctx context.Context, config *Config) *SDK[T] {
|
func NewSDK[T any](ctx context.Context, cfgJson string) *SDK[T] {
|
||||||
client := NewClient[T](ctx, config)
|
client := NewClient[T](ctx, cfgJson)
|
||||||
return &SDK[T]{
|
return &SDK[T]{
|
||||||
Client: client,
|
Client: client,
|
||||||
SplitLedger: NewSplitLedgerService(client),
|
SplitLedger: NewSplitLedgerService(client),
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue