34 lines
1.1 KiB
Go
34 lines
1.1 KiB
Go
// Package pay 实现 pangolin 作为业务方接入 pay v2 统一支付网关:出站客户端
|
|
// (签名下单/查单/换渠道/取消)、购买台账、App 代理端点与 payment.succeeded
|
|
// webhook 接收器。契约真相源:pay 仓 design/pay-v2 分支(见计划文档头)。
|
|
package pay
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/rand"
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"encoding/hex"
|
|
"strings"
|
|
)
|
|
|
|
// hmacSign 逐字节照抄 pay internal/util/sign.go::HMACSign:
|
|
// HMAC-SHA256(secret, strings.Join(parts, "\n")) → 标准 base64(非 hex/url-safe)。
|
|
func hmacSign(secret string, parts ...string) string {
|
|
mac := hmac.New(sha256.New, []byte(secret))
|
|
mac.Write([]byte(strings.Join(parts, "\n")))
|
|
return base64.StdEncoding.EncodeToString(mac.Sum(nil))
|
|
}
|
|
|
|
func hmacVerify(secret, sig string, parts ...string) bool {
|
|
expected := hmacSign(secret, parts...)
|
|
return hmac.Equal([]byte(expected), []byte(sig))
|
|
}
|
|
|
|
// newNonce 返回 32 字符随机 hex(不新增 uuid 依赖)。
|
|
func newNonce() string {
|
|
b := make([]byte, 16)
|
|
_, _ = rand.Read(b)
|
|
return hex.EncodeToString(b)
|
|
}
|