无需为订阅开具发票 - 在审核购买时创建发票

问题描述

现在我正在尝试在审核购买后创建发票。

当客户购买会员资格时,我希望将发票 ID 分配给我的数据库,因为 webhook 能够通过链接将 pdf 发票分配给数据库

当我尝试在代码中制作发票时。然后我不幸遇到了这个错误

无需为订阅开具发票

Stripe Error link

当我查看 Stripe 日志时,这来自错误

   {
      "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"
      }
    }

因此,我的请求 POST 正文是这样的:

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

在这里你可以看到我如何将请求发布到条带。

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!

我曾试着看这里。

Adding shipping to first subscription invoice using stripe

EIDT:

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

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

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

解决方法

如果您使用订阅,系统会根据计费周期为您自动生成发票。

如果您打印出刚刚创建的 Subscription 对象,您可以在 latest_invoice 下找到发票 ID。

-- 关于一次性付款的更新 --

费用和发票在 Stripe 的 API 中是两个独立的概念。费用非常简单,其唯一目的是创建和付款。发票更加复杂,并且除了付款之外还具有附加功能(行项目、自动功能、税收等)。任何一种都可以使用,但这实际上取决于您的业务需求。

如果您想使用发票进行一次性付款,则需要更改您的代码以创建它们(请参阅https://stripe.com/docs/api/invoices/create)。您的代码目前仅适用于费用 - 这些不是 Stripe 账单产品的一部分,不会自动生成发票。