C#:在条带中设置默认付款方式

C# : set default payment method in stripe

我是stripe新手,如何设置stripe默认支付方式

我们可以通过 cardId/sourceId 与 customerId 一起向客户收费吗?

代码:-

private static async Task<string> ChargeCustomer(string customerId)
{
    return await System.Threading.Tasks.Task.Run(() =>
    {
        var myCharge = new StripeChargeCreateOptions
        {
            Amount = 50,
            Currency = "gbp",
            Description = "Charge for property sign and postage",
            CustomerId = customerId
        };

        var chargeService = new StripeChargeService();
        var stripeCharge = chargeService.Create(myCharge);

        return stripeCharge.Id;
    });
}

还有 1 个问题,如何获取收费列表,我正在使用以下代码但出现异常(转换错误):-

  private IEnumerable<StripeCharge> GetChargeList()
    {
        var chargeService = new StripeChargeService();
        return chargeService.List();
    }

我们可以在StripeChargeCreateOptions

的SourceTokenOrExistingSourceId 属性中传递cardId/BankAccountId/TokenId/SourceId
private static async Task<string> ChargeCustomer(string customerId, string cardId)
        {
            try
            {
                return await System.Threading.Tasks.Task.Run(() =>
                {
                    var myCharge = new StripeChargeCreateOptions
                    {
                        Amount = 50,
                        Currency = "gbp",
                        Description = "Charge for property sign and postage",
                        CustomerId = customerId,
                        SourceTokenOrExistingSourceId = cardId
                    };

                    var chargeService = new StripeChargeService();
                    var stripeCharge = chargeService.Create(myCharge);

                    return stripeCharge.Id;
                });
            }
            catch(Exception ex)
            {
                return "";
            }
        }

至set/change默认付款方式:-

 public void ChangeDefaultPayment(string customerId, string sourceId)
    {
        var myCustomer = new StripeCustomerUpdateOptions();
        myCustomer.DefaultSource = sourceId;
        var customerService = new StripeCustomerService();
        StripeCustomer stripeCustomer = customerService.Update(customerId, myCustomer);
    }

仍在寻找如何获取收费列表。

这就是我最终所做的。不确定为什么 Stripe Checkout 没有将订阅设置的卡设置为默认卡。无论如何,这起事件是由 payment_intent.succeeded 网络挂钩触发的。当然有更好的方法,但是...

    var customerService = new CustomerService(Configs.STRIPE_SECRET_KEY);
    var c = customerService.Get(pi.CustomerId);

    if (!string.IsNullOrEmpty(c.InvoiceSettings.DefaultPaymentMethodId)) {
      status = "already has default payment method, no action";
      hsc = HttpStatusCode.OK;
      return;
    }


    var paymentMethodService = new PaymentMethodService(Configs.STRIPE_SECRET_KEY);
    var lopm = paymentMethodService.ListAutoPaging(options: new PaymentMethodListOptions {
      CustomerId = pi.CustomerId,
      Type = "card"
    });

    if (!lopm.Any()) {
      status = "customer has no payment methods";
      hsc = HttpStatusCode.BadRequest;
      return;
    }
    var pm = lopm.FirstOrDefault();
    customerService.Update(pi.CustomerId, options: new CustomerUpdateOptions {
      InvoiceSettings = new CustomerInvoiceSettingsOptions {
        DefaultPaymentMethodId = pm.Id
      }
    });

    hsc = HttpStatusCode.OK;
    return;