Search code examples
ruby-on-rails-4stripe-paymentscoupon

Rails : Stripe : Updating existing subscription with new coupon code


I have a subscription and if I update it with coupon how is the coupon going to apply? Client has already paid the amount and now I am going to apply a 100% discount coupon by editing from my admin dashboard.

How is this handled?

thanks


Solution

  • This is how I did.

    First I updated the customer's subscription:

    customer = Stripe::Customer.retrieve(customer_id)
    subscription = customer.retrieve(subscription_id)
    subscription.coupon = "coupon_id"
    subscription.save
    

    The customer's subscription is then updated with details of the coupon in the discount hash.

    Then I manually refunded the charge object of that customer(if the coupon is 100% discount coupon).

    charge = customer.retrieve(stripe_charge_id)
    refund = charge.refund
    

    This then updates the charge object with amount_refunded with the discounted amount after applying the coupon. Also refunded is set to true with updated refunds hash.

    You can also create a refund for certain amount by passing the amount, like:

    re = Stripe::Refund.create(  
                               charge: charge_id,
                               amount: amount_you_want_to_refund
                              )
    

    For the upcoming invoices, the invoice is created for that discounted amount.