mirror of
https://github.com/plutov/paypal.git
synced 2025-02-02 15:10:36 +01:00
little fixes after understanding
This commit is contained in:
parent
71e0a81e64
commit
3a2323e1ef
17
README.md
17
README.md
|
@ -1,3 +1,20 @@
|
||||||
[![Build Status](https://travis-ci.org/logpacker/paypalsdk.svg?branch=master)](https://travis-ci.org/logpacker/paypalsdk)
|
[![Build Status](https://travis-ci.org/logpacker/paypalsdk.svg?branch=master)](https://travis-ci.org/logpacker/paypalsdk)
|
||||||
|
|
||||||
PayPal REST API
|
PayPal REST API
|
||||||
|
|
||||||
|
#### Usage
|
||||||
|
|
||||||
|
```
|
||||||
|
// Create a client instance
|
||||||
|
c, err := paypalsdk.NewClient("clietnid", "secret", paypalsdk.APIBaseSandBox)
|
||||||
|
```
|
||||||
|
|
||||||
|
```
|
||||||
|
// 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-uri", []string{"address"})
|
||||||
|
```
|
||||||
|
|
||||||
|
```
|
||||||
|
// When you will have authorization_code you can get an access_token
|
||||||
|
accessToken, err := c.GetAccessToken(authCode, "https://example.com/redirect-uri")
|
||||||
|
```
|
||||||
|
|
29
auth.go
29
auth.go
|
@ -1,12 +1,12 @@
|
||||||
package paypalsdk
|
package paypalsdk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
|
||||||
"strings"
|
|
||||||
"net/url"
|
|
||||||
"errors"
|
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetAuthorizationCodeURL returns URL where we need to redirect user
|
// GetAuthorizationCodeURL returns URL where we need to redirect user
|
||||||
|
@ -25,28 +25,33 @@ func (c *Client) GetAuthorizationCodeURL(redirectURI string, scopes []string) (s
|
||||||
"&redirect_uri=" + url.QueryEscape(redirectURI), nil
|
"&redirect_uri=" + url.QueryEscape(redirectURI), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetRefreshToken returns struct of RefreshTokenResponse
|
// GetAccessToken returns struct of TokenResponse
|
||||||
// Client must to get an authorization code before
|
// Client must to get an authorization code before
|
||||||
// redirectURI must match with redirectURI sent to GetAuthorizationCodeURL
|
// redirectURI must match with redirectURI sent to GetAuthorizationCodeURL
|
||||||
func (c *Client) GetRefreshToken(authorizationCode string, redirectURI string) (*RefreshTokenResponse, error) {
|
func (c *Client) GetAccessToken(authorizationCode string, redirectURI string) (*TokenResponse, error) {
|
||||||
if authorizationCode == "" {
|
if authorizationCode == "" {
|
||||||
return &RefreshTokenResponse{}, errors.New("authorizationCode cannot be empty")
|
return &TokenResponse{}, errors.New("authorizationCode cannot be empty")
|
||||||
}
|
}
|
||||||
if redirectURI == "" {
|
if redirectURI == "" {
|
||||||
return &RefreshTokenResponse{}, errors.New("redirectURI cannot be empty")
|
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)))
|
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)
|
req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/identity/openidconnect/tokenservice"), buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &RefreshTokenResponse{}, 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/x-www-form-urlencoded")
|
||||||
|
|
||||||
r := RefreshTokenResponse{}
|
t := TokenResponse{}
|
||||||
err = c.Send(req, &r)
|
err = c.Send(req, &t)
|
||||||
|
|
||||||
return &r, err
|
// Set Token fur current Client
|
||||||
|
if t.Token != "" {
|
||||||
|
c.Token = &t
|
||||||
|
}
|
||||||
|
|
||||||
|
return &t, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,18 +23,18 @@ func TestGetAuthorizationCodeURL(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetRefreshToken(t *testing.T) {
|
func TestGetAccessToken(t *testing.T) {
|
||||||
c, _ := NewClient("clid", "secret", APIBaseSandBox)
|
c, _ := NewClient("clid", "secret", APIBaseSandBox)
|
||||||
|
|
||||||
_, err := c.GetRefreshToken("123", "")
|
_, err := c.GetAccessToken("123", "")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("redirectURI is required in GetRefreshToken")
|
t.Errorf("redirectURI is required in GetRefreshToken")
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = c.GetRefreshToken("", "123")
|
_, err = c.GetAccessToken("", "123")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("authorizationCode is required in GetRefreshToken")
|
t.Errorf("authorizationCode is required in GetRefreshToken")
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = c.GetRefreshToken("123", "123")
|
_, err = c.GetAccessToken("123", "123")
|
||||||
}
|
}
|
||||||
|
|
23
client.go
23
client.go
|
@ -2,12 +2,10 @@ package paypalsdk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"errors"
|
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewClient returns new Client struct
|
// NewClient returns new Client struct
|
||||||
|
@ -25,23 +23,6 @@ func NewClient(clientID string, secret string, APIBase string) (*Client, error)
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAcessToken request a new access token from Paypal
|
|
||||||
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, "/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")
|
|
||||||
|
|
||||||
t := TokenResponse{}
|
|
||||||
err = c.Send(req, &t)
|
|
||||||
|
|
||||||
return &t, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Send makes a request to the API, the response body will be
|
// Send makes a request to the API, the response body will be
|
||||||
// unmarshaled into v, or if v is an io.Writer, the response will
|
// unmarshaled into v, or if v is an io.Writer, the response will
|
||||||
// be written to it without decoding
|
// be written to it without decoding
|
||||||
|
|
|
@ -15,12 +15,4 @@ func main() {
|
||||||
fmt.Println("ERROR: " + err.Error())
|
fmt.Println("ERROR: " + err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
token, err := client.GetAccessToken()
|
|
||||||
if err == nil {
|
|
||||||
fmt.Println("DEBUG: Access token=" + token.Token)
|
|
||||||
} else {
|
|
||||||
fmt.Println("ERROR: " + err.Error())
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ package paypalsdk
|
||||||
|
|
||||||
import ()
|
import ()
|
||||||
|
|
||||||
// CreateDirectCreditCardPatment sends request with payment
|
// CreateDirectCreditCardPayment sends request with payment
|
||||||
func CreateDirectCreditCardPatment(cc CreditCrad, amount Amount) error {
|
func CreateDirectCreditCardPayment(cc CreditCard, amount Amount) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
15
types.go
15
types.go
|
@ -25,18 +25,9 @@ type (
|
||||||
|
|
||||||
// TokenResponse maps to the API response for the /oauth2/token endpoint
|
// TokenResponse maps to the API response for the /oauth2/token endpoint
|
||||||
TokenResponse struct {
|
TokenResponse struct {
|
||||||
Scope string `json:"scope"`
|
RefreshToken string `json:"refresh_token"`
|
||||||
Token string `json:"access_token"`
|
Token string `json:"access_token"`
|
||||||
Type string `json:"token_type"`
|
Type string `json:"token_type"`
|
||||||
AppID string `json:"app_id"`
|
|
||||||
ExpiresIn int `json:"expires_in"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// RefreshTokenResponse maps to the API response for the /v1/identity/openidconnect/tokenservice
|
|
||||||
RefreshTokenResponse struct {
|
|
||||||
Type string `json:"token_type"`
|
|
||||||
RefreshToken string `json:"refresh_token"`
|
|
||||||
AccessToken string `json:"access_token"`
|
|
||||||
ExpiresIn int `json:"expires_in"`
|
ExpiresIn int `json:"expires_in"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,8 +47,8 @@ type (
|
||||||
Issue string `json:"issue"`
|
Issue string `json:"issue"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreditCrad - All info about customer's CC
|
// CreditCard - All info about customer's CC
|
||||||
CreditCrad struct {
|
CreditCard struct {
|
||||||
Type string
|
Type string
|
||||||
Number string
|
Number string
|
||||||
ExpireYear int
|
ExpireYear int
|
||||||
|
|
Loading…
Reference in New Issue
Block a user