首页 > 解决方案 > 如何为共享客户的连接帐户创建条纹发票

问题描述

我已经在我的 Stripe 平台帐户上成功创建了一个客户。现在,我想从我的一个连接帐户中为该客户创建一张发票。

连接帐户向客户提供了一项服务,现在将为客户开具付款发票 - 我正在使用 Stripe Invoice 功能。

但是,即使在按照示例将连接帐户与客户帐户相关联之后,我仍然收到错误“没有这样的客户:cus_XXXX”。

我已按照https://stripe.com/docs/connect/shared-customers#making-tokens为我的平台客户创建令牌,然后使用该令牌创建连接帐户客户,如https://lorenzosfarra 所示。 com/2017/09/19/stripe-shared-customers/但我仍然收到相同的错误消息。即使似乎连接帐户引用已成功创建 - 由于没有返回错误,因此未找到生成的客户 ID。

这是我的创建发票方法

public static async Task<string> CreateCustomerInvoice(string secretKey, string customerId, TenantBillHeaderModel billHeader)
        {
            StripeConfiguration.ApiKey = secretKey;

            var fees = await Database.GetAdminFees();
            var salesTax = await Database.GetCountrySalesTax(13);// Hardcoded for launch
            var billLines = await Database.GetTenantBillDetailForHeaderId(billHeader.TenantBillHeaderId);
            var stripeContact = await Database.GetStripeContact(UserInfo.Instance.Organisation.OrganisationId);
            InvoiceItemCreateOptions invoiceItemOptions = null;

            // Find Fee Percentage
            var tFee = billHeader.GrossAmount * (fees.FirstOrDefault(f => f.AdminFeeId == (int)Persistence.Enums.AdminFeeEnum.ConnectCut)).Percentage / 100;

            // Create token so that customer can be shared with Connect account - See https://stripe.com/docs/connect/shared-customers
            var customerTokenId = await CreateCustomerToken(secretKey, customerId, stripeContact.StripeConnectToken);

            //Associates customer with connect account so that it is shared with platform account
            var connectCustId = await CreateCustomerInConnectAccount(secretKey, customerTokenId, stripeContact.StripeConnectToken);

            foreach (var l in billLines)
            {
                invoiceItemOptions = new InvoiceItemCreateOptions
                {
                    Quantity = l.Quantity,
                    UnitAmount = Convert.ToInt64(l.Amount * 100), // Converting to cents for Stripe
                    Currency = "aud", // Hardcoded for launch
                    CustomerId = connectCustId,
                    Description = l.Name + " (" + l.Description + ")",
                    TaxRates = new List<string> {
                        salesTax.StripeTaxRateId
                      },
                };
                var itemService = new InvoiceItemService();
                InvoiceItem invoiceItem = await itemService.CreateAsync(invoiceItemOptions);
            }

            var invoiceOptions = new InvoiceCreateOptions
            {
                CustomerId = connectCustId,
                AutoAdvance = false, // do not auto-finalize this draft after ~1 hour
                CollectionMethod = "send_invoice", // Stripe will progress the a send state. However it will not email as this is trurned off in portal https://stripe.com/docs/billing/invoices/customizing
                ApplicationFeeAmount = Convert.ToInt64(tFee),
                Description = "Invoice for Advizr Bill: " + billHeader.BillNumber,
                DueDate = billHeader.DueDate
            };

            var service = new InvoiceService();
            Invoice invoice = await service.CreateAsync(invoiceOptions);

            return invoice.Id;
        }

在这里,我创建了一个客户令牌

public static async Task<string> CreateCustomerToken(string secretKey, string customerId, string stripeContactId)
        {
            StripeConfiguration.ApiKey = secretKey;
            RequestOptions requestOptions = null;

            var options = new TokenCreateOptions
            {
                CustomerId = customerId,
            };

            if (stripeContactId != null)
            {
                requestOptions = new RequestOptions
                {
                    StripeAccount = stripeContactId
                };
            }
            var service = new TokenService();
            Token token = await service.CreateAsync(options, requestOptions);

            return token.Id;
        }

在这里我创建/关联客户与连接帐户

public static async Task<string> CreateCustomerInConnectAccount(string secretKey, string customerToken, string connectAccount)
        {
            StripeConfiguration.ApiKey = secretKey;

            var options = new CustomerCreateOptions
            {
                Source = customerToken
            };

            var requestOptions = new RequestOptions
            {
                StripeAccount = connectAccount
            };
            var service = new CustomerService();
            Customer customer = await service.CreateAsync(options, requestOptions);

            return customer.Id;
        }

任何有关如何为共享客户创建发票的指导(存在于平台帐户中并与连接帐户相关联)将不胜感激。

然后客户将支付发票,我将在扣除申请费后使用目的地支付连接帐户。

谢谢

标签: asp.net-mvcstripe-payments

解决方案


在登录并使用 Stripe Support 发出问题后,他们为我提供了答案。

要解决此问题,连接帐户

var requestOptions = new RequestOptions
{
    StripeAccount = stripeContact.StripeConnectToken
};

创建发票行项目时必须提供

var itemService = new InvoiceItemService();
InvoiceItem invoiceItem = await itemService.CreateAsync(invoiceItemOptions, requestOptions);

以及创建发票时。

var service = new InvoiceService();
Invoice invoice = await service.CreateAsync(invoiceOptions, requestOptions);

希望这可以帮助某人。


推荐阅读