Skip to content

Commit

Permalink
v1.5.103
Browse files Browse the repository at this point in the history
  • Loading branch information
iGoogle-ink committed Jul 3, 2024
1 parent 1a66685 commit 23ed6c4
Show file tree
Hide file tree
Showing 22 changed files with 252 additions and 168 deletions.
32 changes: 22 additions & 10 deletions alipay/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Client struct {
aliPayPublicKey *rsa.PublicKey // 支付宝证书公钥内容 alipayPublicCert.crt
autoSign bool
DebugSwitch gopay.DebugSwitch
logger xlog.XLogger
location *time.Location
hc *xhttp.Client
}
Expand All @@ -51,13 +52,16 @@ func NewClient(appid, privateKey string, isProd bool) (client *Client, err error
if err != nil {
return nil, err
}
logger := xlog.NewLogger()
logger.SetLevel(xlog.DebugLevel)
client = &Client{
AppId: appid,
Charset: UTF8,
SignType: RSA2,
IsProd: isProd,
privateKey: priKey,
DebugSwitch: gopay.DebugOff,
logger: logger,
hc: xhttp.NewClient(),
}
return client, nil
Expand All @@ -69,7 +73,7 @@ func NewClient(appid, privateKey string, isProd bool) (client *Client, err error
func (a *Client) AutoVerifySign(alipayPublicKeyContent []byte) {
pubKey, err := xpem.DecodePublicKey(alipayPublicKeyContent)
if err != nil {
xlog.Errorf("AutoVerifySign(%s),err:%+v", alipayPublicKeyContent, err)
a.logger.Errorf("AutoVerifySign(%s),err:%+v", alipayPublicKeyContent, err)
}
if pubKey != nil {
a.aliPayPublicKey = pubKey
Expand All @@ -84,6 +88,19 @@ func (a *Client) SetBodySize(sizeMB int) {
}
}

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

func (a *Client) SetLogger(logger xlog.XLogger) {
if logger != nil {
a.logger = logger
}
}

// SetAESKey 设置 biz_content 的AES加密key,设置此参数默认开启 biz_content 参数加密
// 注意:目前不可用,设置后会报错
func (a *Client) SetAESKey(aesKey string) {
Expand Down Expand Up @@ -128,7 +145,7 @@ func (a *Client) RequestParam(bm gopay.BodyMap, method string) (string, error) {
}

if a.DebugSwitch == gopay.DebugOn {
xlog.Debugf("Alipay_Request: %s", bm.JsonBody())
a.logger.Debugf("Alipay_Request: %s", bm.JsonBody())
}
return bm.EncodeURLParams(), nil
}
Expand Down Expand Up @@ -192,8 +209,8 @@ func (a *Client) pubParamsHandle(bm gopay.BodyMap, method, bizContent string, au
return "", fmt.Errorf("EncryptBizContent Error: %w", err)
}
if a.DebugSwitch == gopay.DebugOn {
xlog.Debugf("Alipay_Origin_BizContent: %s", bizContent)
xlog.Debugf("Alipay_Encrypt_BizContent: %s", encryptBizContent)
a.logger.Debugf("Alipay_Origin_BizContent: %s", bizContent)
a.logger.Debugf("Alipay_Encrypt_BizContent: %s", encryptBizContent)
}
pubBody.Set("biz_content", encryptBizContent)
}
Expand All @@ -205,7 +222,7 @@ func (a *Client) pubParamsHandle(bm gopay.BodyMap, method, bizContent string, au
}
pubBody.Set("sign", sign)
if a.DebugSwitch == gopay.DebugOn {
xlog.Debugf("Alipay_Request: %s", pubBody.JsonBody())
a.logger.Debugf("Alipay_Request: %s", pubBody.JsonBody())
}
param = pubBody.EncodeURLParams()
return
Expand Down Expand Up @@ -249,8 +266,3 @@ func (a *Client) encryptBizContent(originData string) (string, error) {
}
return base64.StdEncoding.EncodeToString(encryptData), nil
}

// SetHttpClient 设置自定义的xhttp.Client
func (a *Client) SetHttpClient(client *xhttp.Client) {
a.hc = client
}
17 changes: 8 additions & 9 deletions alipay/client_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/go-pay/gopay"
"github.com/go-pay/gopay/pkg/xhttp"
"github.com/go-pay/xlog"
"github.com/go-pay/xtime"
)

