Here are the requirements:
- If
initial_invoice?
isfalse
,existing_invoice
always exists. - If
initial_invoice?
istrue
andexisting_invoice
exists, I'd love to use theexisting_invoice
.
I'd love to refactor this code.
existing_invoice = find_existing_invoice(invoice_obj[:id])
invoice = if !initial_invoice?(invoice_obj) || existing_invoice.present?
existing_invoice
else
ac = Account.find_by(stripe_customer_id: invoice_obj[:customer])
create_initial_invoice(invoice_obj, ac)
end
private
def find_existing_invoice(id)
Invoice.find_by(stripe_invoice_id: id)
end
def initial_invoice?(invoice_obj)
sub_id = invoice_obj[:subscription]
return false if sub_id.nil?
sub = Stripe::Subscription.retrieve(sub_id)
return false unless invoice_obj[:period_start] == sub[:created]
true
end