如何为共享客户上的连接帐户创建条带发票

时间:2019-08-20 12:10:32

标签: asp.net-mvc stripe-payments stripe-invoices

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

连接帐户向客户提供了服务,现在将向客户开具发票以付款-我正在使用“分条发票”功能。

但是,即使按照以下示例将连接帐户与客户帐户相关联,我仍然收到错误“没有这样的客户: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;
        }

任何有关如何为共享客户创建发票(存在于平台帐户中并与关联帐户相关联)的指南都将受到赞赏。

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

谢谢

1 个答案:

答案 0 :(得分:0)

登录并获得Stripe支持问题后,他们为我提供了答案。

要解决此问题,请连接帐户

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

希望这对某人有帮助。