I'm working on a project currently. and I'm making use of stripe, the stripe.checkout.session.create function works perfectly.
const session = await stripe.checkout.sessions.create({
line_items: [
{
price_data: {
currency: "usd",
unit_amount: 500,
product_data: {
name: "name of the product",
},
},
quantity: 1,
},
],
mode: "payment",
success_url: "http://example.com/success",
cancel_url: "http://example.com/",
});
but the only issue i have is it receives only unit price.
but i want a field where i can pass only total Price, cause in my code , if the users applys a discount to a product , it deducts it from the product total price meanwhile the unit price for each product is stable.
My question is basically is there a field where i can pass total price.
CodePudding user response:
Short answer is that you can't. You need to use the line_items
parameter and either pass a Price object ID (price_xxx
) or use ad-hoc pricing with the price_data
parameter. Checkout will then compute the total from all line items, factoring in any discounts that you provide.
You have a couple of options to achieve the behaviour you want:
- Re-calculate the
price_data.unit_amount
value to reflect the discount applied by your customers. - Utilise Stripe coupons to apply to the Checkout Session which will take care of the dedication for you.