C#:在条纹中设置默认付款方式
Posted
技术标签:
【中文标题】C#:在条纹中设置默认付款方式【英文标题】:C# : set default payment method in stripe 【发布时间】: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();
【问题讨论】:
【参考方案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);
仍在寻找如何获得费用清单。
【讨论】:
我用支付方式 api 而不是卡 api 尝试了这个,但不起作用。来自@sobelito 的解决方案在这里***.com/a/56784022/1158845 对我有用。【参考方案3】:internal static IEnumerable<StripeCharge> GetChargeList()
var chargeService = new StripeChargeService();
return chargeService.List();
它对我来说很好用。请确保您已在项目中安装“Newtonsoft.Json”dll,因为我在开始条带付款时遇到此错误。 here is the response received from stripe server.
【讨论】:
以上是关于C#:在条纹中设置默认付款方式的主要内容,如果未能解决你的问题,请参考以下文章