Sha256: 84f745849755b1a2b6a133a21a4089d220932b497f87aa633b7b35fdb80d9cc2

Contents?: true

Size: 1.23 KB

Versions: 10

Compression:

Stored size: 1.23 KB

Contents

module Pay
  module Stripe
    module Webhooks

      class ChargeSucceeded
        def call(event)
          object = event.data.object
          user   = Pay.user_model.find_by(
            processor: :stripe,
            processor_id: object.customer
          )

          return unless user.present?
          return if user.charges.where(processor_id: object.id).any?

          charge = create_charge(user, object)
          notify_user(user, charge)
          charge
        end

        def create_charge(user, object)
          charge = user.charges.find_or_initialize_by(
            processor:      :stripe,
            processor_id:   object.id,
          )

          charge.update(
            amount:         object.amount,
            card_last4:     object.source.last4,
            card_type:      object.source.brand,
            card_exp_month: object.source.exp_month,
            card_exp_year:  object.source.exp_year,
            created_at:     Time.zone.at(object.created)
          )

          charge
        end

        def notify_user(user, charge)
          if Pay.send_emails && charge.respond_to?(:receipt)
            Pay::UserMailer.receipt(user, charge).deliver_later
          end
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
pay-1.0.2 lib/pay/stripe/webhooks/charge_succeeded.rb
pay-1.0.1 lib/pay/stripe/webhooks/charge_succeeded.rb
pay-1.0.0 lib/pay/stripe/webhooks/charge_succeeded.rb
pay-1.0.0.rc3 lib/pay/stripe/webhooks/charge_succeeded.rb
pay-1.0.0.rc2 lib/pay/stripe/webhooks/charge_succeeded.rb
pay-1.0.0.rc1 lib/pay/stripe/webhooks/charge_succeeded.rb
pay-1.0.0.beta5 lib/pay/stripe/webhooks/charge_succeeded.rb
pay-1.0.0.beta4 lib/pay/stripe/webhooks/charge_succeeded.rb
pay-1.0.0.beta3 lib/pay/stripe/webhooks/charge_succeeded.rb
pay-1.0.0.beta2 lib/pay/stripe/webhooks/charge_succeeded.rb