mrSidX
mrSidX

Reputation: 357

Attaching metadata to Stripe Payment Link

Is it possible to attach metadata to a Stripe Payment link (that directs to Stripe for payment)?

I've set up a basic link to a Stripe payment and have a link to the payment on my site.

What I hope to achieve is to have a rendered mustache template attached to that link some extra parameters/query data, that will act as a metadata set.

ie: <a href="www.stripe.com/tk_live_foobarlinktopayment?userid={{user._id}}"> <button>Pay Now</button></a> ...

I hope to link who's account is making the payment via this metadata.

Upvotes: 11

Views: 10379

Answers (3)

acoudouy
acoudouy

Reputation: 200

You can now add some information within the Stripe Payment Link URL directly.

You can set:

  • client_reference_id (your reference),
  • prefilled_email (prefill the email in the payment link),
  • prefilled_promo_code (prefill a promo code).

Documentation: https://stripe.com/docs/payment-links/url-parameters

Upvotes: 6

mrSidX
mrSidX

Reputation: 357

I managed to post to the backend endpoint, using checkout.sessions.create.

This creates a payment attempt, where it directs you to a stripe side payment UI.

This payload example is what is passed to the Stripe API, Though I am using node-red and the Stripe payment node, you should be able to do this concept elsewhere.

msg.payload = {
    
     payment_intent_data: {    
         metadata : {
            "user_id": msg.user._id,
            "email" : msg.user.user,
         },  
     },
     payment_method_types : ['card'],
     mode: 'payment',
     success_url: 'https://somesite.com/payment_success',
     cancel_url: 'https://somesite.com/payment_cancel',
 
     line_items : [
         {
             quantity : 1,
             price_data : {
                 currency: 'usd',
                 product_data: {
                     name: 'Some Product'
                 },
                 unit_amount: 1200
             }
         }
     ]
}

Upvotes: 2

Ignas
Ignas

Reputation: 91

The way I've solved this with Payment Links - I generate a unique Payment Link for each purchase and add metadata on Link creation (order_id in my case). This way it gets propagated to webhooks.

Create Payment Link cURL example:

curl https://api.stripe.com/v1/payment_links \
  -u sk_test_51INcGzGixr<..> \ 
  -d "line_items[0][price]"=price_1KPia<..> \
  -d "line_items[0][quantity]"=1 \
  -d "metadata[order_id]"=6735

This involves a bit more code than you might anticipate with Payment Links, but definitely lighter on implementation side that Checkout.

Also, don't forget to cleanup the Payment Links afterwards:

curl https://api.stripe.com/v1/payment_links/plink_1KPoiiGixr8beE1DMeFarexg \
  -u sk_test_51INcGzGixr<..> \
  -d active=false

Upvotes: 7

Related Questions