Skip to content

Commit

Permalink
Fix #271
Browse files Browse the repository at this point in the history
  • Loading branch information
plutov committed Jul 10, 2024
1 parent 3713084 commit 5052fd4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 30 deletions.
28 changes: 2 additions & 26 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package paypal

import (
"context"
"errors"
"fmt"
"math/rand"
"net/http"
Expand Down Expand Up @@ -46,38 +45,15 @@ func createRandomProduct(t *testing.T) Product {
// test the Lock and Unlock methods of the private mutex field
// of Client structure.
func (c *Client) sendWithAuth(req *http.Request, v interface{}) error {
// c.Lock()
c.mu.Lock()
// Note: Here we do not want to `defer c.Unlock()` because we need `c.Send(...)`
// to happen outside of the locked section.

if c.mu.TryLock() {
// if the code is able to acquire a lock
// despite the mutex of c being locked, throw an error
err := errors.New("TryLock succeeded inside sendWithAuth with mutex locked")
return err
}
defer c.mu.Unlock()

if c.Token == nil || (!c.tokenExpiresAt.IsZero() && time.Until(c.tokenExpiresAt) < RequestNewTokenBeforeExpiresIn) {
// c.Token will be updated in GetAccessToken call
if _, err := c.GetAccessToken(req.Context()); err != nil {
// c.Unlock()
c.mu.Unlock()
return err
}
}
req.Header.Set("Authorization", "Bearer "+c.Token.Token)
// Unlock the client mutex before sending the request, this allows multiple requests
// to be in progress at the same time.
// c.Unlock()
c.mu.Unlock()

if !c.mu.TryLock() {
// if the code is unable to acquire a lock
// despite the mutex of c being unlocked, throw an error
err := errors.New("TryLock failed inside sendWithAuth with mutex unlocked")
return err
}
c.mu.Unlock() // undo changes from the previous TryLock

return c.Send(req, v)
}
Expand Down
7 changes: 3 additions & 4 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,14 +483,13 @@ type (
Links []Link `json:"links,omitempty"`
}

// AuthorizeOrderResponse .
AuthorizeOrderResponse struct {
CreateTime *time.Time `json:"create_time,omitempty"`
UpdateTime *time.Time `json:"update_time,omitempty"`
ID string `json:"id,omitempty"`
Status string `json:"status,omitempty"`
Intent string `json:"intent,omitempty"`
PurchaseUnits []PurchaseUnitRequest `json:"purchase_units,omitempty"`
PurchaseUnits []PurchaseUnit `json:"purchase_units,omitempty"`
Payer *PayerWithNameAndPhone `json:"payer,omitempty"`
}

Expand Down Expand Up @@ -826,7 +825,6 @@ type (
Value string `json:"value"`
}

// PurchaseUnit struct
PurchaseUnit struct {
ReferenceID string `json:"reference_id"`
Amount *PurchaseUnitAmount `json:"amount,omitempty"`
Expand Down Expand Up @@ -944,7 +942,8 @@ type (

// CapturedPayments has the amounts for a captured order
CapturedPayments struct {
Captures []CaptureAmount `json:"captures,omitempty"`
Autthorizations []Authorization `json:"authorizations,omitempty"`
Captures []CaptureAmount `json:"captures,omitempty"`
}

// CapturedPurchaseItem are items for a captured order
Expand Down

0 comments on commit 5052fd4

Please sign in to comment.