【问题标题】:C# : set default payment method in stripeC#:在条纹中设置默认付款方式
【发布时间】:2017-12-01 11:48:39
【问题描述】:

我是 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();
    }

【问题讨论】:

    标签: c# asp.net stripe-payments payment-gateway stripe.net


    【解决方案1】:

    这就是我最终要做的。不知道为什么 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;
    

    【讨论】:

    • 在尝试了不同的事情之后,这成功了……我能够将付款方式设置为客户的默认方式。
    【解决方案2】:

    我们可以在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 "";
                }
            }
    

    设置/更改默认付款方式:-

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

    仍在寻找如何获得费用清单。

    【讨论】:

    【解决方案3】:
    internal static IEnumerable<StripeCharge> GetChargeList()
            {
                var chargeService = new StripeChargeService();
                return chargeService.List();
            }
    

    它对我来说很好用。请确保您已在项目中安装“Newtonsoft.Json”dll,因为我在开始条带付款时遇到此错误。 here is the response received from stripe server.

    【讨论】:

      猜你喜欢
      • 2018-01-06
      • 2022-06-10
      • 2021-07-02
      • 2013-04-04
      • 1970-01-01
      • 1970-01-01
      • 2016-10-30
      • 2016-01-05
      • 1970-01-01
      相关资源
      最近更新 更多