137 lines
2.8 KiB
Go
137 lines
2.8 KiB
Go
package internal
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"gitea.cdlsxd.cn/sdk/plugin/proto"
|
|
"plugins/alipay_cpn/internal/po"
|
|
)
|
|
|
|
// 插件通信信息,若不对应则会报错panic
|
|
// https://opendocs.alipay.com/open/282/105997?pathHash=3f0dc947
|
|
// https://www.yuque.com/xv2p76/manual/wpg9cl
|
|
const (
|
|
Tag = "alipay_cpn"
|
|
Version = 1
|
|
CookieKey = "alipay_cpn"
|
|
CookieValue = "alipay_cpn"
|
|
)
|
|
|
|
const baseUri = "https://openapi.alipay.com/gateway.do"
|
|
|
|
const (
|
|
orderMethod = "alipay.user.dtbankcust.channelvoucher.send"
|
|
queryMethod = "alipay.user.dtbankcust.activityorder.query"
|
|
)
|
|
|
|
type AlipayCpnService struct{}
|
|
|
|
func (s *AlipayCpnService) Order(ctx context.Context, request *proto.OrderRequest) (resp2 *proto.OrderResponse, respErr error) {
|
|
|
|
defer func() {
|
|
if err := recover(); err != nil {
|
|
respErr = fmt.Errorf("panic: %v", err)
|
|
}
|
|
}()
|
|
|
|
c, err := transConfig(request.Config)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
poReq, err := orderReq(request.Order, request.Product)
|
|
if err != nil {
|
|
return nil, proto.ErrorParamFail(err.Error())
|
|
}
|
|
|
|
param, err := c.paramReq(poReq, orderMethod)
|
|
if err != nil {
|
|
return nil, proto.ErrorParamFail(err.Error())
|
|
}
|
|
|
|
uv, err := req(c, param)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
_, bodyBytes, err := Post(ctx, uv)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var response *po.OrderResp
|
|
if err = json.Unmarshal(bodyBytes, &response); err != nil {
|
|
return nil, proto.ErrorResponseFail(err.Error())
|
|
}
|
|
|
|
return orderResp(request, response), nil
|
|
}
|
|
|
|
func (s *AlipayCpnService) Query(ctx context.Context, request *proto.QueryRequest) (resp2 *proto.QueryResponse, respErr error) {
|
|
|
|
defer func() {
|
|
if err := recover(); err != nil {
|
|
respErr = fmt.Errorf("panic: %v", err)
|
|
}
|
|
}()
|
|
|
|
c, err := transConfig(request.Config)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
poReq, err := queryReq(request.Order)
|
|
if err != nil {
|
|
return nil, proto.ErrorParamFail(err.Error())
|
|
}
|
|
|
|
param, err := c.paramReq(poReq, queryMethod)
|
|
if err != nil {
|
|
return nil, proto.ErrorParamFail(err.Error())
|
|
}
|
|
|
|
uv, err := req(c, param)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
_, bodyBytes, err := Post(ctx, uv)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var response po.QueryResp
|
|
if err = json.Unmarshal(bodyBytes, &response); err != nil {
|
|
return nil, proto.ErrorResponseFail(err.Error())
|
|
}
|
|
|
|
return queryResp(request, response), nil
|
|
}
|
|
|
|
func (s *AlipayCpnService) Notify(_ context.Context, request *proto.NotifyRequest) (resp2 *proto.NotifyResponse, respErr error) {
|
|
|
|
defer func() {
|
|
if err := recover(); err != nil {
|
|
respErr = fmt.Errorf("panic: %v", err)
|
|
}
|
|
}()
|
|
|
|
c, err := transConfig(request.Config)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
n := notifyReq(request)
|
|
|
|
b, err := Verify(n, c.Npk)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !b {
|
|
return nil, proto.ErrorSignFail("验签失败")
|
|
}
|
|
|
|
return notifyResp(n)
|
|
}
|