2019-10-17 15:42:18 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package consoleapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2019-10-23 18:33:24 +01:00
|
|
|
"io/ioutil"
|
2019-10-17 15:42:18 +01:00
|
|
|
"net/http"
|
2020-03-04 13:23:10 +00:00
|
|
|
"strconv"
|
2019-11-21 14:25:37 +00:00
|
|
|
"time"
|
2019-10-17 15:42:18 +01:00
|
|
|
|
2019-10-23 18:33:24 +01:00
|
|
|
"github.com/gorilla/mux"
|
2019-11-08 20:40:39 +00:00
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
2019-10-23 18:33:24 +01:00
|
|
|
"github.com/zeebo/errs"
|
2019-10-17 15:42:18 +01:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2020-07-10 14:05:17 +01:00
|
|
|
"storj.io/common/uuid"
|
2019-10-17 15:42:18 +01:00
|
|
|
"storj.io/storj/satellite/console"
|
|
|
|
)
|
|
|
|
|
2019-10-23 18:33:24 +01:00
|
|
|
var (
|
|
|
|
// ErrPaymentsAPI - console payments api error type.
|
|
|
|
ErrPaymentsAPI = errs.Class("console payments api error")
|
|
|
|
mon = monkit.Package()
|
|
|
|
)
|
2019-10-17 15:42:18 +01:00
|
|
|
|
2019-11-20 13:46:22 +00:00
|
|
|
// Payments is an api controller that exposes all payment related functionality.
|
2019-10-17 15:42:18 +01:00
|
|
|
type Payments struct {
|
|
|
|
log *zap.Logger
|
|
|
|
service *console.Service
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPayments is a constructor for api payments controller.
|
|
|
|
func NewPayments(log *zap.Logger, service *console.Service) *Payments {
|
|
|
|
return &Payments{
|
|
|
|
log: log,
|
|
|
|
service: service,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetupAccount creates a payment account for the user.
|
|
|
|
func (p *Payments) SetupAccount(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-10-23 18:33:24 +01:00
|
|
|
err = p.service.Payments().SetupAccount(ctx)
|
|
|
|
if err != nil {
|
|
|
|
if console.ErrUnauthorized.Has(err) {
|
|
|
|
p.serveJSONError(w, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.serveJSONError(w, http.StatusInternalServerError, err)
|
2019-10-17 15:42:18 +01:00
|
|
|
return
|
|
|
|
}
|
2019-10-23 18:33:24 +01:00
|
|
|
}
|
2019-10-17 15:42:18 +01:00
|
|
|
|
2019-10-23 18:33:24 +01:00
|
|
|
// AccountBalance returns an integer amount in cents that represents the current balance of payment account.
|
|
|
|
func (p *Payments) AccountBalance(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-10-17 15:42:18 +01:00
|
|
|
|
2019-11-20 13:46:22 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
2019-10-23 18:33:24 +01:00
|
|
|
balance, err := p.service.Payments().AccountBalance(ctx)
|
2019-10-17 15:42:18 +01:00
|
|
|
if err != nil {
|
2019-10-23 18:33:24 +01:00
|
|
|
if console.ErrUnauthorized.Has(err) {
|
|
|
|
p.serveJSONError(w, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-17 15:42:18 +01:00
|
|
|
p.serveJSONError(w, http.StatusInternalServerError, err)
|
2019-10-23 18:33:24 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-12 13:05:35 +00:00
|
|
|
err = json.NewEncoder(w).Encode(&balance)
|
2019-10-23 18:33:24 +01:00
|
|
|
if err != nil {
|
|
|
|
p.log.Error("failed to write json balance response", zap.Error(ErrPaymentsAPI.Wrap(err)))
|
2019-10-17 15:42:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 14:27:44 +00:00
|
|
|
// ProjectsCharges returns how much money current user will be charged for each project which he owns.
|
|
|
|
func (p *Payments) ProjectsCharges(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-11-20 13:46:22 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
2020-03-04 13:23:10 +00:00
|
|
|
sinceStamp, err := strconv.ParseInt(r.URL.Query().Get("from"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
p.serveJSONError(w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
beforeStamp, err := strconv.ParseInt(r.URL.Query().Get("to"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
p.serveJSONError(w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
since := time.Unix(sinceStamp, 0).UTC()
|
|
|
|
before := time.Unix(beforeStamp, 0).UTC()
|
|
|
|
|
|
|
|
charges, err := p.service.Payments().ProjectsCharges(ctx, since, before)
|
2019-11-15 14:27:44 +00:00
|
|
|
if err != nil {
|
|
|
|
if console.ErrUnauthorized.Has(err) {
|
|
|
|
p.serveJSONError(w, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.serveJSONError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(charges)
|
|
|
|
if err != nil {
|
|
|
|
p.log.Error("failed to write json response", zap.Error(ErrPaymentsAPI.Wrap(err)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-23 18:33:24 +01:00
|
|
|
// AddCreditCard is used to save new credit card and attach it to payment account.
|
|
|
|
func (p *Payments) AddCreditCard(w http.ResponseWriter, r *http.Request) {
|
2019-10-17 15:42:18 +01:00
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-10-23 18:33:24 +01:00
|
|
|
bodyBytes, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
p.serveJSONError(w, http.StatusBadRequest, err)
|
2019-10-17 15:42:18 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-23 18:33:24 +01:00
|
|
|
token := string(bodyBytes)
|
2019-10-17 15:42:18 +01:00
|
|
|
|
2019-10-23 18:33:24 +01:00
|
|
|
err = p.service.Payments().AddCreditCard(ctx, token)
|
2019-10-17 15:42:18 +01:00
|
|
|
if err != nil {
|
2019-10-23 18:33:24 +01:00
|
|
|
if console.ErrUnauthorized.Has(err) {
|
|
|
|
p.serveJSONError(w, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-17 15:42:18 +01:00
|
|
|
p.serveJSONError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
2019-10-23 18:33:24 +01:00
|
|
|
}
|
2019-10-17 15:42:18 +01:00
|
|
|
|
2019-10-23 18:33:24 +01:00
|
|
|
// ListCreditCards returns a list of credit cards for a given payment account.
|
|
|
|
func (p *Payments) ListCreditCards(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-11-20 13:46:22 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
2019-10-23 18:33:24 +01:00
|
|
|
cards, err := p.service.Payments().ListCreditCards(ctx)
|
|
|
|
if err != nil {
|
|
|
|
if console.ErrUnauthorized.Has(err) {
|
|
|
|
p.serveJSONError(w, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
2019-10-17 15:42:18 +01:00
|
|
|
|
2019-10-23 18:33:24 +01:00
|
|
|
p.serveJSONError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
2019-10-17 15:42:18 +01:00
|
|
|
|
2019-10-23 18:33:24 +01:00
|
|
|
err = json.NewEncoder(w).Encode(cards)
|
2019-10-17 15:42:18 +01:00
|
|
|
if err != nil {
|
2019-10-23 18:33:24 +01:00
|
|
|
p.log.Error("failed to write json list cards response", zap.Error(ErrPaymentsAPI.Wrap(err)))
|
2019-10-17 15:42:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-23 18:33:24 +01:00
|
|
|
// MakeCreditCardDefault makes a credit card default payment method.
|
|
|
|
func (p *Payments) MakeCreditCardDefault(w http.ResponseWriter, r *http.Request) {
|
2019-10-17 15:42:18 +01:00
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-10-23 18:33:24 +01:00
|
|
|
cardID, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
p.serveJSONError(w, http.StatusBadRequest, err)
|
2019-10-17 15:42:18 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-23 18:33:24 +01:00
|
|
|
err = p.service.Payments().MakeCreditCardDefault(ctx, string(cardID))
|
|
|
|
if err != nil {
|
|
|
|
if console.ErrUnauthorized.Has(err) {
|
|
|
|
p.serveJSONError(w, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
2019-10-17 15:42:18 +01:00
|
|
|
|
2019-10-23 18:33:24 +01:00
|
|
|
p.serveJSONError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
2019-10-17 15:42:18 +01:00
|
|
|
}
|
2019-10-23 18:33:24 +01:00
|
|
|
}
|
2019-10-17 15:42:18 +01:00
|
|
|
|
2019-10-23 18:33:24 +01:00
|
|
|
// RemoveCreditCard is used to detach a credit card from payment account.
|
|
|
|
func (p *Payments) RemoveCreditCard(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-10-17 15:42:18 +01:00
|
|
|
|
2019-10-23 18:33:24 +01:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
cardID := vars["cardId"]
|
|
|
|
|
|
|
|
if cardID == "" {
|
2019-10-17 15:42:18 +01:00
|
|
|
p.serveJSONError(w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-23 18:33:24 +01:00
|
|
|
err = p.service.Payments().RemoveCreditCard(ctx, cardID)
|
2019-10-17 15:42:18 +01:00
|
|
|
if err != nil {
|
2019-10-23 18:33:24 +01:00
|
|
|
if console.ErrUnauthorized.Has(err) {
|
|
|
|
p.serveJSONError(w, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-17 15:42:18 +01:00
|
|
|
p.serveJSONError(w, http.StatusInternalServerError, err)
|
2019-10-23 18:33:24 +01:00
|
|
|
return
|
2019-10-17 15:42:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 16:56:54 +00:00
|
|
|
// BillingHistory returns a list of invoices, transactions and all others billing history items for payment account.
|
|
|
|
func (p *Payments) BillingHistory(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-11-20 13:46:22 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
2019-10-31 16:56:54 +00:00
|
|
|
billingHistory, err := p.service.Payments().BillingHistory(ctx)
|
|
|
|
if err != nil {
|
|
|
|
if console.ErrUnauthorized.Has(err) {
|
|
|
|
p.serveJSONError(w, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.serveJSONError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(billingHistory)
|
|
|
|
if err != nil {
|
|
|
|
p.log.Error("failed to write json billing history response", zap.Error(ErrPaymentsAPI.Wrap(err)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 11:14:34 +00:00
|
|
|
// TokenDeposit creates new deposit transaction and info about address and amount of newly created tx.
|
|
|
|
func (p *Payments) TokenDeposit(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-11-20 13:46:22 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
2019-11-12 11:14:34 +00:00
|
|
|
var requestData struct {
|
2019-11-21 14:25:37 +00:00
|
|
|
Amount int64 `json:"amount"`
|
2019-11-12 11:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = json.NewDecoder(r.Body).Decode(&requestData); err != nil {
|
|
|
|
p.serveJSONError(w, http.StatusBadRequest, err)
|
|
|
|
}
|
|
|
|
|
2019-11-21 14:25:37 +00:00
|
|
|
if requestData.Amount < 0 {
|
|
|
|
p.serveJSONError(w, http.StatusBadRequest, errs.New("amount can not be negative"))
|
|
|
|
}
|
|
|
|
if requestData.Amount == 0 {
|
|
|
|
p.serveJSONError(w, http.StatusBadRequest, errs.New("amount should be greater than zero"))
|
2019-11-12 11:14:34 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 14:25:37 +00:00
|
|
|
tx, err := p.service.Payments().TokenDeposit(ctx, requestData.Amount)
|
2019-11-12 11:14:34 +00:00
|
|
|
if err != nil {
|
|
|
|
if console.ErrUnauthorized.Has(err) {
|
|
|
|
p.serveJSONError(w, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.serveJSONError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var responseData struct {
|
2019-11-21 14:25:37 +00:00
|
|
|
Address string `json:"address"`
|
|
|
|
Amount float64 `json:"amount"`
|
|
|
|
TokenAmount string `json:"tokenAmount"`
|
|
|
|
Rate string `json:"rate"`
|
|
|
|
Status string `json:"status"`
|
2019-12-12 13:09:19 +00:00
|
|
|
Link string `json:"link"`
|
2019-11-21 14:25:37 +00:00
|
|
|
ExpiresAt time.Time `json:"expires"`
|
2019-11-12 11:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
responseData.Address = tx.Address
|
2019-11-21 14:25:37 +00:00
|
|
|
responseData.Amount = float64(requestData.Amount) / 100
|
|
|
|
responseData.TokenAmount = tx.Amount.String()
|
|
|
|
responseData.Rate = tx.Rate.Text('f', 8)
|
|
|
|
responseData.Status = tx.Status.String()
|
2019-12-12 13:09:19 +00:00
|
|
|
responseData.Link = tx.Link
|
2019-11-21 14:25:37 +00:00
|
|
|
responseData.ExpiresAt = tx.CreatedAt.Add(tx.Timeout)
|
2019-11-12 11:14:34 +00:00
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(responseData)
|
|
|
|
if err != nil {
|
|
|
|
p.log.Error("failed to write json token deposit response", zap.Error(ErrPaymentsAPI.Wrap(err)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-10 14:05:17 +01:00
|
|
|
// PaywallEnabled returns is paywall enabled status.
|
|
|
|
func (p *Payments) PaywallEnabled(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
reqID := vars["userId"]
|
|
|
|
|
|
|
|
if reqID == "" {
|
|
|
|
p.serveJSONError(w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
userID, err := uuid.FromString(reqID)
|
|
|
|
if err != nil {
|
|
|
|
p.serveJSONError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
paywallEnabled := p.service.PaywallEnabled(userID)
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(paywallEnabled)
|
|
|
|
if err != nil {
|
|
|
|
p.log.Error("failed to write json paywall enabled response", zap.Error(ErrPaymentsAPI.Wrap(err)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-17 15:42:18 +01:00
|
|
|
// serveJSONError writes JSON error to response output stream.
|
|
|
|
func (p *Payments) serveJSONError(w http.ResponseWriter, status int, err error) {
|
2020-07-22 10:15:21 +01:00
|
|
|
if status == http.StatusInternalServerError {
|
|
|
|
p.log.Error("returning error to client", zap.Int("code", status), zap.Error(err))
|
|
|
|
} else {
|
|
|
|
p.log.Debug("returning error to client", zap.Int("code", status), zap.Error(err))
|
|
|
|
}
|
|
|
|
|
2019-10-17 15:42:18 +01:00
|
|
|
w.WriteHeader(status)
|
|
|
|
|
|
|
|
var response struct {
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
|
|
|
|
|
|
|
response.Error = err.Error()
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(response)
|
|
|
|
if err != nil {
|
2019-10-23 18:33:24 +01:00
|
|
|
p.log.Error("failed to write json error response", zap.Error(ErrPaymentsAPI.Wrap(err)))
|
2019-10-17 15:42:18 +01:00
|
|
|
}
|
|
|
|
}
|