Class: Pay::Paddle::Webhooks::SubscriptionPaymentSucceeded

Inherits:
Object
  • Object
show all
Defined in:
lib/pay/paddle/webhooks/subscription_payment_succeeded.rb

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ SubscriptionPaymentSucceeded

Returns a new instance of SubscriptionPaymentSucceeded.



5
6
7
8
9
10
11
12
# File 'lib/pay/paddle/webhooks/subscription_payment_succeeded.rb', line 5

def initialize(data)
  billable = Pay.find_billable(processor: :paddle, processor_id: data["user_id"])
  return unless billable.present?
  return if billable.charges.where(processor_id: data["subscription_payment_id"]).any?

  charge = create_charge(billable, data)
  notify_user(billable, charge)
end

Instance Method Details

#create_charge(user, data) ⇒ Object



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

def create_charge(user, data)
  charge = user.charges.find_or_initialize_by(
    processor: :paddle,
    processor_id: data["subscription_payment_id"]
  )

  params = {
    amount: Integer(data["sale_gross"].to_f * 100),
    card_type: data["payment_method"],
    paddle_receipt_url: data["receipt_url"],
    created_at: Time.zone.parse(data["event_time"])
  }

  payment_information = user.paddle_payment_information(data["subscription_id"])

  charge.update(params.merge(payment_information))
  user.update(payment_information)

  charge
end

#notify_user(billable, charge) ⇒ Object



35
36
37
38
39
# File 'lib/pay/paddle/webhooks/subscription_payment_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