#Huzaifa
1 messages ยท Page 1 of 1 (latest)
Can you show me the code you have right now for creating your checkout session?
You can definitely include both in one session
i have array of price rules which iam passing in subscription api
const subscription = await stripe.subscriptions.create({
customer: 'cus_N4Zx1P9iB49s7h',
items: [
{price: 'price_1MKQ3DEoy3o4Zqt5XoRQek8a'},
{price: 'price_1MKQ3DEoy3o4Zqt5XoRQek8a'},
],
});
in items array first object has id of subscription product and second is one time product
but its giving me following error
"The price specified is set to type=recurring but this field only accepts prices with type=one_time."
Oh, you are creating the subscription directly. For that you need to create the subscription with just recurring items, then you can add the one off charges as invoice items
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
yeah i saw that api
its not taking subscription products
Ah gothca.
Yeah it looks like you can't use the price object directly. You can provide its info in the price_data argument on the item creation call and it will show up properly on the invoice
in subscription api or invoice item api ??
Invoice item API. Unfortunately you can't use one time prices with the subscriptions API
can you guide me how can i use it with my array of pricerules id
I'm not clear what that means here. Can you tell me a bit more about what you are trying to do?
Is this still just trying to charge for both recurring and one time items on a single invoice?
yes
the scenario is when customer add product to cart
i got price id and saved in array like this
items: [
{price: 'price_1MKQ3DEoy3o4Zqt5XoRQek8a'},
{price: 'price_1MKQ3DEoy3o4Zqt5XoRQek8a'},
],
now one is recurring product and one is one time product
in subscription api i cant use one time product price id
and invoice api i cant use subscription product price id
i need a solution so it charge the customer in one payment method i create
const paymentMethod = await stripe.paymentMethods.create({
type: "card",
card: {
number: req.body.cardno,
exp_month: req.body.expmonth,
exp_year: req.body.expyear,
cvc: req.body.cvv,
},
billing_details: {
address: {
line1: req.body.addresss,
city: req.body.city,
country: req.body.country,
postal_code: req.body.postcode,
},
email: req.body.email,
name: req.body.name,
},
});
you got my point ?
We show how to create invoice items on an invoice in this tutorial https://stripe.com/docs/invoicing/integration/quickstart
The basic steps are:
- Run your subscription creation code, only passing in the recurring prices
- While the
latest_invoiceis in itsdraftstate, create an invoice item for each one time price that you want to add. https://stripe.com/docs/invoicing/integration/quickstart - Finalize the invoice
- Pay the invoice with the payment method that you created like that
so i have to run a loop
Correct
Also apologies but I was wrong before, you can indeed pass in price IDs when creating an invoice item https://stripe.com/docs/api/invoiceitems/create#create_invoiceitem-price
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
let me try with it
const prices = await stripe.prices.search({
query: "product:'" + product.id + "'",
});
iam receiving empty for this
{
object: 'search_result',
data: [],
has_more: false,
next_page: null,
url: '/v1/prices/search'
}
am i writing query wrong
Can you check your API logs in your dashboard and send me the request ID (req_123) from when you made this search call?
Also, I assume this product was created over a minute ago? It can take up to a minute for prices to become available via the search aPI
req_3Hmv56yoXPmDw9
Actually, is there a reason you are using the search call here rather than retrieving the product directly? If you already have the ID, retrieving would be the best way to get the product info
i was saving the prices in my database at the time of creating products
Oh whoops right search was for prices
If you list prices and filter by product ID you should get the right results https://stripe.com/docs/api/prices/list#list_prices-product
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
const myprices = await stripe.prices.search({
query: product:"${product.id}",
});
thats what i did
it means it takes time to make effect on product for prices
List and search are two separate calls, and the list call doesn't need to wait a minute to be able to retrieve a new price
can you check it for me
for (let i = 0; i < req.body.cartItems.length; i++) {
let item = req.body.cartItems[i];
if(item.pricerule.recurring){
let obj = {
price: item.pricerule,
quantity: item.quantity,
};
const subscription = await stripe.subscriptions.create({
customer: customer.id,
add_invoice_items: pricearr,
items: [obj],
default_payment_method: paymentMethod.id,
});
result.push({
task:"subscription",
data:subscription
})
}else{
const invoice = await stripe.invoices.create({
customer: customer.id,
});
const invoiceItem = await stripe.invoiceItems.create({
customer: customerId,
price: item.pricerule,
quantity:item.quantity,
invoice: invoice.id
});
result.push({
task:"invoice",
data:invoiceItem
})
}
}
how can i finalize invoice and subscription and capture it with paymentMethod
It will auto-finalize after an hour, otherwise you can make the finalize API call https://stripe.com/docs/api/invoices/finalize
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
const finalInvoice = await stripe.invoices.finalizeInvoice(
invoicess[0].invoice.id
);
const paymentIntent = await stripe.paymentIntents.retrieve(
finalInvoice.payment_intent
);
const paymentIntentconfirm = await stripe.paymentIntents.confirm(
paymentIntent.id,
{ payment_method: paymentMethod.id }
);
if (paymentIntentconfirm.status === "succeeded") {
res.status(200).json({
status: true,
subscriptionss,
invoicess,
});
is this code is correct >>
Hi there ๐ I'm jumping in as my teammate needed to step away. Please bear with me a moment while I catch up on the context here.
sure
If you're not setting your flow to require manual confirmation for these Payment Intents, then I don't think you need the paymentIntents.confirm request.
ok
i just have run the code
i have three products
one subscription
and
two one times product
it create one subscription and one one time product transaction in stripe
one is missing
do i have to finalize invoice for every product ???
ah
gotcha
it works
Awesome!
it now create subscription and invoice too
but cant i add subscription product in invoice
its creating seperate payment transaction
No, you cannot add a Subscription Item to an Invoice, but you can add Invoice Items to Subscriptions. So if you want to process a single payment, you can use add_invoice_items while creating the Subscription to include those Invoice Items on the first Invoice of the Subscription:
https://stripe.com/docs/api/subscriptions/create#create_subscription-add_invoice_items
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
for that what should i do
- create invoice
- add pricerule in invoice
then add those pricerule in add_invoice_items
?????
No, when creating the subscription, you can provide non-recurring prices into the add_invoice_items array. Doing so will add those items to the first Invoice that is generated for the Subscription.
Awesome! Sorry, I was trying to hunt down the doc that had code snippets for that, and it took a bit longer than expected.
https://stripe.com/docs/billing/invoices/subscription#first-invoice-extra