From 3e7a00c5d2e67cf18819b501e29ceb6d71573dd2 Mon Sep 17 00:00:00 2001 From: Aliaksandr Pliutau Date: Fri, 20 Nov 2015 13:38:40 +0700 Subject: [PATCH] access_token changes --- README.md | 10 +++------- auth.go | 36 ++++-------------------------------- auth_test.go | 32 +------------------------------- examples/main.go | 3 --- payment.go | 2 +- 5 files changed, 9 insertions(+), 74 deletions(-) diff --git a/README.md b/README.md index 71aefa6..55ff8f0 100644 --- a/README.md +++ b/README.md @@ -7,16 +7,12 @@ PayPal REST API ```go // Create a client instance c, err := paypalsdk.NewClient("clietnid", "secret", paypalsdk.APIBaseSandBox) -``` - -```go -// Redirect client to this URL with provided redirect URI and necessary scopes. It's necessary to retreive authorization_code -authCodeURL, err := c.GetAuthorizationCodeURL("https://example.com/redirect-uri1", []string{"address"}) +c.SetLogFile("/tpm/paypal-debug.log") // Set log file if necessary ``` ```go // When you will have authorization_code you can get an access_token -accessToken, err := c.GetAccessToken(authCode, "https://example.com/redirect-uri2") +accessToken, err := c.GetAccessToken() ``` ```go @@ -25,7 +21,7 @@ amount := Amount{ Total: 15.1111, Currency: "USD", } -paymentResult, err := c.CreateDirectPaypalPayment(amount, "http://example.com/redirect-uri3") +paymentResult, err := c.CreateDirectPaypalPayment(amount, "http://example.com/redirect-uri") // If paymentResult.ID is not empty and paymentResult.Links is also // we can redirect user to approval page (paymentResult.Links[0]). diff --git a/auth.go b/auth.go index 4eedc0b..3120d82 100644 --- a/auth.go +++ b/auth.go @@ -2,48 +2,20 @@ package paypalsdk import ( "bytes" - "errors" "fmt" "net/http" - "net/url" - "strings" ) -// 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 strings.Replace(c.APIBase, "api.", "", -1) + "/webapps/auth/protocol/openidconnect/v1/authorize?client_id=" + - c.ClientID + "&response_type=code&scope=" + strings.Join(scopes, "+") + - "&redirect_uri=" + redirectURI, nil -} - // GetAccessToken returns struct of TokenResponse -// Client must to get an authorization code before -// redirectURI must match with redirectURI sent to GetAuthorizationCodeURL -func (c *Client) GetAccessToken(authorizationCode string, redirectURI string) (*TokenResponse, error) { - if authorizationCode == "" { - return &TokenResponse{}, errors.New("authorizationCode cannot be empty") - } - if redirectURI == "" { - return &TokenResponse{}, errors.New("redirectURI cannot be empty") - } - - buf := bytes.NewBuffer([]byte("grant_type=authorization_code&code=" + url.QueryEscape(authorizationCode) + "&redirect_uri=" + url.QueryEscape(redirectURI))) - req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/identity/openidconnect/tokenservice"), buf) +func (c *Client) GetAccessToken() (*TokenResponse, error) { + buf := bytes.NewBuffer([]byte("grant_type=client_credentials")) + req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/oauth2/token"), buf) if err != nil { return &TokenResponse{}, err } req.SetBasicAuth(c.ClientID, c.Secret) - req.Header.Set("Content-type", "application/x-www-form-urlencoded") + req.Header.Set("Content-type", "application/json") t := TokenResponse{} err = c.Send(req, &t) diff --git a/auth_test.go b/auth_test.go index 4cadc5c..8312433 100644 --- a/auth_test.go +++ b/auth_test.go @@ -4,37 +4,7 @@ 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://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) - } - - uri, err = c.GetAuthorizationCodeURL("test", []string{"address"}) - if uri != "https://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 " + uri) - } -} - func TestGetAccessToken(t *testing.T) { c, _ := NewClient("clid", "secret", APIBaseSandBox) - - _, err := c.GetAccessToken("123", "") - if err == nil { - t.Errorf("redirectURI is required in GetRefreshToken") - } - - _, err = c.GetAccessToken("", "123") - if err == nil { - t.Errorf("authorizationCode is required in GetRefreshToken") - } - - _, err = c.GetAccessToken("123", "123") + c.GetAccessToken() } diff --git a/examples/main.go b/examples/main.go index 9a80e10..c17dff9 100644 --- a/examples/main.go +++ b/examples/main.go @@ -15,7 +15,4 @@ func main() { fmt.Println("ERROR: " + err.Error()) os.Exit(1) } - - url, err := client.GetAuthorizationCodeURL("http://test.com", []string{}) - fmt.Println("DEBUG: AuthCodeURL=" + url) } diff --git a/payment.go b/payment.go index 9c4333d..2de3277 100644 --- a/payment.go +++ b/payment.go @@ -12,7 +12,7 @@ import ( func (c *Client) CreateDirectPaypalPayment(amount Amount, redirectURI string) (*PaymentResponse, error) { buf := bytes.NewBuffer([]byte("{\"intent\":\"sale\",\"payer\":{\"payment_method\":\"paypal\"}," + "\"transactions\":[{\"amount\":{\"total\":\"" + strconv.FormatFloat(amount.Total, 'f', 2, 64) + - "\",\"currency\":\"" + amount.Currency + "\"}}],\"redirect_urls\":{\"return_url\":\"" + redirectURI + "\"}}")) + "\",\"currency\":\"" + amount.Currency + "\"},\"description\":\"logpacker.com\"}],\"redirect_urls\":{\"return_url\":\"" + redirectURI + "\"}}")) req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/payment"), buf) if err != nil { return &PaymentResponse{}, err