@@ -63,34 +63,83 @@ export const stripeBillingRouter = router({
63
63
} ) ,
64
64
)
65
65
. query ( async ( { ctx, input } ) => {
66
+ ctx . req . log . debug (
67
+ 'Find active subscription for organization. (organizationId=%s)' ,
68
+ input . organizationId ,
69
+ ) ;
70
+
66
71
const storage = ctx . stripeBilling . storage ;
67
72
const organizationBillingRecord = await storage . getOrganizationBilling ( {
68
73
organizationId : input . organizationId ,
69
74
} ) ;
70
75
71
76
if ( ! organizationBillingRecord ) {
77
+ ctx . req . log . debug (
78
+ 'No billing record found for organization. (organizationId=%s)' ,
79
+ input . organizationId ,
80
+ ) ;
72
81
throw new Error ( `Organization does not have a subscription record!` ) ;
73
82
}
74
83
84
+ ctx . req . log . debug (
85
+ 'Load customer from stripe based on external billing reference. (organizationId=%s, externalBillingReference=%s)' ,
86
+ input . organizationId ,
87
+ organizationBillingRecord . externalBillingReference ,
88
+ ) ;
89
+
75
90
const customer = await ctx . stripeBilling . stripe . customers . retrieve (
76
91
organizationBillingRecord . externalBillingReference ,
77
92
) ;
78
93
94
+ ctx . req . log . debug (
95
+ 'Stripe customer found (organizationId=%s, stripeCustomerId=%s, isDeleted=%s)' ,
96
+ input . organizationId ,
97
+ customer . id ,
98
+ customer . deleted ?? false ,
99
+ ) ;
100
+
79
101
if ( customer . deleted === true ) {
80
102
await storage . deleteOrganizationBilling ( {
81
103
organizationId : input . organizationId ,
82
104
} ) ;
83
105
106
+ ctx . req . log . debug (
107
+ 'Stripe customer is deleted (organizationId=%s, stripeCustomerId=%s, isDeleted=%s)' ,
108
+ input . organizationId ,
109
+ customer . id ,
110
+ customer . deleted ?? false ,
111
+ ) ;
112
+
84
113
return null ;
85
114
}
86
115
87
- const subscriptions = await ctx . stripeBilling . stripe . subscriptions
88
- . list ( {
89
- customer : organizationBillingRecord . externalBillingReference ,
90
- } )
91
- . then ( v => v . data . filter ( r => r . metadata ?. hive_subscription ) ) ;
116
+ ctx . req . log . debug (
117
+ 'Load subscriptions for customer (organizationId=%s, stripeCustomerId=%s)' ,
118
+ input . organizationId ,
119
+ customer . id ,
120
+ ) ;
121
+
122
+ const allSubscriptions = await ctx . stripeBilling . stripe . subscriptions . list ( {
123
+ customer : customer . id ,
124
+ } ) ;
125
+
126
+ ctx . req . log . debug (
127
+ 'Found total of %s subscription(s) (organizationId=%s, stripeCustomerId=%s)' ,
128
+ String ( allSubscriptions . data . length ) ,
129
+ input . organizationId ,
130
+ customer . id ,
131
+ ) ;
132
+
133
+ const actualSubscription =
134
+ allSubscriptions . data . find ( r => r . metadata ?. hive_subscription ) ?? null ;
92
135
93
- const actualSubscription = subscriptions [ 0 ] || null ;
136
+ if ( ! actualSubscription ) {
137
+ ctx . req . log . debug (
138
+ 'Could not find the subscription with the hive metadata. (organizationId=%s, stripeCustomerId=%s)' ,
139
+ input . organizationId ,
140
+ customer . id ,
141
+ ) ;
142
+ }
94
143
95
144
const paymentMethod = await ctx . stripeBilling . stripe . customers . listPaymentMethods (
96
145
customer . id ,
@@ -100,6 +149,14 @@ export const stripeBillingRouter = router({
100
149
} ,
101
150
) ;
102
151
152
+ if ( ! paymentMethod ) {
153
+ ctx . req . log . debug (
154
+ 'Could not find the card payment method for he strip customer. (organizationId=%s, stripeCustomerId=%s)' ,
155
+ input . organizationId ,
156
+ customer . id ,
157
+ ) ;
158
+ }
159
+
103
160
return {
104
161
paymentMethod : paymentMethod . data [ 0 ] || null ,
105
162
subscription : actualSubscription ,
0 commit comments