mirror of
https://github.com/plutov/paypal.git
synced 2025-01-23 10:21:03 +01:00
Fixed #15
This commit is contained in:
parent
959619c327
commit
f65c5dc88e
|
@ -19,12 +19,12 @@ func TestGetAuthorization(t *testing.T) {
|
||||||
|
|
||||||
a, err := c.GetAuthorization(testAuthID)
|
a, err := c.GetAuthorization(testAuthID)
|
||||||
if err != nil || a.ID != testAuthID {
|
if err != nil || a.ID != testAuthID {
|
||||||
t.Errorf("GetAuthorization failed for ID=" + testAuthID)
|
t.Errorf("GetAuthorization failed for ID=%s", testAuthID)
|
||||||
}
|
}
|
||||||
|
|
||||||
a, err = c.GetAuthorization(testFakeAuthID)
|
a, err = c.GetAuthorization(testFakeAuthID)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("GetAuthorization must return error for ID=" + testFakeAuthID)
|
t.Errorf("GetAuthorization must return error for ID=%s", testFakeAuthID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
21
client.go
21
client.go
|
@ -47,6 +47,12 @@ func (c *Client) SetAccessToken(token string) error {
|
||||||
// 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 {
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
resp *http.Response
|
||||||
|
data []byte
|
||||||
|
)
|
||||||
|
|
||||||
// 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")
|
||||||
|
@ -56,7 +62,7 @@ func (c *Client) Send(req *http.Request, v interface{}) error {
|
||||||
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)
|
||||||
c.log(req, resp)
|
c.log(req, resp)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -66,7 +72,7 @@ func (c *Client) Send(req *http.Request, v interface{}) error {
|
||||||
|
|
||||||
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)
|
||||||
|
@ -90,10 +96,19 @@ func (c *Client) Send(req *http.Request, v interface{}) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendWithAuth makes a request to the API and apply OAuth2 header automatically.
|
// SendWithAuth makes a request to the API and apply OAuth2 header automatically.
|
||||||
// If the access token soon to be expired, it will try to get a new one before
|
// If the access token soon to be expired or already expired, it will try to get a new one before
|
||||||
// making the main request
|
// making the main request
|
||||||
|
// 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 {
|
||||||
|
// c.Token willbe updated in GetAccessToken call
|
||||||
|
_, err := c.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
req.Header.Set("Authorization", "Bearer "+c.Token.Token)
|
req.Header.Set("Authorization", "Bearer "+c.Token.Token)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,6 +27,6 @@ func TestNewClient(t *testing.T) {
|
||||||
|
|
||||||
_, err = NewClient(testClientID, testSecret, APIBaseSandBox)
|
_, err = NewClient(testClientID, testSecret, APIBaseSandBox)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("NewClient() must not return error for valid creds: " + err.Error())
|
t.Errorf("NewClient() must not return error for valid creds: %s", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,6 @@ func TestGetUserInfo(t *testing.T) {
|
||||||
|
|
||||||
u, err := c.GetUserInfo("openid")
|
u, err := c.GetUserInfo("openid")
|
||||||
if u.ID != testUserID || err != nil {
|
if u.ID != testUserID || err != nil {
|
||||||
t.Errorf("GetUserInfo must return valid test ID=" + testUserID)
|
t.Errorf("GetUserInfo must return valid test ID=%s", testUserID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,12 +11,12 @@ func TestGetOrder(t *testing.T) {
|
||||||
|
|
||||||
o, err := c.GetOrder(testOrderID)
|
o, err := c.GetOrder(testOrderID)
|
||||||
if err != nil || o.ID != testOrderID {
|
if err != nil || o.ID != testOrderID {
|
||||||
t.Errorf("GetOrder failed for ID=" + testOrderID)
|
t.Errorf("GetOrder failed for ID=%s", testOrderID)
|
||||||
}
|
}
|
||||||
|
|
||||||
o, err = c.GetOrder(testFakeOrderID)
|
o, err = c.GetOrder(testFakeOrderID)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("GetOrder must return error for ID=" + testFakeOrderID)
|
t.Errorf("GetOrder must return error for ID=%s", testFakeOrderID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,6 @@ package paypalsdk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -42,7 +41,7 @@ func TestGetPayments(t *testing.T) {
|
||||||
payments, _ := c.GetPayments()
|
payments, _ := c.GetPayments()
|
||||||
|
|
||||||
if len(payments) != 2 {
|
if len(payments) != 2 {
|
||||||
t.Errorf("2 payments must be returned for GetPayments. Returned: " + strconv.Itoa(len(payments)))
|
t.Errorf("2 payments must be returned for GetPayments. Returned: %d", len(payments))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ func TestGetSale(t *testing.T) {
|
||||||
|
|
||||||
_, err := c.GetSale(testSaleID)
|
_, err := c.GetSale(testSaleID)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("404 must be returned for ID=" + testSaleID)
|
t.Errorf("404 must be returned for ID=%s", testSaleID)
|
||||||
} else {
|
} else {
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
}
|
}
|
||||||
|
@ -23,14 +23,14 @@ func TestRefundSale(t *testing.T) {
|
||||||
|
|
||||||
_, err := c.RefundSale(testSaleID, nil)
|
_, err := c.RefundSale(testSaleID, nil)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("404 must be returned for ID=" + testSaleID)
|
t.Errorf("404 must be returned for ID=%s", testSaleID)
|
||||||
} else {
|
} else {
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = c.RefundSale(testSaleID, &Amount{Total: "7.00", Currency: "USD"})
|
_, err = c.RefundSale(testSaleID, &Amount{Total: "7.00", Currency: "USD"})
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("404 must be returned for ID=" + testSaleID)
|
t.Errorf("404 must be returned for ID=%s", testSaleID)
|
||||||
} else {
|
} else {
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
}
|
}
|
||||||
|
@ -42,7 +42,7 @@ func TestGetRefund(t *testing.T) {
|
||||||
|
|
||||||
_, err := c.GetRefund("1")
|
_, err := c.GetRefund("1")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("404 must be returned for ID=" + testSaleID)
|
t.Errorf("404 must be returned for ID=%s", testSaleID)
|
||||||
} else {
|
} else {
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
}
|
}
|
||||||
|
|
3
types.go
3
types.go
|
@ -13,6 +13,9 @@ const (
|
||||||
|
|
||||||
// APIBaseLive points to the live version of the API
|
// APIBaseLive points to the live version of the API
|
||||||
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 = 60
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
|
Loading…
Reference in New Issue
Block a user