J. Petersen
J. Petersen

Reputation: 47

Nothing to invoice for subscription - create an invoice when the purchase is reviewed

Right now I am trying to create an invoice once the purchase has been reviewed.

It is the case that when the customer has bought the membership, I want invoice id to be assigned to my database in relation to the webhook being able to assign pdf invoice to the database with the link.

When I try to make invoice to last in the code. Then I am unfortunately met with this error.

Nothing to invoice for subscription

Stripe Error link

When I look at Stripe logs this comes from errors.

   {
      "error": {
        "code": "invoice_no_subscription_line_items",
        "doc_url": "https://stripe.com/docs/error-codes/invoice-no-subscription-line-items",
        "message": "Nothing to invoice for subscription",
        "param": "subscription",
        "type": "invalid_request_error"
      }
    }

Thus, my Request POST body is like this:

{
  "customer": "cus_J2ltIOWhr2BSGX",
  "subscription": "sub_J2lto4EVvcfToq"
}

And here can u see how i post request to stripe.

var createCustomer = new CustomerCreateOptions
{
    Source = token,
    Name = model.FuldName,
    Email = model.Mail
};

var addService = new CustomerService();
var customer = await addService.CreateAsync(createCustomer);

var optionsProduct = new ProductCreateOptions
{
    Name = $"Membership - {DateTime.Now}",
    Type = "service",
};
var serviceProduct = new ProductService();
Product product = await serviceProduct.CreateAsync(optionsProduct);

var optionsA = new PriceCreateOptions
{
    UnitAmount = amount,
    Currency = "dkk",
    Recurring = new PriceRecurringOptions
    {
        Interval = Helpers.Stripe.interval,
    },
    Product = product.Id,
};
var serviceA = new PriceService();
var price = await serviceA.CreateAsync(optionsA);

var options = new SubscriptionCreateOptions
{
    Customer = customer.Id,
    Items = new List<SubscriptionItemOptions>
    {
        new SubscriptionItemOptions
        {
            Price = price.Id,
        },
    },
};
var service = new SubscriptionService();
var subscription = await service.CreateAsync(options);

var optionsI = new InvoiceCreateOptions
{
    Customer = customer.Id,
    Subscription = subscription.Id
};
var serviceI = new InvoiceService();
var invoice = await serviceI.CreateAsync(optionsI);//I NEED INVOICE ID FROM HERE!

I have tried to look here.

Adding shipping to first subscription invoice using stripe

EIDT:

var createCustomer = new CustomerCreateOptions
                        {
                            Source = token,
                            Name = model.FuldName,
                            Email = model.Mail
                        };

                        var addService = new CustomerService();
                        var customer = addService.Create(createCustomer);

                        var options = new ChargeCreateOptions
                        {
                            Amount = amount,
                            Currency = "dkk",
                            Description = $"Købt kursus {DateTime.Now}",
                            Customer = customer.Id,
                        };
                        var service = new ChargeService();
                        Charge charge = service.Create(options);

Upvotes: 2

Views: 4437

Answers (1)

karbi
karbi

Reputation: 2163

If you're using Subscriptions, the Invoices will be generated automatically for you based on the billing cycle.

If you print out the Subscription object you just created, you can find the Invoice ID under latest_invoice.

-- UPDATE ABOUT ONE-TIME PAYMENTS --

Charges and Invoices are two separate concepts in Stripe's API. A Charge is very simple and it's only purpose is to create and make a payment. Invoices are more complicated and have an added functionality (line items, automatic functionality, taxes, etc.) on top of just making a payment. Either one is fine to use, but it really depends on what your business needs are.

If you want to be using Invoices for one-time payments, you will need to change your code to create them (see https://stripe.com/docs/api/invoices/create). Your code currently only working with Charges - these are not part of Stripe's Billing product and will not generate an Invoice automatically.

Upvotes: 2

Related Questions