2019-11-26 17:58:51 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2023-04-06 12:41:14 +01:00
|
|
|
package stripe
|
2019-11-26 17:58:51 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2021-06-22 01:09:56 +01:00
|
|
|
"github.com/stripe/stripe-go/v72"
|
2019-11-26 17:58:51 +00:00
|
|
|
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2019-11-26 17:58:51 +00:00
|
|
|
"storj.io/storj/satellite/payments"
|
|
|
|
)
|
|
|
|
|
2020-01-29 00:57:15 +00:00
|
|
|
// ensures that coupons implements payments.Coupons.
|
|
|
|
var _ payments.Coupons = (*coupons)(nil)
|
|
|
|
|
|
|
|
// coupons is an implementation of payments.Coupons.
|
|
|
|
//
|
|
|
|
// architecture: Service
|
|
|
|
type coupons struct {
|
|
|
|
service *Service
|
|
|
|
}
|
|
|
|
|
2023-01-25 21:38:29 +00:00
|
|
|
// ApplyFreeTierCoupon applies the default free tier coupon to the account.
|
|
|
|
func (coupons *coupons) ApplyFreeTierCoupon(ctx context.Context, userID uuid.UUID) (_ *payments.Coupon, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
customerID, err := coupons.service.db.Customers().GetCustomerID(ctx, userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
customer, err := coupons.service.stripeClient.Customers().Update(customerID, &stripe.CustomerParams{
|
2023-03-14 02:59:24 +00:00
|
|
|
Params: stripe.Params{Context: ctx},
|
2023-01-25 21:38:29 +00:00
|
|
|
Coupon: stripe.String(coupons.service.StripeFreeTierCouponID),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2023-03-22 15:29:57 +00:00
|
|
|
return stripeDiscountToPaymentsCoupon(customer.Discount)
|
2023-01-25 21:38:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyCoupon applies the coupon to account if it exists.
|
|
|
|
func (coupons *coupons) ApplyCoupon(ctx context.Context, userID uuid.UUID, couponID string) (_ *payments.Coupon, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
customerID, err := coupons.service.db.Customers().GetCustomerID(ctx, userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2023-03-14 02:59:24 +00:00
|
|
|
customer, err := coupons.service.stripeClient.Customers().Update(customerID, &stripe.CustomerParams{
|
|
|
|
Params: stripe.Params{Context: ctx},
|
|
|
|
Coupon: stripe.String(couponID),
|
|
|
|
})
|
2023-01-25 21:38:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
2023-03-22 15:29:57 +00:00
|
|
|
return stripeDiscountToPaymentsCoupon(customer.Discount)
|
2023-01-25 21:38:29 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 01:09:56 +01:00
|
|
|
// ApplyCouponCode attempts to apply a coupon code to the user via Stripe.
|
2021-08-06 21:14:33 +01:00
|
|
|
func (coupons *coupons) ApplyCouponCode(ctx context.Context, userID uuid.UUID, couponCode string) (_ *payments.Coupon, err error) {
|
2021-06-22 01:09:56 +01:00
|
|
|
defer mon.Task()(&ctx, userID, couponCode)(&err)
|
|
|
|
|
|
|
|
promoCodeIter := coupons.service.stripeClient.PromoCodes().List(&stripe.PromotionCodeListParams{
|
2023-03-14 02:59:24 +00:00
|
|
|
ListParams: stripe.ListParams{Context: ctx},
|
|
|
|
Code: stripe.String(couponCode),
|
2021-06-22 01:09:56 +01:00
|
|
|
})
|
|
|
|
if !promoCodeIter.Next() {
|
2023-01-25 21:38:29 +00:00
|
|
|
return nil, payments.ErrInvalidCoupon.New("Invalid coupon code")
|
2021-06-22 01:09:56 +01:00
|
|
|
}
|
|
|
|
promoCode := promoCodeIter.PromotionCode()
|
|
|
|
|
|
|
|
customerID, err := coupons.service.db.Customers().GetCustomerID(ctx, userID)
|
|
|
|
if err != nil {
|
2021-08-06 21:14:33 +01:00
|
|
|
return nil, Error.Wrap(err)
|
2021-06-22 01:09:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
params := &stripe.CustomerParams{
|
2023-03-14 02:59:24 +00:00
|
|
|
Params: stripe.Params{Context: ctx},
|
2021-06-22 01:09:56 +01:00
|
|
|
PromotionCode: stripe.String(promoCode.ID),
|
|
|
|
}
|
2021-08-06 21:14:33 +01:00
|
|
|
params.AddExpand("discount.promotion_code")
|
2021-06-22 01:09:56 +01:00
|
|
|
|
2021-08-06 21:14:33 +01:00
|
|
|
customer, err := coupons.service.stripeClient.Customers().Update(customerID, params)
|
2021-06-22 01:09:56 +01:00
|
|
|
if err != nil {
|
2021-08-06 21:14:33 +01:00
|
|
|
return nil, Error.Wrap(err)
|
2021-06-22 01:09:56 +01:00
|
|
|
}
|
|
|
|
|
2021-08-06 21:14:33 +01:00
|
|
|
if customer.Discount == nil || customer.Discount.Coupon == nil {
|
|
|
|
return nil, Error.New("invalid discount after coupon code application; user ID:%s, customer ID:%s", userID, customerID)
|
|
|
|
}
|
|
|
|
|
2023-03-22 15:29:57 +00:00
|
|
|
return stripeDiscountToPaymentsCoupon(customer.Discount)
|
2021-06-22 01:09:56 +01:00
|
|
|
}
|
2021-07-08 20:06:07 +01:00
|
|
|
|
2021-08-06 21:14:33 +01:00
|
|
|
// GetByUserID returns the coupon applied to the user.
|
|
|
|
func (coupons *coupons) GetByUserID(ctx context.Context, userID uuid.UUID) (_ *payments.Coupon, err error) {
|
2021-07-08 20:06:07 +01:00
|
|
|
defer mon.Task()(&ctx, userID)(&err)
|
|
|
|
|
|
|
|
customerID, err := coupons.service.db.Customers().GetCustomerID(ctx, userID)
|
|
|
|
if err != nil {
|
2021-08-06 21:14:33 +01:00
|
|
|
return nil, Error.Wrap(err)
|
2021-07-08 20:06:07 +01:00
|
|
|
}
|
|
|
|
|
2023-03-14 02:59:24 +00:00
|
|
|
params := &stripe.CustomerParams{Params: stripe.Params{Context: ctx}}
|
2021-08-06 21:14:33 +01:00
|
|
|
params.AddExpand("discount.promotion_code")
|
2021-07-08 20:06:07 +01:00
|
|
|
|
2021-08-06 21:14:33 +01:00
|
|
|
customer, err := coupons.service.stripeClient.Customers().Get(customerID, params)
|
2021-07-08 20:06:07 +01:00
|
|
|
if err != nil {
|
2021-08-06 21:14:33 +01:00
|
|
|
return nil, err
|
2021-07-08 20:06:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if customer.Discount == nil || customer.Discount.Coupon == nil {
|
2021-08-06 21:14:33 +01:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2023-03-22 15:29:57 +00:00
|
|
|
return stripeDiscountToPaymentsCoupon(customer.Discount)
|
2021-08-06 21:14:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// stripeDiscountToPaymentsCoupon converts a Stripe discount to a payments.Coupon.
|
2023-03-22 15:29:57 +00:00
|
|
|
func stripeDiscountToPaymentsCoupon(dc *stripe.Discount) (coupon *payments.Coupon, err error) {
|
2021-08-06 21:14:33 +01:00
|
|
|
if dc == nil {
|
|
|
|
return nil, Error.New("discount is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if dc.Coupon == nil {
|
|
|
|
return nil, Error.New("discount.Coupon is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
coupon = &payments.Coupon{
|
2023-01-30 22:11:12 +00:00
|
|
|
ID: dc.Coupon.ID,
|
2021-08-06 21:14:33 +01:00
|
|
|
Name: dc.Coupon.Name,
|
|
|
|
AmountOff: dc.Coupon.AmountOff,
|
|
|
|
PercentOff: dc.Coupon.PercentOff,
|
|
|
|
AddedAt: time.Unix(dc.Start, 0),
|
|
|
|
ExpiresAt: time.Unix(dc.End, 0),
|
|
|
|
Duration: payments.CouponDuration(dc.Coupon.Duration),
|
|
|
|
}
|
|
|
|
|
|
|
|
if dc.PromotionCode != nil {
|
|
|
|
coupon.PromoCode = dc.PromotionCode.Code
|
2021-07-08 20:06:07 +01:00
|
|
|
}
|
|
|
|
|
2021-08-06 21:14:33 +01:00
|
|
|
return coupon, nil
|
2021-07-08 20:06:07 +01:00
|
|
|
}
|