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.
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.
Related
When creating a checkout session, I am providing line items with price_data and quantity and in the checkout I see the correct amount. For some payments the customer can use their in-app points which will reduce some amount from the total checkout amount. How can I apply that to the checkout?
Example:
You buy 3 T-shirts (3x20) and one cap (1x15) which means you need to pay 75.00 (of some unit)
You use the in-app option to use your points which gives you 5.00, so now your checkout session must be a custom value (70.00).
I am using this API:
https://stripe.com/docs/api/checkout/sessions/create
The only solution that I came up with was to create a coupon right before creating the checkout and apply it to the checkout, but I don't know if that's safe.
Yes coupon should works with Checkout Session.
Maybe this will work.
var stripe = require("stripe")("YOUR_STRIPE_SECRET");
stripe.checkout.sessions.create(
{
payment_method_types: ["card"],
line_items: [
{
price: "YOUR_PRICE_ID",
quantity: 1,
},
],
success_url: "https://example.com/success?session_id={CHECKOUT_SESSION_ID}",
cancel_url: "https://example.com/cancel",
},
function(err, session) {
console.log(session.id);
}
);
I have a Stripe Checkout Session linked to a connected account. The connected account is subject to a 10% charge from my platform. It works well currently, however now I have added the ability for customers to use coupons in the checkout window, the application fee is being calculated from the total amount before the discount is applied.
For example, if I charge €32, the application fee is €3.20. If a discount reduces the charge to €10, the fee should be €1. But it is still charging €3.20.
I have a function which calculates the application fee amount, the problem is that this is done when the checkout session is created - this happens before the customer enters the discount.
I can't think of a way around this issue since the session is always created prior to the discount being applied. Any help/suggestions are appreciated!!
const calculateApplicationFeeAmount = (total) => total * site.stripeFee;
const session = await stripe.checkout.sessions.create(
{
payment_method_types: ["card"],
line_items: [
{
name: price.name,
amount: price.amount,
currency: site.currency,
quantity: 1,
},
],
payment_intent_data: {
application_fee_amount: calculateApplicationFeeAmount(price.amount),
description: price.name,
},
mode: "payment",
allow_promotion_codes: true,
success_url: `${config.baseUrl.url}/unlock/session/{CHECKOUT_SESSION_ID}`,
cancel_url: `${config.baseUrl.url}/unlock/${deviceID}`,
metadata: {
deviceID: deviceID,
},
},
{
stripeAccount: site.stripeAccount,
}
);
return session;
The best solution here would probably be to :
Use payment_intent_data.capture_method:manual when creating the checkout session
After receiving the checkout.session.completed event, update the application_fee_amount on the PaymentIntent
Capture the PaymentIntent
The limitation is that not all payment methods support manual capture.
Alternatively, you could also refund the application fee later : https://stripe.com/docs/api/fee_refunds/create
I'm attempting to create subscriptions through the Stripe API. I already create the products and items and now need to submit the subscription for the user, but I need to charge the customer for the full price now - no matter which day of the month it is - then charge at the beginning of each month - even if it starts tomorrow.
It looks like I could create a one-off item to charge for now then set up a subscription for the monthly billing cycle, but I'm wondering if I can do it all in one call with the subscription => create function. I do not want to prorate the first month and I cannot see a way to tell it to charge the full price now and set up recurring on the first of each month following. Is there a way to do this?
One way to go about the flow you're describing is to combine the backdate_start_date and billing_cycle_anchor properties. The idea is that when creating the subscription you would set the billing_cycle_anchor to the first of the next month, and you would set the backdate_start_date to the first of the current month. For example, say you wanted to sign up a user for a $10.00 subscription that starts today (February 5th), but you want to bill them for the full $10.00 right away (even though they missed the first 5 days). Then, you want to bill them $10.00 again on March 1st, and the first of every month thereafter. When creating the subscription you would set:
billing_cycle_anchor: 1614556800 (March 1st)
backdate_start_date: 1612137600 (February 1st)
Which would result in a $10.00 invoice today, and a $10.00 invoice again on the first of March, and subsequent $10.00 invoices on the first of every month going forward.
Here's what this would look like in Node:
(async () => {
const product = await stripe.products.create({ name: "t-shirt" });
const customer = await stripe.customers.create({
name: "Jenny Rosen",
email: "jenny.rosen#gmail.com",
payment_method: "pm_card_visa",
invoice_settings: {
default_payment_method: "pm_card_visa",
},
});
const subscription = await stripe.subscriptions.create({
customer: customer.id,
items: [
{
quantity: 1,
price_data: {
unit_amount: 1000,
currency: "usd",
recurring: {
interval: "month",
},
product: product.id,
},
},
],
backdate_start_date: 1612137600,
billing_cycle_anchor: 1614556800,
expand: ["latest_invoice"],
});
console.log(subscription);
})();
I have created a subscription service using Stripe. I can subscribe a user to use recurring payments. This is the relevant code (node):
// Create the subscription
const subscription = await stripe.subscriptions.create({
customer: req.body.customerId,
items: [{ price: req.body.priceId }],
expand: ['latest_invoice.payment_intent'],
});
This works and uses the priceId as shown in the dashboard:
However, it falls over when I sell a product which isn't recurring. I get the error:
The price specified is set to `type=one_time` but this field only accepts prices with `type=recurring`
I understand the error, but I am not sure if I can set a subscription to not be recurring.
My app has 3 tiers:
once off
monthly
yearly
Ideally, I would like not to add a whole new section of code to handle what seems like a subset of what subscriptions do, but even if I do, the paymentIntent object seems to only take an amount rather than the API ID as shown in the picture. Is there any way to do this using the infrastructure I have already built?
You can't create a subscription with a non-recurring price, instead for one-off payments you'd use a PaymentIntent.
Prices are meant for use with subscriptions and the Checkout product, but you can still use the data in them for PaymentIntents. For instance:
// get the price
const price = await stripe.prices.retrieve(req.body.priceId);
// check if the price is recurring or not
if (price.recurring !== null) {
// Create the subscription
const subscription = await stripe.subscriptions.create({
customer: req.body.customerId,
items: [{ price: req.body.priceId }],
expand: ['latest_invoice.payment_intent'],
});
// do something with the subscription
} else {
const pi = await stripe.paymentIntents.create({
customer: req.body.customerId,
currency: 'usd',
amount: price.unit_amount,
});
// do something with the PaymentIntent
}
you can also attach one-time items via the subscription.create call by placing them in add_invoice_items.
see: https://stripe.com/docs/api/subscriptions/create?lang=node#create_subscription-add_invoice_items
In Stripe Checkout, using the server integration, is it possible to preload the customer saved cards?
I have a customer with a previous purchase history and I would like to let him pick a saved card details during checkout.
In the CRM I can see that cus_EyGeAZaPVHwUVg has the credit card stored (the test one for now, 4242...), but upon a new checkout I am not presented with that, even though the email is filled correctly.
I haven't seen any option in the docs, this is the code:
stripe.checkout.sessions.create({
payment_method_types: [paymentMethod],
line_items: [{
name: name,
description: description,
images: [imageUrl],
amount: amount,
currency: currency,
quantity: 1
}],
success_url: 'http://localhost:3000/success',
cancel_url: 'http://localhost:3000/cancel',
customer: 'cus_EyGeAZaPVHwUVg'
})