2019-11-26 17:58:51 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package stripecoinpayments
|
|
|
|
|
|
|
|
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{
|
|
|
|
Coupon: stripe.String(coupons.service.StripeFreeTierCouponID),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2023-03-13 06:52:01 +00:00
|
|
|
return coupons.service.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)
|
|
|
|
}
|
|
|
|
|
|
|
|
customer, err := coupons.service.stripeClient.Customers().Update(customerID, &stripe.CustomerParams{Coupon: stripe.String(couponID)})
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
2023-03-13 06:52:01 +00:00
|
|
|
return coupons.service.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)
|
|
|
|
|
2023-01-30 22:11:12 +00:00
|
|
|
user, err := coupons.service.usersDB.Get(ctx, userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if user.UserAgent != nil {
|
|
|
|
partner := string(user.UserAgent)
|
|
|
|
if plan, ok := coupons.service.packagePlans[partner]; ok {
|
|
|
|
coupon, err := coupons.GetByUserID(ctx, userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if coupon != nil && coupon.ID == plan.CouponID {
|
2023-01-25 21:38:29 +00:00
|
|
|
return nil, payments.ErrCouponConflict.New("coupon for partner '%s' should not be replaced", partner)
|
2023-01-30 22:11:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-22 01:09:56 +01:00
|
|
|
promoCodeIter := coupons.service.stripeClient.PromoCodes().List(&stripe.PromotionCodeListParams{
|
|
|
|
Code: stripe.String(couponCode),
|
|
|
|
})
|
|
|
|
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{
|
|
|
|
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-13 06:52:01 +00:00
|
|
|
return coupons.service.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
|
|
|
}
|
|
|
|
|
2021-08-06 21:14:33 +01:00
|
|
|
params := &stripe.CustomerParams{}
|
|
|
|
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-13 06:52:01 +00:00
|
|
|
return coupons.service.stripeDiscountToPaymentsCoupon(customer.Discount)
|
2021-08-06 21:14:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// stripeDiscountToPaymentsCoupon converts a Stripe discount to a payments.Coupon.
|
2023-03-13 06:52:01 +00:00
|
|
|
func (service *Service) 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")
|
|
|
|
}
|
|
|
|
|
2023-03-13 06:52:01 +00:00
|
|
|
var partnered bool
|
|
|
|
for _, plan := range service.packagePlans {
|
|
|
|
if plan.CouponID == dc.Coupon.ID {
|
|
|
|
partnered = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-06 21:14:33 +01:00
|
|
|
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),
|
2023-03-13 06:52:01 +00:00
|
|
|
Partnered: partnered,
|
2021-08-06 21:14:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|