Skip to content

Commit

Permalink
feat: specify proxy url for paypal client (#370)
Browse files Browse the repository at this point in the history
* feat: proxy base url for paypal client
  • Loading branch information
philo-veritas authored Nov 27, 2023
1 parent 1f59bfa commit d4c6332
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
4 changes: 2 additions & 2 deletions paypal/access_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ func (c *Client) goAuthRefreshToken() {
// 文档:https://developer.paypal.com/docs/api/reference/get-an-access-token
func (c *Client) GetAccessToken() (token *AccessToken, err error) {
var (
baseUrl = baseUrlProd
baseUrl = c.baseUrlProd
url string
)
if !c.IsProd {
baseUrl = baseUrlSandbox
baseUrl = c.baseUrlSandbox
}
url = baseUrl + getAccessToken
// Authorization
Expand Down
42 changes: 27 additions & 15 deletions paypal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ import (

// Client PayPal支付客户端
type Client struct {
Clientid string
Secret string
Appid string
AccessToken string
ExpiresIn int
IsProd bool
ctx context.Context
DebugSwitch gopay.DebugSwitch
hc *xhttp.Client
Clientid string
Secret string
Appid string
AccessToken string
ExpiresIn int
IsProd bool
ctx context.Context
DebugSwitch gopay.DebugSwitch
hc *xhttp.Client
baseUrlProd string
baseUrlSandbox string
}

// NewClient 初始化PayPal支付客户端
Expand All @@ -27,12 +29,14 @@ func NewClient(clientid, secret string, isProd bool) (client *Client, err error)
return nil, gopay.MissPayPalInitParamErr
}
client = &Client{
Clientid: clientid,
Secret: secret,
IsProd: isProd,
ctx: context.Background(),
DebugSwitch: gopay.DebugOff,
hc: xhttp.NewClient(),
Clientid: clientid,
Secret: secret,
IsProd: isProd,
ctx: context.Background(),
DebugSwitch: gopay.DebugOff,
hc: xhttp.NewClient(),
baseUrlProd: baseUrlProd,
baseUrlSandbox: baseUrlSandbox,
}
_, err = client.GetAccessToken()
if err != nil {
Expand All @@ -49,3 +53,11 @@ func (c *Client) SetBodySize(sizeMB int) {
c.hc.SetBodySize(sizeMB)
}
}

// SetProxyUrl 设置代理 Url
// 使用场景:
// 1. 大陆直接调用 PayPal 接口响应较慢,可以在第三地例如硅谷部署代理服务器来加速请求
func (c *Client) SetProxyUrl(proxyUrlProd, proxyUrlSandbox string) {
c.baseUrlProd = proxyUrlProd
c.baseUrlSandbox = proxyUrlSandbox
}
20 changes: 10 additions & 10 deletions paypal/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
)

func (c *Client) doPayPalGet(ctx context.Context, uri string) (res *http.Response, bs []byte, err error) {
var url = baseUrlProd + uri
var url = c.baseUrlProd + uri
if !c.IsProd {
url = baseUrlSandbox + uri
url = c.baseUrlSandbox + uri
}
req := c.hc.Req() // default json
req.Header.Add(HeaderAuthorization, AuthorizationPrefixBearer+c.AccessToken)
Expand All @@ -33,9 +33,9 @@ func (c *Client) doPayPalGet(ctx context.Context, uri string) (res *http.Respons
}

func (c *Client) doPayPalPost(ctx context.Context, bm gopay.BodyMap, path string) (res *http.Response, bs []byte, err error) {
var url = baseUrlProd + path
var url = c.baseUrlProd + path
if !c.IsProd {
url = baseUrlSandbox + path
url = c.baseUrlSandbox + path
}
req := c.hc.Req() // default json
req.Header.Add(HeaderAuthorization, AuthorizationPrefixBearer+c.AccessToken)
Expand All @@ -57,9 +57,9 @@ func (c *Client) doPayPalPost(ctx context.Context, bm gopay.BodyMap, path string
}

func (c *Client) doPayPalPut(ctx context.Context, bm gopay.BodyMap, path string) (res *http.Response, bs []byte, err error) {
var url = baseUrlProd + path
var url = c.baseUrlProd + path
if !c.IsProd {
url = baseUrlSandbox + path
url = c.baseUrlSandbox + path
}
req := c.hc.Req() // default json
req.Header.Add(HeaderAuthorization, AuthorizationPrefixBearer+c.AccessToken)
Expand All @@ -81,9 +81,9 @@ func (c *Client) doPayPalPut(ctx context.Context, bm gopay.BodyMap, path string)
}

func (c *Client) doPayPalPatch(ctx context.Context, patchs []*Patch, path string) (res *http.Response, bs []byte, err error) {
var url = baseUrlProd + path
var url = c.baseUrlProd + path
if !c.IsProd {
url = baseUrlSandbox + path
url = c.baseUrlSandbox + path
}
req := c.hc.Req() // default json
req.Header.Add(HeaderAuthorization, AuthorizationPrefixBearer+c.AccessToken)
Expand All @@ -106,9 +106,9 @@ func (c *Client) doPayPalPatch(ctx context.Context, patchs []*Patch, path string
}

func (c *Client) doPayPalDelete(ctx context.Context, path string) (res *http.Response, bs []byte, err error) {
var url = baseUrlProd + path
var url = c.baseUrlProd + path
if !c.IsProd {
url = baseUrlSandbox + path
url = c.baseUrlSandbox + path
}
req := c.hc.Req() // default json
req.Header.Add(HeaderAuthorization, AuthorizationPrefixBearer+c.AccessToken)
Expand Down

0 comments on commit d4c6332

Please sign in to comment.