Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
iGoogle-ink committed Oct 3, 2024
1 parent 7f55a96 commit 8c54c18
Show file tree
Hide file tree
Showing 14 changed files with 316 additions and 162 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ func main() {

> ### 点击查看不同支付方式的使用文档。方便的话,请留下您认可的小星星,十分感谢!
* #### [支付宝支付(V3版)](https://github.com/go-pay/gopay/blob/main/doc/alipay_v3.md)
* #### [支付宝支付](https://github.com/go-pay/gopay/blob/main/doc/alipay.md)
* #### [微信支付](https://github.com/go-pay/gopay/blob/main/doc/wechat_v3.md)
* #### [微信支付(V3版)](https://github.com/go-pay/gopay/blob/main/doc/wechat_v3.md)
* #### [微信支付(V2版,不推荐)](https://github.com/go-pay/gopay/blob/main/doc/wechat_v2.md)
* #### [QQ支付](https://github.com/go-pay/gopay/blob/main/doc/qq.md)
* #### [通联支付](https://github.com/go-pay/gopay/blob/main/doc/allinpay.md)
* #### [拉卡拉支付](https://github.com/go-pay/gopay/blob/main/doc/lakala.md)
Expand Down
3 changes: 2 additions & 1 deletion alipay/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func (a *Client) SetHttpClient(client *xhttp.Client) {
}
}

