#onggiabietbay_code
1 messages ยท Page 1 of 1 (latest)
๐ 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/1271381781186215947
๐ Have more to share? Add more details, code, screenshots, videos, etc. below.
This is the other method
def charge_customer_for_early_renewal(_plan, coupon)
amount = Stripe::Plan.retrieve(@subscription.plan.id).amount
# Create an invoice item for the early renewal
Stripe::InvoiceItem.create({
customer: @customer.stripe_cus_id,
amount: amount,
currency: 'sgd',
description: 'Early renewal for the next billing cycle'
})
invoice = Stripe::Invoice.create({
customer: @customer.stripe_cus_id,
subscription: @customer.beyond_sub_id,
discounts: [{ coupon: coupon }],
auto_advance: false, # turn off auto charge incase of failure
description: 'Immediate payment charge for Early renewal'
})
invoice.pay
invoice
end
def update_subscription_metadata(subscription_id)
updated_subscription = Stripe::Subscription.update(
subscription_id,
metadata: {
early_renew: true
}
)
end
I see you're applying Coupon to the one-off Invoice and not the Subscription, why?
Because customer will also have discount when they buy this "Early Renewal" feature, so I apply discount to the invoice then pay this invoice. After that I will apply a 100% discount for their existing subscription so the next invoice will count as free
def create_100_percent_discount_coupon
coupon_params = {
percent_off: 100,
duration: 'once',
name: "100_percent_discount_#{Time.now.to_i}"
}
next_cycle_coupon = Stripe::Coupon.create(coupon_params)
return next_cycle_coupon
end
def apply_100_percent_discount_for_next_cycle
# Create a coupon with a 100% discount for the next billing cycle
next_cycle_coupon = create_100_percent_discount_coupon
# Apply the 100% discount coupon to the subscription for the next billing cycle
apply_coupon_to_subscription(next_cycle_coupon, @customer.beyond_sub_id)
puts '100% discount coupon applied for the next billing cycle.'
end
def apply_coupon_to_subscription(coupon, subscription_id)
Stripe::Subscription.update(
subscription_id,
{
coupon: coupon.id,
cancel_at_period_end: false
}
)
@user.clear_subscription_cache
puts 'Coupon applied to the subscription for the next billing cycle.'
end
Could you please share the Subscription ID?
here sub_1LV86cEZMMs54EecI1fkUEyo
I'm not sure why it sometimes creates two invoices and invoice items.
I would really appreciate your help with this; Iโve been struggling with it this morning. XD
not sure if it because I'm using a old Stripe version (2018)
This is sometimes a problem, but this wouldn't create double Invoices.
I see
To simplify things, I wouldn't include subscription parameter when you create the one-off Invoice.
Just make sure when this standalone Invoice is paid - apply a coupon to the Subscription.
I see you made 2 requests to create an Invoice:
https://dashboard.stripe.com/logs/req_PHH3MG6fWiTm0k
https://dashboard.stripe.com/logs/req_lQSLDPwZJNMG64
Hence the 2 Invoices
Yep, that's the part I'm not sure why it happens. My whole method only includes this function to create the invoice and invoice_item once:
ah, so I have to remove the subscription parameter in this place right?
invoice = Stripe::Invoice.create({
customer: @customer.stripe_cus_id,
subscription: @customer.beyond_sub_id, <-- here
discounts: [{ coupon: coupon }],
auto_advance: false, # turn off auto charge incase of failure
description: 'Immediate payment charge for Early renewal'
})
Yes, it's not necessary and can produce unexpected behavior.
ok lemme try
You have to check your code to see why this could be happening.
btw, if I remove that subscription part, how do i assign the price to charge the invoice?
Stripe::InvoiceItem.create({
customer: @customer.stripe_cus_id,
amount: amount,
currency: 'sgd',
description: 'Early renewal for the next billing cycle'
})
invoice = Stripe::Invoice.create({
customer: @customer.stripe_cus_id,
discounts: [{ coupon: coupon }],
auto_advance: false, # turn off auto charge incase of failure
description: 'Immediate payment charge for Early renewal'
})
No, just the amount is fine.
if I create invoice first then assign invoice.id to invoice_item, will it work?
def charge_customer_for_early_renewal(_plan, coupon)
amount = Stripe::Plan.retrieve(@subscription.plan.id).amount
# Create an invoice item for the early renewal
invoice = Stripe::Invoice.create({
customer: @customer.stripe_cus_id,
discounts: [{ coupon: coupon }],
auto_advance: false, # turn off auto charge incase of failure
description: 'Immediate payment charge for Early renewal'
})
Stripe::InvoiceItem.create({
invoice: invoice.id,
customer: @customer.stripe_cus_id,
amount: amount,
currency: 'sgd',
description: 'Early renewal for the next billing cycle'
})
invoice.pay
invoice
end
Yes, how did it attach to the Invoice before?
before it was like this, and i can't assign the invoice.id to invoice_item:
Stripe::InvoiceItem.create({
customer: @customer.stripe_cus_id,
amount: amount,
currency: 'sgd',
description: 'Early renewal for the next billing cycle'
})
invoice = Stripe::Invoice.create({
customer: @customer.stripe_cus_id,
subscription: @customer.beyond_sub_id,
discounts: [{ coupon: coupon }],
auto_advance: false, # turn off auto charge incase of failure
description: 'Immediate payment charge for Early renewal'
})
i think this worked
๐ taking over for my colleague. Let me know if there's any follow-up Qs I can answer!
Okay, thanks, guys. I will do a few tests and apply a hotfix to production to see how it goes. Dealing with a dozen people with duplicate invoices on Friday is driving me crazy ๐
sorry for bother you @fathom flame I think my issue still there, I saw a new customer with a duplicate invoices again. This is his subscription id:
sub_1LdlGEEZMMs54EecBTDMe4WC
I'm unsure why my code keep creating two invoices and charge twice sometimes