stripe / stripe-java

Java library for the Stripe API.
https://stripe.com
MIT License
824 stars 361 forks source link

com.stripe.exception.InvalidRequestException: Quantity is required. Add `quantity` to `line_items[0]` #1584

Closed pjgg closed 1 year ago

pjgg commented 1 year ago

Describe the bug

I am trying to create a programmatic SessionCreateParams.LineItem.PriceData as a part of checkoutForm in Java and I am getting the following exception

com.stripe.exception.InvalidRequestException: Quantity is required. Addquantitytoline_items[0]``

Snippet

 SessionCreateParams params =
            SessionCreateParams.builder()
                .setCustomer(costumerId)
                .setMode(SessionCreateParams.Mode.SUBSCRIPTION)
                .setSuccessUrl("MySuccessURL")
                .setCancelUrl("MyCancelURL")
                .setBillingAddressCollection(SessionCreateParams.BillingAddressCollection.REQUIRED)
                .setAutomaticTax(SessionCreateParams.AutomaticTax.builder().setEnabled(true).build())
                .setCustomerUpdate(SessionCreateParams.CustomerUpdate.builder()
                    .setName(SessionCreateParams.CustomerUpdate.Name.AUTO)
                    .setAddress(SessionCreateParams.CustomerUpdate.Address.AUTO)
                    .build())
                .setTaxIdCollection(SessionCreateParams.TaxIdCollection.builder()
                    .setEnabled(true)
                    .build())
                .setConsentCollection(SessionCreateParams.ConsentCollection
                    .builder()
                    .setTermsOfService(SessionCreateParams.ConsentCollection.TermsOfService.REQUIRED)
                    .build())
                .setSubscriptionData(SessionCreateParams.SubscriptionData.builder()
                    .putMetadata("orgId", orgId)
                    .build())
                .addLineItem(
        SessionCreateParams.LineItem.builder()
            .setPriceData(
                SessionCreateParams.LineItem.PriceData.builder()
                    .setCurrency("usd")
                    .setProductData(
                        SessionCreateParams.LineItem.PriceData.ProductData.builder()
                            .setName("T-shirt")
                            .build()
                    )
                    .setUnitAmount(2000L)
                    .setRecurring(SessionCreateParams.LineItem.PriceData.Recurring.builder().setIntervalCount(1L).setInterval(SessionCreateParams.LineItem.PriceData.Recurring.Interval.MONTH).build())
                    .build()
            )
       //     .setQuantity(1L)
            .build())
                .addLineItem(
                    SessionCreateParams.LineItem.builder()
                        .setPrice(basePriceId)
                        .build())
                .addLineItem(
                    SessionCreateParams.LineItem.builder()
                        .setPrice(meteredPriceId)
                        .build())
                .build();
        Session session = Session.create(params);

Looks like that is required to have a "Quantity".

The problem is that I am looking for a Billed monthly based on usage price and based on the Doc looks like we should not provide a "Quantity" on those cases.

So my question is, is this a bug? or I am missing something else?

To Reproduce

SessionCreateParams params =
            SessionCreateParams.builder()
                .setCustomer(costumerId)
                .setMode(SessionCreateParams.Mode.SUBSCRIPTION)
                .setSuccessUrl("MySuccessURL")
                .setCancelUrl("MyCancelURL")
                .setBillingAddressCollection(SessionCreateParams.BillingAddressCollection.REQUIRED)
                .setAutomaticTax(SessionCreateParams.AutomaticTax.builder().setEnabled(true).build())
                .setCustomerUpdate(SessionCreateParams.CustomerUpdate.builder()
                    .setName(SessionCreateParams.CustomerUpdate.Name.AUTO)
                    .setAddress(SessionCreateParams.CustomerUpdate.Address.AUTO)
                    .build())
                .setTaxIdCollection(SessionCreateParams.TaxIdCollection.builder()
                    .setEnabled(true)
                    .build())
                .setConsentCollection(SessionCreateParams.ConsentCollection
                    .builder()
                    .setTermsOfService(SessionCreateParams.ConsentCollection.TermsOfService.REQUIRED)
                    .build())
                .setSubscriptionData(SessionCreateParams.SubscriptionData.builder()
                    .putMetadata("orgId", orgId)
                    .build())
                .addLineItem(
        SessionCreateParams.LineItem.builder()
            .setPriceData(
                SessionCreateParams.LineItem.PriceData.builder()
                    .setCurrency("usd")
                    .setProductData(
                        SessionCreateParams.LineItem.PriceData.ProductData.builder()
                            .setName("T-shirt")
                            .build()
                    )
                    .setUnitAmount(2000L)
                    .setRecurring(SessionCreateParams.LineItem.PriceData.Recurring.builder().setIntervalCount(1L).setInterval(SessionCreateParams.LineItem.PriceData.Recurring.Interval.MONTH).build())
                    .build()
            )
       //     .setQuantity(1L)
            .build())
                .addLineItem(
                    SessionCreateParams.LineItem.builder()
                        .setPrice(basePriceId)
                        .build())
                .addLineItem(
                    SessionCreateParams.LineItem.builder()
                        .setPrice(meteredPriceId)
                        .build())
                .build();

Expected behavior

I would expect to be able to create a Product with a monthly metering price.

Code snippets

No response

OS

macOS

Java version

17

stripe-java version

latest

API version

latest

Additional context

No response

remi-stripe commented 1 year ago

@pjgg Github issues are limited to bugs with the library itself and can't be used for support questions like yours. I recommend working directly with our support team for help instead if you have further questions after the advice I'll give below. You can contact them here: https://support.stripe.com/contact

The error message explains that you have to pass the quantity parameter so that's what you need to do here. Your code is passing line_items[0][price_data] and not line_items[0][quantity]. For some reason you have the code there but commented out and that parameter is required. If you want usage-based billing you should read this doc first and make sure that the Price you create is properly configured for metered billing. For that, you can't just use price_data in that case and need to create the Price upfront and pass the usage_type: 'metered' parameter.