Skip to content

Commit

Permalink
update http request
Browse files Browse the repository at this point in the history
  • Loading branch information
iGoogle-ink committed Oct 1, 2023
1 parent d91e20e commit 13dbee5
Show file tree
Hide file tree
Showing 16 changed files with 606 additions and 561 deletions.
66 changes: 39 additions & 27 deletions lakala/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,27 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
"hash"
"net/http"
"strings"
"sync"
"time"

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

// Client lakala
type Client struct {
ctx context.Context // 上下文
PartnerCode string // partner_code:商户编码,由4~6位大写字母或数字构成
credentialCode string // credential_code:系统为商户分配的开发校验码,请妥善保管,不要在公开场合泄露
bodySize int // http response body size(MB), default is 10MB
IsProd bool // 是否生产环境
DebugSwitch gopay.DebugSwitch // 调试开关,是否打印日志
hc *xhttp.Client
sha256Hash hash.Hash
}

// NewClient 初始化lakala户端
Expand All @@ -38,14 +42,16 @@ func NewClient(partnerCode, credentialCode string, isProd bool) (client *Client,
credentialCode: credentialCode,
IsProd: isProd,
DebugSwitch: gopay.DebugOff,
hc: xhttp.NewClient(),
sha256Hash: sha256.New(),
}
return client, nil
}

// SetBodySize 设置http response body size(MB)
func (c *Client) SetBodySize(sizeMB int) {
if sizeMB > 0 {
c.bodySize = sizeMB
c.hc.SetBodySize(sizeMB)
}
}

Expand Down Expand Up @@ -82,32 +88,38 @@ func (c *Client) getRsaSign(bm gopay.BodyMap) (sign string, err error) {
ts = bm.Get("time")
nonceStr = bm.Get("nonce_str")
credentialCode = c.credentialCode
mu sync.Mutex
)
if ts == "" || nonceStr == "" {
return "", fmt.Errorf("签名缺少必要的参数")
}
validStr := fmt.Sprintf("%v&%v&%v&%v", partnerCode, ts, nonceStr, credentialCode)
h := sha256.New()
h.Write([]byte(validStr))
sign = strings.ToLower(hex.EncodeToString(h.Sum(nil)))
mu.Lock()
defer func() {
c.sha256Hash.Reset()
mu.Unlock()
}()
c.sha256Hash.Write([]byte(validStr))
sign = strings.ToLower(hex.EncodeToString(c.sha256Hash.Sum(nil)))
return
}

