如何检索条纹支付在Blazor服务器应用程序?



要接受Stripe到我的Blazor服务器应用程序的在线支付,我使用下面的代码(这里简化;完整的代码可从这里的链接获取)

效果很好。付款已处理(测试模式),如果我自己去那里检查付款,我可以在我的Stripe账户上看到它,但我如何才能从Stripe检索代码中的付款信息?我看到每个付款都有一个id,但是我怎么知道客户端刚刚输入的付款的id是什么呢?

private async Task CheckoutHandler(string priceId)
{
status = (false, "");
formDisabled = true;
try
{
string sessionId = await CreateCheckoutSessionAsync(priceId);
await jsRuntime.InvokeVoidAsync("redirectToCheckout", sessionId);
}
catch (Stripe.StripeException e)
{
status = (true, e.Message);
formDisabled = false;
}
}    

public async Task<string> CreateCheckoutSessionAsync(string priceId, string customerId = null)
{
StripeConfiguration.ApiKey = Environment.GetEnvironmentVariable("stripe_key");
var options = new SessionCreateOptions
{
CustomerEmail = "benac421@gmail.com",
Customer = customerId,
PaymentMethodTypes = new List<string> { "card", },
LineItems = new List<SessionLineItemOptions>
{
new SessionLineItemOptions
{
Name = "Payment name",
Description = "Payment for product ",
Amount = Convert.ToInt32(500),
Currency = "usd",
Quantity = 1,
},
},
Mode = "payment",
SuccessUrl = $"https://localhost:44347/success,
CancelUrl = $"https://localhost:44347/checkout",
ClientReferenceId = "paymentid"
};
try
{
return (await new SessionService().CreateAsync(options)).Id;
//var service = new SessionService();
//Session session = service.Create(options);
//return session.Id;
}
catch (Exception ex)
{
string msg = ex.Message;
throw;
}
}   

让我们先介绍一下背景。因为你正在使用Checkout,你将有一个Checkout Session对象,其中包含一个PaymentIntent对象。Checkout对象的id以cs_test_xxx(测试模式)开头,而PaymentIntent对象的id以pi_xxx开头。

现在假设你的客户端已经在你的Checkout会话中输入了一张卡或任何其他支付方式,你将收到一个名为Checkout .session.completed的webhook事件。如果你正确地设置了你的webhook端点,你可以在那里捕获Checkout Session对象,然后相应地找到Payment Intent。

Stripe有一个关于checkout.session.completed的综合指南。试试吧!

最新更新