Asp.Net - Stripe payment intent 在本地有效但在线时出现错误 502

Asp.Net - Stripe payment intent working locally but error 502 online

我正在尝试使用 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)

我终于知道问题出在哪里了,所以如果有人遇到同样的错误,我会解释它是什么以及如何解决它。

我做的第一件事是,按照 karllekko 的建议,在托管服务 运行 时检查服务器日志。为此,我使用了 Serilog,这是一个允许您将日志存储到文件中的包(对我来说这是最简单的解决方案)。 然后我得到了一个更精确的错误:

HttpRequestException: 连接尝试失败,因为连接方在一段时间后没有正确响应...

从这个错误中,我知道我无法在 C# 中使用 stripe's servers. I found a simple ping function 检查我的假设是否正确。

然后我向主机服务提供商咨询了如何访问外部 IP:出于安全原因,他们使用代理服务器。

我在创建使用代理服务器的支付意图之前添加了这段代码:

var handler = new HttpClientHandler
{
    Proxy = new WebProxy(proxyServer),
    UseProxy = true,
};
var httpClient = new HttpClient(handler);

var stripeClient = new StripeClient(
    apiKey: secretApiKey,
    httpClient: new SystemNetHttpClient(httpClient)
);
StripeConfiguration.StripeClient = stripeClient;

我希望如果有人遇到同样的错误,他将能够通过这个答案解决问题。