epay

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 21 Imported by: 4

Documentation

Index

Constants

View Source
const (
	// SignTypeMD5 使用商户密钥计算 MD5 签名,Config.SignType 为空时默认使用。
	SignTypeMD5 = "MD5"
	// SignTypeRSA 使用商户私钥签名、平台公钥验签,算法为 SHA256WithRSA。
	SignTypeRSA = "RSA"
)
View Source
const (
	// PurchaseUrl MD5 模式的页面跳转支付地址。
	PurchaseUrl = "/submit.php"
	// PurchaseRSAUrl RSA 模式的页面跳转支付地址。
	PurchaseRSAUrl = "/api/pay/submit"
)
View Source
const StatusTradeSuccess = "TRADE_SUCCESS"
View Source
const TimestampTolerance = 5 * time.Minute

TimestampTolerance RSA 回调和接口返回数据中 timestamp 与本机时间允许的最大偏差。

Variables

View Source
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")
)
View Source
var ErrInvalidParams = errors.New("epay: invalid signing parameters")

ErrInvalidParams 表示参数无法安全地拼接为易支付待签名字符串。

Functions

func CreateUrlString deprecated

func CreateUrlString(keys []string, values []string) string

CreateUrlString 生成待签名字符串,非法参数返回空字符串。

Deprecated: 使用 CreateUrlStringWithError 处理参数校验错误,不要对失败返回的空字符串签名。

func CreateUrlStringWithError

func CreateUrlStringWithError(keys []string, values []string) (string, error)

CreateUrlStringWithError 生成待签名字符串,例如 a=d&b=e&c=f。 易支付协议不对值进行 URL 编码,因此拒绝会使参数边界产生歧义的分隔符。 参数名不得为空或包含 &、=,参数值不得包含 &;值中的 = 不影响边界。

func GenerateParams deprecated

func GenerateParams(params map[string]string, key string) map[string]string

GenerateParams 生成加签参数,非法参数返回 nil 且不修改输入。

Deprecated: 使用 GenerateParamsWithError 处理参数校验错误。

func GenerateParamsWithError

func GenerateParamsWithError(params map[string]string, key string) (map[string]string, error)

GenerateParamsWithError 校验参数并生成签名,成功时将 sign 和 sign_type 写回 params。 校验失败时返回 ErrInvalidParams 包装错误,且不修改输入。

func MD5String

func MD5String(urlString string, key string) string

MD5String 生成 加盐(商户 key) MD5 字符串

func ParamsFilter

func ParamsFilter(params map[string]string) map[string]string

ParamsFilter 过滤参数,生成签名时需删除 “sign” 和 “sign_type” 参数

func ParamsSort

func ParamsSort(params map[string]string) ([]string, []string)

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。

func ParseRSAPublicKey added in v0.0.6

func ParseRSAPublicKey(key string) (*rsa.PublicKey, error)

ParseRSAPublicKey 解析平台公钥。 支持 PEM 文本,或去掉首尾行的 Base64 文本(可含换行);格式为 PKIX 或 PKCS#1。

Types

type Client

type Client struct {
	Config  *Config
	BaseUrl *url.URL
}

func NewClient

func NewClient(config *Config, baseUrl string) (*Client, error)

NewClient 创建一个新的易支付客户端。 RSA 模式会解析商户私钥和平台公钥,密钥缺失或格式错误时返回 ErrInvalidKey 包装错误。

func (*Client) Purchase

func (c *Client) Purchase(args *PurchaseArgs) (string, map[string]string, error)

Purchase 生成支付链接和参数

func (*Client) SignParams added in v0.0.6

func (c *Client) SignParams(params map[string]string) (map[string]string, error)

SignParams 为请求参数签名,返回写入 pid、sign 和 sign_type 的新参数,不修改输入。 RSA 模式还会写入当前 timestamp,并用商户私钥计算 SHA256WithRSA 签名; 订单查询、退款等接口请求可用它签名,再用 VerifyResponse 校验返回数据。

func (*Client) Verify

func (c *Client) Verify(params map[string]string) (*VerifyRes, error)

Verify 校验回调参数,不修改输入。签名不匹配时 VerifyStatus 为 false。 RSA 模式用平台公钥验签;签名有效但 timestamp 缺失或超出 TimestampTolerance 时返回 ErrInvalidTimestamp。

func (*Client) VerifyResponse added in v0.0.6

func (c *Client) VerifyResponse(body []byte) (map[string]string, error)

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 PurchaseArgs struct {
	// 支付类型
	Type string
	// 商家订单号
	ServiceTradeNo string
	// 商品名称
	Name string
	// 金额
	Money string
	// 设备类型
	Device    DeviceType
	NotifyUrl *url.URL
	ReturnUrl *url.URL
}

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:"-"`
}

Jump to

Keyboard shortcuts

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