IIdentity endpoints

This commit is contained in:
Aliaksandr Pliutau 2016-01-18 15:42:42 +07:00
parent f87e7b6bb0
commit 2a864f6dac
6 changed files with 110 additions and 15 deletions

View File

@ -189,6 +189,15 @@ capture, err := c.CaptureOrder(orderID, &paypalsdk.Amount{Total: "7.00", Currenc
order, err := c.VoidOrder(orderID)
```
#### Identity
```go
// Retreive tolen by authorization code
token, err := c.GrantNewAccessTokenFromAuthCode("<Authorization-Code>", "http://example.com/myapp/return.php")
// ... or by refresh token
token, err := c.GrantNewAccessTokenFromRefreshToken("<Refresh-Token>")
```
#### How to Contribute
* Fork a repository

View File

@ -26,6 +26,6 @@ func TestNewClient(t *testing.T) {
_, err = NewClient(testClientID, testSecret, APIBaseSandBox)
if err != nil {
t.Errorf("NewClient() must not return error for valid creds")
t.Errorf("NewClient() must not return error for valid creds: " + err.Error())
}
}

View File

@ -8,24 +8,24 @@ import (
)
func main() {
client, err := paypalsdk.NewClient("AZgwu4yt5Ba0gyTu1dGBH3txHCJbMuFNvrmQxBaQbfDncDiCs6W_rwJD8Ir-0pZrN-_eq7n9zVd8Y-5f", "EBzA1wRl5t73OMugOieDj_tI3vihfJmGl47ukQT-cpctooIzDu0K7IPESNC0cKodlLSOXzwI8qXSM0rd", paypalsdk.APIBaseSandBox)
c, err := paypalsdk.NewClient("AZgwu4yt5Ba0gyTu1dGBH3txHCJbMuFNvrmQxBaQbfDncDiCs6W_rwJD8Ir-0pZrN-_eq7n9zVd8Y-5f", "EBzA1wRl5t73OMugOieDj_tI3vihfJmGl47ukQT-cpctooIzDu0K7IPESNC0cKodlLSOXzwI8qXSM0rd", paypalsdk.APIBaseSandBox)
if err == nil {
fmt.Println("DEBUG: ClientID=" + client.ClientID + " APIBase=" + client.APIBase)
fmt.Println("DEBUG: ClientID=" + c.ClientID + " APIBase=" + c.APIBase)
} else {
fmt.Println("ERROR: " + err.Error())
}
token, err := client.GetAccessToken()
token, err := c.GetAccessToken()
if err == nil {
fmt.Println("DEBUG: AccessToken=" + token.Token)
} else {
fmt.Println("ERROR: " + err.Error())
}
payment, err := client.GetPayment("PAY-TEST-123")
payment, err := c.GetPayment("PAY-TEST-123")
fmt.Println("DEBUG: PaymentID=" + payment.ID)
payments, err := client.GetPayments()
payments, err := c.GetPayments()
if err == nil {
fmt.Println("DEBUG: PaymentsCount=" + strconv.Itoa(len(payments)))
} else {
@ -60,7 +60,7 @@ func main() {
CancelURL: "http://...",
},
}
paymentResponse, err := client.CreatePayment(p)
paymentResponse, err := c.CreatePayment(p)
if err == nil {
fmt.Println("DEBUG: CreatedPaymentID=" + paymentResponse.Payment.ID)
} else {
@ -68,7 +68,7 @@ func main() {
}
fmt.Println("OK")
sale, err := client.GetSale("1")
sale, err := c.GetSale("1")
if err == nil {
fmt.Println("DEBUG: SaleID=" + sale.ID)
} else {
@ -76,7 +76,7 @@ func main() {
}
fmt.Println("OK")
refund, err := client.RefundSale("1", &paypalsdk.Amount{Total: "7.00", Currency: "USD"})
refund, err := c.RefundSale("1", &paypalsdk.Amount{Total: "7.00", Currency: "USD"})
if err == nil {
fmt.Println("DEBUG: RefundID=" + refund.ID)
} else {
@ -84,7 +84,7 @@ func main() {
}
fmt.Println("OK")
refund, err = client.GetRefund("1")
refund, err = c.GetRefund("1")
if err == nil {
fmt.Println("DEBUG: RefundID=" + refund.ID)
} else {
@ -92,7 +92,7 @@ func main() {
}
fmt.Println("OK")
order, err := client.GetOrder("1")
order, err := c.GetOrder("1")
if err == nil {
fmt.Println("DEBUG: OrderID=" + order.ID)
} else {
@ -100,7 +100,7 @@ func main() {
}
fmt.Println("OK")
auth, err := client.AuthorizeOrder("1", &paypalsdk.Amount{Total: "7.00", Currency: "USD"})
auth, err := c.AuthorizeOrder("1", &paypalsdk.Amount{Total: "7.00", Currency: "USD"})
if err == nil {
fmt.Println("DEBUG: AuthID=" + auth.ID)
} else {
@ -108,7 +108,7 @@ func main() {
}
fmt.Println("OK")
capture, err := client.CaptureOrder("1", &paypalsdk.Amount{Total: "7.00", Currency: "USD"}, true, nil)
capture, err := c.CaptureOrder("1", &paypalsdk.Amount{Total: "7.00", Currency: "USD"}, true, nil)
if err == nil {
fmt.Println("DEBUG: CaptureID=" + capture.ID)
} else {
@ -116,11 +116,25 @@ func main() {
}
fmt.Println("OK")
order, err = client.VoidOrder("1")
order, err = c.VoidOrder("1")
if err == nil {
fmt.Println("DEBUG: OrderID=" + order.ID)
} else {
fmt.Println("ERROR: " + err.Error())
}
fmt.Println("OK")
token1, err := c.GrantNewAccessTokenFromAuthCode("123", "http://example.com/myapp/return.php")
if err == nil {
fmt.Println("DEBUG: Token=" + token1.Token)
} else {
fmt.Println("ERROR: " + err.Error())
}
token2, err := c.GrantNewAccessTokenFromRefreshToken("123")
if err == nil {
fmt.Println("DEBUG: Token=" + token2.Token)
} else {
fmt.Println("ERROR: " + err.Error())
}
}

50
identity.go Normal file
View File

@ -0,0 +1,50 @@
package paypalsdk
import "fmt"
// GrantNewAccessTokenFromAuthCode - Use this call to grant a new access token, using the previously obtained authorization code.
// Endpoint: POST /v1/identity/openidconnect/tokenservice
func (c *Client) GrantNewAccessTokenFromAuthCode(code string, redirectURI string) (*TokenResponse, error) {
type request struct {
GrantType string `json:"grant_type"`
Code string `json:"code"`
RedirectURI string `json:"redirect_uri"`
}
token := &TokenResponse{}
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/identity/openidconnect/tokenservice"), request{GrantType: "authorization_code", Code: code, RedirectURI: redirectURI})
if err != nil {
return token, err
}
err = c.Send(req, token)
if err != nil {
return token, err
}
return token, nil
}
// GrantNewAccessTokenFromRefreshToken - Use this call to grant a new access token, using a refresh token.
// Endpoint: POST /v1/identity/openidconnect/tokenservice
func (c *Client) GrantNewAccessTokenFromRefreshToken(refreshToken string) (*TokenResponse, error) {
type request struct {
GrantType string `json:"grant_type"`
RefreshToken string `json:"refresh_token"`
}
token := &TokenResponse{}
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/identity/openidconnect/tokenservice"), request{GrantType: "refresh_token", RefreshToken: refreshToken})
if err != nil {
return token, err
}
err = c.Send(req, token)
if err != nil {
return token, err
}
return token, nil
}

21
identity_test.go Normal file
View File

@ -0,0 +1,21 @@
package paypalsdk
import "testing"
func TestGrantNewAccessTokenFromAuthCode(t *testing.T) {
c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
token, _ := c.GrantNewAccessTokenFromAuthCode("123", "http://example.com/myapp/return.php")
if token.Token != "" {
t.Errorf("Empty token must be returned for invalid code")
}
}
func TestGrantNewAccessTokenFromRefreshToken(t *testing.T) {
c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
token, _ := c.GrantNewAccessTokenFromRefreshToken("123")
if token.Token != "" {
t.Errorf("Empty token must be returned for invalid refresh token")
}
}

View File

@ -2,6 +2,7 @@ package paypalsdk
import (
"fmt"
"strconv"
"testing"
)
@ -41,7 +42,7 @@ func TestGetPayments(t *testing.T) {
payments, _ := c.GetPayments()
if len(payments) != 5 {
t.Errorf("5 payments must be returned for GetPayments")
t.Errorf("5 payments must be returned for GetPayments. Returned: " + strconv.Itoa(len(payments)))
}
}