Skip to content

Commit 8640004

Browse files
authored
chore: improve strip logging (#6866)
1 parent 33ad7fe commit 8640004

File tree

1 file changed

+63
-6
lines changed
  • packages/services/commerce/src/stripe-billing

1 file changed

+63
-6
lines changed

packages/services/commerce/src/stripe-billing/index.ts

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,34 +63,83 @@ export const stripeBillingRouter = router({
6363
}),
6464
)
6565
.query(async ({ ctx, input }) => {
66+
ctx.req.log.debug(
67+
'Find active subscription for organization. (organizationId=%s)',
68+
input.organizationId,
69+
);
70+
6671
const storage = ctx.stripeBilling.storage;
6772
const organizationBillingRecord = await storage.getOrganizationBilling({
6873
organizationId: input.organizationId,
6974
});
7075

7176
if (!organizationBillingRecord) {
77+
ctx.req.log.debug(
78+
'No billing record found for organization. (organizationId=%s)',
79+
input.organizationId,
80+
);
7281
throw new Error(`Organization does not have a subscription record!`);
7382
}
7483

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+
7590
const customer = await ctx.stripeBilling.stripe.customers.retrieve(
7691
organizationBillingRecord.externalBillingReference,
7792
);
7893

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+
79101
if (customer.deleted === true) {
80102
await storage.deleteOrganizationBilling({
81103
organizationId: input.organizationId,
82104
});
83105

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+
84113
return null;
85114
}
86115

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;
92135

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+
}
94143

95144
const paymentMethod = await ctx.stripeBilling.stripe.customers.listPaymentMethods(
96145
customer.id,
@@ -100,6 +149,14 @@ export const stripeBillingRouter = router({
100149
},
101150
);
102151

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+
103160
return {
104161
paymentMethod: paymentMethod.data[0] || null,
105162
subscription: actualSubscription,

0 commit comments

Comments
 (0)