class CashOut::Charge::Create

Public Instance Methods

execute() click to toggle source
# File lib/cash_out/charge/create.rb, line 11
def execute
  create_charge
end

Private Instance Methods

charge_payor(*args) click to toggle source
# File lib/cash_out/charge/create.rb, line 46
def charge_payor(*args)
  Stripe::Charge.create(*args)
rescue *STRIPE_ERRORS => e
  errors.add(:stripe, e.to_s)
  e.json_body
end
charges_are_positive() click to toggle source
# File lib/cash_out/charge/create.rb, line 78
def charges_are_positive
 # Charge will fail if balance due is $0.00
 # Charge should never be negative
 unless amount_to_charge.positive?
   errors.add(:stripe,I18n.t('cash_out.charge.invalid_charge_amount'))
 end
end
create_charge() click to toggle source
# File lib/cash_out/charge/create.rb, line 17
def create_charge
  # Stripe won't let you create a destination charge if the payout exceeds the charges
  payout_less_than_charge? ? destination_charge : standalone_charge
end
destination_charge() click to toggle source
# File lib/cash_out/charge/create.rb, line 26
def destination_charge
  # This is what we should do in most cases
  # amount_to_charge is charged to payor
  # amount_to_payout goes to payee
  # Remainder is kept in company Stripe account
  # https://stripe.com/docs/connect/destination-charges#collecting-platform-fees
  return errors.add(:stripe, I18n.t('cash_out.charge.no_payee')) unless payee
  charge_payor(destination_charge_params)
end
destination_charge_params() click to toggle source
# File lib/cash_out/charge/create.rb, line 63
def destination_charge_params
  # Charges the payor the total amount owed
  # Sends payout amount to payee
  {
    amount: amount_to_charge,
    currency: "usd",
    description: "",
    customer: payor.stripe_id,
    destination: {
      account: payee.stripe_id,
      amount: amount_to_payout
    }
  }
end
initiate_transfer() click to toggle source
# File lib/cash_out/charge/create.rb, line 42
def initiate_transfer
  CashOut::Connect::Transfer::Create.run(payee: payee, amount_to_payout: amount_to_payout)
end
payout_less_than_charge?() click to toggle source
# File lib/cash_out/charge/create.rb, line 22
def payout_less_than_charge?
  amount_to_payout.positive? && amount_to_payout < amount_to_charge
end
standalone_charge() click to toggle source
# File lib/cash_out/charge/create.rb, line 36
def standalone_charge
  # When dealing with a negative payout, we must create the charge and transfer separately
  charge_payor(standalone_charge_params)
  initiate_transfer
end
standalone_charge_params() click to toggle source
# File lib/cash_out/charge/create.rb, line 53
def standalone_charge_params
  # Charges the payor the total amount owed
  {
    amount: amount_to_charge,
    currency: "usd",
    description: "",
    customer: payor.stripe_id
  }
end