【发布时间】:2021-11-08 14:23:33
【问题描述】:
我正在尝试使用 Stripe 构建 custom payment flow。它在本地工作,但是当我尝试将其放到网上时,在一定时间后出现错误。我问我的网络托管服务他们是否放置了任何过滤器,但显然没有。您将在下面找到代码。对于 Javascript,我基本上复制粘贴了 Stripe 给出的代码。另外,我在 Stripe 端没有任何付款意图,所以我认为我的网络应用程序无法连接到 Stripe。从视觉上看,用户应该输入他的卡号等的表单没有出现。
服务器端代码(C#):
[Authorize]
[Route("/create-payment-intent")]
[ApiController]
public class PaymentIntentApiController : Controller
{
private readonly UserManager<AppUser> userManager;
private readonly ApplicationDbContext db;
public PaymentIntentApiController(UserManager<AppUser> userManager, ApplicationDbContext db)
{
this.userManager = userManager;
this.db = db;
}
[HttpPost]
public async Task<ActionResult> Create(ProductViewModel request)
{
if (request == null)
{
return RedirectToAction("Checkout", "Payment");
}
ComplexDbOperations cdo = new ComplexDbOperations(db);
Data.Objects.Product product = new Data.Objects.Product { period = request.period, sub = request.subscription };
int price = ProductViewModel.calculateStripePrice(await cdo.getPricesAsync(product.ToRole().ToString()));
if (price <= 0)
{
return RedirectToAction("Checkout", "Payment");
}
AppUser user = await userManager.GetUserAsync(User);
if (user == null) return RedirectToAction("Index", "Home");
var paymentIntents = new PaymentIntentService();
var paymentIntentOptions = new PaymentIntentCreateOptions
{
Amount = price,
Currency = "chf",
Description = string.Format("Abonnement {0} pour {1}, Periode : {2}", request.subscription.ToString(), user.Email, request.period.ToString()),
Metadata = new Dictionary<string, string>
{
{"Abonnement", request.subscription.ToString()},
{"Periode", request.period.ToString() },
{"UserId", user.Id },
{"UserEmail", user.Email },
{"subCode", ((int)request.subscription).ToString()},
{"periodCode", ((int)request.period).ToString() },
}
};
var paymentIntent = paymentIntents.Create(paymentIntentOptions);
return Json(new { clientSecret = paymentIntent.ClientSecret });
}
}
我在客户端代码 (js) 上从 Stripe 修改的内容:
// The items the customer wants to buy
var purchase = {
period: parseInt(document.getElementById("period").value),
subscription: parseInt(document.getElementById("subscription").value)
};
// Disable the button until we have Stripe set up on the page
document.querySelector("button").disabled = true;
fetch("/create-payment-intent", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(purchase)
})... (stripe code continuing)
你能帮帮我吗?我什至不知道如何获得有关错误的更多详细信息,因为我无法在程序在线时对其进行调试。
提前致谢。
[更新] 我记录了服务器错误并得到:
System.Net.Http.HttpRequestException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (api.stripe.com:443)
【问题讨论】:
-
错误 502 是网关错误(因此您的网关前门 - 通常是反向代理或负载平衡器 - 无法联系或尝试访问您的服务器超时,因此听起来像是基础架构问题,而不是编程问题(特别是如果您的代码在本地工作)。不确定我们可以在这里为您提供任何帮助
-
好的,谢谢您的回答。那么您认为问题出在我的网络托管服务吗?
-
我不知道。这可能是超时问题...例如,如果您的站点托管在 Azure 中(仅作为示例),则它们有一个负载均衡器,超时时间为 230 秒。如果您的 Web 应用程序的响应时间超过 230 秒,最终用户将收到 502(但是您的应用程序仍然可以工作并结束请求)。但这可能是无数其他事情,不一定与您的编程有关。
-
另请阅读 support.stripe.com/questions/… 。简短的回答是——检查你的服务器日志。
-
感谢您的回答。所以我记录了错误并得到:“System.Net.Http.HttpRequestException:连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立连接失败,因为连接的主机没有响应。(api. stripe.com:443)" 有人处理过这样的 smg 吗?
标签: c# asp.net-core stripe-payments runtime-error