@@ -20,6 +20,7 @@ import (
2020 "github.com/raystack/frontier/billing/credit"
2121
2222 "github.com/raystack/frontier/billing"
23+ billingerrors "github.com/raystack/frontier/billing/errors"
2324
2425 "github.com/raystack/frontier/billing/product"
2526 "github.com/raystack/frontier/pkg/utils"
@@ -330,7 +331,7 @@ func (s *Service) Cancel(ctx context.Context, id string, immediate bool) (Subscr
330331 Prorate : new (true ),
331332 })
332333 if err != nil {
333- return Subscription {}, fmt .Errorf ("failed to cancel subscription at billing provider: %w" , err )
334+ return Subscription {}, fmt .Errorf ("failed to cancel subscription at billing provider: %w" , billingerrors . TranslateStripeError ( err ) )
334335 }
335336 sub .State = string (stripeSubscription .Status )
336337 if stripeSubscription .CanceledAt > 0 {
@@ -359,7 +360,7 @@ func (s *Service) Cancel(ctx context.Context, id string, immediate bool) (Subscr
359360 EndBehavior : stripe .String (string (stripe .SubscriptionScheduleEndBehaviorCancel )),
360361 })
361362 if err != nil {
362- return sub , fmt .Errorf ("failed to cancel subscription schedule at billing provider: %w" , err )
363+ return sub , fmt .Errorf ("failed to cancel subscription schedule at billing provider: %w" , billingerrors . TranslateStripeError ( err ) )
363364 }
364365 sub .Phase .PlanID = ""
365366 sub .Phase .Reason = SubscriptionCancel .String ()
@@ -381,8 +382,8 @@ func (s *Service) createOrGetSchedule(ctx context.Context, sub Subscription) (*s
381382 },
382383 })
383384 if err != nil {
384- // check if it's a subscription not found err
385- if stripeErr , ok := err .( * stripe. Error ); ok && stripeErr . Code == stripe . ErrorCodeResourceMissing {
385+ err = billingerrors . TranslateStripeError ( err )
386+ if errors . Is ( err , billingerrors . ErrProviderResourceMissing ) {
386387 return nil , nil , ErrSubscriptionOnProviderNotFound
387388 }
388389 return nil , nil , fmt .Errorf ("failed to get subscription from billing provider: %w" , err )
@@ -398,7 +399,7 @@ func (s *Service) createOrGetSchedule(ctx context.Context, sub Subscription) (*s
398399 },
399400 })
400401 if err != nil {
401- return nil , nil , fmt .Errorf ("failed to get subscription schedule from billing provider: %w" , err )
402+ return nil , nil , fmt .Errorf ("failed to get subscription schedule from billing provider: %w" , billingerrors . TranslateStripeError ( err ) )
402403 }
403404 stripeSubscription .Schedule = schedule
404405 }
@@ -421,7 +422,7 @@ func (s *Service) createOrGetSchedule(ctx context.Context, sub Subscription) (*s
421422 },
422423 })
423424 if err != nil {
424- return nil , nil , fmt .Errorf ("failed to create subscription schedule at billing provider: %w" , err )
425+ return nil , nil , fmt .Errorf ("failed to create subscription schedule at billing provider: %w" , billingerrors . TranslateStripeError ( err ) )
425426 }
426427 }
427428 return stripeSubscription , stripeSubscription .Schedule , nil
@@ -483,7 +484,7 @@ func (s *Service) UpdateProductQuantity(ctx context.Context, orgID string, curre
483484 PendingInvoiceItemInterval : getPendingInvoiceItemInterval (currentPlan ),
484485 })
485486 if err != nil {
486- return fmt .Errorf ("failed to update subscription quantity at billing provider: %w" , err )
487+ return fmt .Errorf ("failed to update subscription quantity at billing provider: %w" , billingerrors . TranslateStripeError ( err ) )
487488 }
488489 }
489490 }
@@ -561,7 +562,7 @@ func (s *Service) UpdateProductQuantity(ctx context.Context, orgID string, curre
561562 Phases : updatedPhases ,
562563 })
563564 if err != nil {
564- return fmt .Errorf ("failed to update subscription schedule at billing provider: %w" , err )
565+ return fmt .Errorf ("failed to update subscription schedule at billing provider: %w" , billingerrors . TranslateStripeError ( err ) )
565566 }
566567 }
567568
@@ -773,7 +774,7 @@ func (s *Service) ChangePlan(ctx context.Context, id string, changeRequest Chang
773774 },
774775 })
775776 if err != nil {
776- return change , fmt .Errorf ("failed to update subscription schedule at billing provider: %w" , err )
777+ return change , fmt .Errorf ("failed to update subscription schedule at billing provider: %w" , billingerrors . TranslateStripeError ( err ) )
777778 }
778779
779780 // update subscription with new phase
@@ -968,7 +969,7 @@ func (s *Service) CancelUpcomingPhase(ctx context.Context, sub Subscription) err
968969 },
969970 })
970971 if err != nil {
971- return fmt .Errorf ("failed to update subscription schedule at billing provider: %w" , err )
972+ return fmt .Errorf ("failed to update subscription schedule at billing provider: %w" , billingerrors . TranslateStripeError ( err ) )
972973 }
973974
974975 sub .Phase .Reason = ""
0 commit comments