mirror of
https://github.com/plutov/paypal.git
synced 2025-01-23 10:21:03 +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")
|
||||||
|
```
|
||||||
|
|
71
auth.go
71
auth.go
|
@ -1,52 +1,57 @@
|
||||||
package paypalsdk
|
package paypalsdk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"bytes"
|
||||||
"strings"
|
"errors"
|
||||||
"net/url"
|
"fmt"
|
||||||
"errors"
|
"net/http"
|
||||||
"bytes"
|
"net/url"
|
||||||
"fmt"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetAuthorizationCodeURL returns URL where we need to redirect user
|
// GetAuthorizationCodeURL returns URL where we need to redirect user
|
||||||
// After signin in PayPal get authorization_code on redirectURI
|
// After signin in PayPal get authorization_code on redirectURI
|
||||||
func (c *Client) GetAuthorizationCodeURL(redirectURI string, scopes []string) (string, error) {
|
func (c *Client) GetAuthorizationCodeURL(redirectURI string, scopes []string) (string, error) {
|
||||||
if redirectURI == "" {
|
if redirectURI == "" {
|
||||||
return "", errors.New("redirectURI cannot be empty")
|
return "", errors.New("redirectURI cannot be empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(scopes) == 0 {
|
if len(scopes) == 0 {
|
||||||
scopes = []string{"profile", "email"}
|
scopes = []string{"profile", "email"}
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.APIBase + "/webapps/auth/protocol/openidconnect/v1/authorize?client_id=" +
|
return c.APIBase + "/webapps/auth/protocol/openidconnect/v1/authorize?client_id=" +
|
||||||
url.QueryEscape(c.ClientID) + "&response_type=code&scope=" + strings.Join(scopes, "+") +
|
url.QueryEscape(c.ClientID) + "&response_type=code&scope=" + strings.Join(scopes, "+") +
|
||||||
"&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
|
||||||
}
|
}
|
||||||
|
|
50
auth_test.go
50
auth_test.go
|
@ -1,40 +1,40 @@
|
||||||
package paypalsdk
|
package paypalsdk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGetAuthorizationCodeURL(t *testing.T) {
|
func TestGetAuthorizationCodeURL(t *testing.T) {
|
||||||
c, _ := NewClient("clid", "secret", APIBaseSandBox)
|
c, _ := NewClient("clid", "secret", APIBaseSandBox)
|
||||||
|
|
||||||
_, err := c.GetAuthorizationCodeURL("", []string{})
|
_, err := c.GetAuthorizationCodeURL("", []string{})
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("redirectURI is required in GetAuthorizationCodeURL")
|
t.Errorf("redirectURI is required in GetAuthorizationCodeURL")
|
||||||
}
|
}
|
||||||
|
|
||||||
uri, err := c.GetAuthorizationCodeURL("test", []string{})
|
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" {
|
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")
|
t.Errorf("GetAuthorizationCodeURL returns incorrect value for redirectURI=test")
|
||||||
}
|
}
|
||||||
|
|
||||||
uri, err = c.GetAuthorizationCodeURL("test", []string{"address"})
|
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" {
|
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")
|
t.Errorf("GetAuthorizationCodeURL returns incorrect value for redirectURI=test and scope=address")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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")
|
||||||
}
|
}
|
||||||
|
|
111
client.go
111
client.go
|
@ -1,87 +1,68 @@
|
||||||
package paypalsdk
|
package paypalsdk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"errors"
|
||||||
"net/http"
|
"io"
|
||||||
"errors"
|
"io/ioutil"
|
||||||
"bytes"
|
"net/http"
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewClient returns new Client struct
|
// NewClient returns new Client struct
|
||||||
func NewClient(clientID string, secret string, APIBase string) (*Client, error) {
|
func NewClient(clientID string, secret string, APIBase string) (*Client, error) {
|
||||||
if clientID == "" || secret == "" || APIBase == "" {
|
if clientID == "" || secret == "" || APIBase == "" {
|
||||||
return &Client{}, errors.New("ClientID, Secret and APIBase are required to create a Client")
|
return &Client{}, errors.New("ClientID, Secret and APIBase are required to create a Client")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Client{
|
return &Client{
|
||||||
&http.Client{},
|
&http.Client{},
|
||||||
clientID,
|
clientID,
|
||||||
secret,
|
secret,
|
||||||
APIBase,
|
APIBase,
|
||||||
nil,
|
nil,
|
||||||
}, 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
|
||||||
func (c *Client) Send(req *http.Request, v interface{}) error {
|
func (c *Client) Send(req *http.Request, v interface{}) error {
|
||||||
// Set default headers
|
// Set default headers
|
||||||
req.Header.Set("Accept", "application/json")
|
req.Header.Set("Accept", "application/json")
|
||||||
req.Header.Set("Accept-Language", "en_US")
|
req.Header.Set("Accept-Language", "en_US")
|
||||||
|
|
||||||
// Default values for headers
|
// Default values for headers
|
||||||
if req.Header.Get("Content-type") == "" {
|
if req.Header.Get("Content-type") == "" {
|
||||||
req.Header.Set("Content-type", "application/json")
|
req.Header.Set("Content-type", "application/json")
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := c.client.Do(req)
|
resp, err := c.client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
||||||
errResp := &ErrorResponse{Response: resp}
|
errResp := &ErrorResponse{Response: resp}
|
||||||
data, err := ioutil.ReadAll(resp.Body)
|
data, err := ioutil.ReadAll(resp.Body)
|
||||||
|
|
||||||
if err == nil && len(data) > 0 {
|
if err == nil && len(data) > 0 {
|
||||||
json.Unmarshal(data, errResp)
|
json.Unmarshal(data, errResp)
|
||||||
}
|
}
|
||||||
|
|
||||||
return errResp
|
return errResp
|
||||||
}
|
}
|
||||||
|
|
||||||
if v != nil {
|
if v != nil {
|
||||||
if w, ok := v.(io.Writer); ok {
|
if w, ok := v.(io.Writer); ok {
|
||||||
io.Copy(w, resp.Body)
|
io.Copy(w, resp.Body)
|
||||||
} else {
|
} else {
|
||||||
err = json.NewDecoder(resp.Body).Decode(v)
|
err = json.NewDecoder(resp.Body).Decode(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
package paypalsdk
|
package paypalsdk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNewClient(t *testing.T) {
|
func TestNewClient(t *testing.T) {
|
||||||
_, err := NewClient("", "", "")
|
_, err := NewClient("", "", "")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("All arguments are required in NewClient()")
|
t.Errorf("All arguments are required in NewClient()")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,26 +1,18 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"paypalsdk"
|
"paypalsdk"
|
||||||
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
client, err := paypalsdk.NewClient("AZgwu4yt5Ba0gyTu1dGBH3txHCJbMuFNvrmQxBaQbfDncDiCs6W_rwJD8Ir-0pZrN-_eq7n9zVd8Y-5f", "EBzA1wRl5t73OMugOieDj_tI3vihfJmGl47ukQT-cpctooIzDu0K7IPESNC0cKodlLSOXzwI8qXSM0rd", paypalsdk.APIBaseSandBox)
|
client, err := paypalsdk.NewClient("AZgwu4yt5Ba0gyTu1dGBH3txHCJbMuFNvrmQxBaQbfDncDiCs6W_rwJD8Ir-0pZrN-_eq7n9zVd8Y-5f", "EBzA1wRl5t73OMugOieDj_tI3vihfJmGl47ukQT-cpctooIzDu0K7IPESNC0cKodlLSOXzwI8qXSM0rd", paypalsdk.APIBaseSandBox)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
fmt.Println("DEBUG: ClientID=" + client.ClientID + " APIBase=" + client.APIBase)
|
fmt.Println("DEBUG: ClientID=" + client.ClientID + " APIBase=" + client.APIBase)
|
||||||
} else {
|
} else {
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
17
types.go
17
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"`
|
|
||||||
Token string `json:"access_token"`
|
|
||||||
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"`
|
RefreshToken string `json:"refresh_token"`
|
||||||
AccessToken string `json:"access_token"`
|
Token string `json:"access_token"`
|
||||||
|
Type string `json:"token_type"`
|
||||||
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