#ripc0rd
1 messages ยท Page 1 of 1 (latest)
Hello there
payment_intent = stripe.PaymentIntent.retrieve(payment_intent_id)
# Set the default payment method
stripe.Subscription.modify(
subscription_id,
default_payment_method=payment_intent.payment_method
)
or do I need to do that in the webhook?
My challenge is that the frontend (Vue.js) won't know when the webhook was triggered for a given status
any suggestions to help on the matter will be greatly appreciated```
We actually have a parameter that will do this for you when you create the Sub
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
As the guide suggests I am creating a Sub as follows:
# Set your secret key. Remember to switch to your live secret key in production.
# See your keys here: https://dashboard.stripe.com/apikeys
stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
@app.route('/create-subscription', methods=['POST'])
def create_subscription():
data = json.loads(request.data)
customer_id = data['customerId']
price_id = data['priceId']
try:
# Create the subscription. Note we're expanding the Subscription's
# latest invoice and that invoice's payment_intent
# so we can pass it to the front end to confirm the payment
subscription = stripe.Subscription.create(
customer=customer_id,
items=[{
'price': price_id,
}],
payment_behavior='default_incomplete',
payment_settings={'save_default_payment_method': 'on_subscription'},
expand=['latest_invoice.payment_intent'],
)
return jsonify(subscriptionId=subscription.id, clientSecret=subscription.latest_invoice.payment_intent.client_secret)
except Exception as e:
return jsonify(error={'message': e.user_message}), 400```
I then collect payment info using Card element in the Frontend(Vue.js). Once the paymentIntent returns success in the Frontend(Vue.js) I then need to update the database via the backend (Python-Django) with the updated plan and use an async method in the front end to wait once the DB is updated. The async method won't work with the webhook so what is the best practice?
Hmm I'm not sure I fully understand
Why do you need to wait for the database to be updated in the frontend?
My apologies for not being clear
Let me try again
I have a page on the front end that shows what plan the customer is on. The default when they sign-up is a Free plan
Once they click on upgrade they are taken to the below page
Once they select a plan I create a Sub as mentioned in the guide as follows:
subscription = stripe.Subscription.create(
customer=customer_id,
items=[{
'price': price_id,
}],
payment_behavior='default_incomplete',
payment_settings={'save_default_payment_method': 'on_subscription'},
expand=['latest_invoice.payment_intent'],
)```
I send the client secret in the response
return Response({"subscriptionId": subscription.id,
"clientSecret":subscription.latest_invoice.payment_intent.client_secret})```
this is then forwarded to the payment page
Once the payment card is collected and is successful I want to redirect back to the Plan page with the updated plan
But the challenge is based on the guide it is instructing me to provision the service in the webhook ->https://stripe.com/docs/billing/subscriptions/build-subscriptions?ui=elements&element=card#provision-access
Got it
Okay so you basically want to use the Webhook to actually provision in your database. This is the safest way in case the customer drops off before they reach the redirect
However, you can also redirect the customer immediately client-side upon the confirmCardPayment promise resolving successfully
You don't need to wait for your database for that
I actually need the DB to update the clients Plan info -> because the Plan page calls the DB upon mounting to get the plan details
and that's my dilemma
In the backend I need this code to run
stripe.api_key = settings.STRIPE_SECRET_KEY
error = ""
try:
team = Team.objects.filter(created_by=request.user).first()
subscription = stripe.Subscription.retrieve(team.stripe_subscription_id)
product = stripe.Product.retrieve(subscription.plan.product)
team.plan_status = Team.PLAN_ACTIVE
team.plan_end_date = datetime.fromtimestamp(subscription.current_period_end)
team.plan = Plan.objects.get(name=product.name)
team.save()
serializer = TeamSerializer(team)
return Response(serializer.data)
except Exception:
error = "There is something wrong. Please try again!"
return Response({"error": error})```
This will update the client's team info in the DB
In that case you want to pass the Plan info to the frontend as well when you confirm the PaymentIntent
And not rely on the fetching your database for that
is the plan already updated in Stripe once the PaymentIntent is successful in the Sub object?
I am encountering race issues -- the redirect is happening before the webhook updates the subscription object
Hi there ๐ taking over, as my colleague needs to step away
Give me a few minutes to get caught up.
thanks
It sounds like the ideal goal here would be to include a loading spinner that spins until you receive the webhook for the update.
is that possible?
how can my frontend wait for a specific event in the webhook?
i am using Vue.js for my front end
and Django(Python)
for the backend
๐ stepping back in ๐
Yeah the spinner works well too if you want to go that route. In this case you would want to poll your database until there is an update
Or, you can wait for the Webhook and then just have that send a response to your frontend when the Webhook is received.
No I don't really have an example as this isn't Stripe-specific at this point
I'd suspect you could google for some examples of how to poll your backend
Or how to create a listener on your frontend
yes that will be my next step
its my first time trying stripe
i am excited to get this working
Happy to hear it! Let us know if we can help any further with the Stripe-specific parts!