Class: Billy::Paddle::Webhooks::SubscriptionPaymentSucceeded

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

Instance Method Summary collapse

Instance Method Details

#call(event) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/billy/paddle/webhooks/subscription_payment_succeeded.rb', line 6

def call(event)
   = Billy::Account.find_by(processor: "paddle", processor_id: event.user_id)

  if .nil?
    owner = Billy::Paddle.owner_from_passthrough(event.passthrough)
     = owner&. :paddle, processor_id: event.user_id
  end

  if .nil?
    Rails.logger.error("[Billy] Unable to find Billy::Account with: '#{event.passthrough}'")
    return
  end

  return if .charges.where(processor: "paddle", processor_id: event.subscription_payment_id).any?

  charge = create_charge(, event)

  # Send Discord Event
  .discord(kind: :payment_succeeded, desc: "Amount: #{charge.amount_with_currency}")
end

#create_charge(billy_account, event) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/billy/paddle/webhooks/subscription_payment_succeeded.rb', line 27

def create_charge(, event)
  payment_method_details = Billy::PaymentMethod.paddle_details(processor_id: event.subscription_id)

  attributes = {
    amount: (event.sale_gross.to_f * 100).to_i,
    created_at: Time.zone.parse(event.event_time),
    currency: event.currency,
    method: event.payment_method,
    receipt_url: event.receipt_url,
    subscription: .subscriptions.find_by(processor: "paddle", processor_id: event.subscription_id)
  }.merge(payment_method_details)

  charge = .charges.create(processor: "paddle", processor_id: event.subscription_payment_id)
  charge.update!(attributes)

  # Update customer's payment method
  Billy::PaymentMethod.sync(billy_account: , attributes: payment_method_details)

  charge
end