forked from go-packages/paypal
code style efficiencies added
This commit is contained in:
parent
17ba889ca0
commit
2c6b470ed7
|
@ -12,11 +12,12 @@ import (
|
||||||
func (c *Client) GetAuthorization(authID string) (*Authorization, error) {
|
func (c *Client) GetAuthorization(authID string) (*Authorization, error) {
|
||||||
buf := bytes.NewBuffer([]byte(""))
|
buf := bytes.NewBuffer([]byte(""))
|
||||||
req, err := http.NewRequest("GET", fmt.Sprintf("%s%s%s", c.APIBase, "/v2/payments/authorization/", authID), buf)
|
req, err := http.NewRequest("GET", fmt.Sprintf("%s%s%s", c.APIBase, "/v2/payments/authorization/", authID), buf)
|
||||||
|
auth := &Authorization{}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &Authorization{}, err
|
return auth, err
|
||||||
}
|
}
|
||||||
|
|
||||||
auth := &Authorization{}
|
|
||||||
err = c.SendWithAuth(req, auth)
|
err = c.SendWithAuth(req, auth)
|
||||||
return auth, err
|
return auth, err
|
||||||
}
|
}
|
||||||
|
@ -29,11 +30,12 @@ func (c *Client) CaptureAuthorization(authID string, a *Amount, isFinalCapture b
|
||||||
|
|
||||||
buf := bytes.NewBuffer([]byte(`{"amount":{"currency":"` + a.Currency + `,"total":"` + a.Total + `"},"is_final_capture":` + isFinalStr + `}`))
|
buf := bytes.NewBuffer([]byte(`{"amount":{"currency":"` + a.Currency + `,"total":"` + a.Total + `"},"is_final_capture":` + isFinalStr + `}`))
|
||||||
req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/authorization/"+authID+"/capture"), buf)
|
req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/authorization/"+authID+"/capture"), buf)
|
||||||
|
capture := &Capture{}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &Capture{}, err
|
return capture, err
|
||||||
}
|
}
|
||||||
|
|
||||||
capture := &Capture{}
|
|
||||||
err = c.SendWithAuth(req, capture)
|
err = c.SendWithAuth(req, capture)
|
||||||
return capture, err
|
return capture, err
|
||||||
}
|
}
|
||||||
|
@ -43,11 +45,12 @@ func (c *Client) CaptureAuthorization(authID string, a *Amount, isFinalCapture b
|
||||||
func (c *Client) VoidAuthorization(authID string) (*Authorization, error) {
|
func (c *Client) VoidAuthorization(authID string) (*Authorization, error) {
|
||||||
buf := bytes.NewBuffer([]byte(""))
|
buf := bytes.NewBuffer([]byte(""))
|
||||||
req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/authorization/"+authID+"/void"), buf)
|
req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/authorization/"+authID+"/void"), buf)
|
||||||
|
auth := &Authorization{}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &Authorization{}, err
|
return auth, err
|
||||||
}
|
}
|
||||||
|
|
||||||
auth := &Authorization{}
|
|
||||||
err = c.SendWithAuth(req, auth)
|
err = c.SendWithAuth(req, auth)
|
||||||
return auth, err
|
return auth, err
|
||||||
}
|
}
|
||||||
|
@ -58,11 +61,12 @@ func (c *Client) VoidAuthorization(authID string) (*Authorization, error) {
|
||||||
func (c *Client) ReauthorizeAuthorization(authID string, a *Amount) (*Authorization, error) {
|
func (c *Client) ReauthorizeAuthorization(authID string, a *Amount) (*Authorization, error) {
|
||||||
buf := bytes.NewBuffer([]byte(`{"amount":{"currency":"` + a.Currency + `","total":"` + a.Total + `"}}`))
|
buf := bytes.NewBuffer([]byte(`{"amount":{"currency":"` + a.Currency + `","total":"` + a.Total + `"}}`))
|
||||||
req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/authorization/"+authID+"/reauthorize"), buf)
|
req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/authorization/"+authID+"/reauthorize"), buf)
|
||||||
|
auth := &Authorization{}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &Authorization{}, err
|
return auth, err
|
||||||
}
|
}
|
||||||
|
|
||||||
auth := &Authorization{}
|
|
||||||
err = c.SendWithAuth(req, auth)
|
err = c.SendWithAuth(req, auth)
|
||||||
return auth, err
|
return auth, err
|
||||||
}
|
}
|
||||||
|
|
16
billing.go
16
billing.go
|
@ -94,24 +94,24 @@ func (c *Client) CreateBillingAgreement(a BillingAgreement) (*CreateAgreementRes
|
||||||
// Endpoint: POST /v2/payments/billing-agreements/token/agreement-execute
|
// Endpoint: POST /v2/payments/billing-agreements/token/agreement-execute
|
||||||
func (c *Client) ExecuteApprovedAgreement(token string) (*ExecuteAgreementResponse, error) {
|
func (c *Client) ExecuteApprovedAgreement(token string) (*ExecuteAgreementResponse, error) {
|
||||||
req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/billing-agreements/"+token+"/agreement-execute"), nil)
|
req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/billing-agreements/"+token+"/agreement-execute"), nil)
|
||||||
|
response := &ExecuteAgreementResponse{}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &ExecuteAgreementResponse{}, err
|
return response, err
|
||||||
}
|
}
|
||||||
|
|
||||||
req.SetBasicAuth(c.ClientID, c.Secret)
|
req.SetBasicAuth(c.ClientID, c.Secret)
|
||||||
req.Header.Set("Authorization", "Bearer "+c.Token.Token)
|
req.Header.Set("Authorization", "Bearer "+c.Token.Token)
|
||||||
|
|
||||||
e := ExecuteAgreementResponse{}
|
if err = c.SendWithAuth(req, response); err != nil {
|
||||||
|
return response, err
|
||||||
if err = c.SendWithAuth(req, &e); err != nil {
|
|
||||||
return &e, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if e.ID == "" {
|
if response.ID == "" {
|
||||||
return &e, errors.New("Unable to execute agreement with token=" + token)
|
return response, errors.New("Unable to execute agreement with token=" + token)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &e, err
|
return response, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListBillingPlans lists billing-plans
|
// ListBillingPlans lists billing-plans
|
||||||
|
|
16
client.go
16
client.go
|
@ -39,16 +39,16 @@ func (c *Client) GetAccessToken() (*TokenResponse, error) {
|
||||||
|
|
||||||
req.Header.Set("Content-type", "application/x-www-form-urlencoded")
|
req.Header.Set("Content-type", "application/x-www-form-urlencoded")
|
||||||
|
|
||||||
t := TokenResponse{}
|
response := &TokenResponse{}
|
||||||
err = c.SendWithBasicAuth(req, &t)
|
err = c.SendWithBasicAuth(req, response)
|
||||||
|
|
||||||
// Set Token fur current Client
|
// Set Token fur current Client
|
||||||
if t.Token != "" {
|
if response.Token != "" {
|
||||||
c.Token = &t
|
c.Token = response
|
||||||
c.tokenExpiresAt = time.Now().Add(time.Duration(t.ExpiresIn) * time.Second)
|
c.tokenExpiresAt = time.Now().Add(time.Duration(response.ExpiresIn) * time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &t, err
|
return response, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetHTTPClient sets *http.Client to current client
|
// SetHTTPClient sets *http.Client to current client
|
||||||
|
@ -107,7 +107,6 @@ func (c *Client) Send(req *http.Request, v interface{}) error {
|
||||||
|
|
||||||
return errResp
|
return errResp
|
||||||
}
|
}
|
||||||
|
|
||||||
if v == nil {
|
if v == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -130,7 +129,7 @@ func (c *Client) SendWithAuth(req *http.Request, v interface{}) error {
|
||||||
// to happen outside of the locked section.
|
// to happen outside of the locked section.
|
||||||
|
|
||||||
if c.Token != nil {
|
if c.Token != nil {
|
||||||
if !c.tokenExpiresAt.IsZero() && c.tokenExpiresAt.Sub(time.Now()) < 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 {
|
||||||
c.Unlock()
|
c.Unlock()
|
||||||
|
@ -159,7 +158,6 @@ func (c *Client) SendWithBasicAuth(req *http.Request, v interface{}) error {
|
||||||
func (c *Client) NewRequest(method, url string, payload interface{}) (*http.Request, error) {
|
func (c *Client) NewRequest(method, url string, payload interface{}) (*http.Request, error) {
|
||||||
var buf io.Reader
|
var buf io.Reader
|
||||||
if payload != nil {
|
if payload != nil {
|
||||||
var b []byte
|
|
||||||
b, err := json.Marshal(&payload)
|
b, err := json.Marshal(&payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -7,16 +7,13 @@ import (
|
||||||
|
|
||||||
const format = "2006-01-02T15:04:05Z"
|
const format = "2006-01-02T15:04:05Z"
|
||||||
|
|
||||||
type stringable interface {
|
|
||||||
String() string
|
|
||||||
}
|
|
||||||
|
|
||||||
type Filter struct {
|
type Filter struct {
|
||||||
fields []stringable
|
fields []fmt.Stringer
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Filter) String() string {
|
func (s *Filter) String() string {
|
||||||
filter := ""
|
var filter string
|
||||||
for i, f := range s.fields {
|
for i, f := range s.fields {
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
filter = "?" + f.String()
|
filter = "?" + f.String()
|
||||||
|
|
12
identity.go
12
identity.go
|
@ -9,7 +9,7 @@ import (
|
||||||
|
|
||||||
// GrantNewAccessTokenFromAuthCode - Use this call to grant a new access token, using the previously obtained authorization code.
|
// GrantNewAccessTokenFromAuthCode - Use this call to grant a new access token, using the previously obtained authorization code.
|
||||||
// Endpoint: POST /v1/identity/openidconnect/tokenservice
|
// Endpoint: POST /v1/identity/openidconnect/tokenservice
|
||||||
func (c *Client) GrantNewAccessTokenFromAuthCode(code string, redirectURI string) (*TokenResponse, error) {
|
func (c *Client) GrantNewAccessTokenFromAuthCode(code, redirectURI string) (*TokenResponse, error) {
|
||||||
token := &TokenResponse{}
|
token := &TokenResponse{}
|
||||||
|
|
||||||
q := url.Values{}
|
q := url.Values{}
|
||||||
|
@ -57,16 +57,16 @@ func (c *Client) GrantNewAccessTokenFromRefreshToken(refreshToken string) (*Toke
|
||||||
// Endpoint: GET /v1/identity/openidconnect/userinfo/?schema=<Schema>
|
// Endpoint: GET /v1/identity/openidconnect/userinfo/?schema=<Schema>
|
||||||
// Pass the schema that is used to return as per openidconnect protocol. The only supported schema value is openid.
|
// Pass the schema that is used to return as per openidconnect protocol. The only supported schema value is openid.
|
||||||
func (c *Client) GetUserInfo(schema string) (*UserInfo, error) {
|
func (c *Client) GetUserInfo(schema string) (*UserInfo, error) {
|
||||||
u := UserInfo{}
|
u := &UserInfo{}
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", fmt.Sprintf("%s%s%s", c.APIBase, "/v1/identity/openidconnect/userinfo/?schema=", schema), nil)
|
req, err := http.NewRequest("GET", fmt.Sprintf("%s%s%s", c.APIBase, "/v1/identity/openidconnect/userinfo/?schema=", schema), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &u, err
|
return u, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = c.SendWithAuth(req, &u); err != nil {
|
if err = c.SendWithAuth(req, u); err != nil {
|
||||||
return &u, err
|
return u, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &u, nil
|
return u, nil
|
||||||
}
|
}
|
||||||
|
|
23
payout.go
23
payout.go
|
@ -9,12 +9,12 @@ import (
|
||||||
// Endpoint: POST /v1/payments/payouts
|
// Endpoint: POST /v1/payments/payouts
|
||||||
func (c *Client) CreateSinglePayout(p Payout) (*PayoutResponse, error) {
|
func (c *Client) CreateSinglePayout(p Payout) (*PayoutResponse, error) {
|
||||||
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/payouts"), p)
|
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/payouts"), p)
|
||||||
if err != nil {
|
|
||||||
return &PayoutResponse{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
response := &PayoutResponse{}
|
response := &PayoutResponse{}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return response, err
|
||||||
|
}
|
||||||
|
|
||||||
if err = c.SendWithAuth(req, response); err != nil {
|
if err = c.SendWithAuth(req, response); err != nil {
|
||||||
return response, err
|
return response, err
|
||||||
}
|
}
|
||||||
|
@ -27,13 +27,12 @@ func (c *Client) CreateSinglePayout(p Payout) (*PayoutResponse, error) {
|
||||||
// Endpoint: GET /v1/payments/payouts/ID
|
// Endpoint: GET /v1/payments/payouts/ID
|
||||||
func (c *Client) GetPayout(payoutBatchID string) (*PayoutResponse, error) {
|
func (c *Client) GetPayout(payoutBatchID string) (*PayoutResponse, error) {
|
||||||
req, err := c.NewRequest("GET", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/payouts/"+payoutBatchID), nil)
|
req, err := c.NewRequest("GET", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/payouts/"+payoutBatchID), nil)
|
||||||
|
response := &PayoutResponse{}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &PayoutResponse{}, err
|
return response, err
|
||||||
}
|
}
|
||||||
|
|
||||||
response := &PayoutResponse{}
|
|
||||||
|
|
||||||
if err = c.SendWithAuth(req, response); err != nil {
|
if err = c.SendWithAuth(req, response); err != nil {
|
||||||
return response, err
|
return response, err
|
||||||
}
|
}
|
||||||
|
@ -46,13 +45,12 @@ func (c *Client) GetPayout(payoutBatchID string) (*PayoutResponse, error) {
|
||||||
// Endpoint: GET /v1/payments/payouts-item/ID
|
// Endpoint: GET /v1/payments/payouts-item/ID
|
||||||
func (c *Client) GetPayoutItem(payoutItemID string) (*PayoutItemResponse, error) {
|
func (c *Client) GetPayoutItem(payoutItemID string) (*PayoutItemResponse, error) {
|
||||||
req, err := c.NewRequest("GET", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/payouts-item/"+payoutItemID), nil)
|
req, err := c.NewRequest("GET", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/payouts-item/"+payoutItemID), nil)
|
||||||
|
response := &PayoutItemResponse{}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &PayoutItemResponse{}, err
|
return response, err
|
||||||
}
|
}
|
||||||
|
|
||||||
response := &PayoutItemResponse{}
|
|
||||||
|
|
||||||
if err = c.SendWithAuth(req, response); err != nil {
|
if err = c.SendWithAuth(req, response); err != nil {
|
||||||
return response, err
|
return response, err
|
||||||
}
|
}
|
||||||
|
@ -65,13 +63,12 @@ func (c *Client) GetPayoutItem(payoutItemID string) (*PayoutItemResponse, error)
|
||||||
// Endpoint: POST /v1/payments/payouts-item/ID/cancel
|
// Endpoint: POST /v1/payments/payouts-item/ID/cancel
|
||||||
func (c *Client) CancelPayoutItem(payoutItemID string) (*PayoutItemResponse, error) {
|
func (c *Client) CancelPayoutItem(payoutItemID string) (*PayoutItemResponse, error) {
|
||||||
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/payouts-item/"+payoutItemID+"/cancel"), nil)
|
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/payouts-item/"+payoutItemID+"/cancel"), nil)
|
||||||
|
response := &PayoutItemResponse{}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &PayoutItemResponse{}, err
|
return response, err
|
||||||
}
|
}
|
||||||
|
|
||||||
response := &PayoutItemResponse{}
|
|
||||||
|
|
||||||
if err = c.SendWithAuth(req, response); err != nil {
|
if err = c.SendWithAuth(req, response); err != nil {
|
||||||
return response, err
|
return response, err
|
||||||
}
|
}
|
||||||
|
|
24
vault.go
24
vault.go
|
@ -12,13 +12,13 @@ func (c *Client) StoreCreditCard(cc CreditCard) (*CreditCard, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
response := CreditCard{}
|
response := &CreditCard{}
|
||||||
|
|
||||||
if err = c.SendWithAuth(req, &response); err != nil {
|
if err = c.SendWithAuth(req, response); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &response, nil
|
return response, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteCreditCard func
|
// DeleteCreditCard func
|
||||||
|
@ -44,13 +44,13 @@ func (c *Client) GetCreditCard(id string) (*CreditCard, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
response := CreditCard{}
|
response := &CreditCard{}
|
||||||
|
|
||||||
if err = c.SendWithAuth(req, &response); err != nil {
|
if err = c.SendWithAuth(req, response); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &response, nil
|
return response, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetCreditCards func
|
// GetCreditCards func
|
||||||
|
@ -70,13 +70,13 @@ func (c *Client) GetCreditCards(ccf *CreditCardsFilter) (*CreditCards, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
response := CreditCards{}
|
response := &CreditCards{}
|
||||||
|
|
||||||
if err = c.SendWithAuth(req, &response); err != nil {
|
if err = c.SendWithAuth(req, response); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &response, nil
|
return response, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// PatchCreditCard func
|
// PatchCreditCard func
|
||||||
|
@ -87,11 +87,11 @@ func (c *Client) PatchCreditCard(id string, ccf []CreditCardField) (*CreditCard,
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
response := CreditCard{}
|
response := &CreditCard{}
|
||||||
|
|
||||||
if err = c.SendWithAuth(req, &response); err != nil {
|
if err = c.SendWithAuth(req, response); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &response, nil
|
return response, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,11 +13,12 @@ import (
|
||||||
func (c *Client) CreateWebProfile(wp WebProfile) (*WebProfile, error) {
|
func (c *Client) CreateWebProfile(wp WebProfile) (*WebProfile, error) {
|
||||||
url := fmt.Sprintf("%s%s", c.APIBase, "/v1/payment-experience/web-profiles")
|
url := fmt.Sprintf("%s%s", c.APIBase, "/v1/payment-experience/web-profiles")
|
||||||
req, err := c.NewRequest("POST", url, wp)
|
req, err := c.NewRequest("POST", url, wp)
|
||||||
|
response := &WebProfile{}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &WebProfile{}, err
|
return response, err
|
||||||
}
|
}
|
||||||
|
|
||||||
response := &WebProfile{}
|
|
||||||
|
|
||||||
if err = c.SendWithAuth(req, response); err != nil {
|
if err = c.SendWithAuth(req, response); err != nil {
|
||||||
return response, err
|
return response, err
|
||||||
|
|
Loading…
Reference in New Issue
Block a user