mirror of
https://github.com/plutov/paypal.git
synced 2025-01-23 18:31:03 +01:00
Fix access token refresh
This commit is contained in:
parent
f9536247c0
commit
d06b0102ce
|
@ -9,6 +9,7 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httputil"
|
"net/http/httputil"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewClient returns new Client struct
|
// NewClient returns new Client struct
|
||||||
|
@ -45,6 +46,7 @@ func (c *Client) GetAccessToken() (*TokenResponse, error) {
|
||||||
// Set Token fur current Client
|
// Set Token fur current Client
|
||||||
if t.Token != "" {
|
if t.Token != "" {
|
||||||
c.Token = &t
|
c.Token = &t
|
||||||
|
c.tokenExpiresAt = time.Now().Add(time.Duration(t.ExpiresIn) * time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &t, err
|
return &t, err
|
||||||
|
@ -55,6 +57,7 @@ func (c *Client) SetAccessToken(token string) error {
|
||||||
c.Token = &TokenResponse{
|
c.Token = &TokenResponse{
|
||||||
Token: token,
|
Token: token,
|
||||||
}
|
}
|
||||||
|
c.tokenExpiresAt = time.Unix(0, 0)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -124,7 +127,7 @@ func (c *Client) Send(req *http.Request, v interface{}) error {
|
||||||
// client.Token will be updated when changed
|
// client.Token will be updated when changed
|
||||||
func (c *Client) SendWithAuth(req *http.Request, v interface{}) error {
|
func (c *Client) SendWithAuth(req *http.Request, v interface{}) error {
|
||||||
if c.Token != nil {
|
if c.Token != nil {
|
||||||
if c.Token.ExpiresIn < RequestNewTokenBeforeExpiresIn {
|
if !c.tokenExpiresAt.IsZero() && time.Until(c.tokenExpiresAt) < RequestNewTokenBeforeExpiresIn {
|
||||||
// c.Token will be updated in GetAccessToken call
|
// c.Token will be updated in GetAccessToken call
|
||||||
if _, err := c.GetAccessToken(); err != nil {
|
if _, err := c.GetAccessToken(); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
3
types.go
3
types.go
|
@ -15,7 +15,7 @@ const (
|
||||||
APIBaseLive = "https://api.paypal.com"
|
APIBaseLive = "https://api.paypal.com"
|
||||||
|
|
||||||
// RequestNewTokenBeforeExpiresIn is used by SendWithAuth and try to get new Token when it's about to expire
|
// RequestNewTokenBeforeExpiresIn is used by SendWithAuth and try to get new Token when it's about to expire
|
||||||
RequestNewTokenBeforeExpiresIn = 60
|
RequestNewTokenBeforeExpiresIn = time.Duration(60) * time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
// Possible values for `no_shipping` in InputFields
|
// Possible values for `no_shipping` in InputFields
|
||||||
|
@ -113,6 +113,7 @@ type (
|
||||||
APIBase string
|
APIBase string
|
||||||
Log io.Writer // If user set log file name all requests will be logged there
|
Log io.Writer // If user set log file name all requests will be logged there
|
||||||
Token *TokenResponse
|
Token *TokenResponse
|
||||||
|
tokenExpiresAt time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreditCard struct
|
// CreditCard struct
|
||||||
|
|
Loading…
Reference in New Issue
Block a user