alipay

package module
v0.0.0-...-b3e222c Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2022 License: MIT Imports: 20 Imported by: 0

README

AliPay SDK for Golang

安装

启用 Go module
go get github.com/Jason0727/alipay
import github.com/Jason0727/alipay

帮助

在集成的过程中有遇到问题,欢迎加 微信: 13585966246

版本如何初始化

下面用到的 privateKey 需要特别注意一下,如果是通过“支付宝开发平台开发助手”创建的CSR文件,在 CSR 文件所在的目录下会生成相应的私钥文件,我们需要使用该私钥进行签名。

var client, err = alipay.NewClient(appID, privateKey, true)

已实现接口

  • 换取授权访问令牌

    alipay.system.oauth.token - AlipaySystemOauthToken()

  • 支付宝会员授权信息查询接口

    alipay.user.info.share - AlipayUserInfoShare()

  • 小程序发送模板消息

    alipay.open.app.mini.templatemessage.send - AlipayOpenAppMiniTemplateMessageSend()

集成流程

支付宝开放平台申请创建相关的应用,使用自己的支付宝账号登录即可。

沙箱环境

支付宝开放平台为每一个应用提供了沙箱环境,供开发人员开发测试使用。

沙箱环境是独立的,每一个应用都会有一个商家账号和买家账号。

应用信息配置

参考官网文档 进行应用的配置。

本 SDK 中的签名方法默认为 RSA2,采用支付宝提供的 RSA签名&验签工具 生成秘钥时,秘钥的格式必须为 PKCS1,秘钥长度推荐 2048。所以在支付宝管理后台请注意配置 RSA2(SHA256)密钥

生成秘钥对之后,将公钥提供给支付宝(通过支付宝后台上传)对我们请求的数据进行签名验证,我们的代码中将使用私钥对请求数据签名。

请参考 如何生成 RSA 密钥

关于应用私钥 (privateKey)

应用私钥是我们通过工具生成的私钥,调用支付宝接口的时候,我们需要使用该私钥对参数进行签名。

支持 RSA 签名及验证

默认采用的是 RSA2 签名,不再提供 RSA 的支持

License

This project is licensed under the MIT License.

Documentation

Index

Constants

View Source
const (
	ProdURL    = "https://openapi.alipay.com/gateway.do"    // 正式环境请求地址
	SandboxURL = "https://openapi.alipaydev.com/gateway.do" // 沙箱环境请求地址
)
View Source
const (
	Format      = "JSON"                                            // 请求格式
	Charset     = "utf-8"                                           // 字节编码
	Version     = "1.0"                                             // 版本
	SignType    = "RSA2"                                            // 签名方式
	TimeFormat  = "2006-01-02 15:04:05"                             // 日期格式
	ContentType = "application/x-www-form-urlencoded;charset=utf-8" // form表单格式
)
View Source
const (
	EMPTY          = ""
	CertificateEnd = "-----END CERTIFICATE-----"
)
View Source
const (
	HttpGetMethod  = "GET"
	HttpPostMethod = "POST"
)

Variables

View Source
var (
	ErrInitParamsMissed  = errors.New("客户端初始化参数异常")
	MissParamErr         = errors.New("参数不能为空")
	ErrAliCertSnNotMatch = errors.New("当前使用的支付宝公钥证书SN与网关响应报文中的SN不匹配")
	ErrGetSignData       = errors.New("获取报文签名数据失败")
	ErrVerifySignature   = errors.New("验签签名失败")
)
View Source
var (
	AppID = "2017051700859121"

	PrivateKey = "" /* 1588-byte string literal not displayed */

	AppCertPublicKey = `` /* 1290-byte string literal not displayed */

	AlipayRootCert = `` /* 5043-byte string literal not displayed */

	AlipayCertPublicKeyRsa2 = `` /* 2666-byte string literal not displayed */

)

Functions

func DecryptOpenDataToStruct

func DecryptOpenDataToStruct(encryptedData, secretKey string, beanPtr interface{}) (err error)

DecryptOpenDataToStruct 解密支付宝开放数据到 结构体

encryptedData:包括敏感数据在内的完整用户信息的加密数据
secretKey:AES密钥,支付宝管理平台配置
beanPtr:需要解析到的结构体指针
文档:https://opendocs.alipay.com/mini/introduce/aes
文档:https://opendocs.alipay.com/open/common/104567

Types

type AliPayOpenAppMiniTemplateMessageSendData

type AliPayOpenAppMiniTemplateMessageSendData struct {
	*ErrorResponse
}

type AliPayOpenAppMiniTemplateMessageSendResponse

type AliPayOpenAppMiniTemplateMessageSendResponse struct {
	Response     *AliPayOpenAppMiniTemplateMessageSendData `json:"alipay_open_app_mini_templatemessage_send_response"`
	AlipayCertSn string                                    `json:"alipay_cert_sn,omitempty"`
	Sign         string                                    `json:"sign"`
}

type AlipaySystemOauthTokenData

type AlipaySystemOauthTokenData struct {
	AccessToken  string `json:"access_token,omitempty"`
	AlipayUserId string `json:"alipay_user_id,omitempty"`
	ExpiresIn    int64  `json:"expires_in,omitempty"`
	ReExpiresIn  int64  `json:"re_expires_in,omitempty"`
	RefreshToken string `json:"refresh_token,omitempty"`
	UserId       string `json:"user_id,omitempty"`
}

type AlipaySystemOauthTokenResponse

type AlipaySystemOauthTokenResponse struct {
	Response      *AlipaySystemOauthTokenData `json:"alipay_system_oauth_token_response,omitempty"`
	ErrorResponse *ErrorResponse              `json:"error_response,omitempty"`
	AlipayCertSn  string                      `json:"alipay_cert_sn,omitempty"`
	Sign          string                      `json:"sign,omitempty"`
}

