Sha256: 5c7a96fdd56b1a3f5b084c4a4e3e58e05b8ecaa13c992bd289899f8535150e50

Contents?: true

Size: 1.7 KB

Versions: 4

Compression:

Stored size: 1.7 KB

Contents

module Pay
  module Stripe
    module Webhooks
      class SubscriptionCreated
        def call(event)
          object = event.data.object

          # We may already have the subscription in the database, so we can update that record
          subscription = Pay.subscription_model.find_by(processor: :stripe, processor_id: object.id)

          # Create the subscription in the database if we don't have it already
          if subscription.nil?
            # The customer should already be in the database
            owner = Pay.find_billable(processor: :stripe, processor_id: object.customer)

            if owner.nil?
              Rails.logger.error("[Pay] Unable to find Pay::Billable with processor: :stripe and processor_id: '#{object.customer}'. Searched these models: #{Pay.billable_models.join(", ")}")
              return
            end

            subscription = Pay.subscription_model.new(name: Pay.default_product_name, owner: owner, processor: :stripe, processor_id: object.id)
          end

          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

4 entries across 4 versions & 1 rubygems

Version Path
pay-2.4.4 lib/pay/stripe/webhooks/subscription_created.rb
pay-2.4.3 lib/pay/stripe/webhooks/subscription_created.rb
pay-2.4.2 lib/pay/stripe/webhooks/subscription_created.rb
pay-2.4.0 lib/pay/stripe/webhooks/subscription_created.rb