发票人 PDF 给出 null 但会员资格不给出 PDF null

Posted

技术标签:

【中文标题】发票人 PDF 给出 null 但会员资格不给出 PDF null【英文标题】:Invoicer PDF gives null but Membership does not give PDF null 【发布时间】:2021-06-03 05:07:53 【问题描述】:

我现在的问题。这是我必须使客户可以购买只支付一次的物品。因此将 Invoice id 和 PDf 分配给数据库。

现在我只能在 PDF 为空时获取发票 ID。

我已经阅读了更多关于此的内容。 Invoice Id return with null after change using Stripe

var options = new ProductCreateOptions

    Name = "Starter Setup",
;

var service = new ProductService();
var product = service.Create(options);

var optionsA = new PriceCreateOptions

    Product = product.Id,
    UnitAmount = 2000,
    Currency = "usd",
;

var serviceA = new PriceService();
var price = serviceA.Create(optionsA);

var optionsB = new CustomerCreateOptions

    Email = model.Mail,
    Name = model.FuldName,
    Source = token
;

var serviceB = new CustomerService();
var customer = serviceB.Create(optionsB);

var optionsC = new InvoiceItemCreateOptions

    Customer = customer.Id,
    Price = price.Id,
;

var serviceC = new InvoiceItemService();
var invoiceItem = serviceC.Create(optionsC);

var invoiceId = invoiceItem.Id;

var serviceE = new InvoiceService();
var f = serviceE.Get(invoiceId);

var pdf = f.InvoicePdf;// This here gives zero.

如果我这样做,我会解决这个问题。我在这里得到了我想要的发票 ID,但发票上没有显示它为零。


"id": "ii_1IR4UtFnB7TvDVRrzPwWo8ZW",
  "object": "invoiceitem",
  "amount": 2000,
  "currency": "usd",
  "customer": "cus_J3Aqpyt4PwqCcN",
  "date": 1614815575,
  "description": "Starter Setup",
  "discountable": true,
  "discounts": [
  ],
  "invoice": null,
  "livemode": false,
  "metadata": 
  ,
....

有了这个,我的想法是我是否能够以某种方式使我成为会员,然后立即停止,但发票上说购买的只是一件物品,而不是几个月。

我这样做的方式与我这样做的会员资格有关。

var createCustomer = new CustomerCreateOptions

    Source = token,
    Name = model.FuldName,
    Email = model.Mail
;

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

var optionsProduct = new ProductCreateOptions

    Name = $"Single buy - DateTime.Now - Kursus Id : id",
    Type = "service",
;
var serviceProduct = new ProductService();
Product product = serviceProduct.Create(optionsProduct);

var optionsPlan = new PlanCreateOptions

    Currency = "dkk",
    Interval = Helpers.Stripe.interval,
    Nickname =
        $"Single buy - DateTime.Now - Kursus Id : id",
    Amount = amount,
    Product = product.Id,
    IntervalCount = 1
;
var servicePlan = new PlanService();
Plan plan = servicePlan.Create(optionsPlan);

var items = new List<SubscriptionItemOptions>()

    new SubscriptionItemOptions()
    
        Plan = plan.Id,
        Quantity = 1
    ,
;
var createSubscruptionA = new SubscriptionCreateOptions

    Customer = customer.Id,
    Items = items,
    OffSession = true,
;
var addserviceA = new SubscriptionService();
Subscription subscription = addserviceA.Create(createSubscruptionA);

var invoiceId = subscription.LatestInvoiceId;
var service = new InvoiceService();
var pdf = service.Get(invoiceId).InvoicePdf;

我想通过这个实现的。我可以获取 PDF 和 Invoice id,因为我将来会在我的系统中使用它等。

编辑

var optionsB = new CustomerCreateOptions

    Email = model.Mail,
    Name = model.FuldName,
    Source = token
;

var serviceB = new CustomerService();
var customer = serviceB.Create(optionsB);

var optionsC = new InvoiceItemCreateOptions

    Customer = customer.Id,
    Price = price.Id,
;

var serviceC = new InvoiceItemService();
var invoiceItem = serviceC.Create(optionsC);

var invoiceId = invoiceItem.Id;

var invoiceOptions = new InvoiceCreateOptions

    Customer = customer.Id,
    AutoAdvance = true,
;
var invoiceService = new InvoiceService();
var invoice = invoiceService.Create(invoiceOptions);

【问题讨论】:

【参考方案1】:

对于一次性发票,您需要为客户创建发票项目(正如您所做的那样),但重要的是,您需要 create an Invoice 包含这些项目。

此行不适合您要完成的工作:

var invoiceId = invoiceItem.Id;

相反,您需要按照上面链接的文档中所示创建发票:

var invoiceOptions = new InvoiceCreateOptions

  Customer = "cus_123",
  AutoAdvance = true,
;
var invoiceService = new InvoiceService();
var invoice = invoiceService.Create(invoiceOptions);

在您finalize 之后,Invoice 对象将有一个invoice_pdf URL (docs)。

var service = new InvoiceService();
service.FinalizeInvoice(
  "in_123"
);

【讨论】:

好的,当我尝试按照您告诉我的操作时,我收到一条错误消息,提示“无需为客户开具发票”-stripe.com/docs/error-codes/invoice-no-customer-line-items 是的,您需要先创建发票项目,然后为同一客户创建发票。 好的@nolan H。我已经更新/编辑了我的代码。所以你可以看到我做了什么。还是我误解了它的原理? 好的,现在结果如何?它在工作吗?如果没有,会发生什么? 发票项目创建成功了吗?您是否查看过您的开发人员日志以查看它是成功还是返回了错误? dashboard.stripe.com/test/logs

以上是关于发票人 PDF 给出 null 但会员资格不给出 PDF null的主要内容,如果未能解决你的问题,请参考以下文章

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

会员资格不被认可

WooCommerce 会员资格检查用户(具有当前会员计划)是不是能够访问内容

FromBody 字符串参数给出 null

苹果开发账号过期不续费会怎样?

Z3:如何办理协会或会员资格?