#mochihealth
1 messages · Page 1 of 1 (latest)
Hi there
so the process is that we have two pages one to choose the price which creates a payment intent and then the next page is where you pay and would apply any discount codes
So sounds like you want to wait to create your Subscription until the second page then
Not sure exactly what you mean by checking if the promo code is valid
Can you clarify?
like if someone enters GET20OFF I want to validate that it could be applied and show the updated price
So you want to know ahead of actually creating the Sub?
well maybe like after the sub is created but before they pay
If so, you can use the Upcoming Invoice endpoint to simulate creating the Sub: https://stripe.com/docs/api/invoices/upcoming
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
You won't be able to add the promo code after you create the Sub
Since that will finalize the initial invoice
so how can I make the coupon work in my flow
im confused
because I need to create a payment intent at least for first time buyers since there isn't any card on file that I can just attach when I would just create a subscription?
but there wouldnt be an upcoming invoice if I didn't create a subscription no?
You can use the above endpoint to simulate creating a Sub via the subscription_items param
but how about the whole payment intent flow for creating subscriptions
A Subcription creates an invoice which creates a PaymentIntent
Can you clarify where you are confused?
I would have to get rid of it if i only wanted to show the coupon code box after they choose a price and were on the page to pay
Get rid of what?
that entire flow where a paymentintent is created, returned to the frontend and then confirmed on payment
The Sub creates the PaymentIntent for you
You shouldn't be creating a PaymentIntent directly if you are using Subscriptions
You would create the Sub and then grab the latest_invoice.payment_intent.client_secret and pass that to the frontend
yea sorry thats what im talking about
but the client secret needs to be returned before the payment page
and the coupon code would be on the payments page
or would it have to be moved to the first page
Why does it need to be returned before the payment page?
No you can just render Payment Element (create the Sub and pass the client_secret) based on some user action on your payment page.
Like ask them to confirm their choice from the previous page and offer them to enter promo code
Or you can handle the promo code on the previous page
Up to you
this is the first page
this is the next
whre the coupon code would theoretically live
Yep that's fine
So you hide Payment Element and have some sort of button like "add card details" or something
And then when they click that you create the Sub and render Payment Element
That's just an example
But you basically want a user action after they have had the ability to add a promo code to then create the Sub
Use the Upcoming Invoice endpoint like I showed above
Then you can add the discounted price to your UI
but the upcoming invoice endpoint can only be used after the subscription was created right
so like I want to show an error if the code they entered wasnt valide before the subscription is created
oh
it says retrieve an upcoming invoice though
there has to be some invoice created to retrieve right
so how do you do that without creating the subscription
Did you look at the above link? It says there under the subscription param:
The identifier of the subscription for which you’d like to retrieve the upcoming invoice. If not provided, but a subscription_items is provided, you will preview creating a subscription with those items. If neither subscription nor subscription_items is provided, you will retrieve the next upcoming invoice from among the customer’s subscriptions.
Yep! Should do the trick
also is there a way to retrieve a promotion code by the name of it because i only see that it can be retrieved by its id
like how would I grab the coupon id to apply after someone inputs a promotion code
ok and lets say the code had a restriction that it applies to only the first purchase would it throw an error if I tried to preview that with the upcoming invoices endpoint when I put it in for a certain customer?
that had previous purchases
I believe so but can't remember off the top of my head. I'd recommend testing that out