2015-10-15 07:43:50 +02:00
|
|
|
package paypalsdk
|
|
|
|
|
|
|
|
import (
|
2015-12-01 05:35:25 +01:00
|
|
|
"bytes"
|
2015-10-30 08:02:32 +01:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2016-12-01 05:32:21 +01:00
|
|
|
"fmt"
|
2015-10-30 08:02:32 +01:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2015-11-20 04:17:42 +01:00
|
|
|
"net/http/httputil"
|
2017-07-07 00:52:39 +02:00
|
|
|
"time"
|
2015-10-15 07:43:50 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewClient returns new Client struct
|
2015-12-29 10:21:11 +01:00
|
|
|
// APIBase is a base API URL, for testing you can use paypalsdk.APIBaseSandBox
|
2015-10-15 07:43:50 +02:00
|
|
|
func NewClient(clientID string, secret string, APIBase string) (*Client, error) {
|
2015-10-30 08:02:32 +01:00
|
|
|
if clientID == "" || secret == "" || APIBase == "" {
|
2017-03-02 04:43:23 +01:00
|
|
|
return nil, errors.New("ClientID, Secret and APIBase are required to create a Client")
|
2015-10-30 08:02:32 +01:00
|
|
|
}
|
2015-10-15 07:52:16 +02:00
|
|
|
|
2015-10-30 08:02:32 +01:00
|
|
|
return &Client{
|
2017-07-07 11:57:02 +02:00
|
|
|
Client: &http.Client{},
|
2017-03-02 04:43:23 +01:00
|
|
|
ClientID: clientID,
|
|
|
|
Secret: secret,
|
|
|
|
APIBase: APIBase,
|
2015-10-30 08:02:32 +01:00
|
|
|
}, nil
|
2015-10-15 07:43:50 +02:00
|
|
|
}
|
|
|
|
|
2017-03-02 04:43:23 +01:00
|
|
|
// GetAccessToken returns struct of TokenResponse
|
|
|
|
// No need to call SetAccessToken to apply new access token for current Client
|
|
|
|
// Endpoint: POST /v1/oauth2/token
|
|
|
|
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.Header.Set("Content-type", "application/x-www-form-urlencoded")
|
|
|
|
|
|
|
|
t := TokenResponse{}
|
2017-10-21 15:17:58 +02:00
|
|
|
err = c.SendWithBasicAuth(req, &t)
|
2017-03-02 04:43:23 +01:00
|
|
|
|
|
|
|
// Set Token fur current Client
|
|
|
|
if t.Token != "" {
|
|
|
|
c.Token = &t
|
2017-07-07 00:52:39 +02:00
|
|
|
c.tokenExpiresAt = time.Now().Add(time.Duration(t.ExpiresIn) * time.Second)
|
2017-03-02 04:43:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return &t, err
|
2015-11-20 03:51:18 +01:00
|
|
|
}
|
|
|
|
|
2017-07-07 11:57:02 +02:00
|
|
|
// SetHTTPClient sets *http.Client to current client
|
2018-08-29 09:52:31 +02:00
|
|
|
func (c *Client) SetHTTPClient(client *http.Client) {
|
2017-07-07 11:57:02 +02:00
|
|
|
c.Client = client
|
|
|
|
}
|
|
|
|
|
2015-11-19 06:12:42 +01:00
|
|
|
// SetAccessToken sets saved token to current client
|
2018-08-29 09:52:31 +02:00
|
|
|
func (c *Client) SetAccessToken(token string) {
|
2015-11-19 06:12:42 +01:00
|
|
|
c.Token = &TokenResponse{
|
|
|
|
Token: token,
|
|
|
|
}
|
2017-10-19 19:30:53 +02:00
|
|
|
c.tokenExpiresAt = time.Time{}
|
2015-11-19 06:12:42 +01:00
|
|
|
}
|
|
|
|
|
2017-03-02 04:43:23 +01:00
|
|
|
// SetLog will set/change the output destination.
|
|
|
|
// If log file is set paypalsdk will log all requests and responses to this Writer
|
2018-08-29 09:52:31 +02:00
|
|
|
func (c *Client) SetLog(log io.Writer) {
|
2017-03-02 04:43:23 +01:00
|
|
|
c.Log = log
|
|
|
|
}
|
|
|
|
|
2015-10-15 07:43:50 +02:00
|
|
|
// 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
|
|
|
|
// be written to it without decoding
|
|
|
|
func (c *Client) Send(req *http.Request, v interface{}) error {
|
2016-09-19 06:39:05 +02:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
resp *http.Response
|
|
|
|
data []byte
|
|
|
|
)
|
|
|
|
|
2015-10-30 08:02:32 +01:00
|
|
|
// Set default headers
|
|
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
req.Header.Set("Accept-Language", "en_US")
|
2015-10-15 07:43:50 +02:00
|
|
|
|
2015-10-30 08:02:32 +01:00
|
|
|
// Default values for headers
|
|
|
|
if req.Header.Get("Content-type") == "" {
|
|
|
|
req.Header.Set("Content-type", "application/json")
|
|
|
|
}
|
2015-10-15 07:43:50 +02:00
|
|
|
|
2017-07-07 11:57:02 +02:00
|
|
|
resp, err = c.Client.Do(req)
|
2015-11-20 03:51:18 +01:00
|
|
|
c.log(req, resp)
|
|
|
|
|
2015-10-30 08:02:32 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2015-10-15 07:43:50 +02:00
|
|
|
|
2015-10-30 08:02:32 +01:00
|
|
|
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
|
|
|
errResp := &ErrorResponse{Response: resp}
|
2016-09-19 06:39:05 +02:00
|
|
|
data, err = ioutil.ReadAll(resp.Body)
|
2015-10-15 07:43:50 +02:00
|
|
|
|
2015-10-30 08:02:32 +01:00
|
|
|
if err == nil && len(data) > 0 {
|
|
|
|
json.Unmarshal(data, errResp)
|
|
|
|
}
|
2015-10-15 07:43:50 +02:00
|
|
|
|
2015-10-30 08:02:32 +01:00
|
|
|
return errResp
|
|
|
|
}
|
2015-10-15 07:43:50 +02:00
|
|
|
|
2018-08-29 09:52:31 +02:00
|
|
|
if v == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if w, ok := v.(io.Writer); ok {
|
|
|
|
io.Copy(w, resp.Body)
|
|
|
|
return nil
|
2015-10-30 08:02:32 +01:00
|
|
|
}
|
2015-10-15 07:43:50 +02:00
|
|
|
|
2018-08-29 09:52:31 +02:00
|
|
|
return json.NewDecoder(resp.Body).Decode(v)
|
2015-10-15 07:43:50 +02:00
|
|
|
}
|
2015-11-20 03:51:18 +01:00
|
|
|
|
2015-11-20 03:57:45 +01:00
|
|
|
// SendWithAuth makes a request to the API and apply OAuth2 header automatically.
|
2016-09-19 06:39:05 +02:00
|
|
|
// If the access token soon to be expired or already expired, it will try to get a new one before
|
2015-11-20 03:57:45 +01:00
|
|
|
// making the main request
|
2016-09-19 06:39:05 +02:00
|
|
|
// client.Token will be updated when changed
|
2015-11-20 03:57:45 +01:00
|
|
|
func (c *Client) SendWithAuth(req *http.Request, v interface{}) error {
|
2019-02-27 08:26:37 +01:00
|
|
|
c.Lock()
|
2019-03-01 05:17:03 +01:00
|
|
|
// Note: Here we do not want to `defer c.Unlock()` because we need `c.Send(...)`
|
|
|
|
// to happen outside of the locked section.
|
|
|
|
|
2015-12-01 05:35:25 +01:00
|
|
|
if c.Token != nil {
|
2017-07-07 01:08:37 +02:00
|
|
|
if !c.tokenExpiresAt.IsZero() && c.tokenExpiresAt.Sub(time.Now()) < RequestNewTokenBeforeExpiresIn {
|
2017-04-05 05:37:54 +02:00
|
|
|
// c.Token will be updated in GetAccessToken call
|
|
|
|
if _, err := c.GetAccessToken(); err != nil {
|
2019-02-27 08:26:37 +01:00
|
|
|
c.Unlock()
|
2016-09-19 06:39:05 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-01 05:35:25 +01:00
|
|
|
req.Header.Set("Authorization", "Bearer "+c.Token.Token)
|
|
|
|
}
|
2015-11-20 03:57:45 +01:00
|
|
|
|
2019-03-01 05:17:03 +01:00
|
|
|
// Unlock the client mutex before sending the request, this allows multiple requests
|
|
|
|
// to be in progress at the same time.
|
2019-02-27 08:26:37 +01:00
|
|
|
c.Unlock()
|
2015-11-20 03:57:45 +01:00
|
|
|
return c.Send(req, v)
|
|
|
|
}
|
|
|
|
|
2017-10-21 15:17:58 +02:00
|
|
|
// SendWithBasicAuth makes a request to the API using clientID:secret basic auth
|
|
|
|
func (c *Client) SendWithBasicAuth(req *http.Request, v interface{}) error {
|
|
|
|
req.SetBasicAuth(c.ClientID, c.Secret)
|
|
|
|
|
|
|
|
return c.Send(req, v)
|
|
|
|
}
|
|
|
|
|
2015-12-01 05:35:25 +01:00
|
|
|
// NewRequest constructs a request
|
|
|
|
// Convert payload to a JSON
|
|
|
|
func (c *Client) NewRequest(method, url string, payload interface{}) (*http.Request, error) {
|
|
|
|
var buf io.Reader
|
|
|
|
if payload != nil {
|
|
|
|
var b []byte
|
|
|
|
b, err := json.Marshal(&payload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
buf = bytes.NewBuffer(b)
|
|
|
|
}
|
|
|
|
return http.NewRequest(method, url, buf)
|
|
|
|
}
|
|
|
|
|
2015-12-29 10:21:11 +01:00
|
|
|
// log will dump request and response to the log file
|
2016-12-01 05:32:21 +01:00
|
|
|
func (c *Client) log(r *http.Request, resp *http.Response) {
|
2016-05-14 07:39:35 +02:00
|
|
|
if c.Log != nil {
|
2017-04-05 05:37:54 +02:00
|
|
|
var (
|
|
|
|
reqDump string
|
|
|
|
respDump []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
if r != nil {
|
|
|
|
reqDump = fmt.Sprintf("%s %s. Data: %s", r.Method, r.URL.String(), r.Form.Encode())
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
respDump, _ = httputil.DumpResponse(resp, true)
|
|
|
|
}
|
2015-11-20 03:51:18 +01:00
|
|
|
|
2017-04-05 05:37:54 +02:00
|
|
|
c.Log.Write([]byte(fmt.Sprintf("Request: %s\nResponse: %s\n", reqDump, string(respDump))))
|
2015-11-20 03:51:18 +01:00
|
|
|
}
|
|
|
|
}
|