Class: Pay::Stripe::Webhooks::ChargeSucceeded

Inherits:
Object
  • Object
show all
Defined in:
lib/pay/stripe/webhooks/charge_succeeded.rb

Instance Method Summary collapse

Instance Method Details

#call(event) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/pay/stripe/webhooks/charge_succeeded.rb', line 5

def call(event)
  object = event.data.object
  billable = Pay.find_billable(processor: :stripe, processor_id: object.customer)

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

  create_charge(billable, object)
end

#create_charge(billable, object) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pay/stripe/webhooks/charge_succeeded.rb', line 15

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

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

  notify_user(billable, charge)

  charge
end

#notify_user(billable, charge) ⇒ Object



35
36
37
38
39
# File 'lib/pay/stripe/webhooks/charge_succeeded.rb', line 35

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