#Huzaifa

1 messages ยท Page 1 of 1 (latest)

devout sierraBOT
night compass
#

Can you show me the code you have right now for creating your checkout session?

#

You can definitely include both in one session

hard egret
#

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."

night compass
#

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

hard egret
#

how

#

can you guide me

night compass
hard egret
#

yeah i saw that api

its not taking subscription products

night compass
#

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

hard egret
#

in subscription api or invoice item api ??

night compass
#

Invoice item API. Unfortunately you can't use one time prices with the subscriptions API

hard egret
#

can you guide me how can i use it with my array of pricerules id

night compass
#

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?

hard egret
#

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 ?

night compass
#

The basic steps are:

  1. Run your subscription creation code, only passing in the recurring prices
  2. While the latest_invoice is in its draft state, create an invoice item for each one time price that you want to add. https://stripe.com/docs/invoicing/integration/quickstart
  3. Finalize the invoice
  4. Pay the invoice with the payment method that you created like that
hard egret
#

so i have to run a loop

night compass
#

Correct

hard egret
#

let me try with it

hard egret
#

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

night compass
#

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

hard egret
#

req_3Hmv56yoXPmDw9

night compass
#

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

hard egret
#

i was saving the prices in my database at the time of creating products

night compass
#

Oh whoops right search was for prices

hard egret
#

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

night compass
#

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

hard egret
#

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

night compass
hard egret
#

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 >>

elfin grove
#

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.

hard egret
#

sure

elfin grove
#

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.

hard egret
#

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

elfin grove
#

Awesome!

hard egret
#

it now create subscription and invoice too

#

but cant i add subscription product in invoice

its creating seperate payment transaction

elfin grove
#

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

hard egret
#

for that what should i do

  1. create invoice
  2. add pricerule in invoice

then add those pricerule in add_invoice_items

?????

elfin grove
#

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.

hard egret
#

ah perfect

#

it works

elfin grove
hard egret
#

not a problem

#

you helped me alot

#

thank you so much

#

๐Ÿ™‚