// PUT 发起请求
func (c *Client) doPut(ctx context.Context, path string, bm gopay.BodyMap) (bs []byte, err error) {
httpClient := xhttp.NewClient().Type(xhttp.TypeJSON)
if c.bodySize > 0 {
httpClient.SetBodySize(c.bodySize)
}
httpClient.Header.Add("Content-Type", "application/json")
httpClient.Header.Add("Accept", "application/json")
var url = baseUrlProd + path
param, err := c.pubParamsHandle()
if err != nil {
return nil, err
}
req := c.hc.Req()
req.Header.Add("Accept", "application/json")
uri := url + "?" + param
res, bs, err := httpClient.Put(uri).SendBodyMap(bm).EndBytes(ctx)
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)
}
res, bs, err := req.Put(uri).SendBodyMap(bm).EndBytes(ctx)
if err != nil {
return nil, err
}
Expand All @@ -119,19 +131,20 @@ func (c *Client) doPut(ctx context.Context, path string, bm gopay.BodyMap) (bs [

// PUT 发起请求
func (c *Client) doPost(ctx context.Context, path string, bm gopay.BodyMap) (bs []byte, err error) {
httpClient := xhttp.NewClient().Type(xhttp.TypeJSON)
if c.bodySize > 0 {
httpClient.SetBodySize(c.bodySize)
}
httpClient.Header.Add("Content-Type", "application/json")
httpClient.Header.Add("Accept", "application/json")
var url = baseUrlProd + path
param, err := c.pubParamsHandle()
if err != nil {
return nil, err
}
req := c.hc.Req()
req.Header.Add("Accept", "application/json")
uri := url + "?" + param
res, bs, err := httpClient.Post(uri).SendBodyMap(bm).EndBytes(ctx)
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)
}
res, bs, err := req.Post(uri).SendBodyMap(bm).EndBytes(ctx)
if err != nil {
return nil, err
}
Expand All @@ -143,13 +156,6 @@ func (c *Client) doPost(ctx context.Context, path string, bm gopay.BodyMap) (bs

// GET 发起请求
func (c *Client) doGet(ctx context.Context, path, queryParams string) (bs []byte, err error) {
httpClient := xhttp.NewClient().Type(xhttp.TypeJSON)
if c.bodySize > 0 {
httpClient.SetBodySize(c.bodySize)
}
httpClient.Header.Add("Content-Type", "application/json")
httpClient.Header.Add("Accept", "application/json")

var url = baseUrlProd + path
param, err := c.pubParamsHandle()
if err != nil {
Expand All @@ -158,8 +164,14 @@ func (c *Client) doGet(ctx context.Context, path, queryParams string) (bs []byte
if queryParams != "" {
param = param + "&" + queryParams
}
req := c.hc.Req()
req.Header.Add("Accept", "application/json")
uri := url + "?" + param
res, bs, err := httpClient.Get(uri).EndBytes(ctx)
if c.DebugSwitch == gopay.DebugOn {
xlog.Debugf("Lakala_Url: %s", uri)
xlog.Debugf("Lakala_Req_Headers: %#v", req.Header)
}
res, bs, err := req.Get(uri).EndBytes(ctx)
if err != nil {
return nil, err
}
Expand Down
16 changes: 8 additions & 8 deletions paypal/access_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,24 @@ func (c *Client) GetAccessToken() (token *AccessToken, err error) {
url = baseUrl + getAccessToken
// Authorization
authHeader := AuthorizationPrefixBasic + base64.StdEncoding.EncodeToString([]byte(c.Clientid+":"+c.Secret))
// Request
httpClient := xhttp.NewClient()
httpClient.Header.Add(HeaderAuthorization, authHeader)
httpClient.Header.Add("Accept", "*/*")
req := c.hc.Req(xhttp.TypeForm)
req.Header.Add(HeaderAuthorization, authHeader)
req.Header.Add("Accept", "*/*")
// Body
bm := make(gopay.BodyMap)
bm.Set("grant_type", "client_credentials")
if c.DebugSwitch == gopay.DebugOn {
xlog.Debugf("PayPal_RequestBody: %s", bm.JsonBody())
xlog.Debugf("PayPal_Authorization: %s", authHeader)
xlog.Debugf("PayPal_Url: %s", url)
xlog.Debugf("PayPal_Req_Body: %s", bm.JsonBody())
xlog.Debugf("PayPal_Req_Headers: %#v", req.Header)
}
res, bs, err := httpClient.Type(xhttp.TypeForm).Post(url).SendBodyMap(bm).EndBytes(c.ctx)
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_Headers: %#v", res.Header)
xlog.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
144 changes: 3 additions & 141 deletions paypal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ package paypal

import (
"context"
"encoding/json"
"net/http"

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

// Client PayPal支付客户端
Expand All @@ -18,10 +15,10 @@ type Client struct {
Appid string
AccessToken string
ExpiresIn int
bodySize int // http response body size(MB), default is 10MB
IsProd bool
ctx context.Context
DebugSwitch gopay.DebugSwitch
hc *xhttp.Client
}

// NewClient 初始化PayPal支付客户端
Expand All @@ -35,6 +32,7 @@ func NewClient(clientid, secret string, isProd bool) (client *Client, err error)
IsProd: isProd,
ctx: context.Background(),
DebugSwitch: gopay.DebugOff,
hc: xhttp.NewClient(),
}
_, err = client.GetAccessToken()
if err != nil {
Expand All @@ -46,142 +44,6 @@ func NewClient(clientid, secret string, isProd bool) (client *Client, err error)
// SetBodySize 设置http response body size(MB)
func (c *Client) SetBodySize(sizeMB int) {
if sizeMB > 0 {
c.bodySize = sizeMB
c.hc.SetBodySize(sizeMB)
}
}

func (c *Client) doPayPalGet(ctx context.Context, uri string) (res *http.Response, bs []byte, err error) {
var url = baseUrlProd + uri
if !c.IsProd {
url = baseUrlSandbox + uri
}
httpClient := xhttp.NewClient()
if c.bodySize > 0 {
httpClient.SetBodySize(c.bodySize)
}
authHeader := AuthorizationPrefixBearer + c.AccessToken
if c.DebugSwitch == gopay.DebugOn {
xlog.Debugf("PayPal_Url: %s", url)
xlog.Debugf("PayPal_Authorization: %s", authHeader)
}
httpClient.Header.Add(HeaderAuthorization, authHeader)
httpClient.Header.Add("Accept", "*/*")
res, bs, err = httpClient.Type(xhttp.TypeJSON).Get(url).EndBytes(ctx)
if err != nil {
return nil, nil, err
}
if c.DebugSwitch == gopay.DebugOn {
xlog.Debugf("PayPal_Response: %d > %s", res.StatusCode, string(bs))
xlog.Debugf("PayPal_Headers: %#v", res.Header)
}
return res, bs, nil
}

func (c *Client) doPayPalPost(ctx context.Context, bm gopay.BodyMap, path string) (res *http.Response, bs []byte, err error) {
var url = baseUrlProd + path
if !c.IsProd {
url = baseUrlSandbox + path
}
httpClient := xhttp.NewClient()
if c.bodySize > 0 {
httpClient.SetBodySize(c.bodySize)
}
authHeader := AuthorizationPrefixBearer + c.AccessToken
if c.DebugSwitch == gopay.DebugOn {
xlog.Debugf("PayPal_RequestBody: %s", bm.JsonBody())
xlog.Debugf("PayPal_Authorization: %s", authHeader)
}
httpClient.Header.Add(HeaderAuthorization, authHeader)
httpClient.Header.Add("Accept", "*/*")
res, bs, err = httpClient.Type(xhttp.TypeJSON).Post(url).SendBodyMap(bm).EndBytes(ctx)
if err != nil {
return nil, nil, err
}
if c.DebugSwitch == gopay.DebugOn {
xlog.Debugf("PayPal_Response: %d > %s", res.StatusCode, string(bs))
xlog.Debugf("PayPal_Headers: %#v", res.Header)
}
return res, bs, nil
}

func (c *Client) doPayPalPut(ctx context.Context, bm gopay.BodyMap, path string) (res *http.Response, bs []byte, err error) {
var url = baseUrlProd + path
if !c.IsProd {
url = baseUrlSandbox + path
}
httpClient := xhttp.NewClient()
if c.bodySize > 0 {
httpClient.SetBodySize(c.bodySize)
}
authHeader := AuthorizationPrefixBearer + c.AccessToken
if c.DebugSwitch == gopay.DebugOn {
xlog.Debugf("PayPal_RequestBody: %s", bm.JsonBody())
xlog.Debugf("PayPal_Authorization: %s", authHeader)
}
httpClient.Header.Add(HeaderAuthorization, authHeader)
httpClient.Header.Add("Accept", "*/*")
res, bs, err = httpClient.Type(xhttp.TypeJSON).Put(url).SendBodyMap(bm).EndBytes(ctx)
if err != nil {
return nil, nil, err
}
if c.DebugSwitch == gopay.DebugOn {
xlog.Debugf("PayPal_Response: %d > %s", res.StatusCode, string(bs))
xlog.Debugf("PayPal_Headers: %#v", res.Header)
}
return res, bs, nil
}

func (c *Client) doPayPalPatch(ctx context.Context, patchs []*Patch, path string) (res *http.Response, bs []byte, err error) {
var url = baseUrlProd + path
if !c.IsProd {
url = baseUrlSandbox + path
}
httpClient := xhttp.NewClient()
if c.bodySize > 0 {
httpClient.SetBodySize(c.bodySize)
}
authHeader := AuthorizationPrefixBearer + c.AccessToken
if c.DebugSwitch == gopay.DebugOn {
jb, _ := json.Marshal(patchs)
xlog.Debugf("PayPal_RequestBody: %s", string(jb))
xlog.Debugf("PayPal_Authorization: %s", authHeader)
}
httpClient.Header.Add(HeaderAuthorization, authHeader)
httpClient.Header.Add("Accept", "*/*")
res, bs, err = httpClient.Type(xhttp.TypeJSON).Patch(url).SendStruct(patchs).EndBytes(ctx)
if err != nil {
return nil, nil, err
}
if c.DebugSwitch == gopay.DebugOn {
xlog.Debugf("PayPal_Response: %d > %s", res.StatusCode, string(bs))
xlog.Debugf("PayPal_Headers: %#v", res.Header)
}
return res, bs, nil
}

func (c *Client) doPayPalDelete(ctx context.Context, path string) (res *http.Response, bs []byte, err error) {
var url = baseUrlProd + path
if !c.IsProd {
url = baseUrlSandbox + path
}
httpClient := xhttp.NewClient()
if c.bodySize > 0 {
httpClient.SetBodySize(c.bodySize)
}
authHeader := AuthorizationPrefixBearer + c.AccessToken
if c.DebugSwitch == gopay.DebugOn {
xlog.Debugf("PayPal_Url: %s", url)
xlog.Debugf("PayPal_Authorization: %s", authHeader)
}
httpClient.Header.Add(HeaderAuthorization, authHeader)
httpClient.Header.Add("Accept", "*/*")
res, bs, err = httpClient.Type(xhttp.TypeJSON).Patch(url).EndBytes(ctx)
if err != nil {
return nil, nil, err
}
if c.DebugSwitch == gopay.DebugOn {
xlog.Debugf("PayPal_Response: %d > %s", res.StatusCode, string(bs))
xlog.Debugf("PayPal_Headers: %#v", res.Header)
}
return res, bs, nil
}
Loading

0 comments on commit 13dbee5

Please sign in to comment.