satellite/payments: add partnered field to coupon struct

A field has been added to the coupon struct indicating whether it is
associated with a partner's pricing package. This is required to
alter the appearance of partner coupons in the satellite frontend.

References storj/storj-private#172

Change-Id: Ie48ae3902aaa108abf9a399242a0cd98cb53d1c3
This commit is contained in:
Jeremy Wharton 2023-03-13 01:52:01 -05:00
parent 0177ef79da
commit 73ff35f160
3 changed files with 16 additions and 6 deletions

View File

@ -43,6 +43,7 @@ type Coupon struct {
AddedAt time.Time `json:"addedAt"`
ExpiresAt time.Time `json:"expiresAt"`
Duration CouponDuration `json:"duration"`
Partnered bool `json:"partnered"`
}
// CouponDuration represents how many billing periods a coupon is applied.

View File

@ -39,7 +39,7 @@ func (coupons *coupons) ApplyFreeTierCoupon(ctx context.Context, userID uuid.UUI
return nil, Error.Wrap(err)
}
return stripeDiscountToPaymentsCoupon(customer.Discount)
return coupons.service.stripeDiscountToPaymentsCoupon(customer.Discount)
}
// ApplyCoupon applies the coupon to account if it exists.
@ -55,7 +55,7 @@ func (coupons *coupons) ApplyCoupon(ctx context.Context, userID uuid.UUID, coupo
if err != nil {
return nil, Error.Wrap(err)
}
return stripeDiscountToPaymentsCoupon(customer.Discount)
return coupons.service.stripeDiscountToPaymentsCoupon(customer.Discount)
}
// ApplyCouponCode attempts to apply a coupon code to the user via Stripe.
@ -107,7 +107,7 @@ func (coupons *coupons) ApplyCouponCode(ctx context.Context, userID uuid.UUID, c
return nil, Error.New("invalid discount after coupon code application; user ID:%s, customer ID:%s", userID, customerID)
}
return stripeDiscountToPaymentsCoupon(customer.Discount)
return coupons.service.stripeDiscountToPaymentsCoupon(customer.Discount)
}
// GetByUserID returns the coupon applied to the user.
@ -131,11 +131,11 @@ func (coupons *coupons) GetByUserID(ctx context.Context, userID uuid.UUID) (_ *p
return nil, nil
}
return stripeDiscountToPaymentsCoupon(customer.Discount)
return coupons.service.stripeDiscountToPaymentsCoupon(customer.Discount)
}
// stripeDiscountToPaymentsCoupon converts a Stripe discount to a payments.Coupon.
func stripeDiscountToPaymentsCoupon(dc *stripe.Discount) (coupon *payments.Coupon, err error) {
func (service *Service) stripeDiscountToPaymentsCoupon(dc *stripe.Discount) (coupon *payments.Coupon, err error) {
if dc == nil {
return nil, Error.New("discount is nil")
}
@ -144,6 +144,14 @@ func stripeDiscountToPaymentsCoupon(dc *stripe.Discount) (coupon *payments.Coupo
return nil, Error.New("discount.Coupon is nil")
}
var partnered bool
for _, plan := range service.packagePlans {
if plan.CouponID == dc.Coupon.ID {
partnered = true
break
}
}
coupon = &payments.Coupon{
ID: dc.Coupon.ID,
Name: dc.Coupon.Name,
@ -152,6 +160,7 @@ func stripeDiscountToPaymentsCoupon(dc *stripe.Discount) (coupon *payments.Coupo
AddedAt: time.Unix(dc.Start, 0),
ExpiresAt: time.Unix(dc.End, 0),
Duration: payments.CouponDuration(dc.Coupon.Duration),
Partnered: partnered,
}
if dc.PromotionCode != nil {

View File

@ -242,7 +242,7 @@ func (invoices *invoices) ListWithDiscounts(ctx context.Context, userID uuid.UUI
dc := dcAmt.Discount
coupon, err := stripeDiscountToPaymentsCoupon(dc)
coupon, err := invoices.service.stripeDiscountToPaymentsCoupon(dc)
if err != nil {
return nil, nil, Error.Wrap(err)
}