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

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

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

情况是,当客户购买会员资格时,我希望将发票 ID 分配给我的数据库,因为 webhook 能够使用 link 将 pdf 发票分配给数据库。

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

Nothing to invoice for subscription

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

你可以在这里看到我如何 post 请求条纹。

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,
                            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);

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

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

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

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

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