alipay

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2024 License: Apache-2.0 Imports: 18 Imported by: 0

README

alipay

支付宝 Go SDK

使用说明
  • 发送HTTP请求,使用 Client.Do(...)
  • 以下场景使用 Action.Encode(...)
    • alipay.trade.app.pay(app支付接口2.0)
    • alipay.fund.auth.order.app.freeze(线上资金授权冻结接口)
  • 以下场景使用 Client.PageExecute(...)
    • alipay.trade.wap.pay(手机网站支付接口2.0)
    • alipay.trade.page.pay(统一收单下单并支付页面接口)
    • alipay.user.certify.open.certify(身份认证开始认证)
  • 验证回调通知,使用 Client.VerifyNotify(...)
  • JSON结果均以 gjson.Result 返回,理论上支持所有 JSON API
  • 解析加密数据,如:授权的用户信息和手机号,使用 Client.DecodeEncryptData(...)

Documentation

Index

Constants

View Source
const (
	HeaderMethodOverride = "x-http-method-override"
	HeaderRequestID      = "alipay-request-id"
	HeaderTraceID        = "alipay-trace-id"
	HeaderRootCertSN     = "alipay-root-cert-sn"
	HeaderNonce          = "alipay-nonce"
	HeaderTimestamp      = "alipay-timestamp"
	HeaderEncryptType    = "alipay-encrypt-type"
	HeaderAppAuthToken   = "alipay-app-auth-token"
	HeaderSignature      = "alipay-signature"
)
View Source
const CodeOK = "10000" // API请求成功

Variables

This section is empty.

Functions

func FormatPKCS1PrivateKey

func FormatPKCS1PrivateKey(pemStr string) (xcrypto.RSAPadding, []byte)

