diff --git a/auth.go b/auth.go new file mode 100644 index 0000000..1ec522e --- /dev/null +++ b/auth.go @@ -0,0 +1,23 @@ +package paypalsdk + +import ( + "strings" + "net/url" + "errors" +) + +// GetAuthorizationCodeURL returns URL where we need to redirect user +// After signin in PayPal get authorization_code on redirectURI +func (c *Client) GetAuthorizationCodeURL(redirectURI string, scopes []string) (string, error) { + if redirectURI == "" { + return "", errors.New("redirectURI cannot be empty") + } + + if len(scopes) == 0 { + scopes = []string{"profile", "email"} + } + + return c.APIBase + "/webapps/auth/protocol/openidconnect/v1/authorize?client_id=" + + url.QueryEscape(c.ClientID) + "&response_type=code&scope=" + strings.Join(scopes, "+") + + "&redirect_uri=" + url.QueryEscape(redirectURI), nil +} diff --git a/auth_test.go b/auth_test.go new file mode 100644 index 0000000..418ae43 --- /dev/null +++ b/auth_test.go @@ -0,0 +1,24 @@ +package paypalsdk + +import ( + "testing" +) + +func TestGetAuthorizationCodeURL(t *testing.T) { + c, _ := NewClient("clid", "secret", APIBaseSandBox) + + _, err := c.GetAuthorizationCodeURL("", []string{}) + if err == nil { + t.Errorf("redirectURI is required in GetAuthorizationCodeURL") + } + + uri, err := c.GetAuthorizationCodeURL("test", []string{}) + if uri != "https://api.sandbox.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize?client_id=clid&response_type=code&scope=profile+email&redirect_uri=test" { + t.Errorf("GetAuthorizationCodeURL returns incorrect value for redirectURI=test") + } + + uri, err = c.GetAuthorizationCodeURL("test", []string{"address"}) + if uri != "https://api.sandbox.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize?client_id=clid&response_type=code&scope=address&redirect_uri=test" { + t.Errorf("GetAuthorizationCodeURL returns incorrect value for redirectURI=test and scope=address") + } +} diff --git a/types.go b/types.go index e0d04d8..400be7d 100644 --- a/types.go +++ b/types.go @@ -6,12 +6,13 @@ import ( "fmt" ) + const ( // APIBaseSandBox points to the sandbox (for testing) version of the API - APIBaseSandBox = "https://api.sandbox.paypal.com/v1" + APIBaseSandBox = "https://api.sandbox.paypal.com" // APIBaseLive points to the live version of the API - APIBaseLive = "https://api.paypal.com/v1" + APIBaseLive = "https://api.paypal.com" ) type (