Skip to Content Skip to Search
Methods
A
E
M
N
O
P
S
T
Included Modules

Constants

ERROR_TRANSLATION_PATH = "member.payment_forms"
 

Attributes

[RW] billing
[RW] card_token
[RW] change
[RW] conversion
[RW] coupon_code
[RW] order
[RW] performed_by
[RW] plan
[RW] previous_plan
[RW] previous_subscription
[RW] site
[RW] site_owner
[RW] tracking_cookie

Class Public methods

new(args = {})

# File app/actions/action/plan_purchaser.rb, line 39
def initialize(args = {})
  @plan = args.fetch(:plan)
  @member = args.fetch(:member)
  @site_owner = args[:site_owner]

  if site_owner.nil? && member.of_memberspace?
    @site_owner = member.becomes(SiteOwner)
  end

  @billing = billing_klass.new(args)
  @previous_subscription = billing&.old_subscription&.dup
  @previous_plan = previous_subscription&.plan
  @conversion = args[:conversion]
  @site = args[:site] || @plan.site
  # TODO: Site can be nil, is confusing
  # From Github comments:
  # https://github.com/320ny/memberspace_platform/pull/4598#discussion_r709621091
  # The site being present or not is determined by
  # wether we are in the members engine or the admin engine side of things.
  # This also means that we don't have the site available when an admin runs the purchase
  @coupon_code = args[:coupon_code]
  @order = args[:order]
  @tracking_cookie = args[:tracking_cookie]
  @card_token = args[:card_token]

  @performed_by = args[:performed_by] || member.full_name
end

purchase( plan:, member:, payment_method: nil, site: nil, coupon: nil, coupon_code: nil, performed_by: nil )

# File app/actions/action/plan_purchaser.rb, line 16
def self.purchase(
  plan:,
  member:,
  payment_method: nil,
  site: nil,
  coupon: nil,
  coupon_code: nil,
  performed_by: nil
)
  instance = new(
    plan:,
    member:,
    card_token: payment_method,
    site: site || member.site,
    coupon_code: coupon_code || coupon.try(:code),
    performed_by: performed_by
  )

  instance.purchase!

  instance
end

Instance Public methods

after_successful_purchase()

# File app/actions/action/plan_purchaser.rb, line 93
def after_successful_purchase
  remove_pending_plan
  determine_change
  cancel_site_owner_subscriptions
  remove_should_upgrade_events
  enqueue_events
  sweep_invoice
  revert_any_downgraded_features
  true
rescue => e
  add_base_error(e)
  false
end

amount()

# File app/actions/action/plan_purchaser.rb, line 71
def amount
  billing.amount
end

enqueue_events()

# File app/actions/action/plan_purchaser.rb, line 150
def enqueue_events
  publish_new_membership
  publish_member_updated
  dispatch_events
  send_notifications
end

error()

# File app/actions/action/plan_purchaser.rb, line 133
def error
  # TODO: Legacy support ONLY - change collaborators to expect ActiveModel's #errors
  if order.present? && order.error.present?
    order.error
  else
    errors.full_messages.first
  end
end

member()

# File app/actions/action/plan_purchaser.rb, line 67
def member
  site_owner || @member
end

order_id()

# File app/actions/action/plan_purchaser.rb, line 129
def order_id
  ms_order.try(:order_id)
end

purchase!()

# File app/actions/action/plan_purchaser.rb, line 75
def purchase!
  return false unless valid?

  @order = billing.purchase

  return true if order.requires_action?
  return false unless order.success?

  # FIXME: Billing#purchase may mutate its own memory instance of member,
  # leaving our copy woefully out of sync!
  @member = @site_owner = member.reload

  after_successful_purchase
rescue Stripe::StripeError => e
  add_base_error(e)
  false
end

send_notifications()

# File app/actions/action/plan_purchaser.rb, line 107
def send_notifications
  if change
    send_change_membership_notification

    if trialing?
      send_plan_receipt_email
      send_trial_starting_email
    end
  else
    send_plan_receipt_email
    send_new_membership_notification

    if trialing?
      send_trial_starting_email
    end
  end
end

status()

# File app/actions/action/plan_purchaser.rb, line 142
def status
  if order.present?
    order.status
  else
    :conflict
  end
end

trialing?()

# File app/actions/action/plan_purchaser.rb, line 125
def trialing?
  stripe_order&.status == "trialing"
end