95 lines
2.4 KiB
Go
95 lines
2.4 KiB
Go
package internal
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"gitea.cdlsxd.cn/sdk/plugin/proto"
|
|
"gitee.com/chengdu_blue_brothers/openapi-go-sdk-v2/api"
|
|
"gitee.com/chengdu_blue_brothers/openapi-go-sdk-v2/notify"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// 插件通信信息,若不对应则会报错panic
|
|
const (
|
|
Tag = "zltx_v2"
|
|
Version = 1
|
|
CookieKey = "zltx_v2"
|
|
CookieValue = "zltx_v2"
|
|
)
|
|
|
|
type ZLTXV2Service struct{}
|
|
|
|
func (p *ZLTXV2Service) Order(ctx context.Context, request *proto.OrderRequest) (*proto.OrderResponse, error) {
|
|
c, err := transConfig(request.Config)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
client, err := c.client()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
response, errResp, err := api.OrderCreate(ctx, client, c.AppId, c.orderReq(request))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("请求异常[%v]", err)
|
|
}
|
|
if errResp != nil {
|
|
return nil, fmt.Errorf("请求错误[code:%d]-[reason:%s]-[msg:%s]", errResp.Code, errResp.Reason, errResp.Message)
|
|
}
|
|
|
|
return orderResp(request, response), nil
|
|
}
|
|
|
|
func (p *ZLTXV2Service) Query(ctx context.Context, request *proto.QueryRequest) (*proto.QueryResponse, error) {
|
|
c, err := transConfig(request.Config)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
client, err := c.client()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
response, errResp, err := api.OrderQuery(ctx, client, c.AppId, c.queryReq(request))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("请求异常[%v]", err)
|
|
}
|
|
if errResp != nil {
|
|
return nil, fmt.Errorf("请求错误[code:%d]-[reason:%s]-[msg:%s]", errResp.Code, errResp.Reason, errResp.Message)
|
|
}
|
|
|
|
return queryResp(c.AppKey, request, response)
|
|
}
|
|
|
|
func (p *ZLTXV2Service) Notify(_ context.Context, request *proto.NotifyRequest) (*proto.NotifyResponse, error) {
|
|
req := &http.Request{
|
|
Header: nil,
|
|
Body: nil,
|
|
}
|
|
httpHeaders := make(http.Header)
|
|
if err := json.Unmarshal(request.Headers, &httpHeaders); err != nil {
|
|
return nil, fmt.Errorf("headers Unmarshal err [%v]", err)
|
|
}
|
|
newHeaders := make(http.Header)
|
|
for key, values := range httpHeaders {
|
|
newKey := strings.ToTitle(strings.ToLower(key))
|
|
newHeaders[newKey] = values
|
|
}
|
|
req.Header = newHeaders
|
|
req.Body = io.NopCloser(bytes.NewBuffer(request.Body))
|
|
|
|
c, err := transConfig(request.Config)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
n, err := notify.NewNotify(c.AppId, c.AppKey).ParseAndVerify(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return notifyResp(c.AppKey, n)
|
|
}
|