Payment Integration Setup

Configure Stripe webhooks to activate subscriptions automatically

Stripe Webhook Setup

Critical: Set these up in your Stripe Dashboard to activate subscriptions automatically

1

Stripe Dashboard Setup

  1. 1. Go to stripe.com/dashboard
  2. 2. Navigate to Developers → Webhooks
  3. 3. Click "Add endpoint"
  4. 4. Enter your webhook URL: https://yourdomain.com/api/webhooks/stripe
2

Select These Events

Player Subscriptions
checkout.session.completed- Create subscription
customer.subscription.updated- Update status
customer.subscription.deleted- Cancel subscription
invoice.payment_succeeded- Reset credits monthly
invoice.payment_failed- Mark past_due
Coach Subscriptions

Use the same events as player subscriptions above

3

Webhook Handler Logic

checkout.session.completed

// Get customer_email and client_reference_id from event
const email = event.data.object.customer_email;
const subscriptionId = event.data.object.subscription;

// Check metadata or price_id to determine type
const metadata = event.data.object.metadata;

if (metadata.subscription_type === 'coach') {
  // Create CoachSubscription
  await base44.entities.CoachSubscription.create({
    coach_email: email,
    status: 'active',
    stripe_subscription_id: subscriptionId,
    monthly_price: 1500,
    current_period_start: new Date().toISOString(),
    current_period_end: new Date(Date.now() + 30*24*60*60*1000).toISOString()
  });
  
  // Track affiliate if referred
  if (metadata.referred_by) {
    await base44.entities.AffiliateEarning.create({
      affiliate_email: metadata.referred_by,
      affiliate_type: 'coach',
      referred_user_email: email,
      commission_amount: 150, // 10% of $1500
      commission_type: 'coach_subscription',
      payment_status: 'pending'
    });
  }
} else {
  // Create Player Subscription
  const tier = metadata.tier; // 'starter', 'pro', 'elite'
  const credits = {starter: 1, pro: 5, elite: 15}[tier];
  const price = {starter: 49, pro: 199, elite: 499}[tier];
  
  await base44.entities.Subscription.create({
    user_email: email,
    tier: tier,
    stripe_subscription_id: subscriptionId,
    status: 'active',
    credits_remaining: credits,
    credits_per_month: credits,
    price_per_month: price,
    current_period_start: new Date().toISOString(),
    current_period_end: new Date(Date.now() + 30*24*60*60*1000).toISOString()
  });
  
  // Track affiliate if referred
  if (metadata.referred_by) {
    const commission = price * 0.1; // 10% commission
    await base44.entities.AffiliateEarning.create({
      affiliate_email: metadata.referred_by,
      affiliate_type: metadata.referrer_type || 'player',
      referred_user_email: email,
      commission_amount: commission,
      commission_type: 'subscription',
      payment_status: 'pending'
    });
  }
}

invoice.payment_succeeded

// Monthly renewal - reset credits
const subscriptionId = event.data.object.subscription;
const email = event.data.object.customer_email;

// Find subscription
const subs = await base44.entities.Subscription.filter({
  stripe_subscription_id: subscriptionId
});

if (subs.length > 0) {
  const sub = subs[0];
  
  // Reset credits to monthly allowance
  await base44.entities.Subscription.update(sub.id, {
    credits_remaining: sub.credits_per_month,
    current_period_start: new Date().toISOString(),
    current_period_end: new Date(Date.now() + 30*24*60*60*1000).toISOString()
  });
}

customer.subscription.deleted

// Subscription cancelled
const subscriptionId = event.data.object.id;

// Update status to cancelled
const subs = await base44.entities.Subscription.filter({
  stripe_subscription_id: subscriptionId
});

if (subs.length > 0) {
  await base44.entities.Subscription.update(subs[0].id, {
    status: 'cancelled',
    cancelled_at: new Date().toISOString()
  });
}

// Also check CoachSubscription
const coachSubs = await base44.entities.CoachSubscription.filter({
  stripe_subscription_id: subscriptionId
});

if (coachSubs.length > 0) {
  await base44.entities.CoachSubscription.update(coachSubs[0].id, {
    status: 'cancelled'
  });
}
4

Update Payment Links with Metadata

Add these parameters to your Stripe payment links:

Coach Link
https://buy.stripe.com/cNieVd17884zaHB2cn4c806
?prefilled_email={email}
&client_reference_id={email}
&metadata[subscription_type]=coach
Player Starter
https://buy.stripe.com/...
?prefilled_email={email}
&client_reference_id={email}
&metadata[subscription_type]=player
&metadata[tier]=starter

Quick Test

  1. 1. Use Stripe test mode and test card: 4242 4242 4242 4242
  2. 2. Complete a subscription purchase
  3. 3. Check Stripe Dashboard → Webhooks → see if event was received
  4. 4. Check your database - subscription should be created automatically