Sha256: 437a5f9124f419cf06ec89cde35514ed51aa290dd5d6658f734c5dd6ee32f467

Contents?: true

Size: 1.2 KB

Versions: 31

Compression:

Stored size: 1.2 KB

Contents

module Pay
  module Stripe
    module Webhooks
      class SubscriptionUpdated
        def call(event)
          object = event.data.object
          subscription = Pay.subscription_model.find_by(processor: :stripe, processor_id: object.id)

          return if subscription.nil?

          # Delete any subscription attempts that have expired
          if object.status == "incomplete_expired"
            subscription.destroy
            return
          end

          subscription.status = object.status
          subscription.quantity = object.quantity
          subscription.processor_plan = object.plan.id
          subscription.trial_ends_at = Time.at(object.trial_end) if object.trial_end.present?

          # If user was on trial, their subscription ends at the end of the trial
          subscription.ends_at = if object.cancel_at_period_end && subscription.on_trial?
            subscription.trial_ends_at

          # User wasn't on trial, so subscription ends at period end
          elsif object.cancel_at_period_end
            Time.at(object.current_period_end)

            # Subscription isn't marked to cancel at period end
          end

          subscription.save!
        end
      end
    end
  end
end

Version data entries

31 entries across 30 versions & 1 rubygems

Version Path
pay-2.6.11 lib/pay/stripe/webhooks/subscription_updated.rb
pay-2.6.10 lib/pay/stripe/webhooks/subscription_updated.rb
pay-2.6.9 lib/pay/stripe/webhooks/subscription_updated.rb
pay-2.6.8 lib/pay/stripe/webhooks/subscription_updated.rb
pay-2.6.7 lib/pay/stripe_marketplace/webhooks/subscription_updated.rb
pay-2.6.7 lib/pay/stripe/webhooks/subscription_updated.rb
pay-2.6.6 lib/pay/stripe/webhooks/subscription_updated.rb
pay-2.6.5 lib/pay/stripe/webhooks/subscription_updated.rb
pay-2.6.4 lib/pay/stripe/webhooks/subscription_updated.rb
pay-2.6.3 lib/pay/stripe/webhooks/subscription_updated.rb
pay-2.6.2 lib/pay/stripe/webhooks/subscription_updated.rb
pay-2.6.1 lib/pay/stripe/webhooks/subscription_updated.rb
pay-2.6.0 lib/pay/stripe/webhooks/subscription_updated.rb
pay-2.5.0 lib/pay/stripe/webhooks/subscription_updated.rb
pay-2.4.4 lib/pay/stripe/webhooks/subscription_updated.rb
pay-2.4.3 lib/pay/stripe/webhooks/subscription_updated.rb
pay-2.4.2 lib/pay/stripe/webhooks/subscription_updated.rb
pay-2.4.0 lib/pay/stripe/webhooks/subscription_updated.rb
pay-2.3.1 lib/pay/stripe/webhooks/subscription_updated.rb
pay-2.3.0 lib/pay/stripe/webhooks/subscription_updated.rb