Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: specify paypal url #364

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions paypal/access_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (
// 文档: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
32 changes: 22 additions & 10 deletions paypal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@ 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支付客户端
func NewClient(clientid, secret string, isProd bool) (client *Client, err error) {
func NewClient(clientid, secret string, isProd bool, specBaseUrlProd, specBaseUrlSandbox string) (client *Client, err error) {
if clientid == util.NULL || secret == util.NULL {
return nil, gopay.MissPayPalInitParamErr
}
Expand All @@ -38,6 +40,16 @@ func NewClient(clientid, secret string, isProd bool) (client *Client, err error)
if err != nil {
return nil, err
}
if specBaseUrlProd != "" {
client.BaseUrlProd = specBaseUrlProd
} else {
client.BaseUrlProd = baseUrlProd
}
if specBaseUrlSandbox != "" {
client.BaseUrlSandbox = specBaseUrlSandbox
} else {
client.BaseUrlSandbox = baseUrlSandbox
}
return client, nil
}

Expand Down
2 changes: 1 addition & 1 deletion paypal/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) {
client, err = NewClient(Clientid, Secret, false)
client, err = NewClient(Clientid, Secret, false, "", "")
if err != nil {
xlog.Error(err)
return
Expand Down
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