Skip to Content Skip to Search
Methods
F
L
N
R
V

Attributes

[R] stripe_data

Class Public methods

new(stripe_data, member = nil)

# File engines/api/app/actions/api/failed_payment_recorder.rb, line 5
def initialize(stripe_data, member = nil)
  @stripe_data = stripe_data
  @member = member if stripe_data.object == "charge"
end

Instance Public methods

failed_payment()

# File engines/api/app/actions/api/failed_payment_recorder.rb, line 60
def failed_payment
  @failed_payment ||= DB::FailedPayment.find_or_initialize_by(payment_gateway_id: stripe_data.id)
end

log_failed_payment()

# File engines/api/app/actions/api/failed_payment_recorder.rb, line 10
def log_failed_payment
  # Failed payments are tied to subscriptions/members
  return unless subscription && member

  # If subscription is incomplete, we're dealing with the initial payment.
  # This payment isn't subject to regular dunning as it will expire
  # 23 hours after creation.
  return if subscription.incomplete_status?

  failed_payment.assign_attributes(failed_payment_attrs) if failed_payment.new_record?
  failed_payment.invoice_data = invoice_data

  if stripe_data.next_payment_attempt.nil?
    failed_payment.churned_status!
  else
    failed_payment.save!
  end

  update_subscription_canceled_reason
end

recover!()

# File engines/api/app/actions/api/failed_payment_recorder.rb, line 31
def recover!
  return false unless subscription

  if failed_payment.persisted?
    update_failed_payment
  else
    failed_payment.assign_attributes(failed_payment_attrs)
    recover_abandoned
  end
end

recover_abandoned_charge()

# File engines/api/app/actions/api/failed_payment_recorder.rb, line 48
def recover_abandoned_charge
  return unless charge && abandoned_conversion_event

  member.failed_payments.recovered_abandoned_signup_status.create!(
    site: member.site,
    payment_gateway_id: stripe_data.id,
    subscription_id: "recovered_abandoned_charge",
    amount_due: DB::FailedPayment.recovery_amount(charge.amount, plan.amount),
    currency: plan.currency
  )
end

void_or_uncollectable()

# File engines/api/app/actions/api/failed_payment_recorder.rb, line 42
def void_or_uncollectable
  # We only want to update a failed payment if it already exists.  We don't want
  # to create a new failed payment if it wasn't already on the invoice.
  failed_payment.public_send("#{stripe_data.status}_status!") if failed_payment.persisted?
end