Class: Billy::Paddle::Webhooks::SubscriptionPaymentSucceeded
- Inherits:
-
Object
- Object
- Billy::Paddle::Webhooks::SubscriptionPaymentSucceeded
- 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 = Billy::Account.find_by(processor: "paddle", processor_id: event.user_id) if billy_account.nil? owner = Billy::Paddle.owner_from_passthrough(event.passthrough) billy_account = owner&.set_billy_account :paddle, processor_id: event.user_id end if billy_account.nil? Rails.logger.error("[Billy] Unable to find Billy::Account with: '#{event.passthrough}'") return end return if billy_account.charges.where(processor: "paddle", processor_id: event.subscription_payment_id).any? charge = create_charge(billy_account, event) # Send Discord Event billy_account.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(billy_account, 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: billy_account.subscriptions.find_by(processor: "paddle", processor_id: event.subscription_id) }.merge(payment_method_details) charge = billy_account.charges.create(processor: "paddle", processor_id: event.subscription_payment_id) charge.update!(attributes) # Update customer's payment method Billy::PaymentMethod.sync(billy_account: billy_account, attributes: payment_method_details) charge end |