#big-sister_api

1 messages ¡ Page 1 of 1 (latest)

frosty sandBOT
#

👋 Welcome to your new thread!

⏲️ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.

⏱️ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.

🔗 This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1219425682577166497

📝 Have more to share? Add more details, code, screenshots, videos, etc. below.

loud pumiceBOT
gaunt osprey
dark nebula
#

list all coupons doesnt appear that it would return information about invoices or subscriptions

gaunt osprey
#

That is correct, it would not.

dark nebula
#

whats the recommended approach? surely businesses want to be able to measure the revenue associated with a coupon?

gaunt osprey
#

did listing all invoices and detecting for if the coupon used work?

dark nebula
#

not yet

gaunt osprey
#

I would recommend trying that first. Let me know if that does not work.

loud pumiceBOT
dark nebula
#
def revenue_by_month_for_coupon(coupon_name)
  puts "Fetching all invoices..."
  all_invoices = Stripe::Invoice.list # No limit specified

  # Initialize a hash to store revenue by month for the specified coupon
  revenue_by_month = Hash.new(0)
  
  all_invoices.auto_paging_each do |invoice|
    # Check if the invoice has the discount applied and matches the specified coupon name
    if invoice.discount&.coupon&.name == coupon_name
      invoice_month = Time.at(invoice.created).strftime("%Y-%m")
      actual_payment = invoice.amount_paid / 100.0 # Assuming amount_paid is in cents
      revenue_by_month[invoice_month] += actual_payment
      
      puts "Invoice ID: #{invoice.id}, Coupon: #{coupon_name}, Month: #{invoice_month}, Actual Payment: $#{actual_payment}"
    end
  end

  # Output the total revenue by month for the specified coupon
  revenue_by_month.each do |month, revenue|
    puts "Month: #{month}, Total Revenue from Coupon '#{coupon_name}': $#{revenue}"
  end

  return revenue_by_month
end
#

this seems to work at least with our limited data set as a solution for coupon -> revenue that coupon has been associated with

robust sinew
#

that's one way bit Invoices can have multiple discounts

dark nebula
#

doesnt seem to be coming along as part of the response

#

maybe it needs to be requested?

robust sinew
#

yes it's what we call "includable" so it's not returned by default

dark nebula
#

i think for my use case- i want to measure the total revenue of invoices where a coupon appeared, so if there was another coupon i still want to only count the amount paid

#

in this case- its configured right unless im misunderstanding your suggestion

robust sinew
#

One Invoice can have 3 separate Coupons on it. Doing this by looking at invoice.discount won't work in that case because you'd only find one

loud pumiceBOT
robust sinew
#

sorry have to run @little vapor is taking over

dark nebula
#

is there a way to specify that only one coupon can be used?

#

i think we might have that in place already

little vapor
#

You're asking how to configure an Invoice to only use a single coupon?

dark nebula
#

im asking how to prevent the scenario in which invoice.discount wont work

little vapor
#

If you want to make sure Invoices only pass in a single coupon that'd be something you'd have to control in the Invoice creation code on your end - but is there a reason you don't want to use invoice.discounts?