2024-08-30 18:02:15 +08:00
|
|
|
|
package token
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
2024-08-30 18:03:34 +08:00
|
|
|
|
"gitea.cdlsxd.cn/sdk/plugin/alipay"
|
|
|
|
|
"gitea.cdlsxd.cn/sdk/plugin/alipay/utils"
|
2024-08-30 18:02:15 +08:00
|
|
|
|
"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
|
|
|
|
|
}
|