FormatPKCS1PrivateKey 格式化支付宝应用私钥(PKCS#1)

func FormatPKCS1PublicKey

func FormatPKCS1PublicKey(pemStr string) (xcrypto.RSAPadding, []byte)

FormatPKCS1PublicKey 格式化支付宝应用公钥(PKCS#1)

func FormatPKCS8PrivateKey

func FormatPKCS8PrivateKey(pemStr string) (xcrypto.RSAPadding, []byte)

FormatPKCS8PrivateKey 格式化支付宝应用私钥(PKCS#8)

func FormatPKCS8PublicKey

func FormatPKCS8PublicKey(pemStr string) (xcrypto.RSAPadding, []byte)

FormatPKCS8PublicKey 格式化支付宝应用公钥(PKCS#8)

Types

type APIResult

type APIResult struct {
	Code int // HTTP状态码
	Body gjson.Result
}

APIResult API结果 (支付v3)

type Action

type Action struct {
	// contains filtered or unexported fields
}

func NewAction

func NewAction(method string, options ...ActionOption) *Action

NewAction 生成Action

func (*Action) Encode

func (a *Action) Encode(c *Client) (string, error)

Encode 签名并生成请求Body

func (*Action) RespKey

func (a *Action) RespKey() string

RespKey 返回「method」对应的「xxx_response」

type ActionOption

type ActionOption func(a *Action)

ActionOption Action选项

func WithAppAuthToken

func WithAppAuthToken(token string) ActionOption

WithAppAuthToken 设置第三方应用授权Token

func WithAuthToken

func WithAuthToken(token string) ActionOption

WithAuthToken 用户授权令牌

func WithBizContent

func WithBizContent(data lib.X) ActionOption

WithBizContent 设置「biz_content」参数

func WithEncrypt

func WithEncrypt() ActionOption

WithEncrypt 设置请求加密

func WithKVParam

func WithKVParam(k, v string) ActionOption

WithKVParam 设置其它非「biz_content」参数

func WithNotifyURL

func WithNotifyURL(url string) ActionOption

WithNotifyURL 设置异步回调通知URL

func WithOAuthCode

func WithOAuthCode(code string) ActionOption

WithOAuthCode 设置授权码(用授权码来换取授权令牌)

func WithRefreshToken

func WithRefreshToken(token string) ActionOption

WithRefreshToken 设置刷新令牌(用刷新令牌来换取一个新的授权令牌)

func WithReturnURL

func WithReturnURL(url string) ActionOption

WithReturnURL 设置支付成功跳转URL(HTTP/HTTPS开头字符串)

func WithScene

func WithScene(scene string) ActionOption

WithScene 设置业务场景描述

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client 支付宝客户端

func NewClient

func NewClient(appid, aesKey string, options ...Option) *Client

NewClient 生成支付宝客户端

func NewSandbox

func NewSandbox(appid, aesKey string, options ...Option) *Client

NewSandbox 生成支付宝沙箱环境

func (*Client) AppID

func (c *Client) AppID() string

AppID 返回appid

func (*Client) DecodeEncryptData

func (c *Client) DecodeEncryptData(hash crypto.Hash, data, sign string) ([]byte, error)

DecodeEncryptData 解析加密数据,如:授权的用户信息和手机号

func (*Client) Decrypt

func (c *Client) Decrypt(encryptData string) ([]byte, error)

Decrypt 数据解密

func (*Client) Do

func (c *Client) Do(ctx context.Context, method string, options ...ActionOption) (gjson.Result, error)

Do 向支付宝网关发送请求

func (*Client) Encrypt

func (c *Client) Encrypt(data string) (string, error)

Encrypt 数据加密

func (*Client) PageExecute

func (c *Client) PageExecute(method string, options ...ActionOption) (string, error)

PageExecute 致敬官方SDK

func (*Client) Upload

func (c *Client) Upload(ctx context.Context, method string, fieldName, filePath string, formData map[string]string, options ...ActionOption) (gjson.Result, error)

Upload 文件上传,参考:https://opendocs.alipay.com/apis/api_4/alipay.merchant.item.file.upload

func (*Client) UploadWithReader added in v1.2.0

func (c *Client) UploadWithReader(ctx context.Context, method string, fieldName, fileName string, reader io.Reader, formData map[string]string, options ...ActionOption) (gjson.Result, error)

UploadWithReader 文件上传,参考:https://opendocs.alipay.com/apis/api_4/alipay.merchant.item.file.upload

func (*Client) VerifyNotify

func (c *Client) VerifyNotify(form url.Values) (value.V, error)

VerifyNotify 验证回调通知表单数据

type ClientV3

type ClientV3 struct {
	// contains filtered or unexported fields
}

ClientV3 支付宝V3客户端(仅支持v3版本的接口可用)

func NewClientV3

func NewClientV3(appid, aesKey string, options ...V3Option) *ClientV3

NewClientV3 生成支付宝客户端V3

func NewSandboxV3

func NewSandboxV3(appid, aesKey string, options ...V3Option) *ClientV3

NewSandboxV3 生成支付宝沙箱V3

func (*ClientV3) AppID

func (c *ClientV3) AppID() string

AppID 返回appid

func (*ClientV3) Authorization

func (c *ClientV3) Authorization(method, path string, query url.Values, body []byte, header http.Header) (string, error)

Authorization 生成签名并返回 HTTP Authorization

func (*ClientV3) Decrypt

func (c *ClientV3) Decrypt(encryptData string) ([]byte, error)

Decrypt 数据解密

func (*ClientV3) Encrypt

func (c *ClientV3) Encrypt(data string) (string, error)

Encrypt 数据加密

func (*ClientV3) GetJSON

func (c *ClientV3) GetJSON(ctx context.Context, path string, query url.Values, options ...V3HeaderOption) (*APIResult, error)

GetJSON GET请求JSON数据

func (*ClientV3) PostEncrypt

func (c *ClientV3) PostEncrypt(ctx context.Context, path string, params lib.X, options ...V3HeaderOption) (*APIResult, error)

PostJSON POST加密请求

func (*ClientV3) PostJSON

func (c *ClientV3) PostJSON(ctx context.Context, path string, params lib.X, options ...V3HeaderOption) (*APIResult, error)

PostJSON POST请求JSON数据

func (*ClientV3) Upload

func (c *ClientV3) Upload(ctx context.Context, reqPath, fieldName, filePath, bizData string, options ...V3HeaderOption) (*APIResult, error)

Upload 文件上传,参考:https://opendocs.alipay.com/open-v3/054oog?pathHash=7834d743

func (*ClientV3) UploadWithReader added in v1.2.0

func (c *ClientV3) UploadWithReader(ctx context.Context, reqPath, fieldName, fileName string, reader io.Reader, bizData string, options ...V3HeaderOption) (*APIResult, error)

UploadWithReader 文件上传,参考:https://opendocs.alipay.com/open-v3/054oog?pathHash=7834d743

func (*ClientV3) Verify

func (c *ClientV3) Verify(header http.Header, body []byte) error

Verify 验证签名

type GrantType

type GrantType string
const (
	OAuthCode    GrantType = "authorization_code"
	RefreshToken GrantType = "refresh_token"
)

type Option

type Option func(c *Client)

Option 自定义设置项

func WithHttpClient added in v1.2.0

func WithHttpClient(cli *http.Client) Option

WithHttpClient 设置自定义 HTTP Client

func WithLogger

func WithLogger(fn func(ctx context.Context, err error, data map[string]string)) Option

WithLogger 设置日志记录

func WithPrivateKey

func WithPrivateKey(key *xcrypto.PrivateKey) Option

WithPrivateKey 设置商户RSA私钥

func WithPublicKey

func WithPublicKey(key *xcrypto.PublicKey) Option

WithPublicKey 设置平台RSA公钥

type V3HeaderOption

type V3HeaderOption func(h http.Header)

func WithV3AppAuthToken

func WithV3AppAuthToken(token string) V3HeaderOption

func WithV3MethodOverride

func WithV3MethodOverride(method string) V3HeaderOption

func WithV3RootCertSN

func WithV3RootCertSN(sn string) V3HeaderOption

type V3Option

type V3Option func(c *ClientV3)

V3Option 自定义设置项

func WithV3Client

func WithV3Client(cli *http.Client) V3Option

WithV3Client 设置自定义 HTTP Client

func WithV3Logger

func WithV3Logger(fn func(ctx context.Context, err error, data map[string]string)) V3Option

WithV3Logger 设置日志记录

func WithV3PrivateKey

func WithV3PrivateKey(key *xcrypto.PrivateKey) V3Option

WithV3PrivateKey 设置商户RSA私钥

func WithV3PublicKey

func WithV3PublicKey(key *xcrypto.PublicKey) V3Option

WithV3PublicKey 设置平台RSA公钥

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL