Sha256: 91abb8c5c52f89a02a7cdb0f026b5832ceea6b9541a4ce9c5350e5d284b43523

Contents?: true

Size: 1.97 KB

Versions: 17

Compression:

Stored size: 1.97 KB

Contents

module Pay
  module Paddle
    module Webhooks
      class SubscriptionPaymentSucceeded
        def call(event)
          pay_customer = Pay::Customer.find_by(processor: :paddle, processor_id: event.user_id)

          if pay_customer.nil?
            owner = Pay::Paddle.owner_from_passthrough(event.passthrough)
            pay_customer = owner&.set_payment_processor :paddle, 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?

          charge = create_charge(pay_customer, event)
          notify_user(pay_customer.owner, charge)
        end

        def create_charge(pay_customer, event)
          payment_method_details = Pay::Paddle::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::Paddle.parse_passthrough(event.passthrough).except("owner_sgid")
          }.merge(payment_method_details)

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

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

          charge
        end

        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
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
pay-3.0.17 lib/pay/paddle/webhooks/subscription_payment_succeeded.rb
pay-3.0.16 lib/pay/paddle/webhooks/subscription_payment_succeeded.rb
pay-3.0.15 lib/pay/paddle/webhooks/subscription_payment_succeeded.rb
pay-3.0.14 lib/pay/paddle/webhooks/subscription_payment_succeeded.rb
pay-3.0.13 lib/pay/paddle/webhooks/subscription_payment_succeeded.rb
pay-3.0.12 lib/pay/paddle/webhooks/subscription_payment_succeeded.rb
pay-3.0.11 lib/pay/paddle/webhooks/subscription_payment_succeeded.rb
pay-3.0.10 lib/pay/paddle/webhooks/subscription_payment_succeeded.rb
pay-3.0.9 lib/pay/paddle/webhooks/subscription_payment_succeeded.rb
pay-3.0.8 lib/pay/paddle/webhooks/subscription_payment_succeeded.rb
pay-3.0.7 lib/pay/paddle/webhooks/subscription_payment_succeeded.rb
pay-3.0.6 lib/pay/paddle/webhooks/subscription_payment_succeeded.rb
pay-3.0.5 lib/pay/paddle/webhooks/subscription_payment_succeeded.rb
pay-3.0.4 lib/pay/paddle/webhooks/subscription_payment_succeeded.rb
pay-3.0.2 lib/pay/paddle/webhooks/subscription_payment_succeeded.rb
pay-3.0.1 lib/pay/paddle/webhooks/subscription_payment_succeeded.rb
pay-3.0.0 lib/pay/paddle/webhooks/subscription_payment_succeeded.rb