CognxSafeTrack commited on
Commit
e290651
·
1 Parent(s): f530e29

fix(deploy): make Stripe keys optional at initialization

Browse files
Files changed (1) hide show
  1. apps/api/src/services/stripe.ts +23 -18
apps/api/src/services/stripe.ts CHANGED
@@ -1,29 +1,29 @@
1
  import Stripe from 'stripe';
2
 
3
  export class StripeService {
4
- private stripe: Stripe;
5
- private webhookSecret: string;
6
  private clientUrl: string;
7
 
8
  constructor() {
9
  const secretKey = process.env.STRIPE_SECRET_KEY;
10
- if (!secretKey) throw new Error('[StripeService] STRIPE_SECRET_KEY is required');
11
-
12
  const webhookSecret = process.env.STRIPE_WEBHOOK_SECRET;
13
- if (!webhookSecret) throw new Error('[StripeService] STRIPE_WEBHOOK_SECRET is required');
14
 
15
- this.webhookSecret = webhookSecret;
16
  this.clientUrl = process.env.VITE_CLIENT_URL || 'http://localhost:5174';
17
 
18
- this.stripe = new Stripe(secretKey, {
19
- apiVersion: '2025-01-27.acacia' as any,
20
- });
 
 
21
  }
22
 
23
  /**
24
  * Creates a Stripe Checkout Session for a specific track and user.
25
  */
26
  async createCheckoutSession(userId: string, trackId: string, priceId: string, userPhone: string) {
 
27
  try {
28
  const session = await this.stripe.checkout.sessions.create({
29
  payment_method_types: ['card'],
@@ -34,9 +34,8 @@ export class StripeService {
34
  },
35
  ],
36
  mode: 'payment',
37
- success_url: `${this.clientUrl}/payment-success?session_id={CHECKOUT_SESSION_ID}`,
38
- cancel_url: `${this.clientUrl}/payment-cancel`,
39
- client_reference_id: userId,
40
  metadata: {
41
  userId,
42
  trackId,
@@ -46,24 +45,30 @@ export class StripeService {
46
 
47
  return session.url;
48
  } catch (error) {
49
- console.error('[StripeService] Error creating checkout session:', error);
50
- throw new Error('Failed to create payment session');
51
  }
52
  }
53
 
54
  /**
55
  * Verifies the signature of an incoming Stripe webhook.
56
  */
57
- verifyWebhookSignature(payload: string | Buffer, signature: string): Stripe.Event {
 
 
 
 
 
 
 
58
  try {
59
  return this.stripe.webhooks.constructEvent(
60
  payload,
61
  signature,
62
  this.webhookSecret
63
  );
64
- } catch (error) {
65
- console.error('[StripeService] Webhook signature verification failed:', error);
66
- throw error;
67
  }
68
  }
69
  }
 
1
  import Stripe from 'stripe';
2
 
3
  export class StripeService {
4
+ private stripe: Stripe | null = null;
5
+ private webhookSecret: string | null = null;
6
  private clientUrl: string;
7
 
8
  constructor() {
9
  const secretKey = process.env.STRIPE_SECRET_KEY;
 
 
10
  const webhookSecret = process.env.STRIPE_WEBHOOK_SECRET;
 
11
 
12
+ this.webhookSecret = webhookSecret || null;
13
  this.clientUrl = process.env.VITE_CLIENT_URL || 'http://localhost:5174';
14
 
15
+ if (secretKey) {
16
+ this.stripe = new Stripe(secretKey, {
17
+ apiVersion: '2025-01-27.acacia' as any,
18
+ });
19
+ }
20
  }
21
 
22
  /**
23
  * Creates a Stripe Checkout Session for a specific track and user.
24
  */
25
  async createCheckoutSession(userId: string, trackId: string, priceId: string, userPhone: string) {
26
+ if (!this.stripe) throw new Error('[StripeService] STRIPE_SECRET_KEY is not configured');
27
  try {
28
  const session = await this.stripe.checkout.sessions.create({
29
  payment_method_types: ['card'],
 
34
  },
35
  ],
36
  mode: 'payment',
37
+ success_url: `${this.clientUrl}/payment/success?session_id={CHECKOUT_SESSION_ID}&track=${trackId}`,
38
+ cancel_url: `${this.clientUrl}/student?cancel=true`,
 
39
  metadata: {
40
  userId,
41
  trackId,
 
45
 
46
  return session.url;
47
  } catch (error) {
48
+ console.error('[StripeService] Failed to create checkout session:', error);
49
+ throw error;
50
  }
51
  }
52
 
53
  /**
54
  * Verifies the signature of an incoming Stripe webhook.
55
  */
56
+ verifyWebhookSignature(payload: Buffer, signature: string | undefined): Stripe.Event {
57
+ if (!this.stripe || !this.webhookSecret) {
58
+ throw new Error('[StripeService] Stripe is not configured (missing keys)');
59
+ }
60
+ if (!signature) {
61
+ throw new Error('Missing stripe-signature header');
62
+ }
63
+
64
  try {
65
  return this.stripe.webhooks.constructEvent(
66
  payload,
67
  signature,
68
  this.webhookSecret
69
  );
70
+ } catch (err: any) {
71
+ throw new Error(`Webhook Error: ${err.message}`);
 
72
  }
73
  }
74
  }