Expand Down Expand Up @@ -71,7 +70,7 @@ func (a *Client) PostFileAliPayAPISelfV2(ctx context.Context, bm gopay.BodyMap,
bm.Set(k, v)
}
if a.DebugSwitch == gopay.DebugOn {
xlog.Debugf("Alipay_Request: %s", bm.JsonBody())
a.logger.Debugf("Alipay_Request: %s", bm.JsonBody())
}
if a.IsProd {
url = baseUrlUtf8
Expand All @@ -84,7 +83,7 @@ func (a *Client) PostFileAliPayAPISelfV2(ctx context.Context, bm gopay.BodyMap,
return nil
}
if a.DebugSwitch == gopay.DebugOn {
xlog.Debugf("Alipay_Response: %s%d %s%s", xlog.Red, res.StatusCode, xlog.Reset, string(bs))
a.logger.Debugf("Alipay_Response: %d, %s", res.StatusCode, string(bs))
}
if res.StatusCode != 200 {
return fmt.Errorf("HTTP Request Error, StatusCode = %d", res.StatusCode)
Expand Down Expand Up @@ -112,7 +111,7 @@ func (a *Client) doAliPaySelf(ctx context.Context, bm gopay.BodyMap, method stri
bm.Set("sign", sign)
}
if a.DebugSwitch == gopay.DebugOn {
xlog.Debugf("Alipay_Request: %s", bm.JsonBody())
a.logger.Debugf("Alipay_Request: %s", bm.JsonBody())
}
if a.IsProd {
url = baseUrlUtf8
Expand All @@ -124,7 +123,7 @@ func (a *Client) doAliPaySelf(ctx context.Context, bm gopay.BodyMap, method stri
return nil, err
}
if a.DebugSwitch == gopay.DebugOn {
xlog.Debugf("Alipay_Response: %s%d %s%s", xlog.Red, res.StatusCode, xlog.Reset, string(bs))
a.logger.Debugf("Alipay_Response: %d, %s", res.StatusCode, string(bs))
}
if res.StatusCode != 200 {
return nil, fmt.Errorf("HTTP Request Error, StatusCode = %d", res.StatusCode)
Expand Down Expand Up @@ -179,7 +178,7 @@ func (a *Client) doAliPay(ctx context.Context, bm gopay.BodyMap, method string,
return nil, err
}
if a.DebugSwitch == gopay.DebugOn {
xlog.Debugf("Alipay_Response: %s%d %s%s", xlog.Red, res.StatusCode, xlog.Reset, string(bs))
a.logger.Debugf("Alipay_Response: %d, %s", res.StatusCode, string(bs))
}
if res.StatusCode != 200 {
return nil, fmt.Errorf("HTTP Request Error, StatusCode = %d", res.StatusCode)
Expand Down Expand Up @@ -235,7 +234,7 @@ func (a *Client) DoAliPay(ctx context.Context, bm gopay.BodyMap, method string,
return nil, err
}
if a.DebugSwitch == gopay.DebugOn {
xlog.Debugf("Alipay_Response: %s%d %s%s", xlog.Red, res.StatusCode, xlog.Reset, string(bs))
a.logger.Debugf("Alipay_Response: %d, %s", res.StatusCode, string(bs))
}
if res.StatusCode != 200 {
return nil, fmt.Errorf("HTTP Request Error, StatusCode = %d", res.StatusCode)
Expand Down Expand Up @@ -338,7 +337,7 @@ func (a *Client) FileUploadRequest(ctx context.Context, bm gopay.BodyMap, method
}
pubBody.Set("sign", sign)
if a.DebugSwitch == gopay.DebugOn {
xlog.Debugf("Alipay_Query_Request: %s", pubBody.JsonBody())
a.logger.Debugf("Alipay_Query_Request: %s", pubBody.JsonBody())
}
url := baseUrlUtf8 + "&" + pubBody.EncodeURLParams()

Expand All @@ -348,7 +347,7 @@ func (a *Client) FileUploadRequest(ctx context.Context, bm gopay.BodyMap, method
return nil, err
}
if a.DebugSwitch == gopay.DebugOn {
xlog.Debugf("Alipay_Response: %s%d %s%s", xlog.Red, res.StatusCode, xlog.Reset, string(bs))
a.logger.Debugf("Alipay_Response: %d, %s", res.StatusCode, string(bs))
}
if res.StatusCode != 200 {
return nil, fmt.Errorf("HTTP Request Error, StatusCode = %d", res.StatusCode)
Expand Down
2 changes: 1 addition & 1 deletion alipay/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var (
)

func TestMain(m *testing.M) {
xlog.Level = xlog.DebugLevel
xlog.SetLevel(xlog.DebugLevel)
// 初始化支付宝客户端
// appid:应用ID
// privateKey:应用私钥,支持PKCS1和PKCS8
Expand Down
5 changes: 2 additions & 3 deletions alipay/customs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/go-pay/gopay"
"github.com/go-pay/gopay/pkg/xhttp"
"github.com/go-pay/xlog"
)

// alipay.trade.customs.declare(统一收单报关接口)
Expand Down Expand Up @@ -76,15 +75,15 @@ func (a *Client) doAliPayCustoms(ctx context.Context, bm gopay.BodyMap, service

bm.Set("sign_type", RSA).Set("sign", sign)
if a.DebugSwitch == gopay.DebugOn {
xlog.Debugf("Alipay_Request: %s", bm.JsonBody())
a.logger.Debugf("Alipay_Request: %s", bm.JsonBody())
}
// request
res, bs, err := a.hc.Req(xhttp.TypeFormData).Post("https://mapi.alipay.com/gateway.do").SendString(bm.EncodeURLParams()).EndBytes(ctx)
if err != nil {
return nil, err
}
if a.DebugSwitch == gopay.DebugOn {
xlog.Debugf("Alipay_Response: %s%d %s%s", xlog.Red, res.StatusCode, xlog.Reset, string(bs))
a.logger.Debugf("Alipay_Response: %d, %s", res.StatusCode, string(bs))
}
if res.StatusCode != 200 {
return nil, fmt.Errorf("HTTP Request Error, StatusCode = %d", res.StatusCode)
Expand Down
5 changes: 2 additions & 3 deletions alipay/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/go-pay/crypto/xpem"
"github.com/go-pay/crypto/xrsa"
"github.com/go-pay/gopay"
"github.com/go-pay/xlog"
)

// 允许进行 sn 提取的证书签名算法
Expand Down Expand Up @@ -192,7 +191,7 @@ func (a *Client) getRsaSign(bm gopay.BodyMap, signType string) (sign string, err
}
signParams := bm.EncodeAliPaySignParams()
if a.DebugSwitch == gopay.DebugOn {
xlog.Debugf("Alipay_Request_SignStr: %s", signParams)
a.logger.Debugf("Alipay_Request_SignStr: %s", signParams)
}
if _, err = h.Write([]byte(signParams)); err != nil {
return
Expand Down Expand Up @@ -283,7 +282,7 @@ func VerifySyncSignWithCert(alipayPublicKeyCert any, signData, sign string) (ok
func (a *Client) autoVerifySignByCert(sign, signData string, signDataErr error) (err error) {
if a.autoSign && a.aliPayPublicKey != nil {
if a.DebugSwitch == gopay.DebugOn {
xlog.Debugf("Alipay_SyncSignData: %s, Sign=[%s]", signData, sign)
a.logger.Debugf("Alipay_SyncSignData: %s, Sign=[%s]", signData, sign)
}
// 只有证书验签时,才可能出现此error
if signDataErr != nil {
Expand Down
2 changes: 1 addition & 1 deletion apple/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var (
)

func TestMain(m *testing.M) {
xlog.Level = xlog.DebugLevel
xlog.SetLevel(xlog.DebugLevel)
// 初始化客户端
// iss:issuer ID
// bid:bundle ID
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module github.com/go-pay/gopay

go 1.20
go 1.21

require (
github.com/go-pay/crypto v0.0.1
github.com/go-pay/errgroup v0.0.2
github.com/go-pay/util v0.0.2
github.com/go-pay/xlog v0.0.2
github.com/go-pay/xlog v0.0.3
github.com/go-pay/xtime v0.0.2
golang.org/x/crypto v0.24.0
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/go-pay/errgroup v0.0.2 h1:5mZMdm0TDClDm2S3G0/sm0f8AuQRtz0dOrTHDR9R8Cc
github.com/go-pay/errgroup v0.0.2/go.mod h1:0+4b8mvFMS71MIzsaC+gVvB4x37I93lRb2dqrwuU8x8=
github.com/go-pay/util v0.0.2 h1:goJ4f6kNY5zzdtg1Cj8oWC+Cw7bfg/qq2rJangMAb9U=
github.com/go-pay/util v0.0.2/go.mod h1:qM8VbyF1n7YAPZBSJONSPMPsPedhUTktewUAdf1AjPg=
github.com/go-pay/xlog v0.0.2 h1:kUg5X8/5VZAPDg1J5eGjA3MG0/H5kK6Ew0dW/Bycsws=
github.com/go-pay/xlog v0.0.2/go.mod h1:DbjMADPK4+Sjxj28ekK9goqn4zmyY4hql/zRiab+S9E=
github.com/go-pay/xlog v0.0.3 h1:avyMhCL/JgBHreoGx/am/kHxfs1udDOAeVqbmzP/Yes=
github.com/go-pay/xlog v0.0.3/go.mod h1:mH47xbobrdsSHWsmFtSF5agWbMHFP+tK0ZbVCk5OAEw=
github.com/go-pay/xtime v0.0.2 h1:7YR4/iuELsEHpJ6LUO0SVK80hQxDO9MLCfuVYIiTCRM=
github.com/go-pay/xtime v0.0.2/go.mod h1:W1yRbJaSt4CSBcdAtLBQ8xajiN/Pl5hquGczUcUE9xE=
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
Expand Down
33 changes: 25 additions & 8 deletions lakala/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Client struct {
credentialCode string // credential_code:系统为商户分配的开发校验码,请妥善保管,不要在公开场合泄露
IsProd bool // 是否生产环境
DebugSwitch gopay.DebugSwitch // 调试开关,是否打印日志
logger xlog.XLogger
hc *xhttp.Client
sha256Hash hash.Hash
mu sync.Mutex
Expand All @@ -37,12 +38,15 @@ func NewClient(partnerCode, credentialCode string, isProd bool) (client *Client,
if partnerCode == gopay.NULL || credentialCode == gopay.NULL {
return nil, gopay.MissLakalaInitParamErr
}
logger := xlog.NewLogger()
logger.SetLevel(xlog.DebugLevel)
client = &Client{
ctx: context.Background(),
PartnerCode: partnerCode,
credentialCode: credentialCode,
IsProd: isProd,
DebugSwitch: gopay.DebugOff,
logger: logger,
hc: xhttp.NewClient(),
sha256Hash: sha256.New(),
}
Expand All @@ -56,6 +60,19 @@ func (c *Client) SetBodySize(sizeMB int) {
}
}

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

func (c *Client) SetLogger(logger xlog.XLogger) {
if logger != nil {
c.logger = logger
}
}

// 公共参数处理 Query Params
func (c *Client) pubParamsHandle() (param string, err error) {
bm := make(gopay.BodyMap)
Expand Down Expand Up @@ -115,9 +132,9 @@ func (c *Client) doPut(ctx context.Context, path string, bm gopay.BodyMap) (bs [
req.Header.Add("Accept", "application/json")
uri := url + "?" + param
if c.DebugSwitch == gopay.DebugOn {
xlog.Debugf("Lakala_Url: %s", uri)
xlog.Debugf("Lakala_Req_Body: %s", bm.JsonBody())
xlog.Debugf("Lakala_Req_Headers: %#v", req.Header)
c.logger.Debugf("Lakala_Url: %s", uri)
c.logger.Debugf("Lakala_Req_Body: %s", bm.JsonBody())
c.logger.Debugf("Lakala_Req_Headers: %#v", req.Header)
}
res, bs, err := req.Put(uri).SendBodyMap(bm).EndBytes(ctx)
if err != nil {
Expand All @@ -140,9 +157,9 @@ func (c *Client) doPost(ctx context.Context, path string, bm gopay.BodyMap) (bs
req.Header.Add("Accept", "application/json")
uri := url + "?" + param
if c.DebugSwitch == gopay.DebugOn {
xlog.Debugf("Lakala_Url: %s", uri)
xlog.Debugf("Lakala_Req_Body: %s", bm.JsonBody())
xlog.Debugf("Lakala_Req_Headers: %#v", req.Header)
c.logger.Debugf("Lakala_Url: %s", uri)
c.logger.Debugf("Lakala_Req_Body: %s", bm.JsonBody())
c.logger.Debugf("Lakala_Req_Headers: %#v", req.Header)
}
res, bs, err := req.Post(uri).SendBodyMap(bm).EndBytes(ctx)
if err != nil {
Expand All @@ -168,8 +185,8 @@ func (c *Client) doGet(ctx context.Context, path, queryParams string) (bs []byte
req.Header.Add("Accept", "application/json")
uri := url + "?" + param
if c.DebugSwitch == gopay.DebugOn {
xlog.Debugf("Lakala_Url: %s", uri)
xlog.Debugf("Lakala_Req_Headers: %#v", req.Header)
c.logger.Debugf("Lakala_Url: %s", uri)
c.logger.Debugf("Lakala_Req_Headers: %#v", req.Header)
}
res, bs, err := req.Get(uri).EndBytes(ctx)
if err != nil {
Expand Down
15 changes: 7 additions & 8 deletions paypal/access_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ import (
"github.com/go-pay/gopay"
"github.com/go-pay/gopay/pkg/xhttp"
"github.com/go-pay/util/retry"
"github.com/go-pay/xlog"
)

func (c *Client) goAuthRefreshToken() {
defer func() {
if r := recover(); r != nil {
buf := make([]byte, 64<<10)
buf = buf[:runtime.Stack(buf, false)]
xlog.Errorf("paypal_goAuthRefreshToken: panic recovered: %s\n%s", r, buf)
c.logger.Errorf("paypal_goAuthRefreshToken: panic recovered: %s\n%s", r, buf)
}
}()
for {
Expand All @@ -32,7 +31,7 @@ func (c *Client) goAuthRefreshToken() {
return nil
}, 3, time.Second)
if err != nil {
xlog.Errorf("PayPal GetAccessToken Error: %s", err.Error())
c.logger.Errorf("PayPal GetAccessToken Error: %s", err.Error())
}
}
}
Expand All @@ -59,17 +58,17 @@ func (c *Client) GetAccessToken() (token *AccessToken, err error) {
bm := make(gopay.BodyMap)
bm.Set("grant_type", "client_credentials")
if c.DebugSwitch == gopay.DebugOn {
xlog.Debugf("PayPal_Url: %s", url)
xlog.Debugf("PayPal_Req_Body: %s", bm.JsonBody())
xlog.Debugf("PayPal_Req_Headers: %#v", req.Header)
c.logger.Debugf("PayPal_Url: %s", url)
c.logger.Debugf("PayPal_Req_Body: %s", bm.JsonBody())
c.logger.Debugf("PayPal_Req_Headers: %#v", req.Header)
}
res, bs, err := req.Post(url).SendBodyMap(bm).EndBytes(c.ctx)
if err != nil {
return nil, err
}
if c.DebugSwitch == gopay.DebugOn {
xlog.Debugf("PayPal_Response: %d > %s", res.StatusCode, string(bs))
xlog.Debugf("PayPal_Rsp_Headers: %#v", res.Header)
c.logger.Debugf("PayPal_Response: %d > %s", res.StatusCode, string(bs))
c.logger.Debugf("PayPal_Rsp_Headers: %#v", res.Header)
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("HTTP Request Error, StatusCode = %d", res.StatusCode)
Expand Down
Loading

0 comments on commit 23ed6c4

Please sign in to comment.