1 Star 0 Fork 0

eden-w2w/wechatpay-go

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
wechat_pay_credential.go 1.54 KB
一键复制 编辑 原始数据 按行查看 历史
文兄 提交于 2022-01-07 17:45 +08:00 . change mod name
// Copyright 2021 Tencent Inc. All rights reserved.
// Package credentials 微信支付 API v3 Go SDK 请求报文头 Authorization 信息生成器
package credentials
import (
"context"
"fmt"
"time"
"gitee.com/eden-w2w/wechatpay-go/core/auth"
"gitee.com/eden-w2w/wechatpay-go/core/consts"
"gitee.com/eden-w2w/wechatpay-go/utils"
)
// WechatPayCredentials 微信支付请求报文头 Authorization 信息生成器
type WechatPayCredentials struct {
Signer auth.Signer // 数字签名生成器
}
// GenerateAuthorizationHeader 生成请求报文头中的 Authorization 信息,详见:
// https://wechatpay-api.gitbook.io/wechatpay-api-v3/qian-ming-zhi-nan-1/qian-ming-sheng-cheng
func (c *WechatPayCredentials) GenerateAuthorizationHeader(
ctx context.Context, method, canonicalURL, signBody string,
) (string, error) {
if c.Signer == nil {
return "", fmt.Errorf("you must init WechatPayCredentials with signer")
}
nonce, err := utils.GenerateNonce()
if err != nil {
return "", err
}
timestamp := time.Now().Unix()
message := fmt.Sprintf(consts.SignatureMessageFormat, method, canonicalURL, timestamp, nonce, signBody)
signatureResult, err := c.Signer.Sign(ctx, message)
if err != nil {
return "", err
}
authorization := fmt.Sprintf(
consts.HeaderAuthorizationFormat, c.getAuthorizationType(),
signatureResult.MchID, nonce, timestamp, signatureResult.CertificateSerialNo, signatureResult.Signature,
)
return authorization, nil
}
func (c *WechatPayCredentials) getAuthorizationType() string {
return "WECHATPAY2-" + c.Signer.Algorithm()
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/eden-w2w/wechatpay-go.git
git@gitee.com:eden-w2w/wechatpay-go.git
eden-w2w
wechatpay-go
wechatpay-go
v0.2.12

搜索帮助