plugin/alipay/token/client.go

44 lines
1.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package token
import (
"context"
"fmt"
"gitea.cdlsxd.cn/sdk/plugin/alipay"
"gitea.cdlsxd.cn/sdk/plugin/alipay/utils"
"github.com/carlmjohnson/requests"
"time"
)
type Token alipay.Client
func (t *Token) Client(ctx context.Context, code string) (*AlipayTokenResponse, error) {
cert, err := utils.GetCert(t.MchCertPath, t.RootCertPath, t.PukPath, t.AppId)
if err != nil {
return nil, err
}
param := &Param{
AlipayRootCertSn: cert.RootCertSN,
AppCertSn: cert.MchCertSN,
AppId: t.AppId,
Method: "alipay.system.oauth.token",
Format: alipay.Format,
Charset: alipay.Charset,
SignType: alipay.SignType,
Timestamp: time.Now().Format(time.DateTime),
Version: alipay.Version,
Sign: "",
GrantType: "authorization_code",
Code: code,
}
uv, err := utils.UrlValues(t.Prk, param)
if err != nil {
return nil, err
}
var response AlipayTokenResponse
err = requests.URL(alipay.BaseUri).Post().Params(uv).ToJSON(&response).Fetch(ctx)
if err != nil {
return nil, fmt.Errorf("请求异常msg:" + err.Error())
}
return &response, nil
}