Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions billing/checkout/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ func (s State) String() string {
}

var (
ErrNotFound = errors.New("checkout not found")
ErrInvalidUUID = errors.New("invalid syntax of uuid")
ErrInvalidID = errors.New("invalid checkout id")
ErrInvalidDetail = errors.New("invalid checkout detail")
ErrKycCompleted = errors.New("organization kyc completed")
ErrNotFound = errors.New("checkout not found")
ErrInvalidUUID = errors.New("invalid syntax of uuid")
ErrInvalidID = errors.New("invalid checkout id")
ErrInvalidDetail = errors.New("invalid checkout detail")
ErrKycCompleted = errors.New("organization kyc completed")
ErrAlreadySubscribed = errors.New("already subscribed to the plan")
)

type Checkout struct {
Expand Down
4 changes: 2 additions & 2 deletions billing/checkout/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func (s *Service) Create(ctx context.Context, ch Checkout) (Checkout, error) {
if subID, err := s.checkIfAlreadySubscribed(ctx, ch); err != nil {
return Checkout{}, err
} else if subID != "" {
return Checkout{}, fmt.Errorf("already subscribed to the plan")
return Checkout{}, ErrAlreadySubscribed
}

// create subscription items
Expand Down Expand Up @@ -894,7 +894,7 @@ func (s *Service) Apply(ctx context.Context, ch Checkout) (*subscription.Subscri
if subID, err := s.checkIfAlreadySubscribed(ctx, ch); err != nil {
return nil, nil, err
} else if subID != "" {
return nil, nil, fmt.Errorf("already subscribed to the plan")
return nil, nil, ErrAlreadySubscribed
}

if err := s.cancelTrialingSubscription(ctx, ch.CustomerID, ch.PlanID); err != nil {
Expand Down
10 changes: 5 additions & 5 deletions internal/api/v1beta1connect/billing_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ func (h *ConnectHandler) CheckFeatureEntitlement(ctx context.Context, request *c
if errors.Is(err, customer.ErrInvalidUUID) || errors.Is(err, customer.ErrInvalidID) {
return nil, connect.NewError(connect.CodeInvalidArgument, err)
}
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("CheckFeatureEntitlement.GetByOrgID: org_id=%s: %w", request.Msg.GetOrgId(), err))
return nil, mapBillingError(ctx, fmt.Errorf("CheckFeatureEntitlement.GetByOrgID: org_id=%s: %w", request.Msg.GetOrgId(), err))
}

checkStatus, err := h.entitlementService.Check(ctx, cust.ID, request.Msg.GetFeature())
if err != nil {
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("CheckFeatureEntitlement: billing_id=%s org_id=%s feature=%s: %w", cust.ID, request.Msg.GetOrgId(), request.Msg.GetFeature(), err))
return nil, mapBillingError(ctx, fmt.Errorf("CheckFeatureEntitlement: billing_id=%s org_id=%s feature=%s: %w", cust.ID, request.Msg.GetOrgId(), request.Msg.GetFeature(), err))
}

return connect.NewResponse(&frontierv1beta1.CheckFeatureEntitlementResponse{
Expand All @@ -38,7 +38,7 @@ func (h *ConnectHandler) CheckCreditEntitlement(ctx context.Context, request *co
OrgID: request.Msg.GetOrgId(),
})
if err != nil {
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("CheckCreditEntitlement.List: org_id=%s: %w", request.Msg.GetOrgId(), err))
return nil, mapBillingError(ctx, fmt.Errorf("CheckCreditEntitlement.List: org_id=%s: %w", request.Msg.GetOrgId(), err))
}

if len(customerList) == 0 {
Expand All @@ -48,12 +48,12 @@ func (h *ConnectHandler) CheckCreditEntitlement(ctx context.Context, request *co
customer := customerList[0]
customerDetails, err := h.customerService.GetDetails(ctx, customer.ID)
if err != nil {
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("CheckCreditEntitlement.GetDetails: customer_id=%s org_id=%s: %w", customer.ID, request.Msg.GetOrgId(), err))
return nil, mapBillingError(ctx, fmt.Errorf("CheckCreditEntitlement.GetDetails: customer_id=%s org_id=%s: %w", customer.ID, request.Msg.GetOrgId(), err))
}

creditBalance, err := h.creditService.GetBalance(ctx, customer.ID)
if err != nil {
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("CheckCreditEntitlement.GetBalance: customer_id=%s org_id=%s: %w", customer.ID, request.Msg.GetOrgId(), err))
return nil, mapBillingError(ctx, fmt.Errorf("CheckCreditEntitlement.GetBalance: customer_id=%s org_id=%s: %w", customer.ID, request.Msg.GetOrgId(), err))
}

if creditBalance-request.Msg.GetAmount() >= customerDetails.CreditMin {
Expand Down
22 changes: 11 additions & 11 deletions internal/api/v1beta1connect/billing_checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (h *ConnectHandler) CreateCheckout(ctx context.Context, request *connect.Re
// Always infer billing_id from org_id (ignore billing_id from request for security)
billingID, err := h.GetBillingAccountFromOrgID(ctx, request.Msg.GetOrgId())
if err != nil {
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("CreateCheckout.GetBillingAccountFromOrgID: org_id=%s: %w", request.Msg.GetOrgId(), err))
return nil, mapBillingError(ctx, fmt.Errorf("CreateCheckout.GetBillingAccountFromOrgID: org_id=%s: %w", request.Msg.GetOrgId(), err))
}

// check if setup requested
Expand All @@ -31,7 +31,7 @@ func (h *ConnectHandler) CreateCheckout(ctx context.Context, request *connect.Re
CancelUrl: request.Msg.GetCancelUrl(),
})
if err != nil {
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("CreateCheckout.CreateSessionForPaymentMethod: billing_id=%s: %w", billingID, err))
return nil, mapBillingError(ctx, fmt.Errorf("CreateCheckout.CreateSessionForPaymentMethod: billing_id=%s: %w", billingID, err))
}

return connect.NewResponse(&frontierv1beta1.CreateCheckoutResponse{
Expand All @@ -50,7 +50,7 @@ func (h *ConnectHandler) CreateCheckout(ctx context.Context, request *connect.Re
if errors.Is(err, checkout.ErrKycCompleted) {
return nil, connect.NewError(connect.CodeFailedPrecondition, ErrPortalChangesKycCompleted)
}
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("CreateCheckout.CreateSessionForCustomerPortal: billing_id=%s: %w", billingID, err))
return nil, mapBillingError(ctx, fmt.Errorf("CreateCheckout.CreateSessionForCustomerPortal: billing_id=%s: %w", billingID, err))
}

// Audit the customer portal session creation so we can trace who (a super
Expand Down Expand Up @@ -120,7 +120,7 @@ func (h *ConnectHandler) CreateCheckout(ctx context.Context, request *connect.Re
if errors.Is(err, product.ErrPerSeatLimitReached) {
return nil, connect.NewError(connect.CodeInvalidArgument, ErrPerSeatLimitReached)
}
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("CreateCheckout.Create: billing_id=%s plan_id=%s product_id=%s quantity=%d skip_trial=%v cancel_after_trial=%v: %w", billingID, planID, featureID, quantity, skipTrial, cancelAfterTrial, err))
return nil, mapBillingError(ctx, fmt.Errorf("CreateCheckout.Create: billing_id=%s plan_id=%s product_id=%s quantity=%d skip_trial=%v cancel_after_trial=%v: %w", billingID, planID, featureID, quantity, skipTrial, cancelAfterTrial, err))
}

return connect.NewResponse(&frontierv1beta1.CreateCheckoutResponse{
Expand All @@ -132,7 +132,7 @@ func (h *ConnectHandler) DelegatedCheckout(ctx context.Context, request *connect
// Always infer billing_id from org_id (ignore billing_id from request for security)
billingID, err := h.GetBillingAccountFromOrgID(ctx, request.Msg.GetOrgId())
if err != nil {
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("DelegatedCheckout.GetBillingAccountFromOrgID: org_id=%s: %w", request.Msg.GetOrgId(), err))
return nil, mapBillingError(ctx, fmt.Errorf("DelegatedCheckout.GetBillingAccountFromOrgID: org_id=%s: %w", request.Msg.GetOrgId(), err))
}

var planID string
Expand Down Expand Up @@ -161,19 +161,19 @@ func (h *ConnectHandler) DelegatedCheckout(ctx context.Context, request *connect
ProviderCouponID: providerCouponID,
})
if err != nil {
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("DelegatedCheckout.Apply: billing_id=%s plan_id=%s product_id=%s product_quantity=%d skip_trial=%v cancel_after_trial=%v provider_coupon_id=%s: %w", billingID, planID, productID, productQuantity, skipTrial, cancelAfterTrail, providerCouponID, err))
return nil, mapBillingError(ctx, fmt.Errorf("DelegatedCheckout.Apply: billing_id=%s plan_id=%s product_id=%s product_quantity=%d skip_trial=%v cancel_after_trial=%v provider_coupon_id=%s: %w", billingID, planID, productID, productQuantity, skipTrial, cancelAfterTrail, providerCouponID, err))
}

var subsPb *frontierv1beta1.Subscription
if subs != nil {
if subsPb, err = transformSubscriptionToPB(*subs); err != nil {
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("DelegatedCheckout: subscription_id=%s: %w", subs.ID, err))
return nil, mapBillingError(ctx, fmt.Errorf("DelegatedCheckout: subscription_id=%s: %w", subs.ID, err))
}
}
var productPb *frontierv1beta1.Product
if prod != nil {
if productPb, err = transformProductToPB(*prod); err != nil {
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("DelegatedCheckout: product_id=%s: %w", prod.ID, err))
return nil, mapBillingError(ctx, fmt.Errorf("DelegatedCheckout: product_id=%s: %w", prod.ID, err))
}
}