// SetLogger 设置自定义的logger
func (a *Client) SetLogger(logger xlog.XLogger) {
if logger != nil {
a.logger = logger
Expand Down Expand Up @@ -126,7 +127,7 @@ func (a *Client) RequestParam(bm gopay.BodyMap, method string) (string, error) {
return "", gopay.BodyMapNilErr
}
// check if there is biz_content
bz := bm.GetInterface("biz_content")
bz := bm.GetAny("biz_content")
if bzBody, ok := bz.(gopay.BodyMap); ok {
if bodyBs, err = json.Marshal(bzBody); err != nil {
return "", fmt.Errorf("json.Marshal(%v):%w", bzBody, err)
Expand Down
2 changes: 1 addition & 1 deletion alipay/client_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (a *Client) PostAliPayAPISelfV2(ctx context.Context, bodyMap gopay.BodyMap,
bs, bodyBs []byte
)
// check if there is biz_content
bz := bodyMap.GetInterface("biz_content")
bz := bodyMap.GetAny("biz_content")
if bzBody, ok := bz.(gopay.BodyMap); ok {
if bodyBs, err = json.Marshal(bzBody); err != nil {
return fmt.Errorf("json.Marshal(%v):%w", bzBody, err)
Expand Down
4 changes: 2 additions & 2 deletions alipay/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func (a *Client) autoVerifySignByCert(sign, signData string, signDataErr error)
// notifyBean:此参数为异步通知解析的结构体或BodyMap:notifyReq 或 bm,推荐通 BodyMap 验签
// 返回参数ok:是否验签通过
// 返回参数err:错误信息
// 验签文档:https://opendocs.alipay.com/open/200/106120
// 验签文档:https://opendocs.alipay.com/common/02mse7
func VerifySign(alipayPublicKey string, notifyBean any) (ok bool, err error) {
if alipayPublicKey == gopay.NULL || notifyBean == nil {
return false, errors.New("alipayPublicKey or notifyBean is nil")
Expand Down Expand Up @@ -352,7 +352,7 @@ func VerifySign(alipayPublicKey string, notifyBean any) (ok bool, err error) {
// notifyBean:此参数为异步通知解析的结构体或BodyMap:notifyReq 或 bm,推荐通 BodyMap 验签
// 返回参数ok:是否验签通过
// 返回参数err:错误信息
// 验签文档:https://opendocs.alipay.com/open/200/106120
// 验签文档:https://opendocs.alipay.com/common/02mse7
func VerifySignWithCert(aliPayPublicKeyCert, notifyBean any) (ok bool, err error) {
if notifyBean == nil || aliPayPublicKeyCert == nil {
return false, errors.New("aliPayPublicKeyCert or notifyBean is nil")
Expand Down
6 changes: 2 additions & 4 deletions alipay/v3/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ var allowSignatureAlgorithm = map[string]bool{
"SHA512-RSAPSS": true,
}

// GetCertSN 获取证书序列号SN
// certPathOrData x509证书文件路径(appPublicCert.crt、alipayPublicCert.crt) 或证书 buffer
// 返回 sn:证书序列号(app_cert_sn、alipay_cert_sn)
// 返回 err:error 信息
func GetCertSN(certPathOrData any) (sn string, err error) {
func getCertSN(certPathOrData any) (sn string, err error) {
var certData []byte
switch pathOrData := certPathOrData.(type) {
case string:
Expand Down Expand Up @@ -61,11 +60,10 @@ func GetCertSN(certPathOrData any) (sn string, err error) {
return sn, nil
}

// GetRootCertSN 获取root证书序列号SN
// rootCertPathOrData x509证书文件路径(alipayRootCert.crt) 或文件 buffer
// 返回 sn:证书序列号(alipay_root_cert_sn)
// 返回 err:error 信息
func GetRootCertSN(rootCertPathOrData any) (sn string, err error) {
func getRootCertSN(rootCertPathOrData any) (sn string, err error) {
var (
certData []byte
certEnd = `-----END CERTIFICATE-----`
Expand Down
27 changes: 24 additions & 3 deletions alipay/v3/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ func (a *ClientV3) SetRequestIdFunc(requestIdFunc xhttp.RequestIdHandler) {
// alipayRootCertContent:支付宝根证书文件内容
// alipayPublicCertContent:支付宝公钥证书文件内容
func (a *ClientV3) SetCert(appCertContent, alipayRootCertContent, alipayPublicCertContent []byte) (err error) {
appCertSn, err := GetCertSN(appCertContent)
appCertSn, err := getCertSN(appCertContent)
if err != nil {
return fmt.Errorf("get app_cert_sn return err, but alse return alipay client. err: %w", err)
}
rootCertSn, err := GetRootCertSN(alipayRootCertContent)
rootCertSn, err := getRootCertSN(alipayRootCertContent)
if err != nil {
return fmt.Errorf("get alipay_root_cert_sn return err, but alse return alipay client. err: %w", err)
}
publicCertSn, err := GetCertSN(alipayPublicCertContent)
publicCertSn, err := getCertSN(alipayPublicCertContent)
if err != nil {
return fmt.Errorf("get alipay_cert_sn return err, but alse return alipay client. err: %w", err)
}
Expand All @@ -96,6 +96,27 @@ func (a *ClientV3) SetCert(appCertContent, alipayRootCertContent, alipayPublicCe
return nil
}

// SetBodySize 设置http response body size(MB)
func (a *ClientV3) SetBodySize(sizeMB int) {
if sizeMB > 0 {
a.hc.SetBodySize(sizeMB)
}
}

// SetHttpClient 设置自定义的xhttp.Client
func (a *ClientV3) SetHttpClient(client *xhttp.Client) {
if client != nil {
a.hc = client
}
}

// SetLogger 设置自定义的logger
func (a *ClientV3) SetLogger(logger xlog.XLogger) {
if logger != nil {
a.logger = logger
}
}

// SetAESKey 设置 biz_content 的AES加密key,设置此参数默认开启 biz_content 参数加密
// 注意:目前不可用,设置后会报错
func (a *ClientV3) SetAESKey(aesKey string) {
Expand Down
41 changes: 36 additions & 5 deletions alipay/v3/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (

"github.com/go-pay/gopay"
"github.com/go-pay/gopay/alipay/cert"
"github.com/go-pay/gopay/pkg/js"
"github.com/go-pay/util"
"github.com/go-pay/xlog"
)

Expand All @@ -18,7 +20,7 @@ var (

func TestMain(m *testing.M) {
xlog.SetLevel(xlog.DebugLevel)
// 初始化支付宝客户端
// 初始化支付宝客V3户端
// appid:应用ID
// privateKey:应用私钥,支持PKCS1和PKCS8
// isProd:是否是正式环境,沙箱环境请选择新版沙箱应用。
Expand All @@ -27,19 +29,48 @@ func TestMain(m *testing.M) {
xlog.Error(err)
return
}

// 自定义配置http请求接收返回结果body大小,默认 10MB
//client.SetBodySize() // 没有特殊需求,可忽略此配置

// Debug开关,输出/关闭日志
client.DebugSwitch = gopay.DebugOn

// 设置biz_content加密KEY,设置此参数默认开启加密(目前不可用,设置后会报错)
// 设置自定义RequestId生成方法
//client.SetRequestIdFunc()

// 设置biz_content加密KEY,设置此参数默认开启加密(目前不可用)
//client.SetAESKey("KvKUTqSVZX2fUgmxnFyMaQ==")

// 传入证书内容
err := client.SetCert(cert.AppPublicContent, cert.AlipayRootContent, cert.AlipayPublicContentRSA2)
// 传入证书文件路径
//err := client.SetCertSnByPath("cert/appPublicCert.crt", "cert/alipayRootCert.crt", "cert/alipayPublicCert.crt")
err = client.SetCert(cert.AppPublicContent, cert.AlipayRootContent, cert.AlipayPublicContentRSA2)
if err != nil {
xlog.Debug("SetCert:", err)
return
}
os.Exit(m.Run())
}

func TestDoAliPayAPISelfV3(t *testing.T) {
// 请求参数
bm := make(gopay.BodyMap)
bm.Set("subject", "预创建创建订单").
Set("out_trade_no", util.RandomString(32)).
Set("total_amount", "0.01")

rsp := new(struct {
OutTradeNo string `json:"out_trade_no"`
QrCode string `json:"qr_code"`
})
// 创建订单
res, err := client.DoAliPayAPISelfV3(ctx, MethodPost, v3TradePrecreate, bm, rsp)
if err != nil {
xlog.Errorf("client.TradePrecreate(), err:%v", err)
return
}
xlog.Debugf("aliRsp:%s", js.Marshal(rsp))
if res.StatusCode != Success {
xlog.Errorf("aliRsp.StatusCode:%d", res.StatusCode)
return
}
}
2 changes: 1 addition & 1 deletion alipay/v3/payment_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (a *ClientV3) DataBillDownloadUrlQuery(ctx context.Context, bm gopay.BodyMa
return nil, err
}
uri := v3DataBillDownloadUrlQuery + "?" + bm.EncodeURLParams()
authorization, err := a.authorization(MethodGet, uri, bm)
authorization, err := a.authorization(MethodGet, uri, nil)
if err != nil {
return nil, err
}
Expand Down
34 changes: 34 additions & 0 deletions alipay/v3/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package alipay

import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
Expand All @@ -19,6 +20,39 @@ func (d *requestIdFunc) RequestId() string {
return fmt.Sprintf("%s-%d", util.RandomString(21), time.Now().Unix())
}

// DoAliPayAPISelfV3 支付宝接口自行实现方法
func (a *ClientV3) DoAliPayAPISelfV3(ctx context.Context, method, path string, bm gopay.BodyMap, aliRsp any) (res *http.Response, err error) {
var (
bs []byte
authorization string
)
switch method {
case MethodGet:
uri := path + "?" + bm.EncodeURLParams()
authorization, err = a.authorization(MethodGet, uri, nil)
if err != nil {
return nil, err
}
res, bs, err = a.doGet(ctx, uri, authorization)
if err != nil {
return nil, err
}
case MethodPost:
authorization, err = a.authorization(MethodPost, path, bm)
if err != nil {
return nil, err
}
res, bs, err = a.doPost(ctx, bm, path, authorization)
if err != nil {
return nil, err
}
}
if err = json.Unmarshal(bs, aliRsp); err != nil {
return nil, err
}
return res, nil
}

func (a *ClientV3) doPost(ctx context.Context, bm gopay.BodyMap, uri, authorization string) (res *http.Response, bs []byte, err error) {
var url = v3BaseUrlCh + uri
if !a.IsProd {
Expand Down
Loading

0 comments on commit 8c54c18

Please sign in to comment.