type AlipayUserInfoShareData

type AlipayUserInfoShareData struct {
	*ErrorResponse
	UserId   string `json:"user_id,omitempty"`
	Avatar   string `json:"avatar,omitempty"`
	Province string `json:"province,omitempty"`
	City     string `json:"city,omitempty"`
	NickName string `json:"nick_name,omitempty"`
	Gender   string `json:"gender,omitempty"`
	Mobile   string `json:"mobile,omitempty"`
}

type AlipayUserInfoShareResponse

type AlipayUserInfoShareResponse struct {
	Response     *AlipayUserInfoShareData `json:"alipay_user_info_share_response,omitempty"`
	AlipayCertSn string                   `json:"alipay_cert_sn,omitempty"`
	Sign         string                   `json:"sign,omitempty"`
}

type BodyMap

type BodyMap map[string]interface{}

func DecryptOpenDataToBodyMap

func DecryptOpenDataToBodyMap(encryptedData, secretKey string) (bm BodyMap, err error)

DecryptOpenDataToBodyMap 解密支付宝开放数据到 BodyMap

encryptedData:包括敏感数据在内的完整用户信息的加密数据
secretKey:AES密钥,支付宝管理平台配置
文档:https://opendocs.alipay.com/mini/introduce/aes
文档:https://opendocs.alipay.com/open/common/104567

func (BodyMap) CheckEmptyError

func (bm BodyMap) CheckEmptyError(keys ...string) error

CheckEmptyError 校验指定key是否为空

func (BodyMap) Get

func (bm BodyMap) Get(key string) string

获取参数,同 GetString()

func (BodyMap) GetInterface

func (bm BodyMap) GetInterface(key string) interface{}

GetInterface 获取原始参数

func (BodyMap) GetString

func (bm BodyMap) GetString(key string) string

GetString 获取参数并转换成字符串

func (BodyMap) Marshal

func (bm BodyMap) Marshal() (jb string)

Marshal 将 bm 转换成 json

func (BodyMap) Remove

func (bm BodyMap) Remove(key string)

Remove 删除参数

func (BodyMap) Reset

func (bm BodyMap) Reset()

Reset 置空 bm

func (BodyMap) Set

func (bm BodyMap) Set(key string, value interface{}) BodyMap

Set 设置参数

func (BodyMap) SetBodyMap

func (bm BodyMap) SetBodyMap(key string, value func(b BodyMap)) BodyMap

SetBodyMap 回调函数设置 bm

func (BodyMap) Unmarshal

func (bm BodyMap) Unmarshal(ptr interface{}) (err error)

Unmarshal 解析 bm 到 结构体或 map 中

type Client

type Client struct {
	Client *http.Client // http 客户端
	// contains filtered or unexported fields
}

func NewClient

func NewClient(appId, privateKey string, isProd bool) (*Client, error)

NewClient 实例化支付宝客户端 appId: 应用ID privateKey: 应用私钥 isProd: 是否未生产环境

func (*Client) AlipayOpenAppMiniTemplateMessageSend

func (c *Client) AlipayOpenAppMiniTemplateMessageSend(bm BodyMap) (aliRsp *AliPayOpenAppMiniTemplateMessageSendResponse, err error)

AlipayOpenAppMiniTemplateMessageSend 小程序发送模板消息 文档地址: https://opendocs.alipay.com/mini/02cth2

func (*Client) AlipaySystemOauthToken

func (c *Client) AlipaySystemOauthToken(bm BodyMap) (aliRsp *AlipaySystemOauthTokenResponse, err error)

AlipaySystemOauthToken 换取授权访问令牌 文档地址: https://opendocs.alipay.com/open/02xtla

func (*Client) AlipayUserInfoShare

func (c *Client) AlipayUserInfoShare(authToken string) (aliRsp *AlipayUserInfoShareResponse, err error)

AlipayUserInfoShare 支付宝会员授权信息查询 文档地址: https://opendocs.alipay.com/open/02xtlb

func (*Client) AutoVerifySign

func (this *Client) AutoVerifySign(bs []byte, alipayCertSn, sign string) (err error)

AutoVerifySign 自动验签 bs: 响应完整字节数据 alipayCertSn:支付宝返回的证书序号 sign: 支付宝响应的签名

func (*Client) SetCertSn

func (c *Client) SetCertSn(appCertPublicKeyContent, aliPayRootCertContent, aliPayPublicKeyRsaContent string) (err error)

============================================== 证书 ================================== SetCertSn 设置证书序列号 仅支持公钥证书模式

type Code

type Code string
const (
	CodeSuccess          Code = "10000" // 接口调用成功
	CodeUnknowError      Code = "20000" // 服务不可用
	CodeInvalidAuthToken Code = "20001" // 授权权限不足
	CodeMissingParam     Code = "40001" // 缺少必选参数
	CodeInvalidParam     Code = "40002" // 非法的参数
	CodeBusinessFailed   Code = "40004" // 业务处理失败
	CodePermissionDenied Code = "40006" // 权限不足
)

func (Code) IsSuccess

func (c Code) IsSuccess() bool

type ErrorResponse

type ErrorResponse struct {
	Code    Code   `json:"code"`
	Msg     string `json:"msg"`
	SubCode string `json:"sub_code,omitempty"`
	SubMsg  string `json:"sub_msg,omitempty"`
}

func (ErrorResponse) Error

func (e ErrorResponse) Error() string

Directories

Path Synopsis
pkg
aes

Jump to

Keyboard shortcuts

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