Skip to content

Commit

Permalink
paypal 在到期结束前的一半时间内,自动刷新token (#365)
Browse files Browse the repository at this point in the history
* paypal 在到期结束前的一半时间内,自动刷新token
  • Loading branch information
Au authored Nov 16, 2023
1 parent 868337e commit 0bbb3a4
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion paypal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package paypal

import (
"context"
"github.com/go-pay/gopay/pkg/xlog"
"time"

"github.com/go-pay/gopay"
"github.com/go-pay/gopay/pkg/util"
Expand Down Expand Up @@ -34,10 +36,26 @@ func NewClient(clientid, secret string, isProd bool) (client *Client, err error)
DebugSwitch: gopay.DebugOff,
hc: xhttp.NewClient(),
}
_, err = client.GetAccessToken()
token, err := client.GetAccessToken()
if err != nil {
return nil, err
}
// 在到期结束前的一半时间内,自动刷新token
go func(token *AccessToken) {
ticker := time.NewTicker(time.Duration(token.ExpiresIn/2) * time.Second)
for {

Check failure on line 46 in paypal/client.go

View workflow job for this annotation

GitHub Actions / lint

S1000: should use for range instead of for { select {} } (gosimple)
select {
case <-ticker.C:
tokenNew, err := client.GetAccessToken()
if err != nil {
xlog.Errorf("PayPal GetAccessToken Error: %s", err.Error())
continue
}
client.AccessToken = tokenNew.AccessToken
}
}
}(token)

return client, nil
}

Expand Down

0 comments on commit 0bbb3a4

Please sign in to comment.