access_token changes

This commit is contained in:
Aliaksandr Pliutau 2015-11-20 13:38:40 +07:00
parent b8dcf26390
commit 3e7a00c5d2
5 changed files with 9 additions and 74 deletions

View File

@ -7,16 +7,12 @@ PayPal REST API
```go ```go
// Create a client instance // Create a client instance
c, err := paypalsdk.NewClient("clietnid", "secret", paypalsdk.APIBaseSandBox) c, err := paypalsdk.NewClient("clietnid", "secret", paypalsdk.APIBaseSandBox)
``` c.SetLogFile("/tpm/paypal-debug.log") // Set log file if necessary
```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"})
``` ```
```go ```go
// When you will have authorization_code you can get an access_token // 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 ```go
@ -25,7 +21,7 @@ amount := Amount{
Total: 15.1111, Total: 15.1111,
Currency: "USD", 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 // If paymentResult.ID is not empty and paymentResult.Links is also
// we can redirect user to approval page (paymentResult.Links[0]). // we can redirect user to approval page (paymentResult.Links[0]).

36
auth.go
View File

@ -2,48 +2,20 @@ package paypalsdk
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
"net/http" "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 // GetAccessToken returns struct of TokenResponse
// Client must to get an authorization code before func (c *Client) GetAccessToken() (*TokenResponse, error) {
// redirectURI must match with redirectURI sent to GetAuthorizationCodeURL buf := bytes.NewBuffer([]byte("grant_type=client_credentials"))
func (c *Client) GetAccessToken(authorizationCode string, redirectURI string) (*TokenResponse, error) { req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/oauth2/token"), buf)
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)
if err != nil { if err != nil {
return &TokenResponse{}, err return &TokenResponse{}, err
} }
req.SetBasicAuth(c.ClientID, c.Secret) 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{} t := TokenResponse{}
err = c.Send(req, &t) err = c.Send(req, &t)

View File

@ -4,37 +4,7 @@ import (
"testing" "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) { func TestGetAccessToken(t *testing.T) {
c, _ := NewClient("clid", "secret", APIBaseSandBox) c, _ := NewClient("clid", "secret", APIBaseSandBox)
c.GetAccessToken()
_, 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")
} }

View File

@ -15,7 +15,4 @@ func main() {
fmt.Println("ERROR: " + err.Error()) fmt.Println("ERROR: " + err.Error())
os.Exit(1) os.Exit(1)
} }
url, err := client.GetAuthorizationCodeURL("http://test.com", []string{})
fmt.Println("DEBUG: AuthCodeURL=" + url)
} }

View File

@ -12,7 +12,7 @@ import (
func (c *Client) CreateDirectPaypalPayment(amount Amount, redirectURI string) (*PaymentResponse, error) { func (c *Client) CreateDirectPaypalPayment(amount Amount, redirectURI string) (*PaymentResponse, error) {
buf := bytes.NewBuffer([]byte("{\"intent\":\"sale\",\"payer\":{\"payment_method\":\"paypal\"}," + buf := bytes.NewBuffer([]byte("{\"intent\":\"sale\",\"payer\":{\"payment_method\":\"paypal\"}," +
"\"transactions\":[{\"amount\":{\"total\":\"" + strconv.FormatFloat(amount.Total, 'f', 2, 64) + "\"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) req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/payment"), buf)
if err != nil { if err != nil {
return &PaymentResponse{}, err return &PaymentResponse{}, err