<commit_before>require "stripe_event/base"

module StripeEvent
  module Invoice
    class PaymentSucceeded < Base

      def call(event)
        @event = event

        # If the event is an invoice, or has an invoice attached to it,
        # then it is for a user and that user will have a subscription
        return unless invoice?

        @user = user_for_event(@event)

        # move back membership expiration time to the end paid for
        expiration = Time.at(subscription.current_period_end)
        @user.membership.update_attributes!(expires_at: expiration)

        # rebuild the members page in case this activated a membership
        FastlyRails.purge_by_key("members")
      end

      private

      def invoice?
        Stripe::Invoice === @event.data.object ||
          @event.data.object.invoice.present?
      end

      def subscription
        @subscription ||= user_subscription_for_event(@user, @event)
      end

    end
  end
end<commit_msg>Handle Stripe::Charge objects in Stripe::Invoice::PaymentSucceeded<commit_after>require "stripe_event/base"

module StripeEvent
  module Invoice
    class PaymentSucceeded < Base

      def call(event)
        @event = event

        # If the event is an invoice, or has an invoice attached to it,
        # then it is for a user and that user will have a subscription
        return unless invoice? || charge?

        # if it is a charge object then we have to find the subscription
        # through the invoice.  Alternatively, we *could* override the event's
        # data object with the corresponding invoice.
        set_event_subscription if charge?

        @user = user_for_event(@event)

        # move back membership expiration time to the end paid for
        expiration = Time.at(subscription.current_period_end)
        @user.membership.update_attributes!(expires_at: expiration)

        # rebuild the members page in case this activated a membership
        FastlyRails.purge_by_key("members")
      end

      private

      def charge?
        Stripe::Charge === @event.data.object
      end

      def invoice?
        Stripe::Invoice === @event.data.object ||
          @event.data.object.invoice.present?
      end

      def set_event_subscription
        @event.data.object.subscription = Stripe::Invoice.retrieve(
          @event.data.object.invoice
        ).subscription
      end

      def subscription
        @subscription ||= user_subscription_for_event(@user, @event)
      end

    end
  end
end
{'fraction_non_alphanumeric': 0.06983240223463687, 'fraction_numerical': 0.0, 'mean_word_length': 2.496513249651325, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '124759', 'n_tokens_mistral': 709, 'n_tokens_neox': 669, 'n_words': 219}