|
| 1 | +class Stripe::WebhooksController < ApplicationController |
| 2 | + allow_unauthenticated_access |
| 3 | + skip_before_action :require_account |
| 4 | + skip_before_action :verify_authenticity_token |
| 5 | + |
| 6 | + def create |
| 7 | + event = verify_webhook_signature |
| 8 | + return head :bad_request unless event |
| 9 | + |
| 10 | + dispatch_stripe_event(event) |
| 11 | + |
| 12 | + head :ok |
| 13 | + end |
| 14 | + |
| 15 | + private |
| 16 | + def dispatch_stripe_event(event) |
| 17 | + case event.type |
| 18 | + when "checkout.session.completed" |
| 19 | + handle_checkout_completed(event.data.object) |
| 20 | + when "customer.subscription.updated" |
| 21 | + handle_subscription_updated(event.data.object) |
| 22 | + when "customer.subscription.deleted" |
| 23 | + handle_subscription_deleted(event.data.object) |
| 24 | + when "invoice.payment_failed" |
| 25 | + handle_payment_failed(event.data.object) |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + def verify_webhook_signature |
| 30 | + payload = request.body.read |
| 31 | + sig_header = request.env["HTTP_STRIPE_SIGNATURE"] |
| 32 | + |
| 33 | + Stripe::Webhook.construct_event(payload, sig_header, ENV["STRIPE_WEBHOOK_SECRET"]) |
| 34 | + rescue Stripe::SignatureVerificationError => e |
| 35 | + Rails.logger.error "Stripe webhook signature verification failed: #{e.message}" |
| 36 | + nil |
| 37 | + end |
| 38 | + |
| 39 | + def handle_checkout_completed(session) |
| 40 | + return unless session.mode == "subscription" |
| 41 | + |
| 42 | + subscription = find_subscription_by_customer(session.customer) |
| 43 | + return unless subscription |
| 44 | + |
| 45 | + stripe_subscription = Stripe::Subscription.retrieve(session.subscription) |
| 46 | + |
| 47 | + subscription.update! \ |
| 48 | + stripe_subscription_id: stripe_subscription.id, |
| 49 | + plan_key: session.metadata["plan_key"], |
| 50 | + status: stripe_subscription.status, |
| 51 | + current_period_end: extract_current_period_end(stripe_subscription) |
| 52 | + end |
| 53 | + |
| 54 | + def handle_subscription_updated(stripe_subscription) |
| 55 | + if subscription = find_subscription_by_customer(stripe_subscription.customer) |
| 56 | + subscription.update! \ |
| 57 | + status: stripe_subscription.status, |
| 58 | + current_period_end: extract_current_period_end(stripe_subscription), |
| 59 | + cancel_at: stripe_subscription.cancel_at ? Time.at(stripe_subscription.cancel_at) : nil |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + def handle_subscription_deleted(stripe_subscription) |
| 64 | + if subscription = find_subscription_by_customer(stripe_subscription.customer) |
| 65 | + subscription.update!(status: "canceled", stripe_subscription_id: nil) |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + def handle_payment_failed(invoice) |
| 70 | + if subscription = find_subscription_by_customer(invoice.customer) |
| 71 | + subscription.update!(status: "past_due") |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + def find_subscription_by_customer(customer_id) |
| 76 | + Account::Subscription.find_by(stripe_customer_id: customer_id) |
| 77 | + end |
| 78 | + |
| 79 | + def extract_current_period_end(stripe_subscription) |
| 80 | + timestamp = stripe_subscription.items.data.first&.current_period_end |
| 81 | + Time.at(timestamp) if timestamp |
| 82 | + end |
| 83 | +end |
0 commit comments