Skip to content

Commit

Permalink
1. add fetch_exchange_rates log; 2. return payment_url for processing…
Browse files Browse the repository at this point in the history
… order.
  • Loading branch information
zensh committed Sep 17, 2023
1 parent b7134ce commit 16c253e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ go 1.21
require (
github.com/BurntSushi/toml v1.3.2
github.com/fxamacker/cbor/v2 v2.5.0
github.com/go-playground/validator/v10 v10.15.3
github.com/go-playground/validator/v10 v10.15.4
github.com/google/uuid v1.3.1
github.com/joho/godotenv v1.5.1
github.com/klauspost/compress v1.16.7
github.com/redis/go-redis/v9 v9.1.0
github.com/rs/xid v1.5.0
github.com/stretchr/testify v1.8.4
github.com/stripe/stripe-go/v75 v75.4.0
github.com/stripe/stripe-go/v75 v75.6.0
github.com/teambition/gear v1.27.3
go.uber.org/dig v1.17.0
)
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.15.3 h1:S+sSpunYjNPDuXkWbK+x+bA7iXiW296KG4dL3X7xUZo=
github.com/go-playground/validator/v10 v10.15.3/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/go-playground/validator/v10 v10.15.4 h1:zMXza4EpOdooxPel5xDqXEdXG5r+WggpvnAKMsalBjs=
github.com/go-playground/validator/v10 v10.15.4/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
Expand All @@ -54,6 +56,8 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stripe/stripe-go/v75 v75.4.0 h1:7AaKc3Dh3Zg1UMpvflZSc9zYEUW0vx3nfiFjyBuf1fg=
github.com/stripe/stripe-go/v75 v75.4.0/go.mod h1:wT44gah+eCY8Z0aSpY/vQlYYbicU9uUAbAqdaUxxDqE=
github.com/stripe/stripe-go/v75 v75.6.0 h1:KCqcqEFQbGLc76aveGJkpZIghITO5HIlBbKBlvBhhps=
github.com/stripe/stripe-go/v75 v75.6.0/go.mod h1:wT44gah+eCY8Z0aSpY/vQlYYbicU9uUAbAqdaUxxDqE=
github.com/teambition/gear v1.27.3 h1:iWUOJYdBwxU+SZP5aZ2ZYR5FnRGmdgrMbbSpOCZo0go=
github.com/teambition/gear v1.27.3/go.mod h1:d3Nmr6rRPnH5lYSK33W9IDhsaxp/8n14vRrUZu9dP9c=
github.com/teambition/trie-mux v1.5.2 h1:ALTagFwKZXkn1vfSRlODlmoZg+NMeWAm4dyBPQI6a8w=
Expand Down
5 changes: 5 additions & 0 deletions src/api/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ func (a *Checkout) ListCharges(ctx *gear.Context) error {
}

for i := range output {
if output[i].Status == 1 && output[i].ChargeID != nil {
if cs, err := session.Get(*(output[i].ChargeID), nil); err == nil {
output[i].PaymentURL = util.Ptr(cs.URL)
}
}
output[i].ChargeID = nil
output[i].ChargePayload = nil
}
Expand Down
27 changes: 22 additions & 5 deletions src/bll/external_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/teambition/gear"
"github.com/yiwen-ai/wallet-api/src/logging"
"github.com/yiwen-ai/wallet-api/src/service"
"github.com/yiwen-ai/wallet-api/src/util"
)
Expand All @@ -33,7 +34,7 @@ func (b *ExternalAPI) ExchangeRate(ctx context.Context) (*ExchangeRatesOutput, e
_ = b.redis.GetCBOR(ctx, "exchange_rates", &v)
}

r := util.Int63n(7200)
r := util.Int63n(3500) + 100
if v != nil && time.Now().Unix()-r < int64(v.LastUpdate) {
return v, nil
}
Expand All @@ -48,7 +49,15 @@ func (b *ExternalAPI) ExchangeRate(ctx context.Context) (*ExchangeRatesOutput, e
return b.exchangeRate(ctx)
}

func (b *ExternalAPI) exchangeRate(ctx context.Context) (*ExchangeRatesOutput, error) {
func (b *ExternalAPI) exchangeRate(ctx context.Context) (rate *ExchangeRatesOutput, err error) {
defer func() {
if err != nil {
logging.Logger.Err(logging.Log{
"action": "fetch_exchange_rates",
"error": err.Error(),
})
}
}()

// https://www.exchangerate-api.com/docs
h := http.Header{}
Expand All @@ -66,16 +75,17 @@ func (b *ExternalAPI) exchangeRate(ctx context.Context) (*ExchangeRatesOutput, e

output := &exchangeRateOutput{}
api := "https://v6.exchangerate-api.com/v6/245ef0a5e7b4a1799b2d9a64/latest/HKD"
err := util.RequestJSON(ctx, util.ExternalHTTPClient, http.MethodGet, api, nil, output)
err = util.RequestJSON(ctx, util.ExternalHTTPClient, http.MethodGet, api, nil, output)
if err != nil {
return nil, err
}

if output.Result != "success" {
return nil, errors.New("fetch exchange rate failed")
err = errors.New("fetch exchange rate failed")
return nil, err
}

rate := &ExchangeRatesOutput{
rate = &ExchangeRatesOutput{
LastUpdate: output.LastUpdate,
NextUpdate: output.NextUpdate,
Base: output.Base,
Expand All @@ -87,5 +97,12 @@ func (b *ExternalAPI) exchangeRate(ctx context.Context) (*ExchangeRatesOutput, e
return nil, err
}

logging.Logger.Info(logging.Log{
"action": "fetch_exchange_rates",
"base": rate.Base,
"lastUpdate": time.Unix(int64(rate.LastUpdate), 0),
"nextUpdate": time.Unix(int64(rate.NextUpdate), 0),
})

return rate, nil
}
1 change: 1 addition & 0 deletions src/bll/walletbase.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ type ChargeOutput struct {
TxnRefunded *util.ID `json:"txn_refunded,omitempty" cbor:"txn_refunded,omitempty"`
FailureCode *string `json:"failure_code,omitempty" cbor:"failure_code,omitempty"`
FailureMsg *string `json:"failure_msg,omitempty" cbor:"failure_msg,omitempty"`
PaymentURL *string `json:"payment_url" cbor:"payment_url"`
}

func (b *Walletbase) GetCharge(ctx context.Context, uid, id util.ID, fields *string) (*ChargeOutput, error) {
Expand Down

0 comments on commit 16c253e

Please sign in to comment.