2019-10-17 15:42:18 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package consoleapi
|
|
|
|
|
|
|
|
import (
|
2022-12-21 20:27:29 +00:00
|
|
|
"context"
|
2019-10-17 15:42:18 +01:00
|
|
|
"encoding/json"
|
2023-01-26 18:31:13 +00:00
|
|
|
"fmt"
|
2021-06-22 01:09:56 +01:00
|
|
|
"io"
|
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"
|
|
|
|
|
2022-11-21 18:58:42 +00:00
|
|
|
"storj.io/storj/private/web"
|
2019-10-17 15:42:18 +01:00
|
|
|
"storj.io/storj/satellite/console"
|
2023-01-25 21:38:29 +00:00
|
|
|
"storj.io/storj/satellite/payments"
|
2023-01-20 21:40:23 +00:00
|
|
|
"storj.io/storj/satellite/payments/billing"
|
2023-01-26 18:31:13 +00:00
|
|
|
"storj.io/storj/satellite/payments/paymentsconfig"
|
2019-10-17 15:42:18 +01:00
|
|
|
)
|
|
|
|
|
2019-10-23 18:33:24 +01:00
|
|
|
var (
|
|
|
|
// ErrPaymentsAPI - console payments api error type.
|
2021-04-28 09:06:17 +01:00
|
|
|
ErrPaymentsAPI = errs.Class("consoleapi payments")
|
2019-10-23 18:33:24 +01:00
|
|
|
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 {
|
2022-12-21 20:27:29 +00:00
|
|
|
log *zap.Logger
|
|
|
|
service *console.Service
|
|
|
|
accountFreezeService *console.AccountFreezeService
|
2023-01-26 18:31:13 +00:00
|
|
|
packagePlans paymentsconfig.PackagePlans
|
2019-10-17 15:42:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPayments is a constructor for api payments controller.
|
2023-01-26 18:31:13 +00:00
|
|
|
func NewPayments(log *zap.Logger, service *console.Service, accountFreezeService *console.AccountFreezeService, packagePlans paymentsconfig.PackagePlans) *Payments {
|
2019-10-17 15:42:18 +01:00
|
|
|
return &Payments{
|
2022-12-21 20:27:29 +00:00
|
|
|
log: log,
|
|
|
|
service: service,
|
|
|
|
accountFreezeService: accountFreezeService,
|
2023-01-26 18:31:13 +00:00
|
|
|
packagePlans: packagePlans,
|
2019-10-17 15:42:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
2021-10-26 14:30:19 +01:00
|
|
|
couponType, err := p.service.Payments().SetupAccount(ctx)
|
|
|
|
|
2019-10-23 18:33:24 +01:00
|
|
|
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
|
|
|
|
}
|
2021-10-26 14:30:19 +01:00
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(couponType)
|
|
|
|
if err != nil {
|
|
|
|
p.log.Error("failed to write json token deposit response", zap.Error(ErrPaymentsAPI.Wrap(err)))
|
|
|
|
}
|
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)
|
|
|
|
|
2023-03-21 06:48:11 +00:00
|
|
|
var response struct {
|
|
|
|
PriceModels map[string]payments.ProjectUsagePriceModel `json:"priceModels"`
|
|
|
|
Charges payments.ProjectChargesResponse `json:"charges"`
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-03-21 06:48:11 +00:00
|
|
|
response.Charges = charges
|
|
|
|
response.PriceModels = make(map[string]payments.ProjectUsagePriceModel)
|
|
|
|
|
|
|
|
seen := make(map[string]struct{})
|
|
|
|
for _, partnerCharges := range charges {
|
|
|
|
for partner := range partnerCharges {
|
|
|
|
if _, ok := seen[partner]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
response.PriceModels[partner] = *p.service.Payments().GetProjectUsagePriceModel(partner)
|
|
|
|
seen[partner] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(response)
|
2019-11-15 14:27:44 +00:00
|
|
|
if err != nil {
|
2023-03-21 06:48:11 +00:00
|
|
|
p.log.Error("failed to write json project usage and charges response", zap.Error(ErrPaymentsAPI.Wrap(err)))
|
2019-11-15 14:27:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-23 12:04:32 +00:00
|
|
|
// triggerAttemptPaymentIfFrozenOrWarned checks if the account is frozen and if frozen, will trigger attempt to pay outstanding invoices.
|
|
|
|
func (p *Payments) triggerAttemptPaymentIfFrozenOrWarned(ctx context.Context) (err error) {
|
2022-12-21 20:27:29 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
userID, err := p.service.GetUserID(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-23 12:04:32 +00:00
|
|
|
freeze, warning, err := p.accountFreezeService.GetAll(ctx, userID)
|
2022-12-21 20:27:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-23 12:04:32 +00:00
|
|
|
if freeze != nil || warning != nil {
|
2022-12-21 20:27:29 +00:00
|
|
|
err = p.service.Payments().AttemptPayOverdueInvoices(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-23 12:04:32 +00:00
|
|
|
}
|
|
|
|
if freeze != nil {
|
2022-12-21 20:27:29 +00:00
|
|
|
err = p.accountFreezeService.UnfreezeUser(ctx, userID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-23 12:04:32 +00:00
|
|
|
} else if warning != nil {
|
|
|
|
err = p.accountFreezeService.UnWarnUser(ctx, userID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-21 20:27:29 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
2022-10-11 12:39:08 +01:00
|
|
|
bodyBytes, err := io.ReadAll(r.Body)
|
2019-10-23 18:33:24 +01:00
|
|
|
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
|
|
|
|
2023-02-06 20:21:33 +00: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
|
|
|
|
}
|
2022-12-21 20:27:29 +00:00
|
|
|
|
2023-03-23 12:04:32 +00:00
|
|
|
err = p.triggerAttemptPaymentIfFrozenOrWarned(ctx)
|
2022-12-21 20:27:29 +00:00
|
|
|
if err != nil {
|
|
|
|
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
|
|
|
|
2021-08-27 01:51:26 +01:00
|
|
|
if cards == nil {
|
|
|
|
_, err = w.Write([]byte("[]"))
|
|
|
|
} else {
|
|
|
|
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)
|
|
|
|
|
2022-10-11 12:39:08 +01:00
|
|
|
cardID, err := io.ReadAll(r.Body)
|
2019-10-23 18:33:24 +01:00
|
|
|
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
|
|
|
}
|
2022-12-21 20:27:29 +00:00
|
|
|
|
2023-03-23 12:04:32 +00:00
|
|
|
err = p.triggerAttemptPaymentIfFrozenOrWarned(ctx)
|
2022-12-21 20:27:29 +00:00
|
|
|
if err != nil {
|
|
|
|
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
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-08-27 01:51:26 +01:00
|
|
|
if billingHistory == nil {
|
|
|
|
_, err = w.Write([]byte("[]"))
|
|
|
|
} else {
|
|
|
|
err = json.NewEncoder(w).Encode(billingHistory)
|
|
|
|
}
|
|
|
|
|
2019-10-31 16:56:54 +00:00
|
|
|
if err != nil {
|
|
|
|
p.log.Error("failed to write json billing history response", zap.Error(ErrPaymentsAPI.Wrap(err)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-22 01:09:56 +01:00
|
|
|
// ApplyCouponCode applies a coupon code to the user's account.
|
|
|
|
func (p *Payments) ApplyCouponCode(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
// limit the size of the body to prevent excessive memory usage
|
2022-10-11 12:39:08 +01:00
|
|
|
bodyBytes, err := io.ReadAll(io.LimitReader(r.Body, 1*1024*1024))
|
2021-06-22 01:09:56 +01:00
|
|
|
if err != nil {
|
|
|
|
p.serveJSONError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
couponCode := string(bodyBytes)
|
|
|
|
|
2021-08-06 21:14:33 +01:00
|
|
|
coupon, err := p.service.Payments().ApplyCouponCode(ctx, couponCode)
|
2021-06-22 01:09:56 +01:00
|
|
|
if err != nil {
|
2023-01-30 22:11:12 +00:00
|
|
|
status := http.StatusInternalServerError
|
2023-01-25 21:38:29 +00:00
|
|
|
if payments.ErrInvalidCoupon.Has(err) {
|
2023-01-30 22:11:12 +00:00
|
|
|
status = http.StatusBadRequest
|
2023-01-25 21:38:29 +00:00
|
|
|
} else if payments.ErrCouponConflict.Has(err) {
|
2023-01-30 22:11:12 +00:00
|
|
|
status = http.StatusConflict
|
2022-10-31 19:32:04 +00:00
|
|
|
}
|
2023-01-30 22:11:12 +00:00
|
|
|
p.serveJSONError(w, status, err)
|
2021-06-22 01:09:56 +01:00
|
|
|
return
|
|
|
|
}
|
2021-08-06 21:14:33 +01:00
|
|
|
|
|
|
|
if err = json.NewEncoder(w).Encode(coupon); err != nil {
|
|
|
|
p.log.Error("failed to encode coupon", zap.Error(ErrPaymentsAPI.Wrap(err)))
|
|
|
|
}
|
2021-06-22 01:09:56 +01:00
|
|
|
}
|
|
|
|
|
2021-08-06 21:14:33 +01:00
|
|
|
// GetCoupon returns the coupon applied to the user's account.
|
|
|
|
func (p *Payments) GetCoupon(w http.ResponseWriter, r *http.Request) {
|
2021-07-08 20:06:07 +01:00
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
2021-08-06 21:14:33 +01:00
|
|
|
coupon, err := p.service.Payments().GetCoupon(ctx)
|
2021-07-08 20:06:07 +01:00
|
|
|
if err != nil {
|
|
|
|
if console.ErrUnauthorized.Has(err) {
|
|
|
|
p.serveJSONError(w, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.serveJSONError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-06 21:14:33 +01:00
|
|
|
if err = json.NewEncoder(w).Encode(coupon); err != nil {
|
|
|
|
p.log.Error("failed to encode coupon", zap.Error(ErrPaymentsAPI.Wrap(err)))
|
2021-07-08 20:06:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-11 09:02:58 +01:00
|
|
|
// GetWallet returns the wallet address (with balance) already assigned to the user.
|
|
|
|
func (p *Payments) GetWallet(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
walletInfo, err := p.service.Payments().GetWallet(ctx)
|
|
|
|
if err != nil {
|
|
|
|
if console.ErrUnauthorized.Has(err) {
|
|
|
|
p.serveJSONError(w, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
2023-01-20 21:40:23 +00:00
|
|
|
if errs.Is(err, billing.ErrNoWallet) {
|
|
|
|
p.serveJSONError(w, http.StatusNotFound, err)
|
|
|
|
return
|
|
|
|
}
|
2022-05-11 09:02:58 +01:00
|
|
|
|
|
|
|
p.serveJSONError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = json.NewEncoder(w).Encode(walletInfo); err != nil {
|
|
|
|
p.log.Error("failed to encode wallet info", zap.Error(ErrPaymentsAPI.Wrap(err)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClaimWallet will claim a new wallet address. Returns with existing if it's already claimed.
|
|
|
|
func (p *Payments) ClaimWallet(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
walletInfo, err := p.service.Payments().ClaimWallet(ctx)
|
|
|
|
if err != nil {
|
|
|
|
if console.ErrUnauthorized.Has(err) {
|
|
|
|
p.serveJSONError(w, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.serveJSONError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = json.NewEncoder(w).Encode(walletInfo); err != nil {
|
|
|
|
p.log.Error("failed to encode wallet info", zap.Error(ErrPaymentsAPI.Wrap(err)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-16 14:26:27 +01:00
|
|
|
// WalletPayments returns with the list of storjscan transactions for user`s wallet.
|
|
|
|
func (p *Payments) WalletPayments(w http.ResponseWriter, r *http.Request) {
|
2022-05-11 09:02:58 +01:00
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
2022-06-16 14:26:27 +01:00
|
|
|
walletPayments, err := p.service.Payments().WalletPayments(ctx)
|
2022-05-11 09:02:58 +01:00
|
|
|
if err != nil {
|
|
|
|
if console.ErrUnauthorized.Has(err) {
|
|
|
|
p.serveJSONError(w, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.serveJSONError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-16 14:26:27 +01:00
|
|
|
if err = json.NewEncoder(w).Encode(walletPayments); err != nil {
|
|
|
|
p.log.Error("failed to encode payments", zap.Error(ErrPaymentsAPI.Wrap(err)))
|
2022-05-11 09:02:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-12 03:50:31 +00:00
|
|
|
// GetProjectUsagePriceModel returns the project usage price model for the user.
|
|
|
|
func (p *Payments) GetProjectUsagePriceModel(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
2023-03-21 06:48:11 +00:00
|
|
|
user, err := console.GetUser(ctx)
|
2023-01-12 03:50:31 +00:00
|
|
|
if err != nil {
|
|
|
|
p.serveJSONError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-21 06:48:11 +00:00
|
|
|
pricing := p.service.Payments().GetProjectUsagePriceModel(string(user.UserAgent))
|
|
|
|
|
2023-01-12 03:50:31 +00:00
|
|
|
if err = json.NewEncoder(w).Encode(pricing); err != nil {
|
|
|
|
p.log.Error("failed to encode project usage price model", zap.Error(ErrPaymentsAPI.Wrap(err)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-26 18:31:13 +00:00
|
|
|
// PurchasePackage purchases one of the configured paymentsconfig.PackagePlans.
|
|
|
|
func (p *Payments) PurchasePackage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
bodyBytes, err := io.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
p.serveJSONError(w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
token := string(bodyBytes)
|
|
|
|
|
|
|
|
u, err := console.GetUser(ctx)
|
|
|
|
if err != nil {
|
|
|
|
p.serveJSONError(w, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pkg, err := p.packagePlans.Get(u.UserAgent)
|
|
|
|
if err != nil {
|
|
|
|
p.serveJSONError(w, http.StatusNotFound, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
card, err := p.service.Payments().AddCreditCard(ctx, token)
|
|
|
|
if err != nil {
|
|
|
|
switch {
|
|
|
|
case console.ErrUnauthorized.Has(err):
|
|
|
|
p.serveJSONError(w, http.StatusUnauthorized, err)
|
|
|
|
default:
|
|
|
|
p.serveJSONError(w, http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-22 15:28:52 +00:00
|
|
|
description := fmt.Sprintf("%s package plan", string(u.UserAgent))
|
|
|
|
err = p.service.Payments().UpdatePackage(ctx, description, time.Now())
|
2023-01-26 18:31:13 +00:00
|
|
|
if err != nil {
|
2023-03-22 15:28:52 +00:00
|
|
|
if !console.ErrAlreadyHasPackage.Has(err) {
|
|
|
|
p.serveJSONError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = p.service.Payments().Purchase(ctx, pkg.Price, description, card.ID)
|
|
|
|
if err != nil {
|
|
|
|
p.serveJSONError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = p.service.Payments().ApplyCredit(ctx, pkg.Credit, description); err != nil {
|
2023-01-26 18:31:13 +00:00
|
|
|
p.serveJSONError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-02 15:49:45 +00:00
|
|
|
// PackageAvailable returns whether a package plan is configured for the user's partner.
|
|
|
|
func (p *Payments) PackageAvailable(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
u, err := console.GetUser(ctx)
|
|
|
|
if err != nil {
|
|
|
|
p.serveJSONError(w, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pkg, err := p.packagePlans.Get(u.UserAgent)
|
|
|
|
hasPkg := err == nil && pkg != payments.PackagePlan{}
|
|
|
|
|
|
|
|
if err = json.NewEncoder(w).Encode(hasPkg); err != nil {
|
|
|
|
p.log.Error("failed to encode package plan checking 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) {
|
2022-11-21 18:58:42 +00:00
|
|
|
web.ServeJSONError(p.log, w, status, err)
|
2019-10-17 15:42:18 +01:00
|
|
|
}
|