skip to Main Content

I have a register page, and when someone fills out the form and clicks submit, he will save his data in MongoDB, generate a token, set payStatus to false automatically, and redirect me to the payment page of the Stripe API. Everything works.

But when I want the user to pay in the payment page, I want to know who paid to set his pay status in MongoDB to true. I don’t know how I can do it, and please, if there is another good logic to refuse any login without registering and paying, answer me.

2

Answers


  1. I think you can pass the user’s unique identifier along with the payment request to Stripe API. Then, when the payment is confirmed, Stripe will send a webhook to your server indicating that the payment is successful. You can then update the user’s pay status in MongoDB to true using the unique identifier that was included in the payment request.

    To implement this, you will need to:

    Save the user’s unique identifier (e.g., user ID, email address) in MongoDB when they register and generate a token. ✅

    Pass the user’s unique identifier along with the payment request to Stripe API using the metadata parameter.
    Check out – https://stripe.com/docs/api/metadata

    Set up a webhook endpoint on your server to receive events from Stripe API.
    When your server receives a charge.succeeded event from Stripe API, retrieve the user’s unique identifier from the metadata of the event, and use it to update their pay status in MongoDB to true.

    Login or Signup to reply.
  2. I agree with Allan that you can reconcile with the metadata that you set on the Stripe object.

    Depends on which API you use, you might want to listen to different webhook event in order to retrieve the metadata.

    • Checkout Sessions -> listen to checkout.session.completed
    • PaymentIntents -> listen to payment_intent.succeeded
    • Charges (you shouldn’t use it since it’s deprecated) -> listen to charge.succeeded

    Note that a charge.succeeded event is also generated even if you are using Checkout Sessions or PaymentIntents API. However, you can only get the metadata that you set from the original CheckoutSession of PaymentIntent object, not from the Charge object.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search