wallet.go137 lines, 4.0 KB
| 1 | package gateway |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "time" |
| 7 | |
| 8 | walletv1 "github.com/fiston-user/payments/gen/wallet/v1" |
| 9 | "github.com/fiston-user/payments/internal/platform/httpx" |
| 10 | "github.com/fiston-user/payments/internal/platform/idempotency" |
| 11 | "github.com/fiston-user/payments/internal/wallet/walletclient" |
| 12 | "google.golang.org/grpc" |
| 13 | "google.golang.org/grpc/metadata" |
| 14 | ) |
| 15 | |
| 16 | // The public JSON, as the wallet's REST API has always sent it. |
| 17 | type ( |
| 18 | money struct { |
| 19 | Amount int64 `json:"amount"` |
| 20 | Currency string `json:"currency"` |
| 21 | } |
| 22 | walletJSON struct { |
| 23 | ID string `json:"id"` |
| 24 | OwnerID string `json:"owner_id"` |
| 25 | Currency string `json:"currency"` |
| 26 | CreatedAt time.Time `json:"created_at"` |
| 27 | } |
| 28 | balanceJSON struct { |
| 29 | WalletID string `json:"wallet_id"` |
| 30 | Balance money `json:"balance"` |
| 31 | } |
| 32 | depositJSON struct { |
| 33 | TransactionID string `json:"transaction_id"` |
| 34 | WalletID string `json:"wallet_id"` |
| 35 | Amount money `json:"amount"` |
| 36 | CreatedAt time.Time `json:"created_at"` |
| 37 | } |
| 38 | ) |
| 39 | |
| 40 | func (m money) pb() *walletv1.Money { |
| 41 | return &walletv1.Money{AmountMinor: m.Amount, Currency: m.Currency} |
| 42 | } |
| 43 | |
| 44 | func toMoney(m *walletv1.Money) money { |
| 45 | return money{Amount: m.GetAmountMinor(), Currency: m.GetCurrency()} |
| 46 | } |
| 47 | |
| 48 | func toWallet(w *walletv1.Wallet) walletJSON { |
| 49 | return walletJSON{ID: w.GetId(), OwnerID: w.GetOwnerId(), Currency: w.GetCurrency(), |
| 50 | CreatedAt: w.GetCreateTime().AsTime()} |
| 51 | } |
| 52 | |
| 53 | // walletAPI serves the wallet's routes over gRPC. It only translates. |
| 54 | type walletAPI struct { |
| 55 | client *walletclient.Client |
| 56 | } |
| 57 | |
| 58 | // rpc sends a handler's errors through fromRPC. |
| 59 | func rpc(fn httpx.HandlerFunc) httpx.HandlerFunc { |
| 60 | return func(w http.ResponseWriter, r *http.Request) error { |
| 61 | return fromRPC(fn(w, r)) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // asCaller sends the user's own token along, for the service to check again. |
| 66 | func asCaller(r *http.Request) context.Context { |
| 67 | return metadata.AppendToOutgoingContext(r.Context(), "authorization", r.Header.Get("Authorization")) |
| 68 | } |
| 69 | |
| 70 | func (a *walletAPI) create(w http.ResponseWriter, r *http.Request) error { |
| 71 | var req struct { |
| 72 | Currency string `json:"currency"` |
| 73 | } |
| 74 | if err := httpx.DecodeJSON(w, r, &req); err != nil { |
| 75 | return err |
| 76 | } |
| 77 | resp, err := a.client.CreateWallet(asCaller(r), &walletv1.CreateWalletRequest{Currency: req.Currency}) |
| 78 | if err != nil { |
| 79 | return err |
| 80 | } |
| 81 | wallet := toWallet(resp.GetWallet()) |
| 82 | w.Header().Set("Location", "/v1/wallets/"+wallet.ID) |
| 83 | httpx.WriteJSON(w, r, http.StatusCreated, wallet) |
| 84 | return nil |
| 85 | } |
| 86 | |
| 87 | func (a *walletAPI) get(w http.ResponseWriter, r *http.Request) error { |
| 88 | resp, err := a.client.GetWallet(asCaller(r), &walletv1.GetWalletRequest{WalletId: r.PathValue("id")}) |
| 89 | if err != nil { |
| 90 | return err |
| 91 | } |
| 92 | httpx.WriteJSON(w, r, http.StatusOK, toWallet(resp.GetWallet())) |
| 93 | return nil |
| 94 | } |
| 95 | |
| 96 | func (a *walletAPI) balance(w http.ResponseWriter, r *http.Request) error { |
| 97 | resp, err := a.client.GetBalance(asCaller(r), &walletv1.GetBalanceRequest{WalletId: r.PathValue("id")}) |
| 98 | if err != nil { |
| 99 | return err |
| 100 | } |
| 101 | httpx.WriteJSON(w, r, http.StatusOK, balanceJSON{ |
| 102 | WalletID: resp.GetWalletId(), |
| 103 | Balance: toMoney(resp.GetBalance()), |
| 104 | }) |
| 105 | return nil |
| 106 | } |
| 107 | |
| 108 | func (a *walletAPI) deposit(w http.ResponseWriter, r *http.Request) error { |
| 109 | var amount money |
| 110 | if err := httpx.DecodeJSON(w, r, &amount); err != nil { |
| 111 | return err |
| 112 | } |
| 113 | var header metadata.MD |
| 114 | resp, err := a.client.Deposit(asCaller(r), &walletv1.DepositRequest{ |
| 115 | IdempotencyKey: r.Header.Get(idempotency.KeyHeader), |
| 116 | WalletId: r.PathValue("id"), |
| 117 | Amount: amount.pb(), |
| 118 | }, grpc.Header(&header)) |
| 119 | if err != nil { |
| 120 | return err |
| 121 | } |
| 122 | replayed(w, header) |
| 123 | httpx.WriteJSON(w, r, http.StatusCreated, depositJSON{ |
| 124 | TransactionID: resp.GetTransactionId(), |
| 125 | WalletID: resp.GetWalletId(), |
| 126 | Amount: toMoney(resp.GetAmount()), |
| 127 | CreatedAt: resp.GetCreateTime().AsTime(), |
| 128 | }) |
| 129 | return nil |
| 130 | } |
| 131 | |
| 132 | // replayed copies the wallet's replay flag into Idempotent-Replayed. |
| 133 | func replayed(w http.ResponseWriter, header metadata.MD) { |
| 134 | if v := header.Get("idempotent-replayed"); len(v) > 0 { |
| 135 | w.Header().Set(idempotency.ReplayedHeader, v[0]) |
| 136 | } |
| 137 | } |