#ripc0rd

1 messages ยท Page 1 of 1 (latest)

wraith adderBOT
desert siren
#

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

wintry bobcat
#

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?
desert siren
#

Hmm I'm not sure I fully understand

#

Why do you need to wait for the database to be updated in the frontend?

wintry bobcat
#

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

desert siren
#

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

wintry bobcat
#

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

desert siren
#

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

wintry bobcat
#

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

dusk lion
#

Hi there ๐Ÿ‘‹ taking over, as my colleague needs to step away

Give me a few minutes to get caught up.

wintry bobcat
#

thanks

dusk lion
#

It sounds like the ideal goal here would be to include a loading spinner that spins until you receive the webhook for the update.

wintry bobcat
#

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

desert siren
#

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

wintry bobcat
#

is there an example of how to do the latter

#

that would be great

desert siren
#

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

wintry bobcat
#

yes that will be my next step

#

its my first time trying stripe

#

i am excited to get this working

desert siren
#

Happy to hear it! Let us know if we can help any further with the Stripe-specific parts!