Expand All @@ -191,15 +191,15 @@ func (h *ConnectHandler) ListCheckouts(ctx context.Context, request *connect.Req
// Always infer billing_id from org_id (ignore billing_id from request for security)
billingID, err := h.GetBillingAccountFromOrgID(ctx, request.Msg.GetOrgId())
if err != nil {
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("ListCheckouts.GetBillingAccountFromOrgID: org_id=%s: %w", request.Msg.GetOrgId(), err))
return nil, mapBillingError(ctx, fmt.Errorf("ListCheckouts.GetBillingAccountFromOrgID: org_id=%s: %w", request.Msg.GetOrgId(), err))
}

var checkouts []*frontierv1beta1.CheckoutSession
checkoutList, err := h.checkoutService.List(ctx, checkout.Filter{
CustomerID: billingID,
})
if err != nil {
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("ListCheckouts.List: billing_id=%s org_id=%s: %w", billingID, request.Msg.GetOrgId(), err))
return nil, mapBillingError(ctx, fmt.Errorf("ListCheckouts.List: billing_id=%s org_id=%s: %w", billingID, request.Msg.GetOrgId(), err))
}
for _, v := range checkoutList {
checkouts = append(checkouts, transformCheckoutToPB(v))
Expand All @@ -217,7 +217,7 @@ func (h *ConnectHandler) GetCheckout(ctx context.Context, request *connect.Reque

ch, err := h.checkoutService.GetByID(ctx, request.Msg.GetId())
if err != nil {
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("GetCheckout.GetByID: checkout_id=%s: %w", request.Msg.GetId(), err))
return nil, mapBillingError(ctx, fmt.Errorf("GetCheckout.GetByID: checkout_id=%s: %w", request.Msg.GetId(), err))
}

return connect.NewResponse(&frontierv1beta1.GetCheckoutResponse{
Expand Down
Loading
Loading