#onggiabietbay_code

1 messages ยท Page 1 of 1 (latest)

shadow ibexBOT
#

๐Ÿ‘‹ 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.

sharp schooner
#

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
royal crater
#

I see you're applying Coupon to the one-off Invoice and not the Subscription, why?

sharp schooner
#

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
royal crater
#

Could you please share the Subscription ID?

sharp schooner
#

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)

royal crater
sharp schooner
#

I see

royal crater
#

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.

#

Hence the 2 Invoices

sharp schooner
#

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:

sharp schooner
royal crater
sharp schooner
#

ok lemme try

royal crater
sharp schooner
#

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'
    })
royal crater
#

No, just the amount is fine.

sharp schooner
#

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
shadow ibexBOT
royal crater
#

Yes, how did it attach to the Invoice before?

sharp schooner
#

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'
    })

fathom flame
#

๐Ÿ‘‹ taking over for my colleague. Let me know if there's any follow-up Qs I can answer!

sharp schooner
#

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 ๐Ÿ˜„

sharp schooner
#

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