David Ferris
David Ferris

Reputation: 2325

Stripe Invoices - Custom Prices

I have a question for anyone familiar with the Stripe Payments API.

What I'm Trying to Do

I am trying to create an invoice with a custom, dynamic price. This price might vary between customers by a few cents, so it is not feasible to have hundreds of prices for a single product.

The Problem with Invoices/Checkout Session

Usually when a customer activates a subscription or pays for a product, they do so using Invoices. Invoices are created through checkout sessions on our backend like so:

    session = stripe.checkout.Session.create(
        mode='payment',
        customer=customer_id,
        payment_method_types=['card'],
        line_items=[{
            'price': MY_PRICE_ID,
            'quantity': 5,
        }],
    )

Checkout sessions allow price IDs but not custom prices! (or do they?)

The Problem with Payment Intents

Payment intents let you create checkout sessions with dynamic prices on the client, for example:

const paymentIntent = await stripe.paymentIntents.create({
        amount,
        currency: "usd"
});

However this will create a charge in the Customer that isn't assosciated with any product. I would like to associate this payment with a product.

Upvotes: 0

Views: 964

Answers (1)

Khantil Sanghani
Khantil Sanghani

Reputation: 61

I found this from Checkout Session Api documentation. It says that, in line_items you can either pass price or price_data or amount. If amount provided then currency and name is mandatory.

enter image description here

So, following code should work for you,

session = stripe.checkout.Session.create(
    mode='payment',
    customer=customer_id,
    payment_method_types=['card'],
    line_items=[{
        'amount': MY_AMOUNT,
        'currency': 'USD',
        'name': MY_ITEM,
        'quantity': 5,
        ...
    }],
)

If you want to associate the payment with the product then by using price_data following code should work for you,

session = stripe.checkout.Session.create(
    mode='payment',
    customer=customer_id,
    payment_method_types=['card'],
    line_items=[{
        'price_data':{
            'currency': 'USD',
            'product': MY_PRODUCT_ID,
            'unit_amount': MY_AMOUNT,
            ...
         }
    }],
)

Upvotes: 4

Related Questions