Documentation
¶
Index ¶
- Constants
- Variables
- func CreateUrlString(keys []string, values []string) stringdeprecated
- func CreateUrlStringWithError(keys []string, values []string) (string, error)
- func GenerateParams(params map[string]string, key string) map[string]stringdeprecated
- func GenerateParamsWithError(params map[string]string, key string) (map[string]string, error)
- func MD5String(urlString string, key string) string
- func ParamsFilter(params map[string]string) map[string]string
- func ParamsSort(params map[string]string) ([]string, []string)
- func ParseRSAPrivateKey(key string) (*rsa.PrivateKey, error)
- func ParseRSAPublicKey(key string) (*rsa.PublicKey, error)
- type Client
- func (c *Client) Purchase(args *PurchaseArgs) (string, map[string]string, error)
- func (c *Client) SignParams(params map[string]string) (map[string]string, error)
- func (c *Client) Verify(params map[string]string) (*VerifyRes, error)
- func (c *Client) VerifyResponse(body []byte) (map[string]string, error)
- type Config
- type DeviceType
- type PurchaseArgs
- type Service
- type VerifyRes
Constants ¶
const ( // SignTypeMD5 使用商户密钥计算 MD5 签名,Config.SignType 为空时默认使用。 SignTypeMD5 = "MD5" // SignTypeRSA 使用商户私钥签名、平台公钥验签,算法为 SHA256WithRSA。 SignTypeRSA = "RSA" )
const ( // PurchaseUrl MD5 模式的页面跳转支付地址。 PurchaseUrl = "/submit.php" // PurchaseRSAUrl RSA 模式的页面跳转支付地址。 PurchaseRSAUrl = "/api/pay/submit" )
const StatusTradeSuccess = "TRADE_SUCCESS"
const TimestampTolerance = 5 * time.Minute
TimestampTolerance RSA 回调和接口返回数据中 timestamp 与本机时间允许的最大偏差。
Variables ¶
var ( // ErrInvalidKey 表示 RSA 密钥缺失或格式无法解析。 ErrInvalidKey = errors.New("epay: invalid RSA key") // ErrInvalidSignature 表示接口返回数据的签名校验失败。 ErrInvalidSignature = errors.New("epay: invalid signature") // ErrInvalidTimestamp 表示 RSA 数据签名有效,但 timestamp 缺失或超出 TimestampTolerance。 ErrInvalidTimestamp = errors.New("epay: invalid timestamp") )
var ErrInvalidParams = errors.New("epay: invalid signing parameters")
ErrInvalidParams 表示参数无法安全地拼接为易支付待签名字符串。
Functions ¶
func CreateUrlString
deprecated
func CreateUrlStringWithError ¶
CreateUrlStringWithError 生成待签名字符串,例如 a=d&b=e&c=f。 易支付协议不对值进行 URL 编码,因此拒绝会使参数边界产生歧义的分隔符。 参数名不得为空或包含 &、=,参数值不得包含 &;值中的 = 不影响边界。
func GenerateParamsWithError ¶
GenerateParamsWithError 校验参数并生成签名,成功时将 sign 和 sign_type 写回 params。 校验失败时返回 ErrInvalidParams 包装错误,且不修改输入。
func ParamsFilter ¶
ParamsFilter 过滤参数,生成签名时需删除 “sign” 和 “sign_type” 参数
func ParamsSort ¶
ParamsSort 对参数进行排序,返回排序后的 keys 和 values (go 中 map 为乱序)
func ParseRSAPrivateKey ¶ added in v0.0.6
func ParseRSAPrivateKey(key string) (*rsa.PrivateKey, error)
ParseRSAPrivateKey 解析商户私钥。 支持 PEM 文本,或去掉首尾行的 Base64 文本(可含换行);格式为 PKCS#8 或 PKCS#1。
Types ¶
type Client ¶
func (*Client) SignParams ¶ added in v0.0.6
SignParams 为请求参数签名,返回写入 pid、sign 和 sign_type 的新参数,不修改输入。 RSA 模式还会写入当前 timestamp,并用商户私钥计算 SHA256WithRSA 签名; 订单查询、退款等接口请求可用它签名,再用 VerifyResponse 校验返回数据。
func (*Client) Verify ¶
Verify 校验回调参数,不修改输入。签名不匹配时 VerifyStatus 为 false。 RSA 模式用平台公钥验签;签名有效但 timestamp 缺失或超出 TimestampTolerance 时返回 ErrInvalidTimestamp。
func (*Client) VerifyResponse ¶ added in v0.0.6
VerifyResponse 校验 RSA 模式下接口(如订单查询、退款)返回的 JSON,校验通过后以字符串返回各字段。 数字保留原文,true 记为 "1",false 和 null 记为空值;数组和对象不参与签名,也不出现在结果中。 code 不为 0 时返回包含 msg 的错误;签名不匹配返回 ErrInvalidSignature, timestamp 缺失或超出 TimestampTolerance 返回 ErrInvalidTimestamp。
type Config ¶
type Config struct {
PartnerID string
// Key MD5 模式使用的商户密钥。
Key string
// SignType 签名方式:SignTypeMD5(默认)或 SignTypeRSA,不区分大小写。
SignType string
// MerchantPrivateKey RSA 模式使用的商户私钥,用于给请求签名。
// 支持 PEM 文本,或去掉首尾行的 Base64 文本;格式为 PKCS#8 或 PKCS#1。
MerchantPrivateKey string
// PlatformPublicKey RSA 模式使用的平台公钥,用于校验回调和接口返回数据。
// 支持 PEM 文本,或去掉首尾行的 Base64 文本;格式为 PKIX 或 PKCS#1。
PlatformPublicKey string
}
type DeviceType ¶
type DeviceType string
var ( PC DeviceType = "pc" // PC PC端 MOBILE DeviceType = "mobile" // MOBILE 移动端 )
type PurchaseArgs ¶
type Service ¶
type Service interface {
// Purchase 生成支付链接和参数
Purchase(args *PurchaseArgs) (string, map[string]string, error)
// Verify 验证回调参数是否符合签名
Verify(params map[string]string) (*VerifyRes, error)
}
Service 易支付API
type VerifyRes ¶
type VerifyRes struct {
// 支付类型
Type string
// 易支付订单号
TradeNo string `mapstructure:"trade_no"`
// 商家订单号
ServiceTradeNo string `mapstructure:"out_trade_no"`
// 商品名称
Name string
// 金额
Money string
// 订单支付状态
TradeStatus string `mapstructure:"trade_status"`
// 签名检验
VerifyStatus bool `mapstructure:"-"`
}