2019-11-26 17:58:51 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package payments
|
|
|
|
|
|
|
|
import (
|
2020-01-29 00:57:15 +00:00
|
|
|
"context"
|
2019-11-26 17:58:51 +00:00
|
|
|
"time"
|
|
|
|
|
2023-01-25 21:38:29 +00:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2019-11-26 17:58:51 +00:00
|
|
|
)
|
|
|
|
|
2023-01-25 21:38:29 +00:00
|
|
|
var (
|
|
|
|
// ErrInvalidCoupon defines invalid coupon code error.
|
|
|
|
ErrInvalidCoupon = errs.Class("invalid coupon code")
|
|
|
|
// ErrCouponConflict occurs when attempting to replace a protected coupon.
|
|
|
|
ErrCouponConflict = errs.Class("coupon conflict")
|
|
|
|
)
|
|
|
|
|
2020-01-29 00:57:15 +00:00
|
|
|
// Coupons exposes all needed functionality to manage coupons.
|
|
|
|
//
|
|
|
|
// architecture: Service
|
|
|
|
type Coupons interface {
|
2021-08-06 21:14:33 +01:00
|
|
|
// GetByUserID returns the coupon applied to the specified user.
|
|
|
|
GetByUserID(ctx context.Context, userID uuid.UUID) (*Coupon, error)
|
2023-01-25 21:38:29 +00:00
|
|
|
// ApplyFreeTierCoupon applies the free tier coupon to the specified user.
|
|
|
|
ApplyFreeTierCoupon(ctx context.Context, userID uuid.UUID) (*Coupon, error)
|
|
|
|
// ApplyCoupon applies coupon to user based on coupon ID.
|
|
|
|
ApplyCoupon(ctx context.Context, userID uuid.UUID, couponID string) (*Coupon, error)
|
2021-06-22 01:09:56 +01:00
|
|
|
// ApplyCouponCode attempts to apply a coupon code to the user.
|
2021-08-06 21:14:33 +01:00
|
|
|
ApplyCouponCode(ctx context.Context, userID uuid.UUID, couponCode string) (*Coupon, error)
|
2020-01-29 00:57:15 +00:00
|
|
|
}
|
|
|
|
|
2021-08-06 21:14:33 +01:00
|
|
|
// Coupon describes a discount to the payment account of a user.
|
2019-11-26 17:58:51 +00:00
|
|
|
type Coupon struct {
|
2021-08-06 21:14:33 +01:00
|
|
|
ID string `json:"id"`
|
|
|
|
PromoCode string `json:"promoCode"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
AmountOff int64 `json:"amountOff"`
|
|
|
|
PercentOff float64 `json:"percentOff"`
|
|
|
|
AddedAt time.Time `json:"addedAt"`
|
|
|
|
ExpiresAt time.Time `json:"expiresAt"`
|
|
|
|
Duration CouponDuration `json:"duration"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// CouponDuration represents how many billing periods a coupon is applied.
|
|
|
|
type CouponDuration string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// CouponOnce indicates that a coupon can only be applied once.
|
|
|
|
CouponOnce CouponDuration = "once"
|
|
|
|
// CouponRepeating indicates that a coupon is applied every billing period for a definite amount of time.
|
|
|
|
CouponRepeating = "repeating"
|
|
|
|
// CouponForever indicates that a coupon is applied every billing period forever.
|
|
|
|
CouponForever = "forever"
|
|
|
|
)
|
2021-10-26 14:30:19 +01:00
|
|
|
|
|
|
|
// CouponType is an enum representing the outcome a coupon validation check.
|
|
|
|
type CouponType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// NoCoupon represents an invalid coupon registration attempt.
|
|
|
|
NoCoupon CouponType = "noCoupon"
|
|
|
|
// FreeTierCoupon represents the default free tier coupon.
|
|
|
|
FreeTierCoupon = "freeTierCoupon"
|
|
|
|
// SignupCoupon represents a valid promo code coupon.
|
|
|
|
SignupCoupon = "signupCoupon"
|
|
|
|
)
|
2023-01-30 22:11:12 +00:00
|
|
|
|
|
|
|
// PackagePlan is an amount to charge a user one time in exchange for a coupon of greater value.
|
|
|
|
// Price is in cents USD.
|
|
|
|
type PackagePlan struct {
|
|
|
|
CouponID string
|
|
|
|
Price int64
|
|
|
|
}
|