95 lines
2.2 KiB
Go
95 lines
2.2 KiB
Go
package internal
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"gitea.cdlsxd.cn/BaseSystem/plugin/proto"
|
||
"github.com/carlmjohnson/requests"
|
||
"plugins/alipay_redpack/internal/po"
|
||
)
|
||
|
||
// 插件通信信息,若不对应则会报错panic
|
||
const (
|
||
Tag = "alipay_redpack"
|
||
Version = 1
|
||
CookieKey = "alipay_redpack"
|
||
CookieValue = "alipay_redpack"
|
||
)
|
||
|
||
const baseUri = "https://openapi.alipay.com/gateway.do"
|
||
|
||
const (
|
||
orderMethod = "alipay.fund.trans.uni.transfer"
|
||
queryMethod = "alipay.fund.trans.common.query"
|
||
)
|
||
|
||
type AlipayCpnService struct{}
|
||
|
||
func (s *AlipayCpnService) Order(ctx context.Context, request *proto.OrderRequest) (*proto.OrderResponse, error) {
|
||
c, err := transConfig(request.Config)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
poReq, err := orderReq(request.Order, request.Product)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
param, err := c.paramReq(poReq, orderMethod)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
uv, err := req(c, param)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
var response po.OrderResp
|
||
err = requests.URL(c.BaseUri).BodyForm(uv).ToJSON(&response).Fetch(ctx)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("请求异常,msg:" + err.Error())
|
||
}
|
||
|
||
return orderResp(request, response), nil
|
||
}
|
||
|
||
func (s *AlipayCpnService) Query(ctx context.Context, request *proto.QueryRequest) (*proto.QueryResponse, error) {
|
||
c, err := transConfig(request.Config)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
poReq, err := queryReq(request.Order)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
param, err := c.paramReq(poReq, queryMethod)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
uv, err := req(c, param)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
var response po.QueryResp
|
||
err = requests.URL(c.BaseUri).BodyForm(uv).ToJSON(&response).Fetch(ctx)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("请求异常,msg:" + err.Error())
|
||
}
|
||
return queryResp(request, response), nil
|
||
}
|
||
|
||
func (s *AlipayCpnService) Notify(_ context.Context, request *proto.NotifyRequest) (*proto.NotifyResponse, error) {
|
||
c, err := transConfig(request.Config)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
n := notifyReq(request)
|
||
|
||
b, err := Verify(n, c.PukPath)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if !b {
|
||
return nil, fmt.Errorf("验签失败")
|
||
}
|
||
return notifyResp(n), nil
|
||
}
|