Class: Pay::PaddleClassic::Webhooks::SubscriptionPaymentSucceeded

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

Instance Method Summary collapse

Instance Method Details

#call(event) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/pay/paddle_classic/webhooks/subscription_payment_succeeded.rb', line 5

def call(event)
  pay_customer = Pay::Customer.find_by(processor: :paddle_classic, processor_id: event.user_id)

  if pay_customer.nil?
    owner = Pay::PaddleClassic.owner_from_passthrough(event.passthrough)
    pay_customer = owner&.set_payment_processor :paddle_classic, processor_id: event.user_id
  end

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

  return if pay_customer.charges.where(processor_id: event.subscription_payment_id).any?

  pay_charge = create_charge(pay_customer, event)
  notify_user(pay_charge)
end

#create_charge(pay_customer, event) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/pay/paddle_classic/webhooks/subscription_payment_succeeded.rb', line 24

def create_charge(pay_customer, event)
  payment_method_details = Pay::PaddleClassic::PaymentMethod.payment_method_details_for(subscription_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,
    paddle_receipt_url: event.receipt_url,
    subscription: pay_customer.subscriptions.find_by(processor_id: event.subscription_id),
    metadata: Pay::PaddleClassic.parse_passthrough(event.passthrough).except("owner_sgid")
  }.merge(payment_method_details)

  pay_charge = pay_customer.charges.find_or_initialize_by(processor_id: event.subscription_payment_id)
  pay_charge.update!(attributes)

  # Update customer's payment method
  Pay::PaddleClassic::PaymentMethod.sync(pay_customer: pay_customer, attributes: payment_method_details)

  pay_charge
end

#notify_user(pay_charge) ⇒ Object



45
46
47
48
49
# File 'lib/pay/paddle_classic/webhooks/subscription_payment_succeeded.rb', line 45

def notify_user(pay_charge)
  if Pay.send_email?(:receipt, pay_charge)
    Pay.mailer.with(pay_customer: pay_charge.customer, pay_charge: pay_charge).receipt.deliver_later
  end
end