From d4c63327775109b03c31de67a65fa5b4431facc2 Mon Sep 17 00:00:00 2001 From: Philoveritas Date: Mon, 27 Nov 2023 13:41:26 +0800 Subject: [PATCH] feat: specify proxy url for paypal client (#370) * feat: proxy base url for paypal client --- paypal/access_token.go | 4 ++-- paypal/client.go | 42 +++++++++++++++++++++++++++--------------- paypal/request.go | 20 ++++++++++---------- 3 files changed, 39 insertions(+), 27 deletions(-) diff --git a/paypal/access_token.go b/paypal/access_token.go index 83a0c64b..42f9e074 100644 --- a/paypal/access_token.go +++ b/paypal/access_token.go @@ -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 diff --git a/paypal/client.go b/paypal/client.go index 670cd7b0..6efa62b9 100644 --- a/paypal/client.go +++ b/paypal/client.go @@ -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支付客户端 @@ -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 { @@ -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 +} diff --git a/paypal/request.go b/paypal/request.go index 05dc031d..004e442f 100644 --- a/paypal/request.go +++ b/paypal/request.go @@ -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) @@ -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) @@ -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) @@ -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) @@ -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)