Rob Winters
Rob Winters

Reputation: 161

Stripe Checkout- How to pass a value through to webhook?

How can I pass a custom value (in this case a user ID from Firebase) through to a Stripe webhook?

I'm trying to associate a charge.succeeded event with the user ID so I can write the value to the DB from the node server and change the user's routing on the site.

Is this doable or is there a better way to accomplish this?

  <form action="/charge" method="POST">
    <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
      data-key="pk_"
      data-amount="3495"
      data-zip-code="true"
      metadata="test metadata test"
      data-description="Download and save your document"
      data-locale="auto">
    </script>
  </form>

Upvotes: 5

Views: 7038

Answers (1)

andrewnelder
andrewnelder

Reputation: 559

Create a metadata [1] field when you're constructing the charge [2]. When you get the charge.succeeded-event, it should be on the Charge object.

If you're going to accept this value from checkout, you'll need to pass another field along with the form that is getting submitted, then use that to construct the metadata. I've included an example of how to pass additional fields along with the form that is submitted by checkout below:

<form action="/charge" method="POST">

    <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
      data-key="pk_"
      data-amount="3495"
      data-zip-code="true"
      metadata="test metadata test"
      data-description="Download and save your document"
      data-locale="auto">
    </script>

    <!-- THE STUFF YOU'RE MISSING IS BELOW -->

    <input type="text" name="extra-field"></input>

</form>

Just remember though, you'll need to actually issue the Create a Charge [2] API request in your backend for the charge to be made. This is just what gets displayed to the user.

Does that make sense?


[1] https://stripe.com/docs/api#metadata

[2] https://stripe.com/docs/api#create_charge

Upvotes: 4

